Skip to content

Commit

Permalink
added support for res.rawHeaders. fixed #354
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Aug 5, 2015
1 parent 8c89942 commit 080703b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
response.statusCode = Number(interceptor.statusCode) || 200;
response.headers = interceptor.headers || {};
response.rawHeaders = interceptor.rawHeaders || [];
debug('response.rawHeaders:', response.rawHeaders);

// We again set request headers, now for our matched interceptor.
setRequestHeaders(req, options, interceptor);
Expand Down Expand Up @@ -406,6 +408,14 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
});

for(var rawHeaderIndex = 0 ; rawHeaderIndex < response.rawHeaders.length ; rawHeaderIndex += 2) {
var value = response.rawHeaders[rawHeaderIndex + 1];
if (typeof value === "function") {
response.rawHeaders[rawHeaderIndex + 1] = value(req, response, responseBody);
}
}


process.nextTick(function() {
var respond = function() {
debug('emitting response');
Expand Down
4 changes: 4 additions & 0 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ function startScope(basePath, options) {

if (headers !== undefined) {
this.headers = {};
this.rawHeaders = [];

// makes sure all keys in headers are in lower case
for (var key2 in headers) {
if (headers.hasOwnProperty(key2)) {
this.headers[key2.toLowerCase()] = headers[key2];
this.rawHeaders.push(key2);
this.rawHeaders.push(headers[key2]);
}
}
debug('reply.rawHeaders:', this.rawHeaders);
}

// If the content is not encoded we may need to transform the response body.
Expand Down
1 change: 1 addition & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ test("reply headers as function work", function(t) {
path: '/'
}, function (res) {
t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
t.equivalent(res.rawHeaders, ['x-my-headers', 'boo!']); // 67
t.end();
});
});
Expand Down

0 comments on commit 080703b

Please sign in to comment.