node/test/parallel/test-dgram-send-address-types.js
Michael Dawson e912c67d24 dgram: convert to using internal/errors
Covert lib/dgram.js over to using lib/internal/errors.js
for generating Errors. See
[using-internal-errors.md](https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md)
for more details.

I have not addressed the cases that use errnoException() and
exceptionWithHostPort() helper methods as changing these would require
fixing the tests across all of the different files that use them. In
addition, these helpers already add a `code` to the Error and we'll
have to discuss how that interacts with the `code` used by
lib/internal/errors.js.  I believe we should convert all users
of errnoException and exceptionWithHostPort in a PR dedicated to
that conversion.

PR-URL: https://github.com/nodejs/node/pull/12926
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben.bridgewater@fintura.de>
2017-05-24 10:16:46 -04:00

57 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const buf = Buffer.from('test');
const onMessage = common.mustCall((err, bytes) => {
assert.ifError(err);
assert.strictEqual(bytes, buf.length);
}, 6);
const expectedError = { code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message:
/^The "address" argument must be one of type string or falsy$/
};
const client = dgram.createSocket('udp4').bind(0, () => {
const port = client.address().port;
// valid address: false
client.send(buf, port, false, onMessage);
// valid address: empty string
client.send(buf, port, '', onMessage);
// valid address: null
client.send(buf, port, null, onMessage);
// valid address: 0
client.send(buf, port, 0, onMessage);
// valid address: undefined
client.send(buf, port, undefined, onMessage);
// valid address: not provided
client.send(buf, port, onMessage);
// invalid address: object
assert.throws(() => {
client.send(buf, port, []);
}, common.expectsError(expectedError));
// invalid address: nonzero number
assert.throws(() => {
client.send(buf, port, 1);
}, common.expectsError(expectedError));
// invalid address: true
assert.throws(() => {
client.send(buf, port, true);
}, common.expectsError(expectedError));
});
client.unref();