From fb5e7e4c38806c3da50342efbc37f47da1da4f3b Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Fri, 10 Apr 2020 12:22:51 -0400 Subject: [PATCH] test: elevate common http sequences to common There are several instances of data download sequences in test cases. Defined an abstraction for it in common module for easy consumption. --- test/common/index.js | 10 ++++++++++ test/parallel/test-http-response-no-headers.js | 13 ++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index e77e7b95059947..9f770005db7a53 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -223,6 +223,15 @@ function createZeroFilledFile(filename) { fs.closeSync(fd); } +function receive(req, cb) { + let buf = ''; + req.on('data', (data) => { + buf += data; + }); + req.on('end', () => { + cb(buf); + }); +} const pwdCommand = isWindows ? ['cmd.exe', ['/d', '/c', 'cd']] : @@ -688,6 +697,7 @@ const common = { platformTimeout, printSkipMessage, pwdCommand, + receive, rootDir, runWithInvalidFD, skip, diff --git a/test/parallel/test-http-response-no-headers.js b/test/parallel/test-http-response-no-headers.js index 22705936e08254..f69b2f63e5de81 100644 --- a/test/parallel/test-http-response-no-headers.js +++ b/test/parallel/test-http-response-no-headers.js @@ -45,18 +45,13 @@ function test(httpVersion, callback) { }; const req = http.get(options, common.mustCall(function(res) { - let body = ''; - - res.on('data', function(data) { - body += data; - }); - res.on('aborted', common.mustNotCall()); - res.on('end', common.mustCall(function() { - assert.strictEqual(body, expected[httpVersion]); + + common.receive(res, function(data) { + assert.strictEqual(data, expected[httpVersion]); server.close(); if (callback) process.nextTick(callback); - })); + }); })); req.on('error', function(err) {