Skip to content

Commit

Permalink
Merge pull request #427 from neonnoon/feat/add-option-to-disable-use_…
Browse files Browse the repository at this point in the history
…proxy_from_env_var

feat: add option to disable use_proxy_from_env_var
  • Loading branch information
tomas authored Dec 8, 2023
2 parents f9d554e + 93c841e commit 747522b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ var defaults = {
follow_keep_method : false,
follow_if_same_host : false,
follow_if_same_protocol : false,
follow_if_same_location : false
follow_if_same_location : false,
use_proxy_from_env_var : true
}

var aliased = {
Expand Down Expand Up @@ -255,12 +256,14 @@ Needle.prototype.setup = function(uri, options) {
}
}

var env_proxy = utils.get_env_var(['HTTP_PROXY', 'HTTPS_PROXY'], true);
if (!config.proxy && env_proxy) config.proxy = env_proxy;
if (config.use_proxy_from_env_var) {
var env_proxy = utils.get_env_var(['HTTP_PROXY', 'HTTPS_PROXY'], true);
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) {
if (utils.should_proxy_to(uri)) {
if (!config.use_proxy_from_env_var || utils.should_proxy_to(uri)) {
if (config.proxy.indexOf('http') === -1)
config.proxy = 'http://' + config.proxy;

Expand Down
37 changes: 37 additions & 0 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,41 @@ describe('proxy option', function() {

})

describe('when environment variable is set', function() {

describe('and default is unchanged', function() {

before(function() {
process.env.HTTP_PROXY = 'foobar';
})

after(function() {
delete process.env.HTTP_PROXY;
})

it('tries to proxy', function(done) {
send_request({}, proxied('foobar', 80, done))
})

})

describe('and functionality is disabled', function() {

before(function() {
process.env.HTTP_PROXY = 'foobar';
})

after(function() {
delete process.env.HTTP_PROXY;
})

it('ignores proxy', function(done) {
send_request({
use_proxy_from_env_var: false
}, not_proxied(done))
})

})
})

})

0 comments on commit 747522b

Please sign in to comment.