diff --git a/lib/needle.js b/lib/needle.js index 4635be893..c3e0f686a 100644 --- a/lib/needle.js +++ b/lib/needle.js @@ -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; @@ -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. diff --git a/test/proxy_spec.js b/test/proxy_spec.js index 9526e7f7a..3b7c6009c 100644 --- a/test/proxy_spec.js +++ b/test/proxy_spec.js @@ -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() {