test: deflake connection refused proxy tests · nodejs/node@0930c21

@@ -5,7 +5,7 @@ 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 dgram from 'node:dgram';

8+

import http from 'node:http';

991010

if (!common.hasCrypto)

1111

common.skip('missing crypto');

@@ -25,24 +25,35 @@ await once(server, 'listening');

2525

const serverHost = `localhost:${server.address().port}`;

2626

const requestUrl = `https://${serverHost}/test`;

272728-

// Make it fail on connection refused by connecting to a UDP port with TCP.

29-

const udp = dgram.createSocket('udp4');

30-

udp.bind(0, '127.0.0.1');

31-

await once(udp, 'listening');

32-

const port = udp.address().port;

33-34-

const { code, signal, stderr, stdout } = await runProxiedRequest({

35-

NODE_USE_ENV_PROXY: 1,

36-

REQUEST_URL: requestUrl,

37-

HTTPS_PROXY: `http://localhost:${port}`,

38-

NODE_EXTRA_CA_CERTS: fixtures.path('keys', 'fake-startcom-root-cert.pem'),

39-

});

40-41-

// The proxy client should get a connection refused error.

42-

assert.match(stderr, /Error.*connect ECONNREFUSED/);

43-

assert.strictEqual(stdout.trim(), '');

44-

assert.strictEqual(code, 0);

45-

assert.strictEqual(signal, null);

28+

let maxRetries = 10;

29+

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+

}

46574758

server.close();

48-

udp.close();

59+

assert(foundRefused, 'Expected ECONNREFUSED error from proxy request');