0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-net-options-lookup.js
cjihrig 7c73cd4c70
net: emit error on invalid address family
This commit adds proper error handling to net.connect() when
a custom lookup() function returns an invalid address family.

PR-URL: https://github.com/nodejs/node/pull/19415
Fixes: https://github.com/nodejs/node/issues/19407
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-03-19 14:49:34 -04:00

43 lines
870 B
JavaScript

'use strict';
const common = require('../common');
const net = require('net');
['foobar', 1, {}, []].forEach((input) => connectThrows(input));
// Using port 0 as lookup is emitted before connecting.
function connectThrows(input) {
const opts = {
host: 'localhost',
port: 0,
lookup: input
};
common.expectsError(() => {
net.connect(opts);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
}
connectDoesNotThrow(() => {});
function connectDoesNotThrow(input) {
const opts = {
host: 'localhost',
port: 0,
lookup: input
};
return net.connect(opts);
}
{
// Verify that an error is emitted when an invalid address family is returned.
const s = connectDoesNotThrow((host, options, cb) => {
cb(null, '127.0.0.1', 100);
});
s.on('error', common.expectsError({ code: 'ERR_INVALID_ADDRESS_FAMILY' }));
}