-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
112 lines (92 loc) · 2.9 KB
/
test.ts
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
103
104
105
106
107
108
109
110
111
112
import * as tap from 'tap';
import * as http from 'http';
import * as https from 'https';
import { resolve } from 'path';
import { AddressInfo, createServer } from 'net';
import { listen } from './src';
const testIf = (condition: boolean, ...args: Parameters<typeof tap.test>) =>
condition ? tap.test(...args) : tap.skip(...args);
const isWindows = process.platform === 'win32';
tap.test('No arguments', async (t) => {
const server = createServer();
const address = await listen(server);
t.ok(address instanceof URL);
t.equal(address.protocol, 'tcp:');
server.close();
});
tap.test('Invalid arguments', async (t) => {
const server = createServer();
() => {
// @ts-expect-error Passed a RegExp, which is not expected
listen(server, 0, 'adfs', /regexp-not-allowed/);
};
});
tap.test('Throws error if `address()` returns null', async (t) => {
const server = createServer();
server.address = () => null;
await t.rejects(listen(server));
server.close();
});
testIf(!isWindows, 'Returns URL for UNIX pipe', async (t) => {
const server = createServer();
const socket = 'unix.sock';
const address = await listen(server, socket);
t.equal(address.protocol, 'unix:');
t.equal(address.host, encodeURIComponent(resolve(socket)));
server.close();
});
tap.test('http', async (t) => {
const server = http.createServer();
// Using `ListenOptions` interface
const address = await listen(server, {
port: 0,
host: '127.0.0.1',
});
t.equal(address.protocol, 'http:');
server.close();
});
tap.test('https', async (t) => {
const server = https.createServer();
// Using `port`, `host` interface
const address = await listen(server, 0, '127.0.0.1');
t.equal(address.protocol, 'https:');
server.close();
});
testIf(!isWindows, 'http - UNIX pipe', async (t) => {
const socket = 'http.sock';
const server = http.createServer();
const address = await listen(server, socket);
t.equal(address.protocol, 'http+unix:');
server.close();
});
testIf(!isWindows, 'https - UNIX pipe', async (t) => {
const socket = 'https.sock';
const server = https.createServer();
const address = await listen(server, socket);
t.equal(address.protocol, 'https+unix:');
server.close();
});
tap.test('Custom protocol', async (t) => {
const server = createServer();
// @ts-expect-error `protocol` is not defined on `net.Server`
server.protocol = 'ftp';
// Using `ListenOptions` interface
const address = await listen(server);
t.equal(address.protocol, 'ftp:');
server.close();
});
tap.test('EADDRINUSE is thrown', async (t) => {
const port = 63971;
const server1 = createServer();
const server2 = createServer();
await t.resolves(listen(server1, port));
await t.rejects(listen(server2, port));
server1.close();
});
tap.test('IPv6 support', async (t) => {
const server = http.createServer();
const url = await listen(server);
t.equal((server.address() as AddressInfo).family, 'IPv6');
t.equal(url.hostname, '[::]');
server.close();
});