Skip to content

Commit 2aec188

Browse files
nik-blue-lavamonkpow
authored andcommitted
[#453] Author should be able to delete headers in userResHeaderDecorator.
1 parent 1692a00 commit 2aec188

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ first request.
257257

258258
### userResHeaderDecorator
259259

260+
When a `userResHeaderDecorator` is defined, the return of this method will replace (rather than be merged on to) the headers for `userRes`.
261+
260262
```js
261263
app.use('/proxy', proxy('www.google.com', {
262264
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {

app/steps/decorateUserResHeaders.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ function decorateUserResHeaders(container) {
99
return Promise.resolve(container);
1010
}
1111

12+
const clearAllHeaders = (res) => {
13+
for (const header in res._headers) {
14+
res.removeHeader(header)
15+
}
16+
}
17+
1218
return Promise
1319
.resolve(resolverFn(headers, container.user.req, container.user.res, container.proxy.req, container.proxy.res))
1420
.then(function(headers) {
1521
return new Promise(function(resolve) {
22+
clearAllHeaders(container.user.res);
1623
container.user.res.set(headers);
1724
resolve(container);
1825
});

test/decorateUserResHeaders.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,40 @@ describe('when userResHeaderDecorator is defined', function () {
1919
beforeEach(function () {
2020
app = express();
2121
var pTarget = express();
22-
pTarget.use(function (req, res) { res.json(req.headers); });
22+
pTarget.use(function (req, res) {
23+
res.header('x-my-not-so-secret-header', 'minnie-mouse');
24+
res.header('x-my-secret-header', 'mighty-mouse');
25+
res.json(req.headers);
26+
});
2327
serverReference = pTarget.listen(12345);
2428
});
2529

2630
afterEach(function () {
2731
serverReference.close();
2832
});
2933

34+
it('can delete a header', function (done) {
35+
36+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
37+
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
38+
delete headers['x-my-secret-header'];
39+
return headers;
40+
}
41+
}));
42+
43+
app.use(function (req, res) {
44+
res.sendStatus(200);
45+
});
46+
47+
request(app)
48+
.get('/proxy')
49+
.expect(function (res) {
50+
assert(Object.keys(res.headers).indexOf('x-my-not-so-secret-header') > -1);
51+
assert(Object.keys(res.headers).indexOf('x-my-secret-header') === -1);
52+
})
53+
.end(done);
54+
});
55+
3056
it('provides an interface for updating headers', function (done) {
3157

3258
app.use('/proxy', proxy('http://127.0.0.1:12345', {
@@ -47,4 +73,5 @@ describe('when userResHeaderDecorator is defined', function () {
4773
})
4874
.end(done);
4975
});
76+
5077
});

0 commit comments

Comments
 (0)