|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | + |
| 3 | +import { getProxyForUrl } from "@/api/proxy"; |
| 4 | + |
| 5 | +describe("proxy", () => { |
| 6 | + const proxy = "http://proxy.example.com:8080"; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + vi.unstubAllEnvs(); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + vi.unstubAllEnvs(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe("getProxyForUrl", () => { |
| 17 | + describe("proxy resolution", () => { |
| 18 | + it("returns httpProxy when provided", () => { |
| 19 | + expect(getProxyForUrl("https://example.com", proxy, null)).toBe(proxy); |
| 20 | + }); |
| 21 | + |
| 22 | + it("falls back to environment variables when httpProxy is null", () => { |
| 23 | + vi.stubEnv("HTTPS_PROXY", "http://env-proxy.example.com:8080"); |
| 24 | + expect(getProxyForUrl("https://example.com", null, null)).toBe( |
| 25 | + "http://env-proxy.example.com:8080", |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns empty string when no proxy is configured", () => { |
| 30 | + const proxyEnvVars = [ |
| 31 | + "HTTPS_PROXY", |
| 32 | + "https_proxy", |
| 33 | + "HTTP_PROXY", |
| 34 | + "http_proxy", |
| 35 | + "ALL_PROXY", |
| 36 | + "all_proxy", |
| 37 | + "npm_config_https_proxy", |
| 38 | + "npm_config_proxy", |
| 39 | + ]; |
| 40 | + proxyEnvVars.forEach((v) => vi.stubEnv(v, "")); |
| 41 | + |
| 42 | + expect(getProxyForUrl("https://example.com", null, null)).toBe(""); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns empty string for invalid URLs", () => { |
| 46 | + expect(getProxyForUrl("invalid", proxy, null)).toBe(""); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("noProxy handling", () => { |
| 51 | + interface NoProxyBypassCase { |
| 52 | + name: string; |
| 53 | + noProxy: string; |
| 54 | + url: string; |
| 55 | + } |
| 56 | + |
| 57 | + it.each<NoProxyBypassCase>([ |
| 58 | + { |
| 59 | + name: "exact match", |
| 60 | + noProxy: "internal.example.com", |
| 61 | + url: "https://internal.example.com", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "wildcard", |
| 65 | + noProxy: "*.internal.example.com", |
| 66 | + url: "https://api.internal.example.com", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "suffix", |
| 70 | + noProxy: ".example.com", |
| 71 | + url: "https://api.example.com", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "wildcard *", |
| 75 | + noProxy: "*", |
| 76 | + url: "https://any.domain.com", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "port-specific", |
| 80 | + noProxy: "example.com:8443", |
| 81 | + url: "https://example.com:8443", |
| 82 | + }, |
| 83 | + ])( |
| 84 | + "bypasses proxy when hostname matches noProxy ($name)", |
| 85 | + ({ noProxy, url }) => { |
| 86 | + expect(getProxyForUrl(url, proxy, noProxy)).toBe(""); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + it("proxies when hostname does not match noProxy", () => { |
| 91 | + expect( |
| 92 | + getProxyForUrl("https://external.com", proxy, "internal.example.com"), |
| 93 | + ).toBe(proxy); |
| 94 | + }); |
| 95 | + |
| 96 | + it("proxies when port does not match noProxy port", () => { |
| 97 | + expect( |
| 98 | + getProxyForUrl("https://example.com:443", proxy, "example.com:8443"), |
| 99 | + ).toBe(proxy); |
| 100 | + }); |
| 101 | + |
| 102 | + it("handles multiple entries in noProxy (comma-separated)", () => { |
| 103 | + const noProxy = "localhost,127.0.0.1,.internal.com"; |
| 104 | + |
| 105 | + expect(getProxyForUrl("https://localhost", proxy, noProxy)).toBe(""); |
| 106 | + expect(getProxyForUrl("https://127.0.0.1", proxy, noProxy)).toBe(""); |
| 107 | + expect(getProxyForUrl("https://api.internal.com", proxy, noProxy)).toBe( |
| 108 | + "", |
| 109 | + ); |
| 110 | + expect(getProxyForUrl("https://external.com", proxy, noProxy)).toBe( |
| 111 | + proxy, |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe("noProxy fallback chain", () => { |
| 117 | + const targetUrl = "https://internal.example.com"; |
| 118 | + const targetHost = "internal.example.com"; |
| 119 | + |
| 120 | + interface NoProxyFallbackCase { |
| 121 | + name: string; |
| 122 | + noProxy: string | null; |
| 123 | + noProxyFallback: string; |
| 124 | + } |
| 125 | + |
| 126 | + it.each<NoProxyFallbackCase>([ |
| 127 | + { |
| 128 | + name: "noProxy (coder.proxyBypass)", |
| 129 | + noProxy: targetHost, |
| 130 | + noProxyFallback: "other.example.com", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "noProxyFallback when noProxy is null", |
| 134 | + noProxy: null, |
| 135 | + noProxyFallback: targetHost, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "noProxyFallback when noProxy is empty", |
| 139 | + noProxy: "", |
| 140 | + noProxyFallback: targetHost, |
| 141 | + }, |
| 142 | + ])("uses $name", ({ noProxy, noProxyFallback }) => { |
| 143 | + expect(getProxyForUrl(targetUrl, proxy, noProxy, noProxyFallback)).toBe( |
| 144 | + "", |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + interface EnvVarFallbackCase { |
| 149 | + name: string; |
| 150 | + envVar: string; |
| 151 | + } |
| 152 | + |
| 153 | + it.each<EnvVarFallbackCase>([ |
| 154 | + { name: "npm_config_no_proxy", envVar: "npm_config_no_proxy" }, |
| 155 | + { name: "NO_PROXY", envVar: "NO_PROXY" }, |
| 156 | + { name: "no_proxy (lowercase)", envVar: "no_proxy" }, |
| 157 | + ])("falls back to $name env var", ({ envVar }) => { |
| 158 | + // Clear all no_proxy env vars first |
| 159 | + vi.stubEnv("npm_config_no_proxy", ""); |
| 160 | + vi.stubEnv("NO_PROXY", ""); |
| 161 | + vi.stubEnv("no_proxy", ""); |
| 162 | + |
| 163 | + vi.stubEnv(envVar, targetHost); |
| 164 | + expect(getProxyForUrl(targetUrl, proxy, null, null)).toBe(""); |
| 165 | + }); |
| 166 | + |
| 167 | + it("prioritizes noProxy over noProxyFallback over env vars", () => { |
| 168 | + vi.stubEnv("NO_PROXY", "env.example.com"); |
| 169 | + |
| 170 | + // noProxy takes precedence |
| 171 | + expect( |
| 172 | + getProxyForUrl( |
| 173 | + "https://primary.example.com", |
| 174 | + proxy, |
| 175 | + "primary.example.com", |
| 176 | + "fallback.example.com", |
| 177 | + ), |
| 178 | + ).toBe(""); |
| 179 | + |
| 180 | + // noProxyFallback is used when noProxy is null |
| 181 | + expect( |
| 182 | + getProxyForUrl( |
| 183 | + "https://fallback.example.com", |
| 184 | + proxy, |
| 185 | + null, |
| 186 | + "fallback.example.com", |
| 187 | + ), |
| 188 | + ).toBe(""); |
| 189 | + |
| 190 | + // env var is used when both are null |
| 191 | + expect( |
| 192 | + getProxyForUrl("https://env.example.com", proxy, null, null), |
| 193 | + ).toBe(""); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe("protocol and port handling", () => { |
| 198 | + interface ProtocolCase { |
| 199 | + protocol: string; |
| 200 | + url: string; |
| 201 | + } |
| 202 | + |
| 203 | + it.each<ProtocolCase>([ |
| 204 | + { protocol: "http://", url: "http://example.com" }, |
| 205 | + { protocol: "https://", url: "https://example.com" }, |
| 206 | + { protocol: "ws://", url: "ws://example.com" }, |
| 207 | + { protocol: "wss://", url: "wss://example.com" }, |
| 208 | + ])("handles $protocol URLs", ({ url }) => { |
| 209 | + expect(getProxyForUrl(url, proxy, null)).toBe(proxy); |
| 210 | + }); |
| 211 | + |
| 212 | + interface DefaultPortCase { |
| 213 | + protocol: string; |
| 214 | + url: string; |
| 215 | + defaultPort: number; |
| 216 | + } |
| 217 | + |
| 218 | + it.each<DefaultPortCase>([ |
| 219 | + { protocol: "HTTP", url: "http://example.com", defaultPort: 80 }, |
| 220 | + { protocol: "HTTPS", url: "https://example.com", defaultPort: 443 }, |
| 221 | + { protocol: "WS", url: "ws://example.com", defaultPort: 80 }, |
| 222 | + { protocol: "WSS", url: "wss://example.com", defaultPort: 443 }, |
| 223 | + ])( |
| 224 | + "uses default port $defaultPort for $protocol", |
| 225 | + ({ url, defaultPort }) => { |
| 226 | + expect(getProxyForUrl(url, proxy, `example.com:${defaultPort}`)).toBe( |
| 227 | + "", |
| 228 | + ); |
| 229 | + }, |
| 230 | + ); |
| 231 | + }); |
| 232 | + |
| 233 | + describe("proxy scheme handling", () => { |
| 234 | + it("adds scheme to proxy URL when missing", () => { |
| 235 | + expect( |
| 236 | + getProxyForUrl("https://example.com", "proxy.example.com:8080", null), |
| 237 | + ).toBe("https://proxy.example.com:8080"); |
| 238 | + }); |
| 239 | + |
| 240 | + it("uses request scheme when proxy has no scheme", () => { |
| 241 | + expect( |
| 242 | + getProxyForUrl("http://example.com", "proxy.example.com:8080", null), |
| 243 | + ).toBe("http://proxy.example.com:8080"); |
| 244 | + }); |
| 245 | + |
| 246 | + it("preserves existing scheme in proxy URL", () => { |
| 247 | + expect( |
| 248 | + getProxyForUrl( |
| 249 | + "https://example.com", |
| 250 | + "http://proxy.example.com:8080", |
| 251 | + null, |
| 252 | + ), |
| 253 | + ).toBe("http://proxy.example.com:8080"); |
| 254 | + }); |
| 255 | + }); |
| 256 | + }); |
| 257 | +}); |
0 commit comments