|
1 | 1 | import * as vscode from "vscode" |
2 | | -import { initializeNetworkProxy, getProxyConfig, isProxyEnabled, isDebugMode } from "../networkProxy" |
| 2 | +import { initializeNetworkProxy, getProxyConfig, isProxyEnabled, isDebugMode, getSystemProxyUrl } from "../networkProxy" |
3 | 3 |
|
4 | 4 | // Mock global-agent |
5 | 5 | vi.mock("global-agent", () => ({ |
@@ -305,4 +305,68 @@ describe("networkProxy", () => { |
305 | 305 | expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).toBeUndefined() |
306 | 306 | }) |
307 | 307 | }) |
| 308 | + |
| 309 | + describe("getSystemProxyUrl", () => { |
| 310 | + beforeEach(() => { |
| 311 | + // Clear env vars before each test |
| 312 | + delete process.env.HTTPS_PROXY |
| 313 | + delete process.env.https_proxy |
| 314 | + delete process.env.HTTP_PROXY |
| 315 | + delete process.env.http_proxy |
| 316 | + }) |
| 317 | + |
| 318 | + it("should return proxy from HTTPS_PROXY env var", () => { |
| 319 | + process.env.HTTPS_PROXY = "http://proxy.corp:3128" |
| 320 | + const result = getSystemProxyUrl() |
| 321 | + expect(result).toBe("http://proxy.corp:3128") |
| 322 | + }) |
| 323 | + |
| 324 | + it("should return proxy from https_proxy env var when HTTPS_PROXY not set", () => { |
| 325 | + process.env.https_proxy = "http://proxy.corp:3128" |
| 326 | + const result = getSystemProxyUrl() |
| 327 | + expect(result).toBe("http://proxy.corp:3128") |
| 328 | + }) |
| 329 | + |
| 330 | + it("should return proxy from HTTP_PROXY env var as fallback", () => { |
| 331 | + process.env.HTTP_PROXY = "http://proxy.corp:8080" |
| 332 | + const result = getSystemProxyUrl() |
| 333 | + expect(result).toBe("http://proxy.corp:8080") |
| 334 | + }) |
| 335 | + |
| 336 | + it("should return trimmed proxy from VS Code setting", () => { |
| 337 | + mockConfig.get.mockImplementation((key: string) => { |
| 338 | + if (key === "proxy") return " http://proxy.corp:3128 " |
| 339 | + return "" |
| 340 | + }) |
| 341 | + const result = getSystemProxyUrl() |
| 342 | + expect(result).toBe("http://proxy.corp:3128") |
| 343 | + }) |
| 344 | + |
| 345 | + it("should return undefined when no proxy configured", () => { |
| 346 | + mockConfig.get.mockReturnValue(undefined) |
| 347 | + const result = getSystemProxyUrl() |
| 348 | + expect(result).toBeUndefined() |
| 349 | + }) |
| 350 | + |
| 351 | + it("should handle VS Code API errors gracefully", () => { |
| 352 | + vi.mocked(vscode.workspace.getConfiguration).mockImplementation(() => { |
| 353 | + throw new Error("VS Code API unavailable") |
| 354 | + }) |
| 355 | + const result = getSystemProxyUrl() |
| 356 | + expect(result).toBeUndefined() |
| 357 | + }) |
| 358 | + |
| 359 | + it("should not return empty string proxy", () => { |
| 360 | + mockConfig.get.mockReturnValue("") |
| 361 | + const result = getSystemProxyUrl() |
| 362 | + expect(result).toBeUndefined() |
| 363 | + }) |
| 364 | + |
| 365 | + it("should prioritize env vars over VS Code settings", () => { |
| 366 | + process.env.HTTPS_PROXY = "http://env-proxy:3128" |
| 367 | + mockConfig.get.mockReturnValue("http://vscode-proxy:8080") |
| 368 | + const result = getSystemProxyUrl() |
| 369 | + expect(result).toBe("http://env-proxy:3128") |
| 370 | + }) |
| 371 | + }) |
308 | 372 | }) |
0 commit comments