From f391d3cad46a798c7ccaa09f3a7508c16f0ab8f1 Mon Sep 17 00:00:00 2001 From: Mithun Sasidharan Date: Mon, 4 Dec 2017 09:07:07 +0530 Subject: [PATCH] test: refactor code to use common.mustCall PR-URL: https://github.com/nodejs/node/pull/17437 Reviewed-By: Anatoli Papirovski Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- ...test-http-res-write-end-dont-take-array.js | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/test/parallel/test-http-res-write-end-dont-take-array.js b/test/parallel/test-http-res-write-end-dont-take-array.js index fcf9da8c12b0e3..94b105cb18177c 100644 --- a/test/parallel/test-http-res-write-end-dont-take-array.js +++ b/test/parallel/test-http-res-write-end-dont-take-array.js @@ -20,44 +20,39 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const http = require('http'); -let test = 1; +const server = http.createServer(); -const server = http.createServer(function(req, res) { - res.writeHead(200, { 'Content-Type': 'text/plain' }); - if (test === 1) { - // write should accept string - res.write('string'); - // write should accept buffer - res.write(Buffer.from('asdf')); +server.once('request', common.mustCall((req, res) => { + server.on('request', common.mustCall((req, res) => { + res.end(Buffer.from('asdf')); + })); + // write should accept string + res.write('string'); + // write should accept buffer + res.write(Buffer.from('asdf')); - // write should not accept an Array - assert.throws(function() { - res.write(['array']); - }, TypeError, 'first argument must be a string or Buffer'); + // write should not accept an Array + assert.throws(function() { + res.write(['array']); + }, TypeError, 'first argument must be a string or Buffer'); - // end should not accept an Array - assert.throws(function() { - res.end(['moo']); - }, TypeError, 'first argument must be a string or Buffer'); + // end should not accept an Array + assert.throws(function() { + res.end(['moo']); + }, TypeError, 'first argument must be a string or Buffer'); - // end should accept string - res.end('string'); - } else if (test === 2) { - // end should accept Buffer - res.end(Buffer.from('asdf')); - } -}); + // end should accept string + res.end('string'); +})); server.listen(0, function() { // just make a request, other tests handle responses http.get({ port: this.address().port }, function(res) { res.resume(); - // lazy serial test, because we can only call end once per request - test += 1; // do it again to test .end(Buffer); http.get({ port: server.address().port }, function(res) { res.resume();