|
| 1 | +import { expect, mock, test } from "bun:test" |
| 2 | +import type { Did } from "@atproto/oauth-client" |
| 3 | +import { createOAuthFetchOptions } from "./workerd-fetch" |
| 4 | + |
| 5 | +const plcDid = "did:plc:3u26lcxyhiyq3ygsfyrc7xx2" as Did<"plc"> |
| 6 | + |
| 7 | +function createWorkerdOptions( |
| 8 | + fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>, |
| 9 | +) { |
| 10 | + return createOAuthFetchOptions(fetch, true) |
| 11 | +} |
| 12 | + |
| 13 | +test("uses the supplied fetch unchanged when redirect:error is supported", () => { |
| 14 | + const fetch = mock(async () => Response.json({ ok: true })) |
| 15 | + const options = createOAuthFetchOptions(fetch) |
| 16 | + |
| 17 | + expect(Object.is(options.fetch, fetch)).toBe(true) |
| 18 | + expect(options.didResolver).toBeUndefined() |
| 19 | +}) |
| 20 | + |
| 21 | +test("patches redirect:error when the Request constructor rejects it", async () => { |
| 22 | + const fetch = mock(async (_input: string | URL | Request, init?: RequestInit) => { |
| 23 | + if (init?.redirect === "error") { |
| 24 | + throw new TypeError('Invalid redirect value: "error"') |
| 25 | + } |
| 26 | + return Response.json({ id: plcDid }) |
| 27 | + }) |
| 28 | + const options = createWorkerdOptions(fetch) |
| 29 | + |
| 30 | + expect(await options.fetch("https://plc.directory/test", { redirect: "error" })).toEqual( |
| 31 | + expect.objectContaining({ status: 200 }), |
| 32 | + ) |
| 33 | + expect(fetch.mock.calls[0]?.[1]?.redirect).toBe("manual") |
| 34 | +}) |
| 35 | + |
| 36 | +test("patches redirect:error on a Request input", async () => { |
| 37 | + const fetch = mock(async (input: string | URL | Request) => { |
| 38 | + expect(input).toBeInstanceOf(Request) |
| 39 | + expect((input as Request).redirect).toBe("manual") |
| 40 | + return Response.json({ ok: true }) |
| 41 | + }) |
| 42 | + const request = new Request("https://example.com", { redirect: "error" }) |
| 43 | + |
| 44 | + await createWorkerdOptions(fetch).fetch(request) |
| 45 | +}) |
| 46 | + |
| 47 | +test("rejects redirects when patching redirect:error", async () => { |
| 48 | + const fetch = mock(async () => Response.redirect("https://attacker.example/did.json", 302)) |
| 49 | + |
| 50 | + expect( |
| 51 | + createWorkerdOptions(fetch).fetch("https://plc.directory/test", { redirect: "error" }), |
| 52 | + ).rejects.toThrow("Redirects are not allowed") |
| 53 | +}) |
| 54 | + |
| 55 | +test("preserves other redirect modes", async () => { |
| 56 | + const response = Response.json({ ok: true }) |
| 57 | + const fetch = mock(async (_input: string | URL | Request, _init?: RequestInit) => response) |
| 58 | + |
| 59 | + expect( |
| 60 | + await createWorkerdOptions(fetch).fetch("https://example.com", { redirect: "follow" }), |
| 61 | + ).toBe(response) |
| 62 | + expect(fetch.mock.calls[0]?.[1]?.redirect).toBe("follow") |
| 63 | +}) |
| 64 | + |
| 65 | +test("lets the ATProto DID resolver reach the patched fetch", async () => { |
| 66 | + const fetch = mock(async (_input: string | URL | Request, init?: RequestInit) => { |
| 67 | + if (init?.redirect === "error") { |
| 68 | + throw new TypeError('Invalid redirect value: "error"') |
| 69 | + } |
| 70 | + return Response.json({ id: plcDid }) |
| 71 | + }) |
| 72 | + const options = createWorkerdOptions(fetch) |
| 73 | + |
| 74 | + expect(await options.didResolver?.resolve(plcDid)).toEqual({ id: plcDid }) |
| 75 | + expect(fetch.mock.calls[0]?.[1]?.redirect).toBe("manual") |
| 76 | +}) |
| 77 | + |
| 78 | +test("resolves did:web through the same patched fetch", async () => { |
| 79 | + const did = "did:web:example.com" as Did<"web"> |
| 80 | + const fetch = mock(async (input: string | URL | Request, init?: RequestInit) => { |
| 81 | + expect(input).toBeInstanceOf(URL) |
| 82 | + expect((input as URL).href).toBe("https://example.com/.well-known/did.json") |
| 83 | + expect(init?.redirect).toBe("manual") |
| 84 | + return Response.json({ id: did }) |
| 85 | + }) |
| 86 | + const options = createWorkerdOptions(fetch) |
| 87 | + |
| 88 | + expect(await options.didResolver?.resolve(did)).toEqual({ id: did }) |
| 89 | +}) |
0 commit comments