Skip to content

Commit

Permalink
fix: monkey patch getHeaders for pre-v7 Node.js (GH-1409)
Browse files Browse the repository at this point in the history
* monkey patch getHeaders
* support new error message format
  • Loading branch information
William Blankenship committed Jul 12, 2017
1 parent 536a473 commit 82088a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
23 changes: 14 additions & 9 deletions lib/response.js
Expand Up @@ -110,15 +110,20 @@ Response.prototype.get = function get(name) {
};


/**
* retrieves all headers off the response.
* @public
* @function getHeaders
* @returns {Object}
*/
Response.prototype.getHeaders = function getHeaders() {
return (this._headers || {});
};
// If getHeaders is not provided by the Node platform, monkey patch our own.
// This is needed since versions of Node <7 did not come with a getHeaders.
// For more see GH-1408
if (typeof Response.prototype.getHeaders !== 'function') {
/**
* retrieves all headers off the response.
* @public
* @function getHeaders
* @returns {Object}
*/
Response.prototype.getHeaders = function getHeaders() {
return (this._headers || {});
};
}
Response.prototype.headers = Response.prototype.getHeaders;

/**
Expand Down
6 changes: 5 additions & 1 deletion test/response.test.js
Expand Up @@ -542,7 +542,11 @@ test('GH-951: sendRaw accepts only strings or buffers', function (t) {

SERVER.on('uncaughtException', function (req, res, route, err) {
t.ok(err);
t.equal(err.name, 'AssertionError');
// Node v8 uses static error codes
// and `name` includes the error name and error code as well which
// caused this test to break. Just changing the logic to check for
// string instead
t.equal((err.name.indexOf('AssertionError') >= 0), true);
t.equal(err.message, 'res.sendRaw() accepts only strings or buffers');
t.end();
});
Expand Down

0 comments on commit 82088a7

Please sign in to comment.