| 1 | import { afterEach, expect, test, vi } from "vitest"; |
| 2 | import { |
| 3 | getProxyFromEnv, |
| 4 | patternMatchesHostname, |
| 5 | shouldBypassProxy, |
| 6 | } from "./util.js"; |
| 7 | |
| 8 | // Reset environment variables after each test |
| 9 | afterEach(() => { |
| 10 | vi.resetModules(); |
| 11 | process.env = {}; |
| 12 | }); |
| 13 | |
| 14 | // Tests for getProxyFromEnv |
| 15 | test("getProxyFromEnv returns undefined when no proxy is set", () => { |
| 16 | expect(getProxyFromEnv("http:")).toBeUndefined(); |
| 17 | expect(getProxyFromEnv("https:")).toBeUndefined(); |
| 18 | }); |
| 19 | |
| 20 | test("getProxyFromEnv returns HTTP_PROXY for http protocol", () => { |
| 21 | process.env.HTTP_PROXY = "http://proxy.example.com"; |
| 22 | expect(getProxyFromEnv("http:")).toBe("http://proxy.example.com"); |
| 23 | }); |
| 24 | |
| 25 | test("getProxyFromEnv returns lowercase http_proxy for http protocol", () => { |
| 26 | process.env.http_proxy = "http://proxy.example.com"; |
| 27 | expect(getProxyFromEnv("http:")).toBe("http://proxy.example.com"); |
| 28 | }); |
| 29 | |
| 30 | test("getProxyFromEnv prefers HTTP_PROXY over http_proxy for http protocol", () => { |
| 31 | process.env.HTTP_PROXY = "http://upper.example.com"; |
| 32 | process.env.http_proxy = "http://lower.example.com"; |
| 33 | expect(getProxyFromEnv("http:")).toBe("http://upper.example.com"); |
| 34 | }); |
| 35 | |
| 36 | test("getProxyFromEnv returns HTTPS_PROXY for https protocol", () => { |
| 37 | process.env.HTTPS_PROXY = "https://secure.example.com"; |
| 38 | expect(getProxyFromEnv("https:")).toBe("https://secure.example.com"); |
| 39 | }); |
| 40 | |
| 41 | test("getProxyFromEnv returns lowercase https_proxy for https protocol", () => { |
| 42 | process.env.https_proxy = "https://secure.example.com"; |
| 43 | expect(getProxyFromEnv("https:")).toBe("https://secure.example.com"); |
| 44 | }); |
| 45 | |
| 46 | test("getProxyFromEnv falls back to HTTP_PROXY for https protocol when HTTPS_PROXY is not set", () => { |
| 47 | process.env.HTTP_PROXY = "http://fallback.example.com"; |
| 48 | expect(getProxyFromEnv("https:")).toBe("http://fallback.example.com"); |
| 49 | }); |
| 50 | |
| 51 | test("getProxyFromEnv prefers HTTPS_PROXY over other env vars for https protocol", () => { |
| 52 | process.env.HTTPS_PROXY = "https://preferred.example.com"; |
| 53 | process.env.https_proxy = "https://notused1.example.com"; |
| 54 | process.env.HTTP_PROXY = "http://notused2.example.com"; |
| 55 | process.env.http_proxy = "http://notused3.example.com"; |
| 56 | expect(getProxyFromEnv("https:")).toBe("https://preferred.example.com"); |
| 57 | }); |
| 58 | |
| 59 | // Tests for patternMatchesHostname |
| 60 | test("patternMatchesHostname with exact hostname match", () => { |
| 61 | expect(patternMatchesHostname("example.com", "example.com")).toBe(true); |
| 62 | expect(patternMatchesHostname("example.com", "different.com")).toBe(false); |
| 63 | }); |
| 64 | |
| 65 | test("patternMatchesHostname with wildcard domains", () => { |
| 66 | expect(patternMatchesHostname("sub.example.com", "*.example.com")).toBe(true); |
| 67 | expect(patternMatchesHostname("sub.sub.example.com", "*.example.com")).toBe( |
| 68 | true, |
| 69 | ); |
| 70 | expect(patternMatchesHostname("example.com", "*.example.com")).toBe(false); |
| 71 | expect(patternMatchesHostname("sub.different.com", "*.example.com")).toBe( |
| 72 | false, |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | test("patternMatchesHostname with domain suffix", () => { |
| 77 | expect(patternMatchesHostname("sub.example.com", ".example.com")).toBe(true); |
| 78 | expect(patternMatchesHostname("example.com", ".example.com")).toBe(true); |
| 79 | expect(patternMatchesHostname("different.com", ".example.com")).toBe(false); |
| 80 | }); |
| 81 | |
| 82 | test("patternMatchesHostname with case insensitivity", () => { |
| 83 | expect(patternMatchesHostname("EXAMPLE.com", "example.COM")).toBe(true); |
| 84 | expect(patternMatchesHostname("sub.EXAMPLE.com", "*.example.COM")).toBe(true); |
| 85 | }); |
| 86 | |
| 87 | // Port handling tests |
| 88 | test("patternMatchesHostname with exact port match", () => { |
| 89 | expect(patternMatchesHostname("example.com:8080", "example.com:8080")).toBe( |
| 90 | true, |
| 91 | ); |
| 92 | expect(patternMatchesHostname("example.com:8080", "example.com:9090")).toBe( |
| 93 | false, |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | test("patternMatchesHostname with port in pattern but not in hostname", () => { |
| 98 | expect(patternMatchesHostname("example.com", "example.com:8080")).toBe(false); |
| 99 | }); |
| 100 | |
| 101 | test("patternMatchesHostname with port in hostname but not in pattern", () => { |
| 102 | expect(patternMatchesHostname("example.com:8080", "example.com")).toBe(true); |
| 103 | }); |
| 104 | |
| 105 | test("patternMatchesHostname with wildcard domains and ports", () => { |
| 106 | expect( |
| 107 | patternMatchesHostname("sub.example.com:8080", "*.example.com:8080"), |
| 108 | ).toBe(true); |
| 109 | expect( |
| 110 | patternMatchesHostname("sub.example.com:9090", "*.example.com:8080"), |
| 111 | ).toBe(false); |
| 112 | expect(patternMatchesHostname("sub.example.com", "*.example.com:8080")).toBe( |
| 113 | false, |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | test("patternMatchesHostname with domain suffix and ports", () => { |
| 118 | expect( |
| 119 | patternMatchesHostname("sub.example.com:8080", ".example.com:8080"), |
| 120 | ).toBe(true); |
| 121 | expect(patternMatchesHostname("example.com:8080", ".example.com:8080")).toBe( |
| 122 | true, |
| 123 | ); |
| 124 | expect( |
| 125 | patternMatchesHostname("sub.example.com:9090", ".example.com:8080"), |
| 126 | ).toBe(false); |
| 127 | }); |
| 128 | |
| 129 | // Tests for shouldBypassProxy |
| 130 | test("shouldBypassProxy returns false when NO_PROXY is not set", () => { |
| 131 | expect(shouldBypassProxy("example.com", undefined)).toBe(false); |
| 132 | }); |
| 133 | |
| 134 | test("shouldBypassProxy returns true for exact hostname match", () => { |
| 135 | process.env.NO_PROXY = "example.com,another.com"; |
| 136 | expect(shouldBypassProxy("example.com", undefined)).toBe(true); |
| 137 | }); |
| 138 | |
| 139 | test("shouldBypassProxy returns false when hostname doesn't match any NO_PROXY entry", () => { |
| 140 | process.env.NO_PROXY = "example.com,another.com"; |
| 141 | expect(shouldBypassProxy("different.com", undefined)).toBe(false); |
| 142 | }); |
| 143 | |
| 144 | test("shouldBypassProxy handles lowercase no_proxy", () => { |
| 145 | process.env.no_proxy = "example.com"; |
| 146 | expect(shouldBypassProxy("example.com", undefined)).toBe(true); |
| 147 | }); |
| 148 | |
| 149 | test("shouldBypassProxy works with wildcard domains", () => { |
| 150 | process.env.NO_PROXY = "*.example.com"; |
| 151 | expect(shouldBypassProxy("sub.example.com", undefined)).toBe(true); |
| 152 | expect(shouldBypassProxy("example.com", undefined)).toBe(false); |
| 153 | expect(shouldBypassProxy("different.com", undefined)).toBe(false); |
| 154 | }); |
| 155 | |
| 156 | test("shouldBypassProxy works with domain suffix", () => { |
| 157 | process.env.NO_PROXY = ".example.com"; |
| 158 | expect(shouldBypassProxy("sub.example.com", undefined)).toBe(true); |
| 159 | expect(shouldBypassProxy("example.com", undefined)).toBe(true); |
| 160 | expect(shouldBypassProxy("different.com", undefined)).toBe(false); |
| 161 | }); |
| 162 | |
| 163 | test("shouldBypassProxy handles multiple entries with different patterns", () => { |
| 164 | process.env.NO_PROXY = "internal.local,*.example.com,.test.com"; |
| 165 | expect(shouldBypassProxy("internal.local", undefined)).toBe(true); |
| 166 | expect(shouldBypassProxy("sub.example.com", undefined)).toBe(true); |
| 167 | expect(shouldBypassProxy("sub.test.com", undefined)).toBe(true); |
| 168 | expect(shouldBypassProxy("test.com", undefined)).toBe(true); |
| 169 | expect(shouldBypassProxy("example.org", undefined)).toBe(false); |
| 170 | }); |
| 171 | |
| 172 | test("shouldBypassProxy ignores whitespace in NO_PROXY", () => { |
| 173 | process.env.NO_PROXY = " example.com , *.test.org "; |
| 174 | expect(shouldBypassProxy("example.com", undefined)).toBe(true); |
| 175 | expect(shouldBypassProxy("subdomain.test.org", undefined)).toBe(true); |
| 176 | }); |
| 177 | |
| 178 | test("shouldBypassProxy with ports in NO_PROXY", () => { |
| 179 | process.env.NO_PROXY = "example.com:8080,*.test.org:443,.internal.net:8443"; |
| 180 | expect(shouldBypassProxy("example.com:8080", undefined)).toBe(true); |
| 181 | expect(shouldBypassProxy("example.com:9090", undefined)).toBe(false); |
| 182 | expect(shouldBypassProxy("sub.test.org:443", undefined)).toBe(true); |
| 183 | expect(shouldBypassProxy("sub.internal.net:8443", undefined)).toBe(true); |
| 184 | expect(shouldBypassProxy("internal.net:8443", undefined)).toBe(true); |
| 185 | }); |
| 186 | |
| 187 | test("shouldBypassProxy accepts options with noProxy patterns", () => { |
| 188 | const options = { noProxy: ["example.com:8080", "*.internal.net"] }; |
| 189 | expect(shouldBypassProxy("example.com:8080", options)).toBe(true); |
| 190 | expect(shouldBypassProxy("example.com", options)).toBe(false); |
| 191 | expect(shouldBypassProxy("server.internal.net", options)).toBe(true); |
| 192 | }); |
| 193 | |
| 194 | test("shouldBypassProxy combines environment and options noProxy patterns", () => { |
| 195 | process.env.NO_PROXY = "example.org,*.test.com"; |
| 196 | const options = { noProxy: ["example.com:8080", "*.internal.net"] }; |
| 197 | expect(shouldBypassProxy("example.org", options)).toBe(true); |
| 198 | expect(shouldBypassProxy("sub.test.com", options)).toBe(true); |
| 199 | expect(shouldBypassProxy("example.com:8080", options)).toBe(true); |
| 200 | expect(shouldBypassProxy("server.internal.net", options)).toBe(true); |
| 201 | expect(shouldBypassProxy("other.domain", options)).toBe(false); |
| 202 | }); |
| 203 | |
| 204 | test("shouldBypassProxy handles empty noProxy array in options", () => { |
| 205 | process.env.NO_PROXY = "example.org"; |
| 206 | const options = { noProxy: [] }; |
| 207 | expect(shouldBypassProxy("example.org", options)).toBe(true); |
| 208 | expect(shouldBypassProxy("different.com", options)).toBe(false); |
| 209 | }); |
| 210 | |
| 211 | test("shouldBypassProxy handles undefined options", () => { |
| 212 | process.env.NO_PROXY = "example.org"; |
| 213 | expect(shouldBypassProxy("example.org", undefined)).toBe(true); |
| 214 | }); |