|
| 1 | +import { shouldEnableProxy } from "../../../src/node/proxy_agent" |
| 2 | + |
| 3 | +/** |
| 4 | + * Helper function to set an env variable. |
| 5 | + * |
| 6 | + * Returns a function to cleanup the env variable. |
| 7 | + */ |
| 8 | +function setEnv(name: string, value: string) { |
| 9 | + process.env[name] = value |
| 10 | + |
| 11 | + return { |
| 12 | + cleanup() { |
| 13 | + delete process.env[name] |
| 14 | + }, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("shouldEnableProxy", () => { |
| 19 | + // Source: https://stackoverflow.com/a/48042799 |
| 20 | + const OLD_ENV = process.env |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + jest.resetModules() // Most important - it clears the cache |
| 24 | + process.env = { ...OLD_ENV } // Make a copy |
| 25 | + }) |
| 26 | + |
| 27 | + afterAll(() => { |
| 28 | + process.env = OLD_ENV // Restore old environment |
| 29 | + }) |
| 30 | + |
| 31 | + it("returns true when HTTP_PROXY is set", () => { |
| 32 | + const { cleanup } = setEnv("HTTP_PROXY", "http://proxy.example.com") |
| 33 | + expect(shouldEnableProxy()).toBe(true) |
| 34 | + cleanup() |
| 35 | + }) |
| 36 | + it("returns true when HTTPS_PROXY is set", () => { |
| 37 | + const { cleanup } = setEnv("HTTPS_PROXY", "http://proxy.example.com") |
| 38 | + expect(shouldEnableProxy()).toBe(true) |
| 39 | + cleanup() |
| 40 | + }) |
| 41 | + it("returns false when NO_PROXY is set", () => { |
| 42 | + const { cleanup } = setEnv("NO_PROXY", "*") |
| 43 | + expect(shouldEnableProxy()).toBe(false) |
| 44 | + cleanup() |
| 45 | + }) |
| 46 | + it("should return false when neither HTTP_PROXY nor HTTPS_PROXY is set", () => { |
| 47 | + expect(shouldEnableProxy()).toBe(false) |
| 48 | + }) |
| 49 | + it("should return false when NO_PROXY is set to https://example.com", () => { |
| 50 | + const { cleanup } = setEnv("NO_PROXY", "https://example.com") |
| 51 | + expect(shouldEnableProxy()).toBe(false) |
| 52 | + cleanup() |
| 53 | + }) |
| 54 | + it("should return false when NO_PROXY is set to http://example.com", () => { |
| 55 | + const { cleanup } = setEnv("NO_PROXY", "http://example.com") |
| 56 | + expect(shouldEnableProxy()).toBe(false) |
| 57 | + cleanup() |
| 58 | + }) |
| 59 | +}) |
0 commit comments