





















@@ -92,6 +92,32 @@ function requireProxyFetch(
9292return fetchFn;
9393}
949495+function requireUndiciFetchCall(index = 0): unknown[] {
96+const call = undiciFetch.mock.calls[index];
97+if (!call) {
98+throw new Error(`expected undici fetch call at index ${index}`);
99+}
100+return call;
101+}
102+103+function requireUndiciFetchInit(index = 0): Record<string, unknown> {
104+const init = requireUndiciFetchCall(index)[1];
105+if (!init || typeof init !== "object" || Array.isArray(init)) {
106+throw new Error(`expected undici fetch init at index ${index}`);
107+}
108+return init as Record<string, unknown>;
109+}
110+111+function requireHeadersInit(value: unknown, label: string): HeadersInit {
112+if (value === undefined || value instanceof Headers || Array.isArray(value)) {
113+return value as HeadersInit;
114+}
115+if (value && typeof value === "object") {
116+return value as HeadersInit;
117+}
118+throw new Error(`expected ${label} headers`);
119+}
120+95121function clearProxyEnv(): void {
96122for (const key of PROXY_ENV_KEYS) {
97123delete process.env[key];
@@ -128,9 +154,10 @@ describe("makeProxyFetch", () => {
128154129155expect(proxyAgentSpy).toHaveBeenCalledWith(proxyUrl);
130156expect(undiciFetch).toHaveBeenCalledOnce();
131-const [input, init] = undiciFetch.mock.calls[0] ?? [];
157+const [input] = requireUndiciFetchCall();
158+const init = requireUndiciFetchInit();
132159expect(input).toBe("https://api.example.com/v1/audio");
133-expect(init?.dispatcher).toBe(getLastAgent());
160+expect(init.dispatcher).toBe(getLastAgent());
134161});
135162136163it("reuses the same ProxyAgent across calls", async () => {
@@ -139,9 +166,9 @@ describe("makeProxyFetch", () => {
139166const proxyFetch = makeProxyFetch("http://proxy.test:8080");
140167141168await proxyFetch("https://api.example.com/one");
142-const firstDispatcher = undiciFetch.mock.calls[0]?.[1]?.dispatcher;
169+const firstDispatcher = requireUndiciFetchInit().dispatcher;
143170await proxyFetch("https://api.example.com/two");
144-const secondDispatcher = undiciFetch.mock.calls[1]?.[1]?.dispatcher;
171+const secondDispatcher = requireUndiciFetchInit(1).dispatcher;
145172146173expect(proxyAgentSpy).toHaveBeenCalledOnce();
147174expect(secondDispatcher).toBe(firstDispatcher);
@@ -164,13 +191,13 @@ describe("makeProxyFetch", () => {
164191body: form,
165192});
166193167-const passedInit = undiciFetch.mock.calls[0]?.[1];
168-expect(passedInit?.body).toBeInstanceOf(MockUndiciFormData);
169-const passedBody = passedInit?.body as InstanceType<typeof MockUndiciFormData>;
194+const passedInit = requireUndiciFetchInit();
195+expect(passedInit.body).toBeInstanceOf(MockUndiciFormData);
196+const passedBody = passedInit.body as InstanceType<typeof MockUndiciFormData>;
170197expect(passedBody.get("model")).toBe("whisper-1");
171198expect(passedBody.get("file")).toBeInstanceOf(Blob);
172199expect(passedBody.entriesList.find(([key]) => key === "file")?.[2]).toBe("voice.ogg");
173-const sentHeaders = new Headers(passedInit?.headers);
200+const sentHeaders = new Headers(requireHeadersInit(passedInit.headers, "FormData proxy"));
174201expect(sentHeaders.has("content-length")).toBe(false);
175202expect(sentHeaders.has("content-type")).toBe(false);
176203});
@@ -186,7 +213,7 @@ describe("makeProxyFetch", () => {
186213 body,
187214});
188215189-expect(undiciFetch.mock.calls[0]?.[1]?.body).toBe(body);
216+expect(requireUndiciFetchInit().body).toBe(body);
190217});
191218192219it("drops symbol metadata from plain header dictionaries before undici fetch", async () => {
@@ -207,10 +234,12 @@ describe("makeProxyFetch", () => {
207234body: "{}",
208235});
209236210-const passedHeaders = undiciFetch.mock.calls[0]?.[1]?.headers;
237+const passedHeaders = requireUndiciFetchInit().headers;
211238expect(passedHeaders).not.toBe(headers);
212239expect(Object.getOwnPropertySymbols(passedHeaders as object)).toStrictEqual([]);
213-expect(new Headers(passedHeaders).get("content-type")).toBe("application/json");
240+expect(
241+new Headers(requireHeadersInit(passedHeaders, "plain dictionary proxy")).get("content-type"),
242+).toBe("application/json");
214243expect(Object.getOwnPropertySymbols(headers)).toHaveLength(1);
215244});
216245@@ -226,7 +255,7 @@ describe("makeProxyFetch", () => {
226255body: form as unknown as BodyInit,
227256});
228257229-expect(undiciFetch.mock.calls[0]?.[1]?.body).toBe(form);
258+expect(requireUndiciFetchInit().body).toBe(form);
230259});
231260232261it("converts FormData-like bodies from another implementation", async () => {
@@ -245,9 +274,9 @@ describe("makeProxyFetch", () => {
245274body: formLike as unknown as BodyInit,
246275});
247276248-const passedInit = undiciFetch.mock.calls[0]?.[1];
249-expect(passedInit?.body).toBeInstanceOf(MockUndiciFormData);
250-expect(passedInit?.body.get("model")).toBe("whisper-1");
277+const passedBody = requireUndiciFetchInit().body;
278+expect(passedBody).toBeInstanceOf(MockUndiciFormData);
279+expect((passedBody as InstanceType<typeof MockUndiciFormData>).get("model")).toBe("whisper-1");
251280});
252281});
253282@@ -302,9 +331,10 @@ describe("resolveProxyFetchFromEnv", () => {
302331303332await fetchFn("https://api.example.com");
304333expect(undiciFetch).toHaveBeenCalledOnce();
305-const [input, init] = undiciFetch.mock.calls[0] ?? [];
334+const [input] = requireUndiciFetchCall();
335+const init = requireUndiciFetchInit();
306336expect(input).toBe("https://api.example.com");
307-expect(init?.dispatcher).toBe(EnvHttpProxyAgent.lastCreated);
337+expect(init.dispatcher).toBe(EnvHttpProxyAgent.lastCreated);
308338});
309339310340it("converts global FormData bodies when using proxy env fetch", async () => {
@@ -326,10 +356,12 @@ describe("resolveProxyFetchFromEnv", () => {
326356body: form,
327357});
328358329-const passedInit = undiciFetch.mock.calls[0]?.[1];
330-expect(passedInit?.body).toBeInstanceOf(MockUndiciFormData);
331-expect(passedInit?.body.get("model")).toBe("test-model");
332-expect(passedInit?.body.get("file")).toBeInstanceOf(Blob);
359+const passedBody = requireUndiciFetchInit().body;
360+expect(passedBody).toBeInstanceOf(MockUndiciFormData);
361+expect((passedBody as InstanceType<typeof MockUndiciFormData>).get("model")).toBe("test-model");
362+expect((passedBody as InstanceType<typeof MockUndiciFormData>).get("file")).toBeInstanceOf(
363+Blob,
364+);
333365});
334366335367it("returns proxy fetch when HTTP_PROXY is set", () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。