diff --git a/README.md b/README.md index 7d5adcb2..9e3004f1 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,8 @@ first request. ### userResHeaderDecorator +When a `userResHeaderDecorator` is defined, the return of this method will replace (rather than be merged on to) the headers for `userRes`. + ```js app.use('/proxy', proxy('www.google.com', { userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) { diff --git a/app/steps/decorateUserResHeaders.js b/app/steps/decorateUserResHeaders.js index 17fbaa79..0e9d323e 100644 --- a/app/steps/decorateUserResHeaders.js +++ b/app/steps/decorateUserResHeaders.js @@ -9,10 +9,17 @@ function decorateUserResHeaders(container) { return Promise.resolve(container); } + const clearAllHeaders = (res) => { + for (const header in res._headers) { + res.removeHeader(header) + } + } + return Promise .resolve(resolverFn(headers, container.user.req, container.user.res, container.proxy.req, container.proxy.res)) .then(function(headers) { return new Promise(function(resolve) { + clearAllHeaders(container.user.res); container.user.res.set(headers); resolve(container); }); diff --git a/test/decorateUserResHeaders.js b/test/decorateUserResHeaders.js index 43884732..6fd925ba 100644 --- a/test/decorateUserResHeaders.js +++ b/test/decorateUserResHeaders.js @@ -19,7 +19,11 @@ describe('when userResHeaderDecorator is defined', function () { beforeEach(function () { app = express(); var pTarget = express(); - pTarget.use(function (req, res) { res.json(req.headers); }); + pTarget.use(function (req, res) { + res.header('x-my-not-so-secret-header', 'minnie-mouse'); + res.header('x-my-secret-header', 'mighty-mouse'); + res.json(req.headers); + }); serverReference = pTarget.listen(12345); }); @@ -27,6 +31,28 @@ describe('when userResHeaderDecorator is defined', function () { serverReference.close(); }); + it('can delete a header', function (done) { + + app.use('/proxy', proxy('http://127.0.0.1:12345', { + userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) { + delete headers['x-my-secret-header']; + return headers; + } + })); + + app.use(function (req, res) { + res.sendStatus(200); + }); + + request(app) + .get('/proxy') + .expect(function (res) { + assert(Object.keys(res.headers).indexOf('x-my-not-so-secret-header') > -1); + assert(Object.keys(res.headers).indexOf('x-my-secret-header') === -1); + }) + .end(done); + }); + it('provides an interface for updating headers', function (done) { app.use('/proxy', proxy('http://127.0.0.1:12345', { @@ -47,4 +73,5 @@ describe('when userResHeaderDecorator is defined', function () { }) .end(done); }); + });