test: dedupe fetch auth mock reads

This commit is contained in:
Peter Steinberger
2026-05-13 02:44:01 +01:00
parent 73ae1decdf
commit e3a4280788

View File

@@ -4,6 +4,14 @@ import { resolveRequestUrl } from "./request-url.js";
const asFetch = (fn: unknown): typeof fetch => fn as typeof fetch;
function fetchCall(fetchFn: ReturnType<typeof vi.fn>, index: number): [unknown, RequestInit?] {
const call = fetchFn.mock.calls[index];
if (!call) {
throw new Error(`expected fetch call ${index}`);
}
return call as [unknown, RequestInit?];
}
describe("fetchWithBearerAuthScopeFallback", () => {
it("rejects non-https urls when https is required", async () => {
await expect(
@@ -85,7 +93,7 @@ describe("fetchWithBearerAuthScopeFallback", () => {
if (expectedAuthHeader === null) {
return;
}
const secondCallInit = fetchFn.mock.calls.at(1)?.[1] as RequestInit | undefined;
const secondCallInit = fetchCall(fetchFn, 1)[1];
const secondHeaders = new Headers(secondCallInit?.headers);
expect(secondHeaders.get("authorization")).toBe(expectedAuthHeader);
},
@@ -143,10 +151,10 @@ describe("fetchWithBearerAuthScopeFallback", () => {
expect(response.status).toBe(200);
expect(fetchFn).toHaveBeenCalledTimes(2);
expect(
Object.getOwnPropertySymbols(fetchFn.mock.calls.at(0)?.[1]?.headers as object),
).toStrictEqual([]);
expect(new Headers(fetchFn.mock.calls.at(1)?.[1]?.headers).get("authorization")).toBe(
expect(Object.getOwnPropertySymbols(fetchCall(fetchFn, 0)[1]?.headers as object)).toStrictEqual(
[],
);
expect(new Headers(fetchCall(fetchFn, 1)[1]?.headers).get("authorization")).toBe(
"Bearer token-1",
);
expect(Object.getOwnPropertySymbols(headers)).toHaveLength(1);