Skip to content

Commit

Permalink
test: refactor test-http-response-statuscode
Browse files Browse the repository at this point in the history
* move repeated code to function
* use strings for expected error (exposes result for [] as empty string)
* remove unneeded `common.mustCall()` usage with function arguments that
  are not callbacks

PR-URL: #11274
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
Trott committed Feb 13, 2017
1 parent 87df7e6 commit dd1cf8b
Showing 1 changed file with 20 additions and 42 deletions.
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

0 comments on commit dd1cf8b

Please sign in to comment.