mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 23:56:58 +00:00

This completely refactors the `expectsError` behavior: so far it's almost identical to `assert.throws(fn, object)` in case it was used with a function as first argument. It had a magical property check that allowed to verify a functions `type` in case `type` was passed used in the validation object. This pattern is now completely removed and `assert.throws()` should be used instead. The main intent for `common.expectsError()` is to verify error cases for callback based APIs. This is now more flexible by accepting all validation possibilites that `assert.throws()` accepts as well. No magical properties exist anymore. This reduces surprising behavior for developers who are not used to the Node.js core code base. This has the side effect that `common` is used significantly less frequent. PR-URL: https://github.com/nodejs/node/pull/31092 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
155 lines
3.8 KiB
JavaScript
155 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const dgram = require('dgram');
|
|
const multicastAddress = '224.0.0.114';
|
|
|
|
const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
|
|
|
|
// addMembership() on closed socket should throw
|
|
{
|
|
const socket = setup();
|
|
socket.close(common.mustCall(() => {
|
|
assert.throws(() => {
|
|
socket.addMembership(multicastAddress);
|
|
}, {
|
|
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
|
|
name: 'Error',
|
|
message: /^Not running$/
|
|
});
|
|
}));
|
|
}
|
|
|
|
// dropMembership() on closed socket should throw
|
|
{
|
|
const socket = setup();
|
|
socket.close(common.mustCall(() => {
|
|
assert.throws(() => {
|
|
socket.dropMembership(multicastAddress);
|
|
}, {
|
|
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
|
|
name: 'Error',
|
|
message: /^Not running$/
|
|
});
|
|
}));
|
|
}
|
|
|
|
// addMembership() with no argument should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.addMembership();
|
|
}, {
|
|
code: 'ERR_MISSING_ARGS',
|
|
name: 'TypeError',
|
|
message: /^The "multicastAddress" argument must be specified$/
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// dropMembership() with no argument should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.dropMembership();
|
|
}, {
|
|
code: 'ERR_MISSING_ARGS',
|
|
name: 'TypeError',
|
|
message: /^The "multicastAddress" argument must be specified$/
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// addMembership() with invalid multicast address should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => { socket.addMembership('256.256.256.256'); },
|
|
/^Error: addMembership EINVAL$/);
|
|
socket.close();
|
|
}
|
|
|
|
// dropMembership() with invalid multicast address should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => { socket.dropMembership('256.256.256.256'); },
|
|
/^Error: dropMembership EINVAL$/);
|
|
socket.close();
|
|
}
|
|
|
|
// addSourceSpecificMembership with invalid sourceAddress should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.addSourceSpecificMembership(0, multicastAddress);
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
message: 'The "sourceAddress" argument must be of type string. ' +
|
|
'Received type number (0)'
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// addSourceSpecificMembership with invalid sourceAddress should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.addSourceSpecificMembership(multicastAddress, 0);
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
message: 'The "groupAddress" argument must be of type string. ' +
|
|
'Received type number (0)'
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// addSourceSpecificMembership with invalid groupAddress should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.addSourceSpecificMembership(multicastAddress, '0');
|
|
}, {
|
|
code: 'EINVAL',
|
|
message: 'addSourceSpecificMembership EINVAL'
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// dropSourceSpecificMembership with invalid sourceAddress should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.dropSourceSpecificMembership(0, multicastAddress);
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
message: 'The "sourceAddress" argument must be of type string. ' +
|
|
'Received type number (0)'
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// dropSourceSpecificMembership with invalid groupAddress should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.dropSourceSpecificMembership(multicastAddress, 0);
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
message: 'The "groupAddress" argument must be of type string. ' +
|
|
'Received type number (0)'
|
|
});
|
|
socket.close();
|
|
}
|
|
|
|
// dropSourceSpecificMembership with invalid UDP should throw
|
|
{
|
|
const socket = setup();
|
|
assert.throws(() => {
|
|
socket.dropSourceSpecificMembership(multicastAddress, '0');
|
|
}, {
|
|
code: 'EINVAL',
|
|
message: 'dropSourceSpecificMembership EINVAL'
|
|
});
|
|
socket.close();
|
|
}
|