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

2 files changed

Lines changed: 44 additions & 24 deletions

File tree

‎test/parallel/test-http-createConnection.js‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,36 @@ const http = require('http');
2525
constnet=require('net');
2626
constassert=require('assert');
2727

28+
functioncommonHttpGet(fn){
29+
if(typeoffn==='function'){
30+
fn=common.mustCall(fn);
31+
}
32+
returnnewPromise((resolve,reject)=>{
33+
http.get({createConnection: fn},(res)=>{
34+
resolve(res);
35+
}).on('error',(err)=>{
36+
reject(err);
37+
});
38+
});
39+
}
40+
2841
constserver=http.createServer(common.mustCall(function(req,res){
2942
res.end();
30-
},4)).listen(0,'127.0.0.1',function(){
31-
letfn=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+
awaitcommonHttpGet(createConnection);
45+
awaitcommonHttpGet(createConnectionAsync);
46+
awaitcommonHttpGet(createConnectionBoth1);
47+
awaitcommonHttpGet(createConnectionBoth2);
48+
49+
// Errors
50+
awaitassert.rejects(()=>commonHttpGet(createConnectionError),{
51+
message: 'sync'
52+
});
53+
awaitassert.rejects(()=>commonHttpGet(createConnectionAsyncError),{
54+
message: 'async'
5355
});
56+
57+
server.close();
5458
});
5559

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

8084
functioncreateConnectionError(options,cb){
81-
process.nextTick(cb,newError('Could not create socket'));
85+
thrownewError('sync');
86+
}
87+
88+
functioncreateConnectionAsyncError(options,cb){
89+
process.nextTick(cb,newError('async'));
8290
}

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

Lines changed: 12 additions & 0 deletions
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)