|
1 | 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 |
| -} |
| 2 | +import { useEnv } from "../../utils/helpers" |
17 | 3 |
|
18 | 4 | describe("shouldEnableProxy", () => {
|
19 |
| - // Source: https://stackoverflow.com/a/48042799 |
20 |
| - const OLD_ENV = process.env |
| 5 | + const [setHTTPProxy, resetHTTPProxy] = useEnv("HTTP_PROXY") |
| 6 | + const [setHTTPSProxy, resetHTTPSProxy] = useEnv("HTTPS_PROXY") |
| 7 | + const [setNoProxy, resetNoProxy] = useEnv("NO_PROXY") |
21 | 8 |
|
22 | 9 | beforeEach(() => {
|
23 | 10 | 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 |
| 11 | + resetHTTPProxy() |
| 12 | + resetNoProxy() |
| 13 | + resetHTTPSProxy() |
29 | 14 | })
|
30 | 15 |
|
31 | 16 | it("returns true when HTTP_PROXY is set", () => {
|
32 |
| - const { cleanup } = setEnv("HTTP_PROXY", "http://proxy.example.com") |
| 17 | + setHTTPProxy("http://proxy.example.com") |
33 | 18 | expect(shouldEnableProxy()).toBe(true)
|
34 |
| - cleanup() |
35 | 19 | })
|
36 | 20 | it("returns true when HTTPS_PROXY is set", () => {
|
37 |
| - const { cleanup } = setEnv("HTTPS_PROXY", "http://proxy.example.com") |
| 21 | + setHTTPSProxy("https://proxy.example.com") |
38 | 22 | expect(shouldEnableProxy()).toBe(true)
|
39 |
| - cleanup() |
40 | 23 | })
|
41 | 24 | it("returns false when NO_PROXY is set", () => {
|
42 |
| - const { cleanup } = setEnv("NO_PROXY", "*") |
| 25 | + setNoProxy("*") |
43 | 26 | expect(shouldEnableProxy()).toBe(false)
|
44 |
| - cleanup() |
45 | 27 | })
|
46 | 28 | it("should return false when neither HTTP_PROXY nor HTTPS_PROXY is set", () => {
|
47 | 29 | expect(shouldEnableProxy()).toBe(false)
|
48 | 30 | })
|
49 | 31 | it("should return false when NO_PROXY is set to https://example.com", () => {
|
50 |
| - const { cleanup } = setEnv("NO_PROXY", "https://example.com") |
| 32 | + setNoProxy("https://example.com") |
51 | 33 | expect(shouldEnableProxy()).toBe(false)
|
52 |
| - cleanup() |
53 | 34 | })
|
54 | 35 | it("should return false when NO_PROXY is set to http://example.com", () => {
|
55 |
| - const { cleanup } = setEnv("NO_PROXY", "http://example.com") |
| 36 | + setNoProxy("http://example.com") |
56 | 37 | expect(shouldEnableProxy()).toBe(false)
|
57 |
| - cleanup() |
58 | 38 | })
|
59 | 39 | })
|
0 commit comments