nodejs/test/parallel/test-http-proxy-fetch.js
Joyee Cheung 5a7b7d2124
http: support http proxy for fetch under NODE_USE_ENV_PROXY
When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and
`NO_PROXY` environment variables during startup, and tunnels requests
over the specified proxy.

This currently only affects requests sent over `fetch()`. Support for
other built-in `http` and `https` methods is under way.

PR-URL: https://github.com/nodejs/node/pull/57165
Refs: https://github.com/nodejs/undici/issues/1650
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-03-20 14:35:05 +00:00

62 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { once } = require('events');
const http = require('http');
const { createProxyServer, checkProxiedRequest } = require('../common/proxy-server');
(async () => {
// Start a server to process the final request.
const server = http.createServer(common.mustCall((req, res) => {
res.end('Hello world');
}, 2));
server.on('error', common.mustNotCall((err) => { console.error('Server error', err); }));
server.listen(0);
await once(server, 'listening');
// Start a minimal proxy server.
const { proxy, logs } = createProxyServer();
proxy.listen(0);
await once(proxy, 'listening');
const serverHost = `localhost:${server.address().port}`;
// FIXME(undici:4083): undici currently always tunnels the request over
// CONNECT, no matter it's HTTP traffic or not, which is different from e.g.
// how curl behaves.
const expectedLogs = [{
method: 'CONNECT',
url: serverHost,
headers: {
// FIXME(undici:4086): this should be keep-alive.
connection: 'close',
host: serverHost
}
}];
// Check upper-cased HTTPS_PROXY environment variable.
await checkProxiedRequest({
NODE_USE_ENV_PROXY: 1,
FETCH_URL: `http://${serverHost}/test`,
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
}, {
stdout: 'Hello world',
});
assert.deepStrictEqual(logs, expectedLogs);
// Check lower-cased https_proxy environment variable.
logs.splice(0, logs.length);
await checkProxiedRequest({
NODE_USE_ENV_PROXY: 1,
FETCH_URL: `http://${serverHost}/test`,
http_proxy: `http://localhost:${proxy.address().port}`,
}, {
stdout: 'Hello world',
});
assert.deepStrictEqual(logs, expectedLogs);
proxy.close();
server.close();
})().then(common.mustCall());