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 <rtrott@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Rongjian Zhang authored and James M Snell committed 2021-05-14 15:55:09 -07:00
1 parent 284f74d6f9
commit 0ca6d21dfc
2 files changed
+45 -25

No files matched your search

+33 -25
View File
@@ -25,32 +25,36 @@ const http = require('http');
const net = require('net');
const assert = require('assert');
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();
}));
});
});
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', 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'));
}
@@ -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)'
})
);
}