|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { |
| 9 | + EnvHttpProxyAgent, |
| 10 | + setGlobalDispatcher, |
| 11 | + getGlobalDispatcher, |
| 12 | +} from 'undici'; |
| 13 | +import { |
| 14 | + setGlobalProxy, |
| 15 | + fetchWithTimeout, |
| 16 | + createSafeProxyAgent, |
| 17 | +} from './fetch.js'; |
| 18 | + |
| 19 | +describe('Proxy Bypass Integration', () => { |
| 20 | + const originalDispatcher = getGlobalDispatcher(); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + vi.unstubAllEnvs(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + setGlobalDispatcher(originalDispatcher); |
| 28 | + vi.unstubAllEnvs(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should bypass proxy for localhost when NO_PROXY is set', async () => { |
| 32 | + // We use a dummy proxy that will fail if any request is sent to it. |
| 33 | + const dummyProxy = 'http://1.2.3.4:5678'; |
| 34 | + |
| 35 | + // Set a dummy proxy and NO_PROXY for localhost |
| 36 | + vi.stubEnv('NO_PROXY', 'localhost'); |
| 37 | + |
| 38 | + // This will set the global dispatcher to a new EnvHttpProxyAgent |
| 39 | + setGlobalProxy(dummyProxy); |
| 40 | + |
| 41 | + // We expect fetch to localhost to bypass the proxy. |
| 42 | + // Since we don't have a real localhost server running in this unit test, |
| 43 | + // we can't easily do a REAL fetch, but we can verify the dispatcher's behavior. |
| 44 | + |
| 45 | + const dispatcher = getGlobalDispatcher(); |
| 46 | + expect(dispatcher).toBeInstanceOf(EnvHttpProxyAgent); |
| 47 | + |
| 48 | + // To do a real integration test, we would need a local server. |
| 49 | + // Let's start a small http server. |
| 50 | + |
| 51 | + const http = await import('node:http'); |
| 52 | + const server = http.createServer((req, res) => { |
| 53 | + res.end('ok'); |
| 54 | + }); |
| 55 | + |
| 56 | + await new Promise<void>((resolve) => |
| 57 | + server.listen(0, '127.0.0.1', () => resolve()), |
| 58 | + ); |
| 59 | + const address = server.address(); |
| 60 | + if (!address || typeof address === 'string') { |
| 61 | + throw new Error('Server address not found'); |
| 62 | + } |
| 63 | + const port = address.port; |
| 64 | + const url = `http://127.0.0.1:${port}`; |
| 65 | + |
| 66 | + try { |
| 67 | + // We need NO_PROXY to include 127.0.0.1 (or use localhost and fetch localhost) |
| 68 | + vi.stubEnv('NO_PROXY', `127.0.0.1,localhost`); |
| 69 | + setGlobalProxy(dummyProxy); |
| 70 | + |
| 71 | + // If bypass works, this succeeds. If it tries to use the dummy proxy, it fails/times out. |
| 72 | + const response = await fetchWithTimeout(url, 1000); |
| 73 | + expect(await response.text()).toBe('ok'); |
| 74 | + } finally { |
| 75 | + server.close(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it('should USE proxy for other domains when NO_PROXY is set to localhost', async () => { |
| 80 | + const dummyProxy = 'http://1.2.3.4:5678'; |
| 81 | + vi.stubEnv('NO_PROXY', 'localhost'); |
| 82 | + setGlobalProxy(dummyProxy); |
| 83 | + |
| 84 | + // Fetching a public domain should try to use the proxy and fail (since the proxy is dummy). |
| 85 | + // We expect it to time out or fail with a connection error. |
| 86 | + await expect(fetchWithTimeout('http://example.com', 500)).rejects.toThrow(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('createSafeProxyAgent should return a proxy agent that respects NO_PROXY', () => { |
| 90 | + const proxyUrl = 'http://proxy.example.com'; |
| 91 | + const noProxyValue = 'localhost,127.0.0.1'; |
| 92 | + vi.stubEnv('NO_PROXY', noProxyValue); |
| 93 | + |
| 94 | + const agent = createSafeProxyAgent(proxyUrl); |
| 95 | + expect(agent).toBeInstanceOf(EnvHttpProxyAgent); |
| 96 | + // Since we can't easily inspect internal state, this is mostly a type and presence check. |
| 97 | + }); |
| 98 | +}); |
0 commit comments