Skip to content

Commit bfa5e66

Browse files
batrlajoyeecheung
authored andcommitted
test: make proxy tests runnable when there is a system proxy
Some client-proxy tests set HTTP_PROXY variable in the environment. It's OK in general, however, in some specific cases, the user might have already defined his own http_proxy setting in the environment. The lowercase user setting takes precedence and leads to a test failure. The fix cleans up the environment to avoid an interaction between user and test proxy settings. PR-URL: #61473 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 6a447d7 commit bfa5e66

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

test/common/proxy-server.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,22 @@ function spawnPromisified(...args) {
156156
});
157157
}
158158

159+
function spawnOptions(envExtension) {
160+
const env = { ...process.env };
161+
// Cleanup the environment to avoid interference with client proxy tests.
162+
for (const key of ['http_proxy', 'https_proxy', 'no_proxy']) {
163+
delete env[key];
164+
delete env[key.toUpperCase()];
165+
}
166+
return { env: { ...env, ...envExtension } };
167+
}
168+
159169
async function checkProxied(type, envExtension, expectation, cliArgsExtension = []) {
160170
const script = type === 'fetch' ? fixtures.path('fetch-and-log.mjs') : fixtures.path('request-and-log.js');
161171
const { code, signal, stdout, stderr } = await spawnPromisified(
162172
process.execPath,
163-
[...cliArgsExtension, script], {
164-
env: {
165-
NO_LOG_REQUEST: '1',
166-
...process.env,
167-
...envExtension,
168-
},
169-
});
173+
[...cliArgsExtension, script],
174+
spawnOptions({ ...envExtension, NO_LOG_REQUEST: '1' }));
170175

171176
assert.deepStrictEqual({
172177
stderr: stderr.trim(),
@@ -193,24 +198,16 @@ exports.runProxiedRequest = async function(envExtension, cliArgsExtension = [])
193198
const fixtures = require('./fixtures');
194199
return spawnPromisified(
195200
process.execPath,
196-
[...cliArgsExtension, fixtures.path('request-and-log.js')], {
197-
env: {
198-
...process.env,
199-
...envExtension,
200-
},
201-
});
201+
[...cliArgsExtension, fixtures.path('request-and-log.js')],
202+
spawnOptions(envExtension));
202203
};
203204

204205
exports.runProxiedPOST = async function(envExtension) {
205206
const fixtures = require('./fixtures');
206207
return spawnPromisified(
207208
process.execPath,
208-
[fixtures.path('post-resource-and-log.js')], {
209-
env: {
210-
...process.env,
211-
...envExtension,
212-
},
213-
});
209+
[fixtures.path('post-resource-and-log.js')],
210+
spawnOptions(envExtension));
214211
};
215212

216213
exports.startTestServers = async function(options = {}) {

test/parallel/test-http2-allow-http1-upgrade-ws.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const WebSocketServer = require('../common/websocket-server');
2525
await new Promise((resolve, reject) => {
2626
const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
2727
dispatcher: new undici.EnvHttpProxyAgent({
28-
connect: { rejectUnauthorized: false }
28+
connect: { rejectUnauthorized: false },
29+
noProxy: '*',
2930
})
3031
});
3132
ws.addEventListener('open', common.mustCall(() => {

test/parallel/test-inspector-network-fetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ undici.setGlobalDispatcher(new undici.EnvHttpProxyAgent({
1818
connect: {
1919
rejectUnauthorized: false,
2020
},
21+
noProxy: '*',
2122
}));
2223

2324
const session = new inspector.Session();

0 commit comments

Comments
 (0)