|
| 1 | +import { configureProxyFromEnvironment } from './proxy-config'; |
| 2 | + |
| 3 | +// Mock undici |
| 4 | +jest.mock('undici', () => ({ |
| 5 | + ProxyAgent: jest.fn(), |
| 6 | + setGlobalDispatcher: jest.fn(), |
| 7 | + getGlobalDispatcher: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +import { ProxyAgent, setGlobalDispatcher } from 'undici'; |
| 11 | + |
| 12 | +describe('proxy-config', () => { |
| 13 | + let originalEnv: NodeJS.ProcessEnv; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // Save original environment |
| 17 | + originalEnv = { ...process.env }; |
| 18 | + |
| 19 | + // Clear all proxy-related env vars |
| 20 | + delete process.env.https_proxy; |
| 21 | + delete process.env.HTTPS_PROXY; |
| 22 | + delete process.env.http_proxy; |
| 23 | + delete process.env.HTTP_PROXY; |
| 24 | + delete process.env.no_proxy; |
| 25 | + delete process.env.NO_PROXY; |
| 26 | + |
| 27 | + // Clear mocks |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + // Restore original environment |
| 33 | + process.env = originalEnv; |
| 34 | + }); |
| 35 | + |
| 36 | + describe('configureProxyFromEnvironment', () => { |
| 37 | + it('should configure proxy when HTTPS_PROXY is set', () => { |
| 38 | + process.env.HTTPS_PROXY = 'http://proxy.example.com:8080'; |
| 39 | + |
| 40 | + configureProxyFromEnvironment(); |
| 41 | + |
| 42 | + expect(ProxyAgent).toHaveBeenCalledWith({ |
| 43 | + uri: 'http://proxy.example.com:8080', |
| 44 | + }); |
| 45 | + expect(setGlobalDispatcher).toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should configure proxy when https_proxy is set (lowercase)', () => { |
| 49 | + process.env.https_proxy = 'http://proxy.example.com:8080'; |
| 50 | + |
| 51 | + configureProxyFromEnvironment(); |
| 52 | + |
| 53 | + expect(ProxyAgent).toHaveBeenCalledWith({ |
| 54 | + uri: 'http://proxy.example.com:8080', |
| 55 | + }); |
| 56 | + expect(setGlobalDispatcher).toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should prefer HTTPS_PROXY over HTTP_PROXY', () => { |
| 60 | + process.env.HTTPS_PROXY = 'http://https-proxy.example.com:8443'; |
| 61 | + process.env.HTTP_PROXY = 'http://http-proxy.example.com:8080'; |
| 62 | + |
| 63 | + configureProxyFromEnvironment(); |
| 64 | + |
| 65 | + expect(ProxyAgent).toHaveBeenCalledWith({ |
| 66 | + uri: 'http://https-proxy.example.com:8443', |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should fall back to HTTP_PROXY if HTTPS_PROXY is not set', () => { |
| 71 | + process.env.HTTP_PROXY = 'http://proxy.example.com:8080'; |
| 72 | + |
| 73 | + configureProxyFromEnvironment(); |
| 74 | + |
| 75 | + expect(ProxyAgent).toHaveBeenCalledWith({ |
| 76 | + uri: 'http://proxy.example.com:8080', |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not configure proxy when no proxy env vars are set', () => { |
| 81 | + configureProxyFromEnvironment(); |
| 82 | + |
| 83 | + expect(ProxyAgent).not.toHaveBeenCalled(); |
| 84 | + expect(setGlobalDispatcher).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle errors gracefully', () => { |
| 88 | + process.env.HTTPS_PROXY = 'http://proxy.example.com:8080'; |
| 89 | + const mockProxyAgent = ProxyAgent as unknown as jest.Mock; |
| 90 | + mockProxyAgent.mockImplementation(() => { |
| 91 | + throw new Error('ProxyAgent error'); |
| 92 | + }); |
| 93 | + |
| 94 | + // Should not throw |
| 95 | + expect(() => configureProxyFromEnvironment()).not.toThrow(); |
| 96 | + |
| 97 | + // Should still attempt to create ProxyAgent |
| 98 | + expect(ProxyAgent).toHaveBeenCalled(); |
| 99 | + // Should not set dispatcher if ProxyAgent creation fails |
| 100 | + expect(setGlobalDispatcher).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments