Skip to content

Commit a6d6d51

Browse files
trivikraduh95
authored andcommitted
test: deflake connection refused proxy tests
Use 127.0.0.1:10, matching existing refused-connection tests, instead of binding and releasing an ephemeral port that can be reused before the child process connects. Clear NO_PROXY and no_proxy so local proxy bypass settings do not skip the proxy connection attempt. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: #63395 Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-05-15.md#jstest-failure Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent cf6cbbd commit a6d6d51

2 files changed

Lines changed: 24 additions & 55 deletions

File tree

test/client-proxy/test-http-proxy-request-connection-refused.mjs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,19 @@ await once(server, 'listening');
1515
const serverHost = `localhost:${server.address().port}`;
1616
const requestUrl = `http://${serverHost}/test`;
1717

18-
let maxRetries = 10;
1918
let foundRefused = false;
20-
while (maxRetries-- > 0) {
21-
// Make it fail on connection refused by connecting to a port of a closed server.
22-
// If it succeeds, get a different port and retry.
23-
const proxy = http.createServer((req, res) => {
24-
res.destroy();
25-
});
26-
proxy.listen(0);
27-
await once(proxy, 'listening');
28-
const port = proxy.address().port;
29-
proxy.close();
30-
await once(proxy, 'close');
19+
const port = 10;
20+
console.log(`Trying proxy at port ${port}`);
21+
const { stderr } = await runProxiedRequest({
22+
NODE_USE_ENV_PROXY: 1,
23+
REQUEST_URL: requestUrl,
24+
HTTP_PROXY: `http://127.0.0.1:${port}`,
25+
NO_PROXY: '',
26+
no_proxy: '',
27+
REQUEST_TIMEOUT: 5000,
28+
});
3129

32-
console.log(`Trying proxy at port ${port}`);
33-
const { stderr } = await runProxiedRequest({
34-
NODE_USE_ENV_PROXY: 1,
35-
REQUEST_URL: requestUrl,
36-
HTTP_PROXY: `http://localhost:${port}`,
37-
REQUEST_TIMEOUT: 5000,
38-
});
39-
40-
foundRefused = /Error.*connect ECONNREFUSED/.test(stderr);
41-
if (foundRefused) {
42-
// The proxy client should get a connection refused error.
43-
break;
44-
}
45-
}
30+
foundRefused = /Error.*connect ECONNREFUSED/.test(stderr);
4631

4732
server.close();
4833
assert(foundRefused, 'Expected ECONNREFUSED error from proxy request');

test/client-proxy/test-https-proxy-request-connection-refused.mjs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import fixtures from '../common/fixtures.js';
55
import assert from 'node:assert';
66
import { once } from 'events';
77
import { runProxiedRequest } from '../common/proxy-server.js';
8-
import http from 'node:http';
98

109
if (!common.hasCrypto)
1110
common.skip('missing crypto');
@@ -25,35 +24,20 @@ await once(server, 'listening');
2524
const serverHost = `localhost:${server.address().port}`;
2625
const requestUrl = `https://${serverHost}/test`;
2726

28-
let maxRetries = 10;
2927
let foundRefused = false;
30-
while (maxRetries-- > 0) {
31-
// Make it fail on connection refused by connecting to a port of a closed server.
32-
// If it succeeds, get a different port and retry.
33-
const proxy = http.createServer((req, res) => {
34-
res.destroy();
35-
});
36-
proxy.listen(0);
37-
await once(proxy, 'listening');
38-
const port = proxy.address().port;
39-
proxy.close();
40-
await once(proxy, 'close');
41-
42-
console.log(`Trying proxy at port ${port}`);
43-
const { stderr } = await runProxiedRequest({
44-
NODE_USE_ENV_PROXY: 1,
45-
REQUEST_URL: requestUrl,
46-
HTTPS_PROXY: `http://localhost:${port}`,
47-
NODE_EXTRA_CA_CERTS: fixtures.path('keys', 'fake-startcom-root-cert.pem'),
48-
REQUEST_TIMEOUT: 5000,
49-
});
50-
51-
foundRefused = /Error.*connect ECONNREFUSED/.test(stderr);
52-
if (foundRefused) {
53-
// The proxy client should get a connection refused error.
54-
break;
55-
}
56-
}
28+
const port = 10;
29+
console.log(`Trying proxy at port ${port}`);
30+
const { stderr } = await runProxiedRequest({
31+
NODE_USE_ENV_PROXY: 1,
32+
REQUEST_URL: requestUrl,
33+
HTTPS_PROXY: `http://127.0.0.1:${port}`,
34+
NO_PROXY: '',
35+
no_proxy: '',
36+
NODE_EXTRA_CA_CERTS: fixtures.path('keys', 'fake-startcom-root-cert.pem'),
37+
REQUEST_TIMEOUT: 5000,
38+
});
39+
40+
foundRefused = /Error.*connect ECONNREFUSED/.test(stderr);
5741

5842
server.close();
5943
assert(foundRefused, 'Expected ECONNREFUSED error from proxy request');

0 commit comments

Comments
 (0)