From add81338a90dae132f9e74fd5a5905fbcef030b7 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Sun, 23 Nov 2014 13:34:20 -0500 Subject: [PATCH] :pencil: Add host rewrite docs and specs. --- README.md | 1 + lib/http-proxy.js | 1 + ...lib-http-proxy-passes-web-outgoing-test.js | 52 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 1943f3f..45d6e0c 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,7 @@ proxyServer.listen(8015); * **secure**: true/false, if you want to verify the SSL Certs * **xfwd**: true/false, adds x-forward headers * **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies) + * **hostRewrite**: rewrites the location hostname on (301/302/307/308) redirects. If you are using the `proxyServer.listen` method, the following options are also applicable: diff --git a/lib/http-proxy.js b/lib/http-proxy.js index 9a7f81b..b1ad646 100644 --- a/lib/http-proxy.js +++ b/lib/http-proxy.js @@ -41,6 +41,7 @@ module.exports.createProxyServer = * prependPath: * localAddress : * changeOrigin: + * hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null. * } * * NOTE: `options.ws` and `options.ssl` are optional. diff --git a/test/lib-http-proxy-passes-web-outgoing-test.js b/test/lib-http-proxy-passes-web-outgoing-test.js index 0ae0bda..ee1077e 100644 --- a/test/lib-http-proxy-passes-web-outgoing-test.js +++ b/test/lib-http-proxy-passes-web-outgoing-test.js @@ -2,6 +2,58 @@ var httpProxy = require('../lib/http-proxy/passes/web-outgoing'), expect = require('expect.js'); describe('lib/http-proxy/passes/web-outgoing.js', function () { + describe('#setRedirectHostRewrite', function () { + context('rewrites location host to option', function() { + beforeEach(function() { + this.proxyRes = { + statusCode: 301, + headers: { + location: "http://f.com/" + } + }; + + this.options = { + hostRewrite: "x.com" + }; + }); + + it('on 301', function() { + this.proxyRes.statusCode = 301; + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); + }); + + it('on 302', function() { + this.proxyRes.statusCode = 302; + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); + }); + + it('on 307', function() { + this.proxyRes.statusCode = 307; + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); + }); + + it('on 308', function() { + this.proxyRes.statusCode = 308; + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); + }); + + it('not on 200', function() { + this.proxyRes.statusCode = 200; + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://f.com/'); + }); + + it('not when hostRewrite is unset', function() { + httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, {}); + expect(this.proxyRes.headers.location).to.eql('http://f.com/'); + }); + }); + }); + describe('#setConnection', function () { it('set the right connection with 1.0 - `close`', function() { var proxyRes = { headers: {} };