Skip to content

Commit 8a44ee4

Browse files
pd4d10targos
authored andcommitted
test: improve coverage of lib/_http_client.js
PR-URL: #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>
1 parent 8a45b85 commit 8a44ee4

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

test/parallel/test-http-createConnection.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,36 @@ const http = require('http');
2525
const net = require('net');
2626
const assert = require('assert');
2727

28+
function commonHttpGet(fn) {
29+
if (typeof fn === 'function') {
30+
fn = common.mustCall(fn);
31+
}
32+
return new Promise((resolve, reject) => {
33+
http.get({ createConnection: fn }, (res) => {
34+
resolve(res);
35+
}).on('error', (err) => {
36+
reject(err);
37+
});
38+
});
39+
}
40+
2841
const server = http.createServer(common.mustCall(function(req, res) {
2942
res.end();
30-
}, 4)).listen(0, '127.0.0.1', function() {
31-
let fn = common.mustCall(createConnection);
32-
http.get({ createConnection: fn }, function(res) {
33-
res.resume();
34-
fn = common.mustCall(createConnectionAsync);
35-
http.get({ createConnection: fn }, function(res) {
36-
res.resume();
37-
fn = common.mustCall(createConnectionBoth1);
38-
http.get({ createConnection: fn }, function(res) {
39-
res.resume();
40-
fn = common.mustCall(createConnectionBoth2);
41-
http.get({ createConnection: fn }, function(res) {
42-
res.resume();
43-
fn = common.mustCall(createConnectionError);
44-
http.get({ createConnection: fn }, function(res) {
45-
assert.fail('Unexpected response callback');
46-
}).on('error', common.mustCall(function(err) {
47-
assert.strictEqual(err.message, 'Could not create socket');
48-
server.close();
49-
}));
50-
});
51-
});
52-
});
43+
}, 4)).listen(0, '127.0.0.1', async () => {
44+
await commonHttpGet(createConnection);
45+
await commonHttpGet(createConnectionAsync);
46+
await commonHttpGet(createConnectionBoth1);
47+
await commonHttpGet(createConnectionBoth2);
48+
49+
// Errors
50+
await assert.rejects(() => commonHttpGet(createConnectionError), {
51+
message: 'sync'
52+
});
53+
await assert.rejects(() => commonHttpGet(createConnectionAsyncError), {
54+
message: 'async'
5355
});
56+
57+
server.close();
5458
});
5559

5660
function createConnection() {
@@ -78,5 +82,9 @@ function createConnectionBoth2(options, cb) {
7882
}
7983

8084
function createConnectionError(options, cb) {
81-
process.nextTick(cb, new Error('Could not create socket'));
85+
throw new Error('sync');
86+
}
87+
88+
function createConnectionAsyncError(options, cb) {
89+
process.nextTick(cb, new Error('async'));
8290
}

test/parallel/test-http-insecure-parser-per-stream.js

+12
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,15 @@ const MakeDuplexPair = require('../common/duplexpair');
8080
'Hello: foo\x08foo\r\n' +
8181
'\r\n\r\n');
8282
}
83+
84+
// Test 5: Invalid argument type
85+
{
86+
assert.throws(
87+
() => http.request({ insecureHTTPParser: 0 }, common.mustNotCall()),
88+
common.expectsError({
89+
code: 'ERR_INVALID_ARG_TYPE',
90+
message: 'The "options.insecureHTTPParser" property must be of' +
91+
' type boolean. Received type number (0)'
92+
})
93+
);
94+
}

0 commit comments

Comments
 (0)