Skip to content

Commit

Permalink
[#453] Author should be able to delete headers in userResHeaderDecora…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
nik-blue-lava authored and monkpow committed Jul 21, 2020
1 parent 1692a00 commit 2aec188
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions app/steps/decorateUserResHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
29 changes: 28 additions & 1 deletion test/decorateUserResHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,40 @@ 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);
});

afterEach(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', {
Expand All @@ -47,4 +73,5 @@ describe('when userResHeaderDecorator is defined', function () {
})
.end(done);
});

});

0 comments on commit 2aec188

Please sign in to comment.