Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: improve coverage of lib/_http_client.js
  • Loading branch information
pd4d10 committed May 8, 2021
commit c8cf3afeb397a8e18ef31056b7ec6a8f69f52e93
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)'
})
);
}