test(proxy): make env proxy tests windows-safe

This commit is contained in:
Peter Steinberger
2026-03-13 04:17:10 +00:00
parent 6472949f25
commit fc2b796f02
2 changed files with 31 additions and 36 deletions

View File

@@ -64,22 +64,16 @@ describe("resolveProxyFetchFromEnv", () => {
afterEach(() => vi.unstubAllEnvs()); afterEach(() => vi.unstubAllEnvs());
it("returns undefined when no proxy env vars are set", () => { it("returns undefined when no proxy env vars are set", () => {
vi.stubEnv("HTTPS_PROXY", ""); expect(resolveProxyFetchFromEnv({})).toBeUndefined();
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("https_proxy", "");
vi.stubEnv("http_proxy", "");
expect(resolveProxyFetchFromEnv()).toBeUndefined();
}); });
it("returns proxy fetch using EnvHttpProxyAgent when HTTPS_PROXY is set", async () => { it("returns proxy fetch using EnvHttpProxyAgent when HTTPS_PROXY is set", async () => {
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("HTTPS_PROXY", "http://proxy.test:8080");
delete process.env.https_proxy;
delete process.env.http_proxy;
undiciFetch.mockResolvedValue({ ok: true }); undiciFetch.mockResolvedValue({ ok: true });
const fetchFn = resolveProxyFetchFromEnv(); const fetchFn = resolveProxyFetchFromEnv({
HTTP_PROXY: "",
HTTPS_PROXY: "http://proxy.test:8080",
});
expect(fetchFn).toBeDefined(); expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled(); expect(envAgentSpy).toHaveBeenCalled();
@@ -91,48 +85,47 @@ describe("resolveProxyFetchFromEnv", () => {
}); });
it("returns proxy fetch when HTTP_PROXY is set", () => { it("returns proxy fetch when HTTP_PROXY is set", () => {
vi.stubEnv("HTTPS_PROXY", ""); const fetchFn = resolveProxyFetchFromEnv({
vi.stubEnv("HTTP_PROXY", "http://fallback.test:3128"); HTTPS_PROXY: "",
delete process.env.https_proxy; HTTP_PROXY: "http://fallback.test:3128",
delete process.env.http_proxy; });
const fetchFn = resolveProxyFetchFromEnv();
expect(fetchFn).toBeDefined(); expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled(); expect(envAgentSpy).toHaveBeenCalled();
}); });
it("returns proxy fetch when lowercase https_proxy is set", () => { it("returns proxy fetch when lowercase https_proxy is set", () => {
vi.stubEnv("HTTPS_PROXY", ""); const fetchFn = resolveProxyFetchFromEnv({
vi.stubEnv("HTTP_PROXY", ""); HTTPS_PROXY: "",
vi.stubEnv("http_proxy", ""); HTTP_PROXY: "",
vi.stubEnv("https_proxy", "http://lower.test:1080"); http_proxy: "",
https_proxy: "http://lower.test:1080",
const fetchFn = resolveProxyFetchFromEnv(); });
expect(fetchFn).toBeDefined(); expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled(); expect(envAgentSpy).toHaveBeenCalled();
}); });
it("returns proxy fetch when lowercase http_proxy is set", () => { it("returns proxy fetch when lowercase http_proxy is set", () => {
vi.stubEnv("HTTPS_PROXY", ""); const fetchFn = resolveProxyFetchFromEnv({
vi.stubEnv("HTTP_PROXY", ""); HTTPS_PROXY: "",
vi.stubEnv("https_proxy", ""); HTTP_PROXY: "",
vi.stubEnv("http_proxy", "http://lower-http.test:1080"); https_proxy: "",
http_proxy: "http://lower-http.test:1080",
const fetchFn = resolveProxyFetchFromEnv(); });
expect(fetchFn).toBeDefined(); expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled(); expect(envAgentSpy).toHaveBeenCalled();
}); });
it("returns undefined when EnvHttpProxyAgent constructor throws", () => { it("returns undefined when EnvHttpProxyAgent constructor throws", () => {
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("https_proxy", "");
vi.stubEnv("http_proxy", "");
vi.stubEnv("HTTPS_PROXY", "not-a-valid-url");
envAgentSpy.mockImplementationOnce(() => { envAgentSpy.mockImplementationOnce(() => {
throw new Error("Invalid URL"); throw new Error("Invalid URL");
}); });
const fetchFn = resolveProxyFetchFromEnv(); const fetchFn = resolveProxyFetchFromEnv({
HTTP_PROXY: "",
https_proxy: "",
http_proxy: "",
HTTPS_PROXY: "not-a-valid-url",
});
expect(fetchFn).toBeUndefined(); expect(fetchFn).toBeUndefined();
}); });
}); });

View File

@@ -51,8 +51,10 @@ export function getProxyUrlFromFetch(fetchImpl?: typeof fetch): string | undefin
* Returns undefined when no proxy is configured. * Returns undefined when no proxy is configured.
* Gracefully returns undefined if the proxy URL is malformed. * Gracefully returns undefined if the proxy URL is malformed.
*/ */
export function resolveProxyFetchFromEnv(): typeof fetch | undefined { export function resolveProxyFetchFromEnv(
if (!hasEnvHttpProxyConfigured("https")) { env: NodeJS.ProcessEnv = process.env,
): typeof fetch | undefined {
if (!hasEnvHttpProxyConfigured("https", env)) {
return undefined; return undefined;
} }
try { try {