deno.land / std@0.166.0 / node / _tools / test / parallel / test-net-server-listen-options.js

test-net-server-listen-options.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';const common = require('../common');const assert = require('assert');const net = require('net');
function close() { this.close(); }
{ // Test listen() net.createServer().listen().on('listening', common.mustCall(close)); // Test listen(cb) net.createServer().listen(common.mustCall(close)); // Test listen(port) net.createServer().listen(0).on('listening', common.mustCall(close)); // Test listen({port}) net.createServer().listen({ port: 0 }) .on('listening', common.mustCall(close));}
// Test listen(port, cb) and listen({ port }, cb) combinationsconst listenOnPort = [ (port, cb) => net.createServer().listen({ port }, cb), (port, cb) => net.createServer().listen(port, cb),];
{ const assertPort = () => { return common.expectsError({ code: 'ERR_SOCKET_BAD_PORT', name: 'RangeError' }); };
for (const listen of listenOnPort) { // Arbitrary unused ports listen('0', common.mustCall(close)); listen(0, common.mustCall(close)); listen(undefined, common.mustCall(close)); listen(null, common.mustCall(close)); // Test invalid ports assert.throws(() => listen(-1, common.mustNotCall()), assertPort()); assert.throws(() => listen(NaN, common.mustNotCall()), assertPort()); assert.throws(() => listen(123.456, common.mustNotCall()), assertPort()); assert.throws(() => listen(65536, common.mustNotCall()), assertPort()); assert.throws(() => listen(1 / 0, common.mustNotCall()), assertPort()); assert.throws(() => listen(-1 / 0, common.mustNotCall()), assertPort()); } // In listen(options, cb), port takes precedence over path assert.throws(() => { net.createServer().listen({ port: -1, path: common.PIPE }, common.mustNotCall()); }, assertPort());}
{ function shouldFailToListen(options) { const fn = () => { net.createServer().listen(options, common.mustNotCall()); };
if (typeof options === 'object' && !(('port' in options) || ('path' in options))) { assert.throws(fn, { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError', message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/, }); } else { assert.throws(fn, { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError', message: /^The argument 'options' is invalid\. Received .+$/, }); } }
shouldFailToListen(false, { port: false }); shouldFailToListen({ port: false }); shouldFailToListen(true); shouldFailToListen({ port: true }); // Invalid fd as listen(handle) shouldFailToListen({ fd: -1 }); // Invalid path in listen(options) shouldFailToListen({ path: -1 });
// Neither port or path are specified in options shouldFailToListen({}); shouldFailToListen({ host: 'localhost' }); shouldFailToListen({ host: 'localhost:3000' }); shouldFailToListen({ host: { port: 3000 } }); shouldFailToListen({ exclusive: true });}
std

Version Info

Tagged at
a year ago