Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/node/proxy_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function newProxyAgent(inVSCode: boolean): http.Agent {

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

const httpProxy = proxyFromEnv.getProxyForUrl(`http://example.com`)
Expand Down
59 changes: 59 additions & 0 deletions test/unit/node/proxy_agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { shouldEnableProxy } from "../../../src/node/proxy_agent"

/**
* Helper function to set an env variable.
*
* Returns a function to cleanup the env variable.
*/
function setEnv(name: string, value: string) {
Copy link
Contributor

@GirlBossRush GirlBossRush Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: given this is a specific test helper, it would be nice if we could move the cleanup behavior into the beforeEach so that it may only have to be declared once. This is a bit like a React hook, and allows us to target a specific key, rather than dereferencing the entire process.env object.

// test_helpers.ts 
function useEnv(key: string) {
  const initialValue = process.env[key]
  const setValue = (nextValue: string | undefined) => process.env[key] = nextValue
  const resetValue = () => process.env[key] => initialValue

  return [setValue, resetValue]
}

// test.ts

const [setHTTPProxy, resetHTTPproxy] = useEnv(“HTTP_PROXY”)
const [setNoProxy, resetNoProxy] = useEnv(“NO_PROXY”)
{
  beforeEach() {
    jest.resetModule()
    resetHTTPproxy()
    resetNoProxy()
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh waaaay better than my solution. The original cleanup() I wrote was partially inspired by previous experience cleaning up useEffect functions. I really like your implementation. Thanks for writing that out! I'll make these changes.

process.env[name] = value

return {
cleanup() {
delete process.env[name]
},
}
}

describe("shouldEnableProxy", () => {
// Source: https://stackoverflow.com/a/48042799
const OLD_ENV = process.env

beforeEach(() => {
jest.resetModules() // Most important - it clears the cache
process.env = { ...OLD_ENV } // Make a copy
})

afterAll(() => {
process.env = OLD_ENV // Restore old environment
})

it("returns true when HTTP_PROXY is set", () => {
const { cleanup } = setEnv("HTTP_PROXY", "http://proxy.example.com")
expect(shouldEnableProxy()).toBe(true)
cleanup()
})
it("returns true when HTTPS_PROXY is set", () => {
const { cleanup } = setEnv("HTTPS_PROXY", "http://proxy.example.com")
expect(shouldEnableProxy()).toBe(true)
cleanup()
})
it("returns false when NO_PROXY is set", () => {
const { cleanup } = setEnv("NO_PROXY", "*")
expect(shouldEnableProxy()).toBe(false)
cleanup()
})
it("should return false when neither HTTP_PROXY nor HTTPS_PROXY is set", () => {
expect(shouldEnableProxy()).toBe(false)
})
it("should return false when NO_PROXY is set to https://example.com", () => {
const { cleanup } = setEnv("NO_PROXY", "https://example.com")
expect(shouldEnableProxy()).toBe(false)
cleanup()
})
it("should return false when NO_PROXY is set to http://example.com", () => {
const { cleanup } = setEnv("NO_PROXY", "http://example.com")
expect(shouldEnableProxy()).toBe(false)
cleanup()
})
})