Skip to content

Commit add8133

Browse files
committed
📝 Add host rewrite docs and specs.
1 parent daf66a7 commit add8133

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ proxyServer.listen(8015);
325325
* **secure**: true/false, if you want to verify the SSL Certs
326326
* **xfwd**: true/false, adds x-forward headers
327327
* **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
328+
* **hostRewrite**: rewrites the location hostname on (301/302/307/308) redirects.
328329

329330
If you are using the `proxyServer.listen` method, the following options are also applicable:
330331

lib/http-proxy.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module.exports.createProxyServer =
4141
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
4242
* localAddress : <Local interface string to bind for outgoing connections>
4343
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
44+
* hostRewrite: rewrites the location hostname on (301/302/307/308) redirects, Default: null.
4445
* }
4546
*
4647
* NOTE: `options.ws` and `options.ssl` are optional.

test/lib-http-proxy-passes-web-outgoing-test.js

+52
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,58 @@ var httpProxy = require('../lib/http-proxy/passes/web-outgoing'),
22
expect = require('expect.js');
33

44
describe('lib/http-proxy/passes/web-outgoing.js', function () {
5+
describe('#setRedirectHostRewrite', function () {
6+
context('rewrites location host to option', function() {
7+
beforeEach(function() {
8+
this.proxyRes = {
9+
statusCode: 301,
10+
headers: {
11+
location: "http://f.com/"
12+
}
13+
};
14+
15+
this.options = {
16+
hostRewrite: "x.com"
17+
};
18+
});
19+
20+
it('on 301', function() {
21+
this.proxyRes.statusCode = 301;
22+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
23+
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
24+
});
25+
26+
it('on 302', function() {
27+
this.proxyRes.statusCode = 302;
28+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
29+
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
30+
});
31+
32+
it('on 307', function() {
33+
this.proxyRes.statusCode = 307;
34+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
35+
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
36+
});
37+
38+
it('on 308', function() {
39+
this.proxyRes.statusCode = 308;
40+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
41+
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/');
42+
});
43+
44+
it('not on 200', function() {
45+
this.proxyRes.statusCode = 200;
46+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, this.options);
47+
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
48+
});
49+
50+
it('not when hostRewrite is unset', function() {
51+
httpProxy.setRedirectHostRewrite({}, {}, this.proxyRes, {});
52+
expect(this.proxyRes.headers.location).to.eql('http://f.com/');
53+
});
54+
});
55+
});
56+
557
describe('#setConnection', function () {
658
it('set the right connection with 1.0 - `close`', function() {
759
var proxyRes = { headers: {} };

0 commit comments

Comments
 (0)