Added changeOrigin option with test and docs

This commit is contained in:
Alex Oshchepkov 2014-10-29 07:25:19 +05:00
parent f70015f001
commit 796ab0bcc5
3 changed files with 31 additions and 0 deletions

View File

@ -40,6 +40,7 @@ module.exports.createProxyServer =
* toProxy: <true/false, explicitly specify if we are proxying to another proxy> * toProxy: <true/false, explicitly specify if we are proxying to another proxy>
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path> * 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> * 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>
* } * }
* *
* NOTE: `options.ws` and `options.ssl` are optional. * NOTE: `options.ws` and `options.ssl` are optional.

View File

@ -74,6 +74,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.path = common.urlJoin(targetPath, outgoingPath); outgoing.path = common.urlJoin(targetPath, outgoingPath);
if (options.changeOrigin) {
outgoing.headers.host = outgoing.host;
}
return outgoing; return outgoing;
}; };

View File

@ -264,4 +264,30 @@ describe('#createProxyServer.web() using own http server', function () {
source.listen('8080'); source.listen('8080');
http.request('http://127.0.0.1:8086', function() {}).end(); http.request('http://127.0.0.1:8086', function() {}).end();
}); });
it('should proxy the request and handle changeOrigin option', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
changeOrigin: true
});
function requestHandler(req, res) {
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
source.close();
proxyServer.close();
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8080');
done();
});
proxyServer.listen('8081');
source.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
});
}); });