1'use strict';
2// Flags: --expose-internals
3const common = require('../common');
4const assert = require('assert');
5const net = require('net');
6const { normalizedArgsSymbol } = require('internal/net');
7
8function validateNormalizedArgs(input, output) {
9  const args = net._normalizeArgs(input);
10
11  assert.deepStrictEqual(args, output);
12  assert.strictEqual(args[normalizedArgsSymbol], true);
13}
14
15// Test creation of normalized arguments.
16const res = [{}, null];
17res[normalizedArgsSymbol] = true;
18validateNormalizedArgs([], res);
19res[0].port = 1234;
20validateNormalizedArgs([{ port: 1234 }], res);
21res[1] = assert.fail;
22validateNormalizedArgs([{ port: 1234 }, assert.fail], res);
23
24// Connecting to the server should fail with a standard array.
25{
26  const server = net.createServer(common.mustNotCall('should not connect'));
27
28  server.listen(common.mustCall(() => {
29    const port = server.address().port;
30    const socket = new net.Socket();
31
32    assert.throws(() => {
33      socket.connect([{ port }, assert.fail]);
34    }, {
35      code: 'ERR_MISSING_ARGS'
36    });
37    server.close();
38  }));
39}
40
41// Connecting to the server should succeed with a normalized array.
42{
43  const server = net.createServer(common.mustCall((connection) => {
44    connection.end();
45    server.close();
46  }));
47
48  server.listen(common.mustCall(() => {
49    const port = server.address().port;
50    const socket = new net.Socket();
51    const args = net._normalizeArgs([{ port }, common.mustCall()]);
52
53    socket.connect(args);
54  }));
55}
56