test: dedupe fetch guard mock reads

This commit is contained in:
Peter Steinberger
2026-05-12 23:02:32 +01:00
parent c6ebc1d7e8
commit fc94d7541c

View File

@@ -97,6 +97,10 @@ function expectDispatcherAttached(value: unknown): void {
expect(getDispatcherClassName(value)).toMatch(/^(Agent|Mock)$/u);
}
function firstMockCall<T extends unknown[]>(mock: { mock: { calls: T[] } }): T | undefined {
return mock.mock.calls[0];
}
function getSecondRequestHeaders(fetchImpl: ReturnType<typeof vi.fn>): Headers {
const [, secondInit] = fetchImpl.mock.calls.at(1) as [string, RequestInit];
return new Headers(secondInit.headers);
@@ -301,7 +305,7 @@ describe("fetchWithSsrFGuard hardening", () => {
).rejects.toThrow(/private|internal|blocked/i);
expect(fetchImpl).not.toHaveBeenCalled();
expect(logWarnMock).toHaveBeenCalledTimes(1);
const [warning] = logWarnMock.mock.calls.at(0) as [string];
const [warning] = firstMockCall(logWarnMock) as [string];
expect(warning).toContain(
"security: blocked URL fetch (qa-audit) targetOrigin=http://127.0.0.1:8080",
);
@@ -602,7 +606,7 @@ describe("fetchWithSsrFGuard hardening", () => {
},
});
expect(fetchImpl).toHaveBeenCalledTimes(1);
const fetchCall = fetchImpl.mock.calls.at(0) as [string, { dispatcher?: unknown }] | undefined;
const fetchCall = firstMockCall(fetchImpl) as [string, { dispatcher?: unknown }] | undefined;
expect(fetchCall?.[0]).toBe("https://public.example/resource");
if (!fetchCall?.[1].dispatcher) {
throw new Error("Expected proxy dispatcher");