network-proxy.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. resolveTuiProxyConfiguration,
  4. shouldBypassTuiProxy,
  5. } from '../../src/cli/network-proxy.js';
  6. const PROXY_URL = 'http://proxy.example.invalid:8080';
  7. describe('NO_PROXY environment precedence', () => {
  8. it.each([undefined, '', ' \t\n '])(
  9. 'falls back to no_proxy when NO_PROXY is %j',
  10. (noProxy) => {
  11. const configuration = resolveTuiProxyConfiguration({
  12. HTTPS_PROXY: PROXY_URL,
  13. NO_PROXY: noProxy,
  14. no_proxy: ' internal.example.invalid ',
  15. });
  16. expect(configuration.mode).toBe('proxy');
  17. if (configuration.mode !== 'proxy') throw new Error('Expected proxy configuration');
  18. expect(
  19. shouldBypassTuiProxy(new URL('https://internal.example.invalid/v1'), configuration.noProxy),
  20. ).toBe(true);
  21. expect(
  22. shouldBypassTuiProxy(new URL('https://external.example.invalid/v1'), configuration.noProxy),
  23. ).toBe(false);
  24. for (const host of ['localhost', '127.0.0.1', '[::1]']) {
  25. expect(shouldBypassTuiProxy(new URL(`http://${host}/`), configuration.noProxy)).toBe(true);
  26. }
  27. },
  28. );
  29. it('prefers a nonblank NO_PROXY over no_proxy', () => {
  30. const configuration = resolveTuiProxyConfiguration({
  31. HTTPS_PROXY: PROXY_URL,
  32. NO_PROXY: ' upper.example.invalid ',
  33. no_proxy: 'lower.example.invalid',
  34. });
  35. expect(configuration.mode).toBe('proxy');
  36. if (configuration.mode !== 'proxy') throw new Error('Expected proxy configuration');
  37. expect(shouldBypassTuiProxy(new URL('https://upper.example.invalid/'), configuration.noProxy)).toBe(
  38. true,
  39. );
  40. expect(shouldBypassTuiProxy(new URL('https://lower.example.invalid/'), configuration.noProxy)).toBe(
  41. false,
  42. );
  43. });
  44. it.each([undefined, '', ' \t '])(
  45. 'preserves the loopback defaults when both spellings are %j',
  46. (noProxy) => {
  47. const configuration = resolveTuiProxyConfiguration({
  48. HTTPS_PROXY: PROXY_URL,
  49. NO_PROXY: noProxy,
  50. no_proxy: noProxy,
  51. });
  52. expect(configuration.mode).toBe('proxy');
  53. if (configuration.mode !== 'proxy') throw new Error('Expected proxy configuration');
  54. expect(configuration.noProxy).toBe('localhost,127.0.0.1,::1');
  55. },
  56. );
  57. it('stays direct when only a bypass list is configured', () => {
  58. expect(resolveTuiProxyConfiguration({ NO_PROXY: '', no_proxy: 'internal.example.invalid' })).toEqual(
  59. { mode: 'direct' },
  60. );
  61. });
  62. });