From 796ab0bcc56a574d2cf21d77044f4f647918103d Mon Sep 17 00:00:00 2001 From: Alex Oshchepkov Date: Wed, 29 Oct 2014 07:25:19 +0500 Subject: [PATCH] Added changeOrigin option with test and docs --- lib/http-proxy.js | 1 + lib/http-proxy/common.js | 4 +++ ...lib-http-proxy-passes-web-incoming-test.js | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/lib/http-proxy.js b/lib/http-proxy.js index ccaaab730..c14d1a2d0 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -40,6 +40,7 @@ module.exports.createProxyServer = * toProxy: * prependPath: * localAddress : + * changeOrigin: true/false, Default: false - changes the origin of the host header to the target URL> * } * * NOTE: `options.ws` and `options.ssl` are optional. diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 693198af3..4d5b5f136 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -74,6 +74,10 @@ common.setupOutgoing = function(outgoing, options, req, forward) { outgoing.path = common.urlJoin(targetPath, outgoingPath); + if (options.changeOrigin) { + outgoing.headers.host = outgoing.host; + } + return outgoing; }; diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 2bd86d87b..f70001035 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -264,4 +264,30 @@ describe('#createProxyServer.web() using own http server', function () { source.listen('8080'); 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(); + }); }); \ No newline at end of file