| 1 | import { jest } from "@jest/globals"; |
| 2 | import { getProxyFromEnv, shouldBypassProxy } from "./util.js"; |
| 3 | |
| 4 | // Reset environment variables after each test |
| 5 | afterEach(() => { |
| 6 | jest.resetModules(); |
| 7 | process.env = {}; |
| 8 | }); |
| 9 | |
| 10 | // Tests for getProxyFromEnv |
| 11 | test("getProxyFromEnv returns undefined when no proxy is set", () => { |
| 12 | expect(getProxyFromEnv("http:")).toBeUndefined(); |
| 13 | expect(getProxyFromEnv("https:")).toBeUndefined(); |
| 14 | }); |
| 15 | |
| 16 | test("getProxyFromEnv returns HTTP_PROXY for http protocol", () => { |
| 17 | process.env.HTTP_PROXY = "http://proxy.example.com"; |
| 18 | expect(getProxyFromEnv("http:")).toBe("http://proxy.example.com"); |
| 19 | }); |
| 20 | |
| 21 | test("getProxyFromEnv returns lowercase http_proxy for http protocol", () => { |
| 22 | process.env.http_proxy = "http://proxy.example.com"; |
| 23 | expect(getProxyFromEnv("http:")).toBe("http://proxy.example.com"); |
| 24 | }); |
| 25 | |
| 26 | test("getProxyFromEnv prefers HTTP_PROXY over http_proxy for http protocol", () => { |
| 27 | process.env.HTTP_PROXY = "http://upper.example.com"; |
| 28 | process.env.http_proxy = "http://lower.example.com"; |
| 29 | expect(getProxyFromEnv("http:")).toBe("http://upper.example.com"); |
| 30 | }); |
| 31 | |
| 32 | test("getProxyFromEnv returns HTTPS_PROXY for https protocol", () => { |
| 33 | process.env.HTTPS_PROXY = "https://secure.example.com"; |
| 34 | expect(getProxyFromEnv("https:")).toBe("https://secure.example.com"); |
| 35 | }); |
| 36 | |
| 37 | test("getProxyFromEnv returns lowercase https_proxy for https protocol", () => { |
| 38 | process.env.https_proxy = "https://secure.example.com"; |
| 39 | expect(getProxyFromEnv("https:")).toBe("https://secure.example.com"); |
| 40 | }); |
| 41 | |
| 42 | test("getProxyFromEnv falls back to HTTP_PROXY for https protocol when HTTPS_PROXY is not set", () => { |
| 43 | process.env.HTTP_PROXY = "http://fallback.example.com"; |
| 44 | expect(getProxyFromEnv("https:")).toBe("http://fallback.example.com"); |
| 45 | }); |
| 46 | |
| 47 | test("getProxyFromEnv prefers HTTPS_PROXY over other env vars for https protocol", () => { |
| 48 | process.env.HTTPS_PROXY = "https://preferred.example.com"; |
| 49 | process.env.https_proxy = "https://notused1.example.com"; |
| 50 | process.env.HTTP_PROXY = "http://notused2.example.com"; |
| 51 | process.env.http_proxy = "http://notused3.example.com"; |
| 52 | expect(getProxyFromEnv("https:")).toBe("https://preferred.example.com"); |
| 53 | }); |
| 54 | |
| 55 | // Tests for shouldBypassProxy |
| 56 | test("shouldBypassProxy returns false when NO_PROXY is not set", () => { |
| 57 | expect(shouldBypassProxy("example.com")).toBe(false); |
| 58 | }); |
| 59 | |
| 60 | test("shouldBypassProxy returns true for exact hostname match", () => { |
| 61 | process.env.NO_PROXY = "example.com,another.com"; |
| 62 | expect(shouldBypassProxy("example.com")).toBe(true); |
| 63 | }); |
| 64 | |
| 65 | test("shouldBypassProxy returns false when hostname doesn't match any NO_PROXY entry", () => { |
| 66 | process.env.NO_PROXY = "example.com,another.com"; |
| 67 | expect(shouldBypassProxy("different.com")).toBe(false); |
| 68 | }); |
| 69 | |
| 70 | test("shouldBypassProxy handles lowercase no_proxy", () => { |
| 71 | process.env.no_proxy = "example.com"; |
| 72 | expect(shouldBypassProxy("example.com")).toBe(true); |
| 73 | }); |
| 74 | |
| 75 | test("shouldBypassProxy works with wildcard domains", () => { |
| 76 | process.env.NO_PROXY = "*.example.com"; |
| 77 | expect(shouldBypassProxy("sub.example.com")).toBe(true); |
| 78 | expect(shouldBypassProxy("example.com")).toBe(false); |
| 79 | expect(shouldBypassProxy("different.com")).toBe(false); |
| 80 | }); |
| 81 | |
| 82 | test("shouldBypassProxy works with domain suffix", () => { |
| 83 | process.env.NO_PROXY = ".example.com"; |
| 84 | expect(shouldBypassProxy("sub.example.com")).toBe(true); |
| 85 | expect(shouldBypassProxy("example.com")).toBe(true); |
| 86 | expect(shouldBypassProxy("different.com")).toBe(false); |
| 87 | }); |
| 88 | |
| 89 | test("shouldBypassProxy handles multiple entries with different patterns", () => { |
| 90 | process.env.NO_PROXY = "internal.local,*.example.com,.test.com"; |
| 91 | expect(shouldBypassProxy("internal.local")).toBe(true); |
| 92 | expect(shouldBypassProxy("sub.example.com")).toBe(true); |
| 93 | expect(shouldBypassProxy("sub.test.com")).toBe(true); |
| 94 | expect(shouldBypassProxy("test.com")).toBe(true); |
| 95 | expect(shouldBypassProxy("example.org")).toBe(false); |
| 96 | }); |
| 97 | |
| 98 | test("shouldBypassProxy ignores whitespace in NO_PROXY", () => { |
| 99 | process.env.NO_PROXY = " example.com , *.test.org "; |
| 100 | expect(shouldBypassProxy("example.com")).toBe(true); |
| 101 | expect(shouldBypassProxy("subdomain.test.org")).toBe(true); |
| 102 | }); |