Skip to content

Commit

Permalink
[#272] Workflow step for modifying headers. (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkpow authored Sep 28, 2017
1 parent ed81ee2 commit 7387986
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ request, and all additional requests would return the value resolved on the
first request.


### userResHeaderDecorator

```js
app.use('/proxy', proxy('www.google.com', {
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {
// recieves an Object of headers, returns an Object of headers.
return headers;
}
}));
```


#### decorateRequest

REMOVED: See ```proxyReqOptDecorator``` and ```proxyReqBodyDecorator```.
Expand Down
22 changes: 22 additions & 0 deletions app/steps/decorateUserResHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';


function decorateUserResHeaders(container) {
var resolverFn = container.options.userResHeaderDecorator;
var headers = container.user.res._headers;

if (!resolverFn) {
return Promise.resolve(container);
}

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) {
container.user.res.set(headers);
resolve(container);
});
});
}

module.exports = decorateUserResHeaders;
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var copyProxyResHeadersToUserRes = require('./app/steps/copyProxyResHeadersToUse
var decorateProxyReqBody = require('./app/steps/decorateProxyReqBody');
var decorateProxyReqOpts = require('./app/steps/decorateProxyReqOpts');
var decorateUserRes = require('./app/steps/decorateUserRes');
var decorateUserResHeaders = require('./app/steps/decorateUserResHeaders');
var maybeSkipToNextHandler = require('./app/steps/maybeSkipToNextHandler');
var prepareProxyReq = require('./app/steps/prepareProxyReq');
var resolveProxyHost = require('./app/steps/resolveProxyHost');
Expand Down Expand Up @@ -51,6 +52,7 @@ module.exports = function proxy(host, userOptions) {
.then(sendProxyRequest)
.then(maybeSkipToNextHandler)
.then(copyProxyResHeadersToUserRes)
.then(decorateUserResHeaders)
.then(decorateUserRes)
.then(sendUserRes)
.catch(next);
Expand Down
1 change: 1 addition & 0 deletions lib/resolveOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function resolveOptions(options) {
proxyReqOptDecorator: options.proxyReqOptDecorator,
proxyReqBodyDecorator: options.proxyReqBodyDecorator,
userResDecorator: options.userResDecorator || options.intercept,
userResHeaderDecorator: options.userResHeaderDecorator,
filter: options.filter || defaultFilter,
// For backwards compatability, we default to legacy behavior for newly added settings.
parseReqBody: isUnset(options.parseReqBody) ? true : options.parseReqBody,
Expand Down
49 changes: 49 additions & 0 deletions test/decorateUserResHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

var assert = require('assert');
var express = require('express');
var request = require('supertest');
var proxy = require('../');

describe('when userResHeaderDecorator is defined', function() {

this.timeout(10000);

var app, serverReference;

afterEach(function() {
serverReference.close();
});

beforeEach(function() {
app = express();
var pTarget = express();
pTarget.use(function(req, res) { res.json(req.headers); });
serverReference = pTarget.listen(12345);
});

afterEach(function() {
serverReference.close();
});

it('provides an interface for updating headers', function(done) {

app.use('/proxy', proxy('http://127.0.0.1:12345', {
userResHeaderDecorator: function(headers /*, userReq, userRes, proxyReq, proxyRes */) {
headers.boltedonheader = 'franky';
return headers;
}
}));

app.use(function(req, res) {
res.sendStatus(200);
});

request(app)
.get('/proxy')
.expect(function(res) {
assert(res.headers.boltedonheader === 'franky');
})
.end(done);
});
});

0 comments on commit 7387986

Please sign in to comment.