update unit tests

This commit is contained in:
Steve-Mcl 2024-03-17 16:36:24 +00:00
parent bf065ee11d
commit dcfb4c9a79

View File

@ -62,19 +62,19 @@ const { getProxyForUrl } = require("nr-test-utils").require('@node-red/nodes/cor
* @param {object} env - The environment variables to use for the test * @param {object} env - The environment variables to use for the test
* @param {*} expected - The expected result * @param {*} expected - The expected result
* @param {*} input - The input to test * @param {*} input - The input to test
* @param {import('../../../../../packages/node_modules/@node-red/nodes/core/network/lib/proxyHelper').ProxyOptions} options - The options to use for getProxyForUrl * @param {import('../../../../../packages/node_modules/@node-red/nodes/core/network/lib/proxyHelper').ProxyOptions} [options] - The options to use for getProxyForUrl
* @param {string} [testName] - The name of the test (auto computed if not provided)
*/ */
function testProxyUrl(env, expected, input, options) { function testProxyUrl(env, expected, input, options, testName) {
assert(typeof env === 'object' && env !== null); assert(typeof env === 'object' && env !== null);
// Copy object to make sure that the in param does not get modified between // Copy object to make sure that the in param does not get modified between
// the call of this function and the use of it below. // the call of this function and the use of it below.
env = JSON.parse(JSON.stringify(env)); env = JSON.parse(JSON.stringify(env));
const title = 'getProxyForUrl(' + JSON.stringify(input) + ')' + const title = testName || 'Proxy for URL ' + JSON.stringify(input) + ' === ' + JSON.stringify(expected);
' === ' + JSON.stringify(expected);
// Save call stack for later use. // Save call stack for later use.
const stack = {}; let stack = {};
Error.captureStackTrace(stack, testProxyUrl); Error.captureStackTrace(stack, testProxyUrl);
// Only use the last stack frame because that shows where this function is // Only use the last stack frame because that shows where this function is
// called, and that is sufficient for our purpose. No need to flood the logs // called, and that is sufficient for our purpose. No need to flood the logs
@ -88,7 +88,6 @@ function testProxyUrl(env, expected, input, options) {
// }); // });
options = options || {}; options = options || {};
options.env = options.env || env || process.env; options.env = options.env || env || process.env;
options.mode = options.mode || 'strict';
actual = getProxyForUrl(input, options); actual = getProxyForUrl(input, options);
if (expected === actual) { if (expected === actual) {
return; // Good! return; // Good!
@ -105,12 +104,16 @@ function testProxyUrl(env, expected, input, options) {
}); });
} }
describe.only('Proxy Helper', function () { describe('Proxy Helper', function () {
describe('No proxy variables', function () { describe('No proxy variables', function () {
const env = {}; const env = {};
testProxyUrl(env, '', 'http://example.com'); testProxyUrl(env, '', 'http://example.com');
testProxyUrl(env, '', 'https://example.com'); testProxyUrl(env, '', 'https://example.com');
testProxyUrl(env, '', 'ftp://example.com'); testProxyUrl(env, '', 'ftp://example.com');
testProxyUrl(env, '', 'ws://example.com');
testProxyUrl(env, '', 'wss://example.com');
testProxyUrl(env, '', 'mqtt://example.com');
testProxyUrl(env, '', 'mqtts://example.com');
}); });
describe('Invalid URLs', function () { describe('Invalid URLs', function () {
@ -139,32 +142,37 @@ describe.only('Proxy Helper', function () {
testProxyUrl(env, '', { host: 1, protocol: 'x' }); testProxyUrl(env, '', { host: 1, protocol: 'x' });
}); });
describe('Proxy options', function () { describe('Proxy options', function () {
describe('allowUpperCase:false should prevent *_PROXY being returned', function () { describe('lowerCaseOnly:true should prevent *_PROXY being returned', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://upper-case-proxy'; env.HTTP_PROXY = 'http://upper-case-proxy';
env.HTTPS_PROXY = 'https://upper-case-proxy'; env.HTTPS_PROXY = 'https://upper-case-proxy';
testProxyUrl(env, '', 'http://example', { lowerCaseOnly: true }); env.http_proxy = '';
testProxyUrl(env, '', 'https://example', { lowerCaseOnly: true }); env.https_proxy = '';
testProxyUrl(env, 'http://upper-case-proxy', 'http://example'); //returns lower case due to precedence env.no_proxy = '';
testProxyUrl(env, 'https://upper-case-proxy', 'https://example'); //returns lower case due to precedence testProxyUrl(env, '', 'http://example', { lowerCaseOnly: true }, 'returns empty string because `lowerCaseOnly` is set and http_proxy is not set');
testProxyUrl(env, '', 'https://example', { lowerCaseOnly: true }, 'returns empty string because `lowerCaseOnly` is set and https_proxy is not set');
testProxyUrl(env, 'http://upper-case-proxy', 'http://example', null, 'returns HTTP_PROXY because lowerCaseOnly is false by default');
testProxyUrl(env, 'https://upper-case-proxy', 'https://example', null, 'returns HTTPS_PROXY because lowerCaseOnly is false by default');
}); });
describe('favourLowerCase:false should cause *_PROXY to being used before *_proxy', function () { describe('favourLowerCase:false should cause *_PROXY to being used before *_proxy', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://upper-case-proxy'; env.HTTP_PROXY = 'http://upper-case-proxy';
env.http_proxy = 'http://lower-case-proxy'; env.http_proxy = 'http://lower-case-proxy';
testProxyUrl(env, 'http://upper-case-proxy', 'http://example', { favourUpperCase: true }); testProxyUrl(env, 'http://upper-case-proxy', 'http://example', { favourUpperCase: true }, 'returns HTTP_PROXY by due to `favourUpperCase`');
testProxyUrl(env, 'http://lower-case-proxy', 'http://example'); // lowercase takes precedence by default testProxyUrl(env, 'http://lower-case-proxy', 'http://example', null, 'returns http_proxy by as it takes precedence by default');
}); });
describe('includeNpm:false should not return npm_config_*_proxy env vars', function () { describe('includeNpm:false should not return npm_config_*_proxy env vars', function () {
const env = {}; const env = {};
env.npm_config_http_proxy = 'http://npm-proxy'; env.npm_config_http_proxy = 'http://npm-proxy';
env.npm_config_https_proxy = 'https://npm-proxy';
testProxyUrl(env, '', 'http://example', { excludeNpm: true }); testProxyUrl(env, '', 'http://example', { excludeNpm: true });
testProxyUrl(env, 'http://npm-proxy', 'http://example'); // lowercase takes precedence by default testProxyUrl(env, 'http://npm-proxy', 'http://example'); // lowercase takes precedence by default
testProxyUrl(env, 'https://npm-proxy', 'https://example');
}); });
describe('legacyMode:true should process urls proxy in node-red < v3.1 compatibility mode', function () { describe('When legacy mode is true, should process urls proxy in node-red <= v3.1 compatibility mode', function () {
const env = {}; const env = {};
// legacy mode does not consider npm_config_*_proxy // legacy mode does not consider npm_config_*_proxy
env.npm_config_http_proxy = 'http://npm-proxy'; env.npm_config_http_proxy = 'http://npm-proxy';
@ -173,27 +181,60 @@ describe.only('Proxy Helper', function () {
// legacy mode does not consider all_proxy // legacy mode does not consider all_proxy
env.all_proxy = 'http://all-proxy'; env.all_proxy = 'http://all-proxy';
testProxyUrl(env, '', 'http://example/2', { mode: 'legacy' }); testProxyUrl(env, '', 'http://example/2', { mode: 'legacy' }); // returns empty string in "legacy" mode
testProxyUrl(env, 'http://npm-proxy', 'http://example/2');
// legacy mode does not consider *_proxy // legacy mode does not consider *_proxy
env.npm_config_http_proxy = null; env.npm_config_http_proxy = null;
env.http_proxy = 'http://http-proxy'; env.http_proxy = 'http://http-proxy';
env.no_proxy = 'example'; env.no_proxy = 'example';
testProxyUrl(env, '', 'http://example/3a', { mode: 'legacy' }); testProxyUrl(env, '', 'http://example/3a', { mode: 'legacy' });
testProxyUrl(env, '', 'http://example/3b');
// legacy mode does not consider protocol_proxy for https urls and uses http_proxy // legacy mode does not consider protocol_proxy for https urls and uses http_proxy instead
env.https_proxy = 'https://https-proxy'; env.https_proxy = 'https://https-proxy';
env.no_proxy = ''; env.no_proxy = '';
testProxyUrl(env, 'http://http-proxy', 'https://example/4', { mode: 'legacy' }); testProxyUrl(env, 'http://http-proxy', 'https://example/4', { mode: 'legacy' }); // returns http_proxy instead of https_proxy
testProxyUrl(env, 'https://https-proxy', 'https://example/4');
// legacy mode favours UPPER_CASE over lower_case // legacy mode favours UPPER_CASE over lower_case
env.HTTP_PROXY = 'http://http-proxy-upper'; env.HTTP_PROXY = 'http://http-proxy-upper';
env.http_proxy = 'http://http-proxy';
env.no_proxy = ''; env.no_proxy = '';
testProxyUrl(env, 'http://http-proxy-upper', 'http://example/5', { mode: 'legacy' }); testProxyUrl(env, 'http://http-proxy-upper', 'http://example/5', { mode: 'legacy' }, 'returns HTTP_PROXY "http://http-proxy-upper" because mode is "legacy"');
testProxyUrl(env, 'http://http-proxy', 'http://example/5');
// no_proxy with protocols that have a default port are not considered
// * i.e. if no_proxy contains "example.com:443", then "https://example.com" should be excluded
// * i.e. if no_proxy contains "example.com:80", then "http://example.com" should be excluded
// * i.e. if no_proxy contains "example.com:1880", then "mqtt://example.com" should be excluded
env.HTTP_PROXY = 'http://http-proxy';
env.http_proxy = 'http://http-proxy';
env.no_proxy = 'example.com:80';
testProxyUrl(env, 'http://http-proxy', 'http://example.com', { mode: 'legacy' }, 'incorrectly returns http_proxy for "http://example.com" when mode is "legacy"');
testProxyUrl(env, '', 'http://example.com:80', { mode: 'legacy' }); // works as expected
testProxyUrl(env, '', 'http://example.com:8080', { mode: 'legacy' }, 'incorrectly returns http_proxy for "http://example.com:8080" when mode is "legacy"');
// legacy mode does not correctly process no_proxy with protocols that have a default port
env.HTTP_PROXY = 'http://http-proxy';
env.http_proxy = 'http://http-proxy';
env.no_proxy = 'example.com:80';
testProxyUrl(env, '', 'http://example.com:80', { mode: 'legacy' }); // works as expected
testProxyUrl(env, 'http://http-proxy', 'http://example.com', { mode: 'legacy' }, 'incorrectly returns http_proxy for "http://example.com" when no_proxy is "example.com:80" and mode is "legacy"');
env.HTTP_PROXY = 'http://http-proxy';
env.NO_PROXY = '[::1],[::2]:80,10.0.0.1,10.0.0.2:80';
testProxyUrl(env, '', 'http://[::1]/', { mode: 'legacy' });
testProxyUrl(env, '', 'http://[::1]:80/', { mode: 'legacy' });
testProxyUrl(env, '', 'http://[::1]:1337/', { mode: 'legacy' });
testProxyUrl(env, 'http://http-proxy', 'http://[::2]/', { mode: 'legacy' }); // http://[::2]/ is essentially the same as http://[::2]:80, this should NOT be proxied
testProxyUrl(env, '', 'http://[::2]:80/', { mode: 'legacy' });
testProxyUrl(env, 'http://http-proxy', 'http://[::2]:1337/', { mode: 'legacy' });
testProxyUrl(env, '', 'http://10.0.0.1/', { mode: 'legacy' });
testProxyUrl(env, '', 'http://10.0.0.1:80/', { mode: 'legacy' });
testProxyUrl(env, '', 'http://10.0.0.1:1337/', { mode: 'legacy' });
testProxyUrl(env, 'http://http-proxy', 'http://10.0.0.2/', { mode: 'legacy' }); // http://10.0.0.2 is essentially the same as http://10.0.0.2:80, this should NOT be proxied
testProxyUrl(env, '', 'http://10.0.0.2:80/', { mode: 'legacy' });
testProxyUrl(env, 'http://http-proxy', 'http://10.0.0.2:1337/', { mode: 'legacy' });
}); });
}); });
@ -210,17 +251,16 @@ describe.only('Proxy Helper', function () {
testProxyUrl(env, 'http://priority', 'http://example'); testProxyUrl(env, 'http://priority', 'http://example');
}); });
describe('http_proxy with non-sensical value', function () { describe('http_proxy with nonsense value', function () {
const env = {}; const env = {};
// Crazy values should be passed as-is. It is the responsibility of the // Crazy values should be passed as-is. It is the responsibility of the
// one who launches the application that the value makes sense. // one who launches the application that the value makes sense.
// TODO: Should we be stricter and perform validation?
env.HTTP_PROXY = 'Crazy \n!() { ::// }'; env.HTTP_PROXY = 'Crazy \n!() { ::// }';
testProxyUrl(env, 'Crazy \n!() { ::// }', 'http://wow'); testProxyUrl(env, 'Crazy \n!() { ::// }', 'http://wow');
// The implementation assumes that the HTTP_PROXY environment variable is // The implementation assumes that the HTTP_PROXY environment variable is
// somewhat reasonable, and if the scheme is missing, it is added. // somewhat reasonable, and if the scheme is missing, it is added.
// Garbage in, garbage out some would say... // Garbage in, garbage out!
env.HTTP_PROXY = 'crazy without colon slash slash'; env.HTTP_PROXY = 'crazy without colon slash slash';
testProxyUrl(env, 'http://crazy without colon slash slash', 'http://wow'); testProxyUrl(env, 'http://crazy without colon slash slash', 'http://wow');
}); });
@ -229,14 +269,14 @@ describe.only('Proxy Helper', function () {
const env = {}; const env = {};
// Assert that there is no fall back to http_proxy // Assert that there is no fall back to http_proxy
env.HTTP_PROXY = 'http://unexpected.proxy'; env.HTTP_PROXY = 'http://unexpected.proxy';
testProxyUrl(env, '', 'https://example'); testProxyUrl(env, '', 'https://example', null, 'https URL is not proxied when only HTTP_PROXY is set');
env.HTTPS_PROXY = 'http://https-proxy'; env.HTTPS_PROXY = 'http://https-proxy';
testProxyUrl(env, 'http://https-proxy', 'https://example'); testProxyUrl(env, 'http://https-proxy', 'https://example');
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
env.https_proxy = 'http://priority'; env.https_proxy = 'http://priority';
testProxyUrl(env, 'http://priority', 'https://example'); testProxyUrl(env, 'http://priority', 'https://example', null, 'https_proxy takes precedence over HTTPS_PROXY');
}); });
describe('ftp_proxy', function () { describe('ftp_proxy', function () {
@ -260,17 +300,18 @@ describe.only('Proxy Helper', function () {
describe('mqtt_proxy', function () { describe('mqtt_proxy', function () {
const env = {}; const env = {};
// Something else than http_proxy / https, as a sanity check. // Something else than http_proxy / https, as a sanity check.
env.mqtt_proxy = 'mqtt://mqtt-proxy'; env.mqtt_proxy = 'tcp://mqtt-proxy';
env.no_proxy = 'mqtt://mqtt-proxy'; env.no_proxy = 'direct';
testProxyUrl(env, 'mqtt://mqtt-proxy', 'mqtt://example1'); testProxyUrl(env, '', 'mqtt://direct');
testProxyUrl(env, 'tcp://mqtt-proxy', 'mqtt://example1');
testProxyUrl(env, '', 'mqtts://example2'); testProxyUrl(env, '', 'mqtts://example2');
}); });
describe('all_proxy', function () { describe('all_proxy', function () {
const env = {}; const env = {};
env.ALL_PROXY = 'http://catch-all'; env.ALL_PROXY = 'http://catch-all';
testProxyUrl(env, 'http://catch-all', 'https://example'); testProxyUrl(env, 'http://catch-all', 'http://example');
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
env.all_proxy = 'http://priority'; env.all_proxy = 'http://priority';
@ -292,118 +333,140 @@ describe.only('Proxy Helper', function () {
describe('no_proxy empty', function () { describe('no_proxy empty', function () {
const env = {}; const env = {};
env.HTTPS_PROXY = 'http://proxy'; env.HTTPS_PROXY = 'http://i-am-proxy';
// NO_PROXY set but empty. // NO_PROXY set but empty.
env.NO_PROXY = ''; env.NO_PROXY = '';
testProxyUrl(env, 'http://proxy', 'https://example1'); testProxyUrl(env, 'http://i-am-proxy', 'https://example1');
// No entries in NO_PROXY (comma). // No entries in NO_PROXY (comma).
env.NO_PROXY = ','; env.NO_PROXY = ',';
testProxyUrl(env, 'http://proxy', 'https://example2'); testProxyUrl(env, 'http://i-am-proxy', 'https://example2');
// No entries in NO_PROXY (whitespace). // No entries in NO_PROXY (whitespace).
env.NO_PROXY = ' '; env.NO_PROXY = ' ';
testProxyUrl(env, 'http://proxy', 'https://example3'); testProxyUrl(env, 'http://i-am-proxy', 'https://example3');
// No entries in NO_PROXY (multiple whitespace / commas). // No entries in NO_PROXY (multiple whitespace / commas).
env.NO_PROXY = ',\t,,,\n, ,\r'; env.NO_PROXY = ',\t,,,\n, ,\r';
testProxyUrl(env, 'http://proxy', 'https://example4'); testProxyUrl(env, 'http://i-am-proxy', 'https://example4');
}); });
describe('no_proxy=example (single host)', function () { describe('no_proxy=example (single host)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'example'; env.NO_PROXY = 'example';
testProxyUrl(env, '', 'http://example'); testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'http://example:80'); testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:0'); testProxyUrl(env, '', 'http://example:0');
testProxyUrl(env, '', 'http://example:1337'); testProxyUrl(env, '', 'http://example:1337');
testProxyUrl(env, 'http://proxy', 'http://sub.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://a.b.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://a.b.example');
testProxyUrl(env, 'http://proxy', 'http://host/example'); testProxyUrl(env, 'http://i-am-proxy', 'http://host/example');
}); });
describe('no_proxy=sub.example (subdomain)', function () { describe('no_proxy=sub.example (subdomain)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'sub.example'; env.NO_PROXY = 'sub.example';
testProxyUrl(env, '', 'http://sub.example'); testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80'); testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337'); testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, 'http://proxy', 'http://example'); testProxyUrl(env, 'http://i-am-proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:1337'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:1337');
testProxyUrl(env, 'http://proxy', 'http://bus.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://bus.example');
testProxyUrl(env, 'http://proxy', 'http://bus.example:80'); testProxyUrl(env, 'http://i-am-proxy', 'http://bus.example:80');
testProxyUrl(env, 'http://proxy', 'http://bus.example:1337'); testProxyUrl(env, 'http://i-am-proxy', 'http://bus.example:1337');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://a.b.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://a.b.example');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://host/example'); testProxyUrl(env, 'http://i-am-proxy', 'http://host/example');
}); });
describe('no_proxy=example:80 (host + port)', function () { describe('no_proxy=example:80 (host + port)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'example:80'; env.NO_PROXY = 'example:80';
testProxyUrl(env, '', 'http://example'); testProxyUrl(env, '', 'http://example');
testProxyUrl(env, '', 'http://example:80'); testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:0'); testProxyUrl(env, '', 'http://example:0');
testProxyUrl(env, 'http://proxy', 'http://example:1337'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:1337');
testProxyUrl(env, 'http://proxy', 'http://sub.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://a.b.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://a.b.example');
}); });
describe('no_proxy=.example (host suffix)', function () { describe('no_proxy=.example (host suffix)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '.example'; env.NO_PROXY = '.example';
testProxyUrl(env, 'http://proxy', 'http://example'); testProxyUrl(env, 'http://i-am-proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:1337'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example'); testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80'); testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337'); testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, '', 'http://a.b.example'); testProxyUrl(env, '', 'http://a.b.example');
}); });
describe('no_proxy=.example (host suffix + port)', function () {
const env = {};
env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '.example:8080';
testProxyUrl(env, 'http://i-am-proxy', 'http://example');
testProxyUrl(env, 'http://i-am-proxy', 'http://example:80');
testProxyUrl(env, 'http://i-am-proxy', 'http://example:8080');
testProxyUrl(env, 'http://i-am-proxy', 'http://sub.example');
testProxyUrl(env, 'http://i-am-proxy', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:8080');
testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, 'http://i-am-proxy', 'http://a.b.example');
testProxyUrl(env, '', 'http://a.b.example:8080');
});
describe('no_proxy=*', function () { describe('no_proxy=*', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.HTTPS_PROXY = 'https://i-am-proxy';
env.NO_PROXY = '*'; env.NO_PROXY = '*';
testProxyUrl(env, '', 'http://example.com'); testProxyUrl(env, '', 'http://example.com');
testProxyUrl(env, '', 'http://example:80');
testProxyUrl(env, '', 'http://example:1337');
testProxyUrl(env, '', 'https://example.com');
testProxyUrl(env, '', 'https://example:443');
}); });
describe('no_proxy=*.example (host suffix with *.)', function () { describe('no_proxy=*.example (host suffix with *.)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '*.example'; env.NO_PROXY = '*.example';
testProxyUrl(env, 'http://proxy', 'http://example'); testProxyUrl(env, 'http://i-am-proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://example:80'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:80');
testProxyUrl(env, 'http://proxy', 'http://example:1337'); testProxyUrl(env, 'http://i-am-proxy', 'http://example:1337');
testProxyUrl(env, '', 'http://sub.example'); testProxyUrl(env, '', 'http://sub.example');
testProxyUrl(env, '', 'http://sub.example:80'); testProxyUrl(env, '', 'http://sub.example:80');
testProxyUrl(env, '', 'http://sub.example:1337'); testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, '', 'http://a.b.example'); testProxyUrl(env, '', 'http://a.b.example');
}); });
describe('no_proxy=*example (substring suffix)', function () { describe('no_proxy=*example (substring suffix)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '*example'; env.NO_PROXY = '*example';
const t = getProxyForUrl('http://example', { env }); const t = getProxyForUrl('http://example', { env });
@ -415,27 +478,27 @@ describe.only('Proxy Helper', function () {
testProxyUrl(env, '', 'http://sub.example:1337'); testProxyUrl(env, '', 'http://sub.example:1337');
testProxyUrl(env, '', 'http://prefexample'); testProxyUrl(env, '', 'http://prefexample');
testProxyUrl(env, '', 'http://a.b.example'); testProxyUrl(env, '', 'http://a.b.example');
testProxyUrl(env, 'http://proxy', 'http://example.no'); testProxyUrl(env, 'http://i-am-proxy', 'http://example.no');
testProxyUrl(env, 'http://proxy', 'http://host/example'); testProxyUrl(env, 'http://i-am-proxy', 'http://host/example');
}); });
describe('no_proxy=.*example (arbitrary wildcards are NOT supported)', describe('no_proxy=.*example (arbitrary wildcards are NOT supported)',
function () { function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '.*example'; env.NO_PROXY = '.*example';
testProxyUrl(env, 'http://proxy', 'http://example'); testProxyUrl(env, 'http://i-am-proxy', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://sub.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://sub.example');
testProxyUrl(env, 'http://proxy', 'http://prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://prefexample');
testProxyUrl(env, 'http://proxy', 'http://x.prefexample'); testProxyUrl(env, 'http://i-am-proxy', 'http://x.prefexample');
testProxyUrl(env, 'http://proxy', 'http://a.b.example'); testProxyUrl(env, 'http://i-am-proxy', 'http://a.b.example');
}); });
describe('no_proxy=[::1],[::2]:80,10.0.0.1,10.0.0.2:80 (IP addresses)', describe('no_proxy=[::1],[::2]:80,10.0.0.1,10.0.0.2:80 (IP addresses)',
function () { function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '[::1],[::2]:80,10.0.0.1,10.0.0.2:80'; env.NO_PROXY = '[::1],[::2]:80,10.0.0.1,10.0.0.2:80';
testProxyUrl(env, '', 'http://[::1]/'); testProxyUrl(env, '', 'http://[::1]/');
@ -444,7 +507,7 @@ describe.only('Proxy Helper', function () {
testProxyUrl(env, '', 'http://[::2]/'); testProxyUrl(env, '', 'http://[::2]/');
testProxyUrl(env, '', 'http://[::2]:80/'); testProxyUrl(env, '', 'http://[::2]:80/');
testProxyUrl(env, 'http://proxy', 'http://[::2]:1337/'); testProxyUrl(env, 'http://i-am-proxy', 'http://[::2]:1337/');
testProxyUrl(env, '', 'http://10.0.0.1/'); testProxyUrl(env, '', 'http://10.0.0.1/');
testProxyUrl(env, '', 'http://10.0.0.1:80/'); testProxyUrl(env, '', 'http://10.0.0.1:80/');
@ -452,30 +515,30 @@ describe.only('Proxy Helper', function () {
testProxyUrl(env, '', 'http://10.0.0.2/'); testProxyUrl(env, '', 'http://10.0.0.2/');
testProxyUrl(env, '', 'http://10.0.0.2:80/'); testProxyUrl(env, '', 'http://10.0.0.2:80/');
testProxyUrl(env, 'http://proxy', 'http://10.0.0.2:1337/'); testProxyUrl(env, 'http://i-am-proxy', 'http://10.0.0.2:1337/');
testProxyUrl(env, 'http://proxy', 'http://10.0.0.3/'); testProxyUrl(env, 'http://i-am-proxy', 'http://10.0.0.3/');
testProxyUrl(env, 'http://proxy', 'http://10.0.0.3:80/'); testProxyUrl(env, 'http://i-am-proxy', 'http://10.0.0.3:80/');
testProxyUrl(env, 'http://proxy', 'http://10.0.0.3:1337/'); testProxyUrl(env, 'http://i-am-proxy', 'http://10.0.0.3:1337/');
}); });
describe('no_proxy=127.0.0.1/32 (CIDR is NOT supported)', function () { describe('no_proxy=127.0.0.1/32 (CIDR is NOT supported)', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '127.0.0.1/32'; env.NO_PROXY = '127.0.0.1/32';
testProxyUrl(env, 'http://proxy', 'http://127.0.0.1'); testProxyUrl(env, 'http://i-am-proxy', 'http://127.0.0.1');
testProxyUrl(env, 'http://proxy', 'http://127.0.0.1/32'); testProxyUrl(env, 'http://i-am-proxy', 'http://127.0.0.1/32');
}); });
describe('no_proxy=127.0.0.1 does NOT match localhost', function () { describe('no_proxy=127.0.0.1 does NOT match localhost', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = '127.0.0.1'; env.NO_PROXY = '127.0.0.1';
testProxyUrl(env, '', 'http://127.0.0.1'); testProxyUrl(env, '', 'http://127.0.0.1');
// We're not performing DNS queries, so this shouldn't match. // We're not performing DNS queries, so this shouldn't match.
testProxyUrl(env, 'http://proxy', 'http://localhost'); testProxyUrl(env, 'http://i-am-proxy', 'http://localhost');
}); });
describe('no_proxy with protocols that have a default port', function () { describe('no_proxy with protocols that have a default port', function () {
@ -529,9 +592,10 @@ describe.only('Proxy Helper', function () {
describe('no_proxy should not be case-sensitive', function () { describe('no_proxy should not be case-sensitive', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'XXX,YYY,ZzZ'; env.NO_PROXY = 'XXX,YYY,ZzZ';
testProxyUrl(env, 'http://i-am-proxy', 'http://abc');
testProxyUrl(env, '', 'http://xxx'); testProxyUrl(env, '', 'http://xxx');
testProxyUrl(env, '', 'http://XXX'); testProxyUrl(env, '', 'http://XXX');
testProxyUrl(env, '', 'http://yyy'); testProxyUrl(env, '', 'http://yyy');
@ -542,7 +606,7 @@ describe.only('Proxy Helper', function () {
describe('no_proxy should accept space separated entries', function () { describe('no_proxy should accept space separated entries', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'X X X,Y Y Y,Z z Z'; env.NO_PROXY = 'X X X,Y Y Y,Z z Z';
testProxyUrl(env, '', 'http://x x x'); testProxyUrl(env, '', 'http://x x x');
@ -626,23 +690,23 @@ describe.only('Proxy Helper', function () {
}); });
describe('npm_config_no_proxy should work', function () { describe('npm_config_no_proxy should work', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
env.npm_config_no_proxy = 'example'; env.npm_config_no_proxy = 'example';
testProxyUrl(env, '', 'http://example'); testProxyUrl(env, '', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://otherwebsite'); testProxyUrl(env, 'http://i-am-proxy', 'http://otherwebsite');
}); });
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
describe('npm_config_no_proxy should take precedence over NO_PROXY', function () { describe('npm_config_no_proxy should take precedence over NO_PROXY', function () {
const env = {}; const env = {};
env.HTTP_PROXY = 'http://proxy'; env.HTTP_PROXY = 'http://i-am-proxy';
env.NO_PROXY = 'otherwebsite'; env.NO_PROXY = 'otherwebsite';
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
env.npm_config_no_proxy = 'example'; env.npm_config_no_proxy = 'example';
testProxyUrl(env, '', 'http://example'); testProxyUrl(env, '', 'http://example');
testProxyUrl(env, 'http://proxy', 'http://otherwebsite'); testProxyUrl(env, 'http://i-am-proxy', 'http://otherwebsite');
}); });
}); });
}); });