Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http proxy support #366

Merged
merged 12 commits into from Aug 24, 2016
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -295,7 +295,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
}
}
```

Expand Down
21 changes: 17 additions & 4 deletions lib/adapters/http.js
Expand Up @@ -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()];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add change this line to var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()] || process.env.all_proxy || process.env.ALL_PROXY] so that we can support all_proxy environment variables

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;
Expand Down
85 changes: 84 additions & 1 deletion test/unit/adapters/http.js
Expand Up @@ -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();
},

Expand Down Expand Up @@ -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/';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can setting http_proxy affect other tests (executed after this test)? If so, maybe we should reset http_proxy in tearDown.


axios.get('http://localhost:4444/').then(function(res) {
test.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy');
test.done();
});
});
});
}
};