-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.test.ts
More file actions
44 lines (39 loc) · 1.65 KB
/
fetch.test.ts
File metadata and controls
44 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { test, expect, describe, afterEach, mock } from "bun:test";
import { loggedFetch } from "./fetch.ts";
const originalFetch = globalThis.fetch;
describe("loggedFetch", () => {
afterEach(() => {
globalThis.fetch = originalFetch;
});
test("sets a Clerk-CLI User-Agent on outbound requests", async () => {
globalThis.fetch = mock(
async () => new Response("ok", { status: 200 }),
) as unknown as typeof fetch;
await loggedFetch("https://example.test/x", { tag: "test" });
const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!;
expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//);
});
test("preserves a caller-provided User-Agent", async () => {
globalThis.fetch = mock(
async () => new Response("ok", { status: 200 }),
) as unknown as typeof fetch;
await loggedFetch("https://example.test/x", {
tag: "test",
headers: { "User-Agent": "Custom/1.0" },
});
const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!;
expect(init.headers.get("User-Agent")).toBe("Custom/1.0");
});
test("preserves other caller-provided headers", async () => {
globalThis.fetch = mock(
async () => new Response("ok", { status: 200 }),
) as unknown as typeof fetch;
await loggedFetch("https://example.test/x", {
tag: "test",
headers: { Authorization: "Bearer abc" },
});
const [, init] = (globalThis.fetch as unknown as ReturnType<typeof mock>).mock.calls[0]!;
expect(init.headers.get("Authorization")).toBe("Bearer abc");
expect(init.headers.get("User-Agent")).toMatch(/^Clerk-CLI\//);
});
});