Skip to content

Commit

Permalink
Test NO_PROXY
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Apr 7, 2022
1 parent 1e581a3 commit 933fd79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
29 changes: 17 additions & 12 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ function should_proxy_to(url) {
var host, hosts = no_proxy.split(',');
for (var i in hosts) {
host = hosts[i];
if (host_and_ports_match(host, url))
if (host_and_ports_match(host, url)) {
return false;
}
}

return true;
Expand Down Expand Up @@ -339,18 +340,22 @@ Needle.prototype.setup = function(uri, options) {
if (!config.proxy && env_proxy) config.proxy = env_proxy;

// if proxy is present, set auth header from either url or proxy_user option.
if (config.proxy && should_proxy_to(uri)) {
if (config.proxy.indexOf('http') === -1)
config.proxy = 'http://' + config.proxy;

if (config.proxy.indexOf('@') !== -1) {
var proxy = (url.parse(config.proxy).auth || '').split(':');
options.proxy_user = proxy[0];
options.proxy_pass = proxy[1];
}
if (config.proxy) {
if (should_proxy_to(uri)) {
if (config.proxy.indexOf('http') === -1)
config.proxy = 'http://' + config.proxy;

if (config.proxy.indexOf('@') !== -1) {
var proxy = (url.parse(config.proxy).auth || '').split(':');
options.proxy_user = proxy[0];
options.proxy_pass = proxy[1];
}

if (options.proxy_user)
config.headers['proxy-authorization'] = auth.basic(options.proxy_user, options.proxy_pass);
if (options.proxy_user)
config.headers['proxy-authorization'] = auth.basic(options.proxy_user, options.proxy_pass);
} else {
delete config.proxy;
}
}

// now that all our headers are set, overwrite them if instructed.
Expand Down
37 changes: 33 additions & 4 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,41 @@ describe('proxy option', function() {

describe('when valid url is passed', function() {

it('proxies request', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', done))
describe('without NO_PROXY env var set', function() {
it('proxies request', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', done))
})

it('does not set a Proxy-Authorization header', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, no_proxy_auth(done));
})
})

it('does not set a Proxy-Authorization header', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, no_proxy_auth(done));
describe('with NO_PROXY env var set', function() {

it('proxies request if matching host not found in list', function(done) {
process.env.NO_PROXY = 'foo';
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', function() {
delete process.env.NO_PROXY;
done();
}))
})

it('proxies request if matching host in list but different port', function(done) {
process.env.NO_PROXY = 'localhost';
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', function() {
delete process.env.NO_PROXY;
done();
}))
})

it('does not proxy if matching host found in list', function(done) {
process.env.NO_PROXY = 'foo,' + url;
send_request({ proxy: nonexisting_host + ':123/done' }, not_proxied(function() {
delete process.env.NO_PROXY;
done();
}))
})
})

describe('and proxy url contains user:pass', function() {
Expand Down

0 comments on commit 933fd79

Please sign in to comment.