Skip to content

Commit

Permalink
Merge pull request #792 from ashubham/master
Browse files Browse the repository at this point in the history
Adding the nodejs0.12 auth option
  • Loading branch information
jcrugzz committed Mar 12, 2015
2 parents 245d73a + e907d7b commit 507f818
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports.createProxyServer =
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
* localAddress : <Local interface string to bind for outgoing connections>
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
* }
*
Expand Down
4 changes: 4 additions & 0 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
extend(outgoing.headers, options.headers);
}

if (options.auth) {
outgoing.auth = options.auth;
}

if (isSSL.test(options[forward || 'target'].protocol)) {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
}
Expand Down
2 changes: 2 additions & 0 deletions test/lib-http-proxy-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('lib/http-proxy/common.js', function () {
},
headers: {'fizz': 'bang', 'overwritten':true},
localAddress: 'local.address',
auth:'username:pass'
},
{
method : 'i',
Expand All @@ -37,6 +38,7 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.headers.fizz).to.eql('bang');
expect(outgoing.headers.overwritten).to.eql(true);
expect(outgoing.localAddress).to.eql('local.address');
expect(outgoing.auth).to.eql('username:pass');
});

it('should not override agentless upgrade header', function () {
Expand Down
29 changes: 28 additions & 1 deletion test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,31 @@ describe('#createProxyServer.web() using own http server', function () {

http.request('http://127.0.0.1:8081', function() {}).end();
});
});

it('should proxy the request with the Authorization header set', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
auth: 'user:pass'
});

function requestHandler(req, res) {
proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
source.close();
proxyServer.close();
var auth = new Buffer(req.headers.authorization.split(' ')[1], 'base64');
expect(req.method).to.eql('GET');
expect(auth.toString()).to.eql('user:pass');
done();
});

proxyServer.listen('8081');
source.listen('8080');

http.request('http://127.0.0.1:8081', function() {}).end();
});
});

0 comments on commit 507f818

Please sign in to comment.