test-proxy-environment.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { readFileSync } from 'node:fs'
  2. import { describe, expect, it } from 'vitest'
  3. import { PROXY_ENV_NAMES } from '../packages/util/http-proxy/src/policy.ts'
  4. import { clearAmbientProxyEnv, TEST_PROXY_SETUP_FILE, vitestConfigFiles } from './test-proxy-environment.ts'
  5. describe('ambient proxy environment', () => {
  6. it('clears every name the policy resolver reads, in both casings', () => {
  7. const env: NodeJS.ProcessEnv = {
  8. HTTP_PROXY: 'http://p:1', http_proxy: 'http://p:1',
  9. HTTPS_PROXY: 'http://p:1', https_proxy: 'http://p:1',
  10. ALL_PROXY: 'http://p:1', all_proxy: 'http://p:1',
  11. NO_PROXY: 'example.com', no_proxy: 'example.com',
  12. NODE_USE_ENV_PROXY: '1',
  13. PATH: '/usr/bin',
  14. }
  15. expect(clearAmbientProxyEnv(env)).toHaveLength(PROXY_ENV_NAMES.length + 1)
  16. expect(env).toEqual({ PATH: '/usr/bin' })
  17. })
  18. it('reports only the names that were set, and touches nothing else', () => {
  19. const env: NodeJS.ProcessEnv = { all_proxy: 'http://p:1', HOME: '/home/me' }
  20. expect(clearAmbientProxyEnv(env)).toEqual(['all_proxy'])
  21. expect(env).toEqual({ HOME: '/home/me' })
  22. })
  23. // A runtime assertion that this process is clear would pass either way: importing the module
  24. // above already ran it. What can actually regress is the wiring — a new Vitest project, or a
  25. // config that lists only the invariant host — so that is what this pins.
  26. const declared = vitestConfigFiles()
  27. .map(config => ({ config, slots: readFileSync(config, 'utf8').match(/setupFiles: \[[^\]]*\]/g) ?? [] }))
  28. .filter(entry => entry.slots.length > 0)
  29. it('finds the configurations that declare a setup at all', () => {
  30. // Guards the discovery itself: a glob that stopped matching would make every case below vacuous.
  31. expect(declared.map(entry => entry.config)).toEqual([
  32. 'vitest.config.ts', 'vitest.e2e.config.ts', 'vitest.expected.config.ts', 'vitest.snapshot.config.ts',
  33. ])
  34. })
  35. it.each(declared)('$config runs the setup in every setupFiles it declares', ({ slots }) => {
  36. for (const slot of slots) expect(slot).toContain(TEST_PROXY_SETUP_FILE)
  37. })
  38. })