From 2ddcceb1dcaea91d9c646c7f8185ebfa9d8a4531 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 8 May 2021 19:01:04 +0800 Subject: [PATCH] test: improve coverage of lib/_http_client.js PR-URL: https://github.com/nodejs/node/pull/38599 Refs: https://coverage.nodejs.org/coverage-f37c26b8a2e10d0a/lib/_http_client.js.html#L200 Reviewed-By: Rich Trott Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- test/parallel/test-http-createConnection.js | 56 +++++++++++-------- .../test-http-insecure-parser-per-stream.js | 12 ++++ 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/test/parallel/test-http-createConnection.js b/test/parallel/test-http-createConnection.js index a65b9a158c679d..1425b964a75aa3 100644 --- a/test/parallel/test-http-createConnection.js +++ b/test/parallel/test-http-createConnection.js @@ -25,32 +25,36 @@ const http = require('http'); const net = require('net'); const assert = require('assert'); +function commonHttpGet(fn) { + if (typeof fn === 'function') { + fn = common.mustCall(fn); + } + return new Promise((resolve, reject) => { + http.get({ createConnection: fn }, (res) => { + resolve(res); + }).on('error', (err) => { + reject(err); + }); + }); +} + const server = http.createServer(common.mustCall(function(req, res) { res.end(); -}, 4)).listen(0, '127.0.0.1', function() { - let fn = common.mustCall(createConnection); - http.get({ createConnection: fn }, function(res) { - res.resume(); - fn = common.mustCall(createConnectionAsync); - http.get({ createConnection: fn }, function(res) { - res.resume(); - fn = common.mustCall(createConnectionBoth1); - http.get({ createConnection: fn }, function(res) { - res.resume(); - fn = common.mustCall(createConnectionBoth2); - http.get({ createConnection: fn }, function(res) { - res.resume(); - fn = common.mustCall(createConnectionError); - http.get({ createConnection: fn }, function(res) { - assert.fail('Unexpected response callback'); - }).on('error', common.mustCall(function(err) { - assert.strictEqual(err.message, 'Could not create socket'); - server.close(); - })); - }); - }); - }); +}, 4)).listen(0, '127.0.0.1', async () => { + await commonHttpGet(createConnection); + await commonHttpGet(createConnectionAsync); + await commonHttpGet(createConnectionBoth1); + await commonHttpGet(createConnectionBoth2); + + // Errors + await assert.rejects(() => commonHttpGet(createConnectionError), { + message: 'sync' + }); + await assert.rejects(() => commonHttpGet(createConnectionAsyncError), { + message: 'async' }); + + server.close(); }); function createConnection() { @@ -78,5 +82,9 @@ function createConnectionBoth2(options, cb) { } function createConnectionError(options, cb) { - process.nextTick(cb, new Error('Could not create socket')); + throw new Error('sync'); +} + +function createConnectionAsyncError(options, cb) { + process.nextTick(cb, new Error('async')); } diff --git a/test/parallel/test-http-insecure-parser-per-stream.js b/test/parallel/test-http-insecure-parser-per-stream.js index d2b8ffe708770c..2ba245c8f419b1 100644 --- a/test/parallel/test-http-insecure-parser-per-stream.js +++ b/test/parallel/test-http-insecure-parser-per-stream.js @@ -80,3 +80,15 @@ const MakeDuplexPair = require('../common/duplexpair'); 'Hello: foo\x08foo\r\n' + '\r\n\r\n'); } + +// Test 5: Invalid argument type +{ + assert.throws( + () => http.request({ insecureHTTPParser: 0 }, common.mustNotCall()), + common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "options.insecureHTTPParser" property must be of' + + ' type boolean. Received type number (0)' + }) + ); +}