test: preserve caller User-Agent headers

This commit is contained in:
2026-10-04 18:54:15 +00:00 Unverified
parent cccecab81f
commit 62f2362a2e
2 changed files with 16 additions and 0 deletions
+2
View File
@@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Finder (MacOS) folder config # Finder (MacOS) folder config
.DS_Store .DS_Store
.codegraph
+14
View File
@@ -30,6 +30,7 @@ describe("authenticated API transport", () => {
expect(new Headers(init?.headers).get("content-type")).toBe( expect(new Headers(init?.headers).get("content-type")).toBe(
"application/json", "application/json",
); );
expect(new Headers(init?.headers).has("user-agent")).toBe(false);
expect(init?.body).toBe('{"name":"demo"}'); expect(init?.body).toBe('{"name":"demo"}');
return Response.json({ id: "workspace-1" }); return Response.json({ id: "workspace-1" });
}, },
@@ -64,6 +65,19 @@ describe("authenticated API transport", () => {
).resolves.toBeUndefined(); ).resolves.toBeUndefined();
}); });
test("preserves a caller-provided User-Agent without adding one by default", async () => {
const sent: Array<string | null> = [];
globalThis.fetch = mock(async (_input, init) => {
sent.push(new Headers(init?.headers).get("user-agent"));
return new Response(null, { status: 204 });
}) as unknown as typeof fetch;
const options = { authenticated: false, baseUrl: "https://api.test" };
await apiRequest("/plain", {}, options);
await apiRequest("/custom", { headers: { "user-agent": "my-app" } }, options);
expect(sent).toEqual([null, "my-app"]);
});
test("treats an empty successful JSON response as no content", async () => { test("treats an empty successful JSON response as no content", async () => {
globalThis.fetch = mock( globalThis.fetch = mock(
async () => new Response("", { status: 200 }), async () => new Response("", { status: 200 }),