diff --git a/README.md b/README.md index a0870a2..402d591 100644 --- a/README.md +++ b/README.md @@ -221,15 +221,17 @@ module.exports = function(options) { }; ``` -If the transform needs to change the `Content-Type` of the response, a `contentType` property can be declared on the transform function that the proxy will recognize and set the header accordingly. +If the transform needs to change the headers of the response, a `headers` object can be declared on the transform function that the proxy will recognize and overwrite each header accordingly. + +If you wish to only overwrite headers, you can omit the transform function. ```js module.exports = function() { return { - name: 'appender', - contentType: 'application/json', - transform: function() { - return through2(...) + name: 'header-overwrite', + headers: { + 'content-type': 'application/json', + 'location': 'http://localhost' } }; } diff --git a/lib/proxy.js b/lib/proxy.js index 614aa59..519ed4a 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -281,11 +281,14 @@ module.exports = function(options) { // Pipe the stream through each transform in sequence transforms.forEach(function(transform) { debug('applying transform %s', transform.name); - if (transform.contentType) { - headers['Content-Type'] = transform.contentType; + if (transform.headers) { + for (var key in transform.headers) { + headers[key] = transform.headers[key]; + } + } + if (transform.transform) { + stream = stream.pipe(transform.transform()); } - - stream = stream.pipe(transform.transform()); }); return stream; diff --git a/test/transforms.js b/test/transforms.js index 0ce1964..9d66228 100644 --- a/test/transforms.js +++ b/test/transforms.js @@ -35,6 +35,17 @@ describe('response transforms', function() { .end(done); }); + it('transforms headers', function(done) { + this.proxyOptions.transforms = [ + headerTransform('text/html', 'http://localhost') + ]; + supertest(this.server).get('/proxy') + .expect(200) + .expect('Content-Type', /^text\/html/) + .expect('location', 'http://localhost') + .end(done); + }); + it('recreates the transform stream between requests', function(done) { var self = this; supertest(self.server).get('/proxy') @@ -101,13 +112,25 @@ describe('response transforms', function() { function appenderTransform(appendText, contentType) { return { name: 'appender', - contentType: contentType, - transform: function() { - return through2(function(chunk, enc, cb) { + headers: { + 'Content-Type': contentType + }, + transform: function () { + return through2(function (chunk, enc, cb) { this.push(chunk + appendText); cb(); }); } }; } + + function headerTransform(contentType, location) { + return { + name: 'overwrite-headers', + headers: { + 'Content-Type': contentType, + 'location': location + } + } + } });