• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const dgram = require('dgram');
6
7const PORT = 12345;
8
9const client = dgram.createSocket('udp4');
10client.connect(PORT, common.mustCall(() => {
11  const remoteAddr = client.remoteAddress();
12  assert.strictEqual(remoteAddr.port, PORT);
13  assert.throws(() => {
14    client.connect(PORT, common.mustNotCall());
15  }, {
16    name: 'Error',
17    message: 'Already connected',
18    code: 'ERR_SOCKET_DGRAM_IS_CONNECTED'
19  });
20
21  client.disconnect();
22  assert.throws(() => {
23    client.disconnect();
24  }, {
25    name: 'Error',
26    message: 'Not connected',
27    code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
28  });
29
30  assert.throws(() => {
31    client.remoteAddress();
32  }, {
33    name: 'Error',
34    message: 'Not connected',
35    code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
36  });
37
38  client.once('connect', common.mustCall(() => client.close()));
39  client.connect(PORT);
40}));
41
42assert.throws(() => {
43  client.connect(PORT);
44}, {
45  name: 'Error',
46  message: 'Already connected',
47  code: 'ERR_SOCKET_DGRAM_IS_CONNECTED'
48});
49
50assert.throws(() => {
51  client.disconnect();
52}, {
53  name: 'Error',
54  message: 'Not connected',
55  code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
56});
57
58[ 0, null, 78960, undefined ].forEach((port) => {
59  assert.throws(() => {
60    client.connect(port);
61  }, {
62    name: 'RangeError',
63    message: /^Port should be > 0 and < 65536/,
64    code: 'ERR_SOCKET_BAD_PORT'
65  });
66});
67