2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-05 16:42:32 +03:00

Adding support for http_proxy and https_proxy environment variables

This commit is contained in:
Hubert Boma Manilla
2016-08-24 17:05:26 +01:00
committed by Nick Uraltsev
parent 96d7ac2a0c
commit 93ae90ae6e
3 changed files with 108 additions and 6 deletions
+7 -1
View File
@@ -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
}
}
```
+17 -4
View File
@@ -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;
+84 -1
View File
@@ -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();
});
});
});
}
};