node/test/parallel/test-dgram-send-address-types.js
Christopher Hiller 32679c73c1 dgram: improve signature of Socket.prototype.send
- Do not require presence of `address` parameter to use `callback`
  parameter; `address` is *always* optional
- Improve exception messaging if `address` is invalid type
- If `address` is an invalid type, guarantee a synchronously thrown
  exception
- Update documentation to reflect signature changes
- Add coverage around valid, undocumented types for `address` parameter.
- Add coverage around known invalid, but uncovered, types for `address`
  parameter.

PR-URL: https://github.com/nodejs/node/pull/10473
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-02-17 13:12:51 -08:00

52 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const port = common.PORT;
const buf = Buffer.from('test');
const client = dgram.createSocket('udp4');
const onMessage = common.mustCall((err, bytes) => {
assert.ifError(err);
assert.strictEqual(bytes, buf.length);
}, 6);
// 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);
const expectedError = new RegExp('^TypeError: Invalid arguments: address ' +
'must be a nonempty string or falsy$');
// invalid address: object
assert.throws(() => {
client.send(buf, port, []);
}, expectedError);
// invalid address: nonzero number
assert.throws(() => {
client.send(buf, port, 1);
}, expectedError);
// invalid address: true
assert.throws(() => {
client.send(buf, port, true);
}, expectedError);
client.unref();