From 93ae90ae6e1d99cdbae383875bc54977ba6c2769 Mon Sep 17 00:00:00 2001 From: Hubert Boma Manilla Date: Wed, 24 Aug 2016 17:05:26 +0100 Subject: [PATCH] Adding support for http_proxy and https_proxy environment variables --- README.md | 8 +++- lib/adapters/http.js | 21 ++++++++-- test/unit/adapters/http.js | 85 +++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3fb4ea4..79b1c61 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,13 @@ These are the available config options for making requests. Only the `url` is re // and https requests, respectively, in node.js. This allows to configure options like // `keepAlive` that are not enabled by default. httpAgent: new http.Agent({ keepAlive: true }), - httpsAgent: new https.Agent({ keepAlive: true }) + httpsAgent: new https.Agent({ keepAlive: true }), + + // 'proxy' defines the hostname and port of the proxy server + proxy: { + host: '127.0.0.1', + port: 9000 + } } ``` diff --git a/lib/adapters/http.js b/lib/adapters/http.js index bf01831..4d76a01 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -79,10 +79,23 @@ module.exports = function httpAdapter(config) { auth: auth }; - if (config.proxy) { - options.host = config.proxy.host; - options.port = config.proxy.port; - options.path = parsed.protocol + '//' + parsed.hostname + options.path; + var proxy = config.proxy; + if (!proxy) { + var proxyEnv = parsed.protocol.slice(0, -1) + '_proxy'; + var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()]; + if (proxyUrl) { + var parsedProxyUrl = url.parse(proxyUrl); + proxy = { + host: parsedProxyUrl.hostname, + port: parsedProxyUrl.port + }; + } + } + + if (proxy) { + options.host = proxy.host; + options.port = proxy.port; + options.path = parsed.protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path; } var transport; diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 36d734d..6fa99bd 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -3,12 +3,21 @@ var http = require('http'); var url = require('url'); var zlib = require('zlib'); var fs = require('fs'); -var server; +var server, proxy; module.exports = { tearDown: function (callback) { server.close(); server = null; + if (proxy) { + proxy.close() + proxy = null; + } + + if (process.env.http_proxy) { + process.env.http_proxy = null; + } + callback(); }, @@ -237,5 +246,79 @@ module.exports = { }); }); }); + }, + + testProxy: function(test) { + server = http.createServer(function(req, res) { + res.setHeader('Content-Type', 'text/html; charset=UTF-8'); + res.end('12345'); + }).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 + '6789'); + }); + }); + + }).listen(4000, function() { + axios.get('http://localhost:4444/', { + proxy: { + host: 'localhost', + port: 4000 + } + }).then(function(res) { + test.equal(res.data, '123456789', 'should pass through proxy'); + test.done(); + }); + }); + }); + }, + + testHTTPProxyEnv: 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://localhost:4000/'; + + axios.get('http://localhost:4444/').then(function(res) { + test.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy'); + test.done(); + }); + }); + }); } };