Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-http-response-statuscode #11274

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 20 additions & 42 deletions test/parallel/test-http-response-statuscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,54 @@ const http = require('http');
const MAX_REQUESTS = 13;
let reqNum = 0;

const createErrorMessage = (code) => {
return new RegExp(`^RangeError: Invalid status code: ${code}$`);
};
function test(res, header, code) {
const errRegExp = new RegExp(`^RangeError: Invalid status code: ${code}$`);
assert.throws(() => {
res.writeHead(header);
}, errRegExp);
}

const server = http.Server(common.mustCall(function(req, res) {
switch (reqNum) {
case 0:
assert.throws(common.mustCall(() => {
res.writeHead(-1);
}), createErrorMessage(-1));
test(res, -1, '-1');
break;
case 1:
assert.throws(common.mustCall(() => {
res.writeHead(Infinity);
}), createErrorMessage(Infinity));
test(res, Infinity, 'Infinity');
break;
case 2:
assert.throws(common.mustCall(() => {
res.writeHead(NaN);
}), createErrorMessage(NaN));
test(res, NaN, 'NaN');
break;
case 3:
assert.throws(common.mustCall(() => {
res.writeHead({});
}), createErrorMessage('\\[object Object\\]'));
test(res, {}, '\\[object Object\\]');
break;
case 4:
assert.throws(common.mustCall(() => {
res.writeHead(99);
}), createErrorMessage(99));
test(res, 99, '99');
break;
case 5:
assert.throws(common.mustCall(() => {
res.writeHead(1000);
}), createErrorMessage(1000));
test(res, 1000, '1000');
break;
case 6:
assert.throws(common.mustCall(() => {
res.writeHead('1000');
}), createErrorMessage('1000'));
test(res, '1000', '1000');
break;
case 7:
assert.throws(common.mustCall(() => {
res.writeHead(null);
}), createErrorMessage(null));
test(res, null, 'null');
break;
case 8:
assert.throws(common.mustCall(() => {
res.writeHead(true);
}), createErrorMessage(true));
test(res, true, 'true');
break;
case 9:
assert.throws(common.mustCall(() => {
res.writeHead([]);
}), createErrorMessage([]));
test(res, [], '');
break;
case 10:
assert.throws(common.mustCall(() => {
res.writeHead('this is not valid');
}), createErrorMessage('this is not valid'));
test(res, 'this is not valid', 'this is not valid');
break;
case 11:
assert.throws(common.mustCall(() => {
res.writeHead('404 this is not valid either');
}), createErrorMessage('404 this is not valid either'));
test(res, '404 this is not valid either', '404 this is not valid either');
break;
case 12:
assert.throws(common.mustCall(() => {
res.writeHead();
}), createErrorMessage(undefined));
assert.throws(() => { res.writeHead(); },
/^RangeError: Invalid status code: undefined$/);
this.close();
break;
default:
Expand Down