
























@@ -102,24 +102,38 @@ function getSecondRequestHeaders(fetchImpl: ReturnType<typeof vi.fn>): Headers {
102102return new Headers(secondInit.headers);
103103}
104104105+function requireRecord(value: unknown, label: string): Record<string, unknown> {
106+if (!value || typeof value !== "object" || Array.isArray(value)) {
107+throw new Error(`expected ${label}`);
108+}
109+return value as Record<string, unknown>;
110+}
111+112+function getFirstRequestInit(fetchImpl: ReturnType<typeof vi.fn>): RequestInit {
113+const [call] = fetchImpl.mock.calls;
114+if (!call) {
115+throw new Error("expected first fetch call");
116+}
117+const [, init] = call as [string, RequestInit | undefined];
118+return requireRecord(init, "first fetch init") as RequestInit;
119+}
120+105121function getSecondRequestInit(fetchImpl: ReturnType<typeof vi.fn>): RequestInit {
106122const [, secondInit] = fetchImpl.mock.calls[1] as [string, RequestInit];
107123return secondInit;
108124}
109125110126function expectAgentConstructorOptions(params: { bodyTimeout: number; headersTimeout: number }) {
111-const options = agentCtor.mock.calls[0]?.[0] as
112-| {
113-connect?: { lookup?: unknown };
114-allowH2?: boolean;
115-bodyTimeout?: number;
116-headersTimeout?: number;
117-}
118-| undefined;
119-expect(typeof options?.connect?.lookup).toBe("function");
120-expect(options?.allowH2).toBe(false);
121-expect(options?.bodyTimeout).toBe(params.bodyTimeout);
122-expect(options?.headersTimeout).toBe(params.headersTimeout);
127+const [call] = agentCtor.mock.calls;
128+if (!call) {
129+throw new Error("expected Agent constructor call");
130+}
131+const options = requireRecord(call[0], "Agent constructor options");
132+const connect = requireRecord(options.connect, "Agent connect options");
133+expect(typeof connect.lookup).toBe("function");
134+expect(options.allowH2).toBe(false);
135+expect(options.bodyTimeout).toBe(params.bodyTimeout);
136+expect(options.headersTimeout).toBe(params.headersTimeout);
123137}
124138125139async function expectRedirectFailure(params: {
@@ -708,7 +722,7 @@ describe("fetchWithSsrFGuard hardening", () => {
708722});
709723710724expect(result.response.status).toBe(200);
711-const firstHeaders = fetchImpl.mock.calls[0]?.[1]?.headers;
725+const firstHeaders = getFirstRequestInit(fetchImpl).headers;
712726expect(firstHeaders).not.toBe(headers);
713727expect(Object.getOwnPropertySymbols(firstHeaders as object)).toStrictEqual([]);
714728const secondHeaders = getSecondRequestHeaders(fetchImpl);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。