Skip to content

Commit 232a7b1

Browse files
committed
[548] Update docs and tests to show how to retain proxyRes headers.
1 parent dc85c03 commit 232a7b1

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
@@ -32,7 +32,6 @@ describe('when userResHeaderDecorator is defined', function () {
3232
});
3333

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

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

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

0 commit comments

Comments
 (0)