Skip to content

Commit 67e9eca

Browse files
committed
feat: add tests for shouldEnableProxy
1 parent 7b8cd25 commit 67e9eca

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/node/proxy_agent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function newProxyAgent(inVSCode: boolean): http.Agent {
6262

6363
// If they have $NO_PROXY set to example.com then this check won't work!
6464
// But that's drastically unlikely.
65-
function shouldEnableProxy(): boolean {
65+
export function shouldEnableProxy(): boolean {
6666
let shouldEnable = false
6767

6868
const httpProxy = proxyFromEnv.getProxyForUrl(`http://example.com`)

test/unit/node/proxy_agent.test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)