Skip to content

Commit 2048e8f

Browse files
committed
[548] Update docs and tests to show how to retain proxyRes headers.
1 parent 23c5d51 commit 2048e8f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ first request.
276276

277277
When a `userResHeaderDecorator` is defined, the return of this method will replace (rather than be merged on to) the headers for `userRes`.
278278

279+
> Note that by default, headers from the PROXY response CLOBBER all headers that may have previously been set on the userResponse.
280+
> Authors have the option of constructing any combination of proxyRes and userRes headers in the `userResHeaderDecorator`.
281+
> Check the tests for this method for examples.
282+
283+
279284
```js
280285
app.use('/proxy', proxy('www.google.com', {
281286
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {

test/decorateUserResHeaders.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe('when userResHeaderDecorator is defined', function () {
3333
});
3434

3535
it('can delete a header', function (done) {
36-
3736
app.use('/proxy', proxy('http://127.0.0.1:12345', {
3837
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
3938
delete headers['x-my-secret-header'];
@@ -55,7 +54,6 @@ describe('when userResHeaderDecorator is defined', function () {
5554
});
5655

5756
it('provides an interface for updating headers', function (done) {
58-
5957
app.use('/proxy', proxy('http://127.0.0.1:12345', {
6058
userResHeaderDecorator: function (headers /*, userReq, userRes, proxyReq, proxyRes */) {
6159
headers.boltedonheader = 'franky';
@@ -75,4 +73,39 @@ describe('when userResHeaderDecorator is defined', function () {
7573
.end(done);
7674
});
7775

76+
it('author has option to copy proxyResponse headers to userResponse', function (done) {
77+
app.use('/proxy', proxy('http://127.0.0.1:12345', {
78+
userResHeaderDecorator: function (headers, userReq) { // proxyReq
79+
// Copy specific headers from the proxy request to the user response
80+
//
81+
// We can copy them to new name
82+
if (userReq.headers['x-custom-header']) {
83+
headers['x-proxied-custom-header'] = userReq.headers['x-custom-header'];
84+
}
85+
if (userReq.headers['x-user-agent']) {
86+
headers['x-proxied-user-agent'] = userReq.headers['x-user-agent'];
87+
}
88+
89+
// We can copy them to the same name
90+
headers['x-copied-header-1'] = userReq.headers['x-copied-header-1'];
91+
headers['x-copied-header-2'] = userReq.headers['x-copied-header-2'];
92+
return headers;
93+
}
94+
}));
95+
96+
request(app)
97+
.get('/proxy')
98+
.set('x-custom-header', 'custom-value')
99+
.set('x-user-agent', 'test-agent')
100+
.set('x-copied-header-1', 'value1')
101+
.set('x-copied-header-2', 'value2')
102+
.expect(function (res) {
103+
// Verify the original headers were proxied to the response
104+
assert.equal(res.headers['x-proxied-custom-header'], 'custom-value');
105+
assert.equal(res.headers['x-proxied-user-agent'], 'test-agent');
106+
assert.equal(res.headers['x-copied-header-1'], 'value1');
107+
assert.equal(res.headers['x-copied-header-2'], 'value2');
108+
})
109+
.end(done);
110+
});
78111
});

0 commit comments

Comments
 (0)