|
| 1 | +// This tests that NO_PROXY matches alternate loopback host spellings. |
| 2 | + |
| 3 | +import * as common from '../common/index.mjs'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import { once } from 'events'; |
| 6 | +import http from 'node:http'; |
| 7 | +import { runProxiedRequest } from '../common/proxy-server.js'; |
| 8 | + |
| 9 | +if (!common.hasIPv6) { |
| 10 | + common.skip('missing IPv6 support'); |
| 11 | +} |
| 12 | + |
| 13 | +const server4 = http.createServer(common.mustCall((req, res) => { |
| 14 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 15 | + res.end('Hello World\n'); |
| 16 | +}, 2)); |
| 17 | +server4.listen(0, '127.0.0.1'); |
| 18 | +await once(server4, 'listening'); |
| 19 | + |
| 20 | +const server6 = http.createServer(common.mustCall((req, res) => { |
| 21 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 22 | + res.end('Hello World\n'); |
| 23 | +}, 1)); |
| 24 | +server6.listen(0, '::1'); |
| 25 | +await once(server6, 'listening'); |
| 26 | + |
| 27 | +// Start a proxy server that should NOT be used. |
| 28 | +const proxy = http.createServer(); |
| 29 | +proxy.on('request', common.mustNotCall()); |
| 30 | +proxy.on('connect', common.mustNotCall()); |
| 31 | +proxy.listen(0); |
| 32 | +await once(proxy, 'listening'); |
| 33 | + |
| 34 | +// A trailing dot in a DNS name should match the dotless NO_PROXY entry. |
| 35 | +{ |
| 36 | + const { code, signal, stderr, stdout } = await runProxiedRequest({ |
| 37 | + NODE_USE_ENV_PROXY: 1, |
| 38 | + REQUEST_URL: `http://localhost.:${server4.address().port}/test`, |
| 39 | + HTTP_PROXY: `http://localhost:${proxy.address().port}`, |
| 40 | + NO_PROXY: 'localhost', |
| 41 | + RESOLVE_TO_LOCALHOST: 'localhost.', |
| 42 | + }); |
| 43 | + |
| 44 | + assert.match(stdout, /Status Code: 200/); |
| 45 | + assert.match(stdout, /Hello World/); |
| 46 | + assert.match(stdout, /Resolving lookup for localhost\./); |
| 47 | + assert.strictEqual(stderr.trim(), ''); |
| 48 | + assert.strictEqual(code, 0); |
| 49 | + assert.strictEqual(signal, null); |
| 50 | +} |
| 51 | + |
| 52 | +// Bracketed IPv6 URL hosts should match unbracketed NO_PROXY entries. |
| 53 | +{ |
| 54 | + const { code, signal, stderr, stdout } = await runProxiedRequest({ |
| 55 | + NODE_USE_ENV_PROXY: 1, |
| 56 | + REQUEST_URL: `http://[::1]:${server6.address().port}/test`, |
| 57 | + HTTP_PROXY: `http://localhost:${proxy.address().port}`, |
| 58 | + NO_PROXY: '::1', |
| 59 | + }); |
| 60 | + |
| 61 | + assert.match(stdout, /Status Code: 200/); |
| 62 | + assert.match(stdout, /Hello World/); |
| 63 | + assert.strictEqual(stderr.trim(), ''); |
| 64 | + assert.strictEqual(code, 0); |
| 65 | + assert.strictEqual(signal, null); |
| 66 | +} |
| 67 | + |
| 68 | +// IPv4-mapped IPv6 loopback should match its IPv4 NO_PROXY entry. |
| 69 | +{ |
| 70 | + const { code, signal, stderr, stdout } = await runProxiedRequest({ |
| 71 | + NODE_USE_ENV_PROXY: 1, |
| 72 | + REQUEST_URL: `http://[::ffff:7f00:1]:${server4.address().port}/test`, |
| 73 | + HTTP_PROXY: `http://localhost:${proxy.address().port}`, |
| 74 | + NO_PROXY: 'localhost,127.0.0.1,::1', |
| 75 | + }); |
| 76 | + |
| 77 | + assert.match(stdout, /Status Code: 200/); |
| 78 | + assert.match(stdout, /Hello World/); |
| 79 | + assert.strictEqual(stderr.trim(), ''); |
| 80 | + assert.strictEqual(code, 0); |
| 81 | + assert.strictEqual(signal, null); |
| 82 | +} |
| 83 | + |
| 84 | +proxy.close(); |
| 85 | +server6.close(); |
| 86 | +server4.close(); |
0 commit comments