|
| 1 | +// Tests that NODE_PROXY_TUNNEL=false disables CONNECT tunneling |
| 2 | +// and uses direct HTTP forwarding instead. |
| 3 | + |
| 4 | +import * as common from '../common/index.mjs'; |
| 5 | +import assert from 'node:assert'; |
| 6 | +import http from 'node:http'; |
| 7 | +import { once } from 'events'; |
| 8 | +import { createProxyServer, checkProxiedFetch } from '../common/proxy-server.js'; |
| 9 | + |
| 10 | +// Start a minimal proxy server. |
| 11 | +const { proxy, logs } = createProxyServer(); |
| 12 | +proxy.listen(0); |
| 13 | +await once(proxy, 'listening'); |
| 14 | + |
| 15 | +delete process.env.NODE_USE_ENV_PROXY; // Ensure the environment variable is not set. |
| 16 | + |
| 17 | +// Start a HTTP server to process the final request. |
| 18 | +const server = http.createServer(common.mustCall((req, res) => { |
| 19 | + res.end('Hello world'); |
| 20 | +}, 2)); |
| 21 | +server.on('error', common.mustNotCall((err) => { console.error('Server error', err); })); |
| 22 | +server.listen(0); |
| 23 | +await once(server, 'listening'); |
| 24 | + |
| 25 | +const serverHost = `localhost:${server.address().port}`; |
| 26 | +const requestUrl = `http://${serverHost}/test`; |
| 27 | + |
| 28 | +// Test: with NODE_PROXY_TUNNEL=false, fetch should NOT use CONNECT tunneling |
| 29 | +// but instead use direct HTTP forwarding. |
| 30 | +{ |
| 31 | + await checkProxiedFetch({ |
| 32 | + FETCH_URL: requestUrl, |
| 33 | + HTTP_PROXY: `http://localhost:${proxy.address().port}`, |
| 34 | + NODE_USE_ENV_PROXY: '1', |
| 35 | + NODE_PROXY_TUNNEL: 'false', |
| 36 | + }, { |
| 37 | + stdout: 'Hello world', |
| 38 | + }); |
| 39 | + |
| 40 | + // With proxyTunnel: false, undici uses Http1ProxyWrapper which rewrites |
| 41 | + // the request path to include the origin (e.g. "localhost:PORT/test") |
| 42 | + // and sends it as a normal GET request instead of CONNECT. |
| 43 | + assert.strictEqual(logs[0].method, 'GET'); |
| 44 | + assert.strictEqual(logs[0].url, requestUrl); |
| 45 | + assert.strictEqual(logs[0].headers.host, serverHost); |
| 46 | +} |
| 47 | + |
| 48 | +// Test: without NODE_PROXY_TUNNEL (default), fetch still uses CONNECT tunneling. |
| 49 | +{ |
| 50 | + logs.splice(0, logs.length); |
| 51 | + await checkProxiedFetch({ |
| 52 | + FETCH_URL: requestUrl, |
| 53 | + HTTP_PROXY: `http://localhost:${proxy.address().port}`, |
| 54 | + NODE_USE_ENV_PROXY: '1', |
| 55 | + }, { |
| 56 | + stdout: 'Hello world', |
| 57 | + }); |
| 58 | + |
| 59 | + // Without NODE_PROXY_TUNNEL set, CONNECT tunneling is used by default. |
| 60 | + assert.deepStrictEqual(logs, [{ |
| 61 | + method: 'CONNECT', |
| 62 | + url: serverHost, |
| 63 | + headers: { |
| 64 | + 'connection': 'close', |
| 65 | + 'proxy-connection': 'keep-alive', |
| 66 | + 'host': serverHost, |
| 67 | + }, |
| 68 | + }]); |
| 69 | +} |
| 70 | + |
| 71 | +server.close(); |
| 72 | +proxy.close(); |
0 commit comments