@@ -4,6 +4,14 @@ import { resolveRequestUrl } from "./request-url.js";
|
4 | 4 | |
5 | 5 | const asFetch = (fn: unknown): typeof fetch => fn as typeof fetch; |
6 | 6 | |
| 7 | +function fetchCall(fetchFn: ReturnType<typeof vi.fn>, index: number): [unknown, RequestInit?] { |
| 8 | +const call = fetchFn.mock.calls[index]; |
| 9 | +if (!call) { |
| 10 | +throw new Error(`expected fetch call ${index}`); |
| 11 | +} |
| 12 | +return call as [unknown, RequestInit?]; |
| 13 | +} |
| 14 | + |
7 | 15 | describe("fetchWithBearerAuthScopeFallback", () => { |
8 | 16 | it("rejects non-https urls when https is required", async () => { |
9 | 17 | await expect( |
@@ -85,7 +93,7 @@ describe("fetchWithBearerAuthScopeFallback", () => {
|
85 | 93 | if (expectedAuthHeader === null) { |
86 | 94 | return; |
87 | 95 | } |
88 | | -const secondCallInit = fetchFn.mock.calls.at(1)?.[1] as RequestInit | undefined; |
| 96 | +const secondCallInit = fetchCall(fetchFn, 1)[1]; |
89 | 97 | const secondHeaders = new Headers(secondCallInit?.headers); |
90 | 98 | expect(secondHeaders.get("authorization")).toBe(expectedAuthHeader); |
91 | 99 | }, |
@@ -143,10 +151,10 @@ describe("fetchWithBearerAuthScopeFallback", () => {
|
143 | 151 | |
144 | 152 | expect(response.status).toBe(200); |
145 | 153 | expect(fetchFn).toHaveBeenCalledTimes(2); |
146 | | -expect( |
147 | | -Object.getOwnPropertySymbols(fetchFn.mock.calls.at(0)?.[1]?.headers as object), |
148 | | -).toStrictEqual([]); |
149 | | -expect(new Headers(fetchFn.mock.calls.at(1)?.[1]?.headers).get("authorization")).toBe( |
| 154 | +expect(Object.getOwnPropertySymbols(fetchCall(fetchFn, 0)[1]?.headers as object)).toStrictEqual( |
| 155 | +[], |
| 156 | +); |
| 157 | +expect(new Headers(fetchCall(fetchFn, 1)[1]?.headers).get("authorization")).toBe( |
150 | 158 | "Bearer token-1", |
151 | 159 | ); |
152 | 160 | expect(Object.getOwnPropertySymbols(headers)).toHaveLength(1); |
|