Skip to content

Commit

Permalink
test: improve coverage of lib/_http_client.js
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pd4d10 authored and targos committed May 17, 2021
1 parent 36bb724 commit 5a0b521
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
56 changes: 32 additions & 24 deletions test/parallel/test-http-createConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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'));
}
12 changes: 12 additions & 0 deletions test/parallel/test-http-insecure-parser-per-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
})
);
}

0 comments on commit 5a0b521

Please sign in to comment.