|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +describe('Proxy Support', () => { |
| 4 | + beforeEach(() => { |
| 5 | + vi.resetModules(); |
| 6 | + vi.clearAllMocks(); |
| 7 | + }); |
| 8 | + |
| 9 | + describe('fetchUtil', () => { |
| 10 | + it('should export proxyFetch and getProxyStatus functions', async () => { |
| 11 | + // Mock config with proxy disabled |
| 12 | + vi.doMock('../../config.js', () => ({ |
| 13 | + PROXY_URL: '', |
| 14 | + PROXY_ENABLED: false |
| 15 | + })); |
| 16 | + |
| 17 | + const { proxyFetch, getProxyStatus } = await import('../../fetchUtil.js'); |
| 18 | + |
| 19 | + expect(proxyFetch).toBeDefined(); |
| 20 | + expect(typeof proxyFetch).toBe('function'); |
| 21 | + expect(getProxyStatus).toBeDefined(); |
| 22 | + expect(typeof getProxyStatus).toBe('function'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return disabled status when proxy is not configured', async () => { |
| 26 | + vi.doMock('../../config.js', () => ({ |
| 27 | + PROXY_URL: '', |
| 28 | + PROXY_ENABLED: false |
| 29 | + })); |
| 30 | + |
| 31 | + const { getProxyStatus } = await import('../../fetchUtil.js'); |
| 32 | + const status = getProxyStatus(); |
| 33 | + |
| 34 | + expect(status.enabled).toBe(false); |
| 35 | + expect(status.url).toBeNull(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return enabled status when proxy is configured', async () => { |
| 39 | + vi.doMock('../../config.js', () => ({ |
| 40 | + PROXY_URL: 'http://proxy.example.com:8080', |
| 41 | + PROXY_ENABLED: true |
| 42 | + })); |
| 43 | + |
| 44 | + const { getProxyStatus } = await import('../../fetchUtil.js'); |
| 45 | + const status = getProxyStatus(); |
| 46 | + |
| 47 | + expect(status.enabled).toBe(true); |
| 48 | + expect(status.url).toBe('http://proxy.example.com:8080'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should hide password in proxy URL when returning status', async () => { |
| 52 | + vi.doMock('../../config.js', () => ({ |
| 53 | + PROXY_URL: 'http://user:password@proxy.example.com:8080', |
| 54 | + PROXY_ENABLED: true |
| 55 | + })); |
| 56 | + |
| 57 | + const { getProxyStatus } = await import('../../fetchUtil.js'); |
| 58 | + const status = getProxyStatus(); |
| 59 | + |
| 60 | + expect(status.enabled).toBe(true); |
| 61 | + expect(status.url).toBe('http://user:****@proxy.example.com:8080'); |
| 62 | + expect(status.url).not.toContain('password'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should use standard fetch when proxy is disabled', async () => { |
| 66 | + vi.doMock('../../config.js', () => ({ |
| 67 | + PROXY_URL: '', |
| 68 | + PROXY_ENABLED: false |
| 69 | + })); |
| 70 | + |
| 71 | + global.fetch = vi.fn().mockResolvedValue({ |
| 72 | + ok: true, |
| 73 | + json: async () => ({ test: 'data' }) |
| 74 | + }); |
| 75 | + |
| 76 | + const { proxyFetch } = await import('../../fetchUtil.js'); |
| 77 | + |
| 78 | + await proxyFetch('https://example.com/api', { method: 'GET' }); |
| 79 | + |
| 80 | + expect(global.fetch).toHaveBeenCalledWith('https://example.com/api', { method: 'GET' }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle proxy configuration errors gracefully', async () => { |
| 84 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 85 | + |
| 86 | + vi.doMock('../../config.js', () => ({ |
| 87 | + PROXY_URL: 'invalid-url', |
| 88 | + PROXY_ENABLED: true |
| 89 | + })); |
| 90 | + |
| 91 | + global.fetch = vi.fn().mockResolvedValue({ |
| 92 | + ok: true, |
| 93 | + json: async () => ({ test: 'data' }) |
| 94 | + }); |
| 95 | + |
| 96 | + const { proxyFetch } = await import('../../fetchUtil.js'); |
| 97 | + |
| 98 | + // Should fall back to standard fetch if proxy initialization fails |
| 99 | + const result = await proxyFetch('https://example.com/api'); |
| 100 | + |
| 101 | + expect(result).toBeDefined(); |
| 102 | + expect(global.fetch).toHaveBeenCalled(); |
| 103 | + |
| 104 | + consoleErrorSpy.mockRestore(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('Config', () => { |
| 109 | + it('should export PROXY_URL and PROXY_ENABLED', async () => { |
| 110 | + const config = await import('../../config.js'); |
| 111 | + |
| 112 | + expect(config).toHaveProperty('PROXY_URL'); |
| 113 | + expect(config).toHaveProperty('PROXY_ENABLED'); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments