Skip to content

Commit

Permalink
Merge a70434b into 54f3a5d
Browse files Browse the repository at this point in the history
  • Loading branch information
bomsy committed Jul 5, 2016
2 parents 54f3a5d + a70434b commit 1c15852
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -289,7 +289,13 @@ These are the available config options for making requests. Only the `url` is re

// `maxRedirects` defines the maximum number of redirects to follow in node.js.
// If set to 0, no redirects will be followed.
maxRedirects: 5 // default
maxRedirects: 5, // default

// 'proxy' defines the hostname and port of the proxy server
proxy: {
host: '127.0.0.1',
port: 9000
}
}
```

Expand Down
11 changes: 10 additions & 1 deletion lib/adapters/http.js
Expand Up @@ -70,10 +70,19 @@ 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;
options.path = parsed.protocol + '//' + parsed.hostname + options.path;
options.path = parsed.protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;
}

var transport;
Expand Down
80 changes: 79 additions & 1 deletion test/unit/adapters/http.js
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 @@ -222,5 +226,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();
});
});
});
}
};

0 comments on commit 1c15852

Please sign in to comment.