From 93c841ea8f6df1924d0d98e0c6fc46563b6551c3 Mon Sep 17 00:00:00 2001 From: Mathias Fischer Date: Tue, 31 Oct 2023 11:39:43 +0100 Subject: [PATCH] feat: add option to disable use_proxy_from_env_var --- lib/needle.js | 11 +++++++---- test/proxy_spec.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/needle.js b/lib/needle.js index df5f1264f..74a51f06b 100644 --- a/lib/needle.js +++ b/lib/needle.js @@ -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 = { @@ -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; diff --git a/test/proxy_spec.js b/test/proxy_spec.js index b85d6185a..46b0b5ec3 100644 --- a/test/proxy_spec.js +++ b/test/proxy_spec.js @@ -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)) + }) + + }) + }) + })