From ccfa9e2e48972cad3b9a1b4cfc310761a8fc512d Mon Sep 17 00:00:00 2001 From: Edo Rivai Date: Tue, 24 Oct 2017 14:04:31 +0400 Subject: [PATCH] Add option retainOriginalHost --- index.js | 2 +- test/index.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 08cad8d..d6d1e0e 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ module.exports = function(options) { }; // set "Host" header to options.host (without protocol prefix), strip trailing slash - if (options.host) + if (options.host && options.retainOriginalHost !== true) opt.headers.host = options.host .slice(options.host.indexOf('://') + 3) .replace(/\/$/, ''); diff --git a/test/index.js b/test/index.js index 60a0a63..9861901 100644 --- a/test/index.js +++ b/test/index.js @@ -50,6 +50,12 @@ describe('koa-proxy', function() { ctx.status = 200; return; } + if (ctx.path === '/retain-original-host') { + var headers = ctx.request.header; + ctx.body = headers; + ctx.status = 200; + return; + } if (ctx.querystring) { // To test query string ctx.body = ctx.querystring; @@ -139,6 +145,24 @@ describe('koa-proxy', function() { }); }); + it('should have option retainOriginalHost', function(done) { + var app = new Koa(); + app.use(proxy({ + host: 'http://localhost:1234', + retainOriginalHost: true + })); + var server = http.createServer(app.callback()); + request(server) + .get('/retain-original-host') + .set('Host', 'localhost') // manually set host, since supertest randomizes the port + .expect(200) + .end(function(err, res) { + if (err) return done(err); + res.body.host.should.equal('localhost'); + done(); + }); + }); + it("should have option host and map", function(done) { var app = new Koa(); app.use(proxy({