2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

Support proxy auth (#483)

* Adding proxy auth
This commit is contained in:
Marc Mignonsin
2016-10-19 11:02:42 +02:00
committed by Rubén Norte
parent b78f3fe792
commit df6d3ce6cf
3 changed files with 148 additions and 3 deletions
+126 -1
View File
@@ -248,7 +248,7 @@ module.exports = {
});
},
testProxy: function(test) {
testHTTPProxy: function(test) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.end('12345');
@@ -322,6 +322,131 @@ module.exports = {
});
},
testHTTPProxyAuth: function(test) {
server = http.createServer(function(req, res) {
res.end();
}).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
};
var proxyAuth = request.headers['proxy-authorization'];
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(proxyAuth);
});
});
}).listen(4000, function() {
axios.get('http://localhost:4444/', {
proxy: {
host: 'localhost',
port: 4000,
auth: {
username: 'user',
password: 'pass'
}
}
}).then(function(res) {
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
test.done();
});
});
});
},
testHTTPProxyAuthFromEnv: function(test) {
server = http.createServer(function(req, res) {
res.end();
}).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
};
var proxyAuth = request.headers['proxy-authorization'];
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(proxyAuth);
});
});
}).listen(4000, function() {
process.env.http_proxy = 'http://user:pass@localhost:4000/';
axios.get('http://localhost:4444/').then(function(res) {
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy');
test.done();
});
});
});
},
testHTTPProxyAuthWithHeader: function (test) {
server = http.createServer(function(req, res) {
res.end();
}).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
};
var proxyAuth = request.headers['proxy-authorization'];
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(proxyAuth);
});
});
}).listen(4000, function() {
axios.get('http://localhost:4444/', {
proxy: {
host: 'localhost',
port: 4000,
auth: {
username: 'user',
password: 'pass'
}
},
headers: {
'Proxy-Authorization': 'Basic abc123'
}
}).then(function(res) {
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
test.done();
});
});
});
},
testCancel: function(test) {
var source = axios.CancelToken.source();
server = http.createServer(function (req, res) {