|
| 1 | +/** |
| 2 | + * Unit tests for server/utils/proxy-agent.ts |
| 3 | + * |
| 4 | + * Covers: |
| 5 | + * - Returns null when HTTP_PROXY is not set |
| 6 | + * - Creates and returns a ProxyAgent when HTTP_PROXY is set |
| 7 | + * - Calls setGlobalDispatcher with the created agent |
| 8 | + * - Reads CA certificate when CUSTOM_CA_PATH is set and file exists |
| 9 | + * - Throws (and optionally calls process.exit) when CUSTOM_CA_PATH file is missing |
| 10 | + * - exitOnError=false rethrows instead of calling process.exit |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 14 | + |
| 15 | +// --- Mocks --- |
| 16 | + |
| 17 | +const mockSetGlobalDispatcher = vi.fn(); |
| 18 | +const mockProxyAgentInstance = { uri: '' }; |
| 19 | +const MockProxyAgent = vi.fn((opts: { uri: string }) => { |
| 20 | + mockProxyAgentInstance.uri = opts.uri; |
| 21 | + return mockProxyAgentInstance; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock('undici', () => ({ |
| 25 | + ProxyAgent: MockProxyAgent, |
| 26 | + setGlobalDispatcher: mockSetGlobalDispatcher, |
| 27 | +})); |
| 28 | + |
| 29 | +const mockExistsSync = vi.fn(); |
| 30 | +const mockReadFileSync = vi.fn(); |
| 31 | + |
| 32 | +vi.mock('fs', () => ({ |
| 33 | + default: { existsSync: mockExistsSync, readFileSync: mockReadFileSync }, |
| 34 | + existsSync: mockExistsSync, |
| 35 | + readFileSync: mockReadFileSync, |
| 36 | +})); |
| 37 | + |
| 38 | +// Import AFTER mocks are registered |
| 39 | +const { initializeProxyAgent } = await import('../server/utils/proxy-agent'); |
| 40 | + |
| 41 | +describe('initializeProxyAgent', () => { |
| 42 | + const originalEnv = process.env; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + process.env = { ...originalEnv }; |
| 46 | + vi.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + process.env = originalEnv; |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns null when HTTP_PROXY is not set', () => { |
| 54 | + delete process.env.HTTP_PROXY; |
| 55 | + const result = initializeProxyAgent(); |
| 56 | + expect(result).toBeNull(); |
| 57 | + expect(MockProxyAgent).not.toHaveBeenCalled(); |
| 58 | + expect(mockSetGlobalDispatcher).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('creates a ProxyAgent and sets global dispatcher when HTTP_PROXY is set', () => { |
| 62 | + process.env.HTTP_PROXY = 'http://proxy.example.com:8080'; |
| 63 | + delete process.env.CUSTOM_CA_PATH; |
| 64 | + |
| 65 | + const result = initializeProxyAgent(); |
| 66 | + |
| 67 | + expect(MockProxyAgent).toHaveBeenCalledOnce(); |
| 68 | + expect(MockProxyAgent).toHaveBeenCalledWith( |
| 69 | + expect.objectContaining({ uri: 'http://proxy.example.com:8080' }) |
| 70 | + ); |
| 71 | + expect(mockSetGlobalDispatcher).toHaveBeenCalledOnce(); |
| 72 | + expect(mockSetGlobalDispatcher).toHaveBeenCalledWith(mockProxyAgentInstance); |
| 73 | + expect(result).toBe(mockProxyAgentInstance); |
| 74 | + }); |
| 75 | + |
| 76 | + it('reads the CA file and passes tls options when CUSTOM_CA_PATH is set and file exists', () => { |
| 77 | + process.env.HTTP_PROXY = 'http://proxy.example.com:8080'; |
| 78 | + process.env.CUSTOM_CA_PATH = '/certs/custom-ca.crt'; |
| 79 | + const fakeBuffer = Buffer.from('cert-data'); |
| 80 | + mockExistsSync.mockReturnValue(true); |
| 81 | + mockReadFileSync.mockReturnValue(fakeBuffer); |
| 82 | + |
| 83 | + const result = initializeProxyAgent(); |
| 84 | + |
| 85 | + expect(mockExistsSync).toHaveBeenCalledWith('/certs/custom-ca.crt'); |
| 86 | + expect(mockReadFileSync).toHaveBeenCalledWith('/certs/custom-ca.crt'); |
| 87 | + expect(MockProxyAgent).toHaveBeenCalledWith( |
| 88 | + expect.objectContaining({ |
| 89 | + uri: 'http://proxy.example.com:8080', |
| 90 | + tls: { ca: [fakeBuffer] }, |
| 91 | + }) |
| 92 | + ); |
| 93 | + expect(result).toBe(mockProxyAgentInstance); |
| 94 | + }); |
| 95 | + |
| 96 | + it('throws an error when CUSTOM_CA_PATH is set but the file does not exist (exitOnError=false)', () => { |
| 97 | + process.env.HTTP_PROXY = 'http://proxy.example.com:8080'; |
| 98 | + process.env.CUSTOM_CA_PATH = '/certs/missing-ca.crt'; |
| 99 | + mockExistsSync.mockReturnValue(false); |
| 100 | + |
| 101 | + expect(() => initializeProxyAgent(false)).toThrowError( |
| 102 | + 'CUSTOM_CA_PATH file not found: /certs/missing-ca.crt' |
| 103 | + ); |
| 104 | + expect(mockSetGlobalDispatcher).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('calls process.exit(1) when initialization fails and exitOnError=true', () => { |
| 108 | + process.env.HTTP_PROXY = 'http://proxy.example.com:8080'; |
| 109 | + process.env.CUSTOM_CA_PATH = '/certs/missing-ca.crt'; |
| 110 | + mockExistsSync.mockReturnValue(false); |
| 111 | + |
| 112 | + // Throw a sentinel so execution stops after process.exit — matching real process termination. |
| 113 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation( |
| 114 | + (() => { throw new Error('process.exit called'); }) as () => never |
| 115 | + ); |
| 116 | + |
| 117 | + expect(() => initializeProxyAgent(true)).toThrowError('process.exit called'); |
| 118 | + expect(exitSpy).toHaveBeenCalledWith(1); |
| 119 | + |
| 120 | + exitSpy.mockRestore(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments