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());
it("returns undefined when no proxy env vars are set", () => {
vi.stubEnv("HTTPS_PROXY", "");
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("https_proxy", "");
vi.stubEnv("http_proxy", "");
expect(resolveProxyFetchFromEnv()).toBeUndefined();
expect(resolveProxyFetchFromEnv({})).toBeUndefined();
});
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 });
const fetchFn = resolveProxyFetchFromEnv();
const fetchFn = resolveProxyFetchFromEnv({
HTTP_PROXY: "",
HTTPS_PROXY: "http://proxy.test:8080",
});
expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled();
@@ -91,48 +85,47 @@ describe("resolveProxyFetchFromEnv", () => {
});
it("returns proxy fetch when HTTP_PROXY is set", () => {
vi.stubEnv("HTTPS_PROXY", "");
vi.stubEnv("HTTP_PROXY", "http://fallback.test:3128");
delete process.env.https_proxy;
delete process.env.http_proxy;
const fetchFn = resolveProxyFetchFromEnv();
const fetchFn = resolveProxyFetchFromEnv({
HTTPS_PROXY: "",
HTTP_PROXY: "http://fallback.test:3128",
});
expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled();
});
it("returns proxy fetch when lowercase https_proxy is set", () => {
vi.stubEnv("HTTPS_PROXY", "");
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("http_proxy", "");
vi.stubEnv("https_proxy", "http://lower.test:1080");
const fetchFn = resolveProxyFetchFromEnv();
const fetchFn = resolveProxyFetchFromEnv({
HTTPS_PROXY: "",
HTTP_PROXY: "",
http_proxy: "",
https_proxy: "http://lower.test:1080",
});
expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled();
});
it("returns proxy fetch when lowercase http_proxy is set", () => {
vi.stubEnv("HTTPS_PROXY", "");
vi.stubEnv("HTTP_PROXY", "");
vi.stubEnv("https_proxy", "");
vi.stubEnv("http_proxy", "http://lower-http.test:1080");
const fetchFn = resolveProxyFetchFromEnv();
const fetchFn = resolveProxyFetchFromEnv({
HTTPS_PROXY: "",
HTTP_PROXY: "",
https_proxy: "",
http_proxy: "http://lower-http.test:1080",
});
expect(fetchFn).toBeDefined();
expect(envAgentSpy).toHaveBeenCalled();
});
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(() => {
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();
});
});

View File

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