Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
}
Expand Down
11 changes: 7 additions & 4 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 26 additions & 3 deletions test/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}
}
}
});