|
| 1 | +import { test, expect, describe, afterEach, mock } from "bun:test"; |
| 2 | +import { loggedFetch } from "./fetch.ts"; |
| 3 | + |
| 4 | +const originalFetch = globalThis.fetch; |
| 5 | + |
| 6 | +describe("loggedFetch", () => { |
| 7 | + afterEach(() => { |
| 8 | + globalThis.fetch = originalFetch; |
| 9 | + }); |
| 10 | + |
| 11 | + test("sets a Clerk-CLI User-Agent on outbound requests", async () => { |
| 12 | + globalThis.fetch = mock( |
| 13 | + async () => new Response("ok", { status: 200 }), |
| 14 | + ) as unknown as typeof fetch; |
| 15 | + await loggedFetch("https://example.test/x", { tag: "test" }); |
| 16 | + const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; |
| 17 | + expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//); |
| 18 | + }); |
| 19 | + |
| 20 | + test("preserves a caller-provided User-Agent", async () => { |
| 21 | + globalThis.fetch = mock( |
| 22 | + async () => new Response("ok", { status: 200 }), |
| 23 | + ) as unknown as typeof fetch; |
| 24 | + await loggedFetch("https://example.test/x", { |
| 25 | + tag: "test", |
| 26 | + headers: { "User-Agent": "Custom/1.0" }, |
| 27 | + }); |
| 28 | + const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; |
| 29 | + expect(init.headers.get("User-Agent")).toBe("Custom/1.0"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("preserves other caller-provided headers", async () => { |
| 33 | + globalThis.fetch = mock( |
| 34 | + async () => new Response("ok", { status: 200 }), |
| 35 | + ) as unknown as typeof fetch; |
| 36 | + await loggedFetch("https://example.test/x", { |
| 37 | + tag: "test", |
| 38 | + headers: { Authorization: "Bearer abc" }, |
| 39 | + }); |
| 40 | + const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!; |
| 41 | + expect(init.headers.get("Authorization")).toBe("Bearer abc"); |
| 42 | + expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//); |
| 43 | + }); |
| 44 | +}); |
0 commit comments