Skip to content

Commit

Permalink
support process.env.HTTP(HTTPS)_PROXY, process.env.http(https)_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
bomsy committed Jul 5, 2016
1 parent 0b88009 commit a70434b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ module.exports = function httpAdapter(resolve, reject, config) {
auth: auth
};

var proxyEnv = parsed.protocol.slice(0, -1) + '_proxy';
var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
if (proxyUrl) {
var proxy = url.parse(proxyUrl);
options.host = proxy.hostname;
options.port = proxy.port;
options.path = parsed.protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
}

if (config.proxy) {
options.host = config.proxy.host;
options.port = config.proxy.port;
Expand Down
53 changes: 47 additions & 6 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ 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;
}
callback();
},

Expand Down Expand Up @@ -227,34 +231,71 @@ module.exports = {
testProxy: function(test) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('server response');
res.end('12345');
}).listen(4444, function() {
// proxy
var proxyServer = http.createServer(function(request, response) {
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 + ' through proxy');
response.end(body + '6789');
});
});

}).listen(4000, function() {
axios.get('http://localhost:4444/', {
proxy: {
host: 'localhost',
port: 4000
}
}).then(function(res) {
test.equal(res.data, 'server response through proxy');
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();
});
});
Expand Down

0 comments on commit a70434b

Please sign in to comment.