Skip to content

Commit 6d928a0

Browse files
authored
Merge pull request #3846 from cdr/jsjoeio-test-should-enable-proxy
feat: add tests for shouldEnableProxy
2 parents 3969a39 + 85d8c14 commit 6d928a0

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
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/helpers.test.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { promises as fs } from "fs"
2-
import { tmpdir } from "../../test/utils/helpers"
2+
import { tmpdir, useEnv } from "../../test/utils/helpers"
33

44
/**
55
* This file is for testing test helpers (not core code).
@@ -12,3 +12,30 @@ describe("test helpers", () => {
1212
expect(fs.access(pathToTempDir)).resolves.toStrictEqual(undefined)
1313
})
1414
})
15+
16+
describe("useEnv", () => {
17+
beforeAll(() => {
18+
jest.resetModules()
19+
process.env.TEST_USE_ENV = "test environment variable"
20+
})
21+
afterAll(() => {
22+
delete process.env.TEST_USE_ENV
23+
})
24+
it("should set and reset the env var", () => {
25+
const envKey = "TEST_ENV_VAR"
26+
const [setValue, resetValue] = useEnv(envKey)
27+
setValue("hello-world")
28+
expect(process.env[envKey]).toEqual("hello-world")
29+
resetValue()
30+
expect(process.env[envKey]).toEqual(undefined)
31+
})
32+
it("should set and reset the env var where a value was already set", () => {
33+
const envKey = "TEST_USE_ENV"
34+
expect(process.env[envKey]).toEqual("test environment variable")
35+
const [setValue, resetValue] = useEnv(envKey)
36+
setValue("hello there")
37+
expect(process.env[envKey]).toEqual("hello there")
38+
resetValue()
39+
expect(process.env[envKey]).toEqual("test environment variable")
40+
})
41+
})

test/unit/node/proxy_agent.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { shouldEnableProxy } from "../../../src/node/proxy_agent"
2+
import { useEnv } from "../../utils/helpers"
3+
4+
describe("shouldEnableProxy", () => {
5+
const [setHTTPProxy, resetHTTPProxy] = useEnv("HTTP_PROXY")
6+
const [setHTTPSProxy, resetHTTPSProxy] = useEnv("HTTPS_PROXY")
7+
const [setNoProxy, resetNoProxy] = useEnv("NO_PROXY")
8+
9+
beforeEach(() => {
10+
jest.resetModules() // Most important - it clears the cache
11+
resetHTTPProxy()
12+
resetNoProxy()
13+
resetHTTPSProxy()
14+
})
15+
16+
it("returns true when HTTP_PROXY is set", () => {
17+
setHTTPProxy("http://proxy.example.com")
18+
expect(shouldEnableProxy()).toBe(true)
19+
})
20+
it("returns true when HTTPS_PROXY is set", () => {
21+
setHTTPSProxy("https://proxy.example.com")
22+
expect(shouldEnableProxy()).toBe(true)
23+
})
24+
it("returns false when NO_PROXY is set", () => {
25+
setNoProxy("*")
26+
expect(shouldEnableProxy()).toBe(false)
27+
})
28+
it("should return false when neither HTTP_PROXY nor HTTPS_PROXY is set", () => {
29+
expect(shouldEnableProxy()).toBe(false)
30+
})
31+
it("should return false when NO_PROXY is set to https://example.com", () => {
32+
setNoProxy("https://example.com")
33+
expect(shouldEnableProxy()).toBe(false)
34+
})
35+
it("should return false when NO_PROXY is set to http://example.com", () => {
36+
setNoProxy("http://example.com")
37+
expect(shouldEnableProxy()).toBe(false)
38+
})
39+
})

test/utils/helpers.ts

+24
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,27 @@ export async function tmpdir(testName: string): Promise<string> {
3737

3838
return await fs.mkdtemp(path.join(dir, `${testName}-`), { encoding: "utf8" })
3939
}
40+
41+
/**
42+
* @description Helper function to use an environment variable.
43+
*
44+
* @returns an array (similar to useState in React) with a function
45+
* to set the value and reset the value
46+
*/
47+
export function useEnv(key: string): [(nextValue: string | undefined) => string | undefined, () => void] {
48+
const initialValue = process.env[key]
49+
const setValue = (nextValue: string | undefined) => (process.env[key] = nextValue)
50+
// Node automatically converts undefined to string 'undefined'
51+
// when assigning an environment variable.
52+
// which is why we need to delete it if it's supposed to be undefined
53+
// Source: https://stackoverflow.com/a/60147167
54+
const resetValue = () => {
55+
if (initialValue !== undefined) {
56+
process.env[key] = initialValue
57+
} else {
58+
delete process.env[key]
59+
}
60+
}
61+
62+
return [setValue, resetValue]
63+
}

0 commit comments

Comments
 (0)