Skip to content

Commit

Permalink
Don't access res._headers directly when helpers exist
Browse files Browse the repository at this point in the history
res._headers is considered internal to node itself. Its value will
change in an backwards incompatible way in the future node releases.

Use the documented helper functions instead when they are available.
  • Loading branch information
ilkkao committed Feb 21, 2017
1 parent f3029f5 commit 990ddc4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ var proto = module.exports = {
}

// unset all headers, and set those specified
this.res._headers = {};
if (this.res.getHeaderNames) {
this.res.getHeaderNames().forEach(function(name) { this.removeHeader(name) }, this.res);
} else {
this.res._headers = {}; // Node < 8
}

this.set(err.headers);

// force text/plain
Expand Down
4 changes: 3 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ module.exports = {
*/

get header() {
return this.res._headers || {};
return this.res.getHeaders
? this.res.getHeaders()
: this.res._headers || {}; // Node < 8
},

/**
Expand Down
10 changes: 10 additions & 0 deletions test/response/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe('res.header', function(){
res.header.should.eql({ 'x-foo': 'bar' });
})

it('should use res.getHeades helper when it is available', function(){
var res = response(null, {
_headers: {},
getHeaders: function(){
return { 'x-foo': 'baz' }
}
});
res.header.should.eql({ 'x-foo': 'baz' });
})

describe('when res._headers not present', function (){
it('should return empty object', function (){
var res = response();
Expand Down

0 comments on commit 990ddc4

Please sign in to comment.