Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding interpretation of no_proxy #565

Closed
wants to merge 8 commits into from
23 changes: 23 additions & 0 deletions lib/adapters/http.js
Expand Up @@ -13,6 +13,23 @@ var pkg = require('./../../package.json');
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');


function isANoProxyHost(host) {
var noProxy = process.env.no_proxy || process.env.NO_PROXY;
if (!noProxy) {
return false;
}
var isANoProxyProxy = false;
noProxy.split(',')
.forEach(function noProxyMatchProxy(np) {
var npTrimmed = np.trim();
if (host.lastIndexOf(npTrimmed) === host.length - npTrimmed.length) {
isANoProxyProxy = true;
}
});
return isANoProxyProxy;
}

/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
return new Promise(function dispatchHttpRequest(resolve, reject) {
Expand Down Expand Up @@ -99,6 +116,9 @@ module.exports = function httpAdapter(config) {
password: proxyUrlAuth[1]
};
}
if (isANoProxyHost(parsed.hostname)) {
proxy = undefined;
}
}
}

Expand Down Expand Up @@ -224,3 +244,6 @@ module.exports = function httpAdapter(config) {
}
});
};


module.exports._isANoProxyHost = isANoProxyHost;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't export functions just for testing. This can be tested throught the behaviour of the request.

80 changes: 77 additions & 3 deletions test/unit/adapters/http.js
Expand Up @@ -3,21 +3,27 @@ var http = require('http');
var url = require('url');
var zlib = require('zlib');
var fs = require('fs');
var adapter = require('../../../lib/adapters/http.js');
var server, proxy;

module.exports = {
tearDown: function (callback) {
server.close();
server = null;
if (server) {
server.close();
server = null;
}
if (proxy) {
proxy.close()
proxy.close();
proxy = null;
}

if (process.env.http_proxy) {
delete process.env.http_proxy;
}

delete process.env.no_proxy;
delete process.env.NO_PROXY;

callback();
},

Expand Down Expand Up @@ -338,6 +344,7 @@ module.exports = {
}).listen(4000, function() {
// set the env variable
process.env.http_proxy = 'http://localhost:4000/';
process.env.no_proxy = 'my.corp.com';

axios.get('http://localhost:4444/').then(function(res) {
test.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy');
Expand All @@ -347,6 +354,73 @@ module.exports = {
});
},

testIsANoProxyHost: function(test) {
[
{h: 'bliss.mit.edu', no_p: undefined, res: false},
{h: 'bliss.mit.edu', no_p: 'localhost', res: false},
{h: 'bliss.mit.edu', no_p: 'localhost, my.corp.com', res: false},
{h: 'bliss.mit.edu', no_p: 'mit.edu', res: true},
{h: 'bliss.mit.edu', no_p: 'mit.edu,localhost', res: true},
{h: 'bliss.mit.edu', no_p: ' mit.edu', res: true},
{h: 'bliss.mit.edu', no_p: 'mit.edu ', res: true},
{h: 'bliss.mit.edu', no_p: 'localhost, mit.edu', res: true},
{h: 'bliss.mit.edu', no_p: 'u', res: true},
{h: '127.0.0.1', no_p: '0.0.1', res: true},
{h: '127.0.0.1', no_p: 'localhost', res: false}
].forEach(function(data) {
process.env.no_proxy = data.no_p;
test.equal(adapter._isANoProxyHost(data.h), data.res,
'When no_proxy="' + data.no_p + '", then host "' + data.h + '" must ' + (data.res ? 'not ' : '') + 'be proxied');
delete process.env.no_proxy;

process.env.NO_PROXY = data.no_p;
test.equal(adapter._isANoProxyHost(data.h), data.res,
'When NO_PROXY="' + data.no_p + '", then host "' + data.h + '" must ' + (data.res ? 'not ' : '') + 'be proxied');
delete process.env.NO_PROXY;
});

test.done();
},

testHTTPNoProxyEnv: function(test) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('4567');
}).listen(4444, function() {
proxy = http.createServer(function(request, response) {
var parsed = url.parse(request.url);
var opts = {
host: parsed.hostname,
port: parsed.port,
path: parsed.path
};

http.get(opts, function(res) {
var body = '';
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.end(body + '1234');
});
});
}).listen(4000, function() {
// set the env variable
process.env.http_proxy = 'http://127.0.0.1:4000/';
process.env.no_proxy = 'localhost';

axios.get('http://localhost:4444/').then(function(res) {
test.equal(res.data, '4567', 'should not use proxy set by process.env.http_proxy as of process.env.no_proxy');
test.done();
}, function() {
test.ok(false, 'error occured during request with no_proxy');
test.done();
});
});
});
},

testHTTPProxyAuth: function(test) {
server = http.createServer(function(req, res) {
res.end();
Expand Down