Files
node/test/client-proxy/test-http-set-global-proxy-from-env-override-fetch.mjs
Joyee Cheung c0108bfc55 http: add http.setGlobalProxyFromEnv()
This adds an API to dynamically enable built-in proxy support
for all of fetch() and http.request()/https.request(), so
that users do not have to be aware of them all and configure them
one by one.

PR-URL: https://github.com/nodejs/node/pull/60953
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
2025-12-10 16:12:28 +00:00

45 lines
1.4 KiB
JavaScript

// Tests that http.setGlobalProxyFromEnv() overrides configuration from NODE_USE_ENV_PROXY.
import '../common/index.mjs';
import assert from 'node:assert';
import { once } from 'node:events';
import { startTestServers, checkProxiedFetch, createProxyServer } from '../common/proxy-server.js';
const { proxyLogs, shutdown, proxyUrl, httpEndpoint: { serverHost, requestUrl } } = await startTestServers({
httpEndpoint: true,
});
const { proxy: proxy2, logs: proxyLogs2 } = createProxyServer();
proxy2.listen(0);
await once(proxy2, 'listening');
await checkProxiedFetch({
FETCH_URL: requestUrl,
NODE_USE_ENV_PROXY: '1',
http_proxy: proxyUrl,
SET_GLOBAL_PROXY: JSON.stringify({
http_proxy: `http://localhost:${proxy2.address().port}`,
}),
}, {
stdout: 'Hello world',
});
shutdown();
proxy2.close();
// Verify request did NOT go through original proxy, but the overriden one.
assert.deepStrictEqual(proxyLogs, []);
// FIXME(undici:4083): undici currently always tunnels the request over
// CONNECT if proxyTunnel is not explicitly set to false, but what we
// need is for it to be automatically false for HTTP requests to be
// consistent with curl.
const expectedLogs = [{
method: 'CONNECT',
url: serverHost,
headers: {
'connection': 'close',
'host': serverHost,
'proxy-connection': 'keep-alive',
},
}];
assert.deepStrictEqual(proxyLogs2, expectedLogs);