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: https://github.com/nodejs/node/pull/11221
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
cjihrig 2017-02-07 12:47:11 -05:00
Родитель c239581e64
Коммит a4bb9fdb89
2 изменённых файлов: 13 добавлений и 11 удалений

Просмотреть файл

@ -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: