diff --git a/lib/http-proxy/passes/web-outgoing.js b/lib/http-proxy/passes/web-outgoing.js index 46352f6e377affc701773328d528bd7e4352f242..5d036c567650c0bba49812be81dc7524c2422e6f 100644 --- a/lib/http-proxy/passes/web-outgoing.js +++ b/lib/http-proxy/passes/web-outgoing.js @@ -66,7 +66,9 @@ module.exports = { // <-- } if (options.protocolRewrite) { u.protocol = options.protocolRewrite; - } + } else if (options.autoRewrite) { + u.protocol = req.socket.encrypted ? 'https' : 'http'; + } proxyRes.headers['location'] = u.format(); } diff --git a/test/lib-http-proxy-passes-web-outgoing-test.js b/test/lib-http-proxy-passes-web-outgoing-test.js index a509cf1aef977c7eed7ad88b70d40bc18788d222..f02cc9ed8c550bf2d530f7a3118d733db7010520 100644 --- a/test/lib-http-proxy-passes-web-outgoing-test.js +++ b/test/lib-http-proxy-passes-web-outgoing-test.js @@ -7,7 +7,8 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () { this.req = { headers: { host: 'ext-auto.com' - } + }, + socket: {} }; this.proxyRes = { statusCode: 301, @@ -65,15 +66,16 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () { }); }); - context('rewrites location host with autoRewrite', function() { + context('rewrites location host and protocol with autoRewrite', function() { beforeEach(function() { this.options.autoRewrite = true; + this.req.socket.encrypted: true; }); [201, 301, 302, 307, 308].forEach(function(code) { it('on ' + code, function() { this.proxyRes.statusCode = code; httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); - expect(this.proxyRes.headers.location).to.eql('http://ext-auto.com/'); + expect(this.proxyRes.headers.location).to.eql('https://ext-auto.com/'); }); }); @@ -128,17 +130,25 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () { expect(this.proxyRes.headers.location).to.eql('http://backend.com/'); }); - it('works together with hostRewrite', function() { + it('works together with host from hostRewrite', function() { this.options.hostRewrite = 'ext-manual.com'; httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); expect(this.proxyRes.headers.location).to.eql('https://ext-manual.com/'); }); - it('works together with autoRewrite', function() { + it('works together with host from autoRewrite', function() { this.options.autoRewrite = true; httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); expect(this.proxyRes.headers.location).to.eql('https://ext-auto.com/'); }); + + it('takes precedence over autoRewrite', function() { + this.options.autoRewrite = true; + this.options.protocolRewrite = 'http'; + this.req.socket.encrypted = true; + httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); + expect(this.proxyRes.headers.location).to.eql('http://ext-auto.com/'); + }); }); });