From 71833db3c75a61134c3da888621478fa4da5d74a Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 9 Feb 2017 14:23:00 -0800 Subject: [PATCH] test: refactor test-http-response-statuscode * 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: https://github.com/nodejs/node/pull/11274 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Santiago Gimeno --- .../parallel/test-http-response-statuscode.js | 62 ++++++------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/test/parallel/test-http-response-statuscode.js b/test/parallel/test-http-response-statuscode.js index 1b2d4d598e1841..1abbe67d80a66d 100644 --- a/test/parallel/test-http-response-statuscode.js +++ b/test/parallel/test-http-response-statuscode.js @@ -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: