Permalink
Browse files

http: include provided status code in range error

ServerResponse#writeHead() coerces the user provided status code
to a number and then performs a range check. If the check fails,
a range error is thrown. The coerced status code is included in
the error message. This commit uses the user provided status code
instead.

PR-URL: #11221
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
cjihrig committed Feb 7, 2017
1 parent c239581 commit a4bb9fdb893c8f2c36a6b77862e29b2608e080c2
Showing with 13 additions and 11 deletions.
  1. +3 −1 lib/_http_server.js
  2. +10 −10 test/parallel/test-http-response-statuscode.js
View
@@ -161,9 +161,11 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() {
ServerResponse.prototype.writeHead = writeHead;
function writeHead(statusCode, reason, obj) {
var originalStatusCode = statusCode;
statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
throw new RangeError(`Invalid status code: ${statusCode}`);
throw new RangeError(`Invalid status code: ${originalStatusCode}`);
if (typeof reason === 'string') {
// writeHead(statusCode, reasonPhrase[, headers])
@@ -20,17 +20,17 @@ const server = http.Server(common.mustCall(function(req, res) {
case 1:
assert.throws(common.mustCall(() => {
res.writeHead(Infinity);
}), createErrorMessage(0));
}), createErrorMessage(Infinity));
break;
case 2:
assert.throws(common.mustCall(() => {
res.writeHead(NaN);
}), createErrorMessage(0));
}), createErrorMessage(NaN));
break;
case 3:
assert.throws(common.mustCall(() => {
res.writeHead({});
}), createErrorMessage(0));
}), createErrorMessage('\\[object Object\\]'));
break;
case 4:
assert.throws(common.mustCall(() => {
@@ -45,37 +45,37 @@ const server = http.Server(common.mustCall(function(req, res) {
case 6:
assert.throws(common.mustCall(() => {
res.writeHead('1000');
}), createErrorMessage(1000));
}), createErrorMessage('1000'));
break;
case 7:
assert.throws(common.mustCall(() => {
res.writeHead(null);
}), createErrorMessage(0));
}), createErrorMessage(null));
break;
case 8:
assert.throws(common.mustCall(() => {
res.writeHead(true);
}), createErrorMessage(1));
}), createErrorMessage(true));
break;
case 9:
assert.throws(common.mustCall(() => {
res.writeHead([]);
}), createErrorMessage(0));
}), createErrorMessage([]));
break;
case 10:
assert.throws(common.mustCall(() => {
res.writeHead('this is not valid');
}), createErrorMessage(0));
}), createErrorMessage('this is not valid'));
break;
case 11:
assert.throws(common.mustCall(() => {
res.writeHead('404 this is not valid either');
}), createErrorMessage(0));
}), createErrorMessage('404 this is not valid either'));
break;
case 12:
assert.throws(common.mustCall(() => {
res.writeHead();
}), createErrorMessage(0));
}), createErrorMessage(undefined));
this.close();
break;
default:

0 comments on commit a4bb9fd

Please sign in to comment.