-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest.js
More file actions
233 lines (184 loc) · 4.76 KB
/
test.js
File metadata and controls
233 lines (184 loc) · 4.76 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {promisify} from 'node:util';
import http from 'node:http';
import http2 from 'node:http2';
import test from 'ava';
import delay from 'delay';
import createTestServer from 'create-test-server';
import waitForLocalhost from './index.js';
test('main', async t => {
t.plan(2);
const server = await createTestServer();
server.head('/', async (request, response) => {
await delay(1000);
response.end();
t.pass();
});
await waitForLocalhost({port: server.port});
t.pass();
await server.close();
});
test('use get method', async t => {
t.plan(2);
const server = await createTestServer();
server.get('/', async (request, response) => {
await delay(1000);
response.end();
t.pass();
});
await waitForLocalhost({
port: server.port,
useGet: true,
});
t.pass();
await server.close();
});
test('use custom path', async t => {
t.plan(2);
const server = await createTestServer();
server.get('/health', async (request, response) => {
await delay(1000);
response.end();
t.pass();
});
await waitForLocalhost({
port: server.port,
path: '/health',
});
t.pass();
await server.close();
});
test('use custom statusCodes', async t => {
t.plan(2);
const server = await createTestServer();
server.get('/', async (request, response) => {
await delay(1000);
response.status(202).end();
t.pass();
});
await waitForLocalhost({
port: server.port,
statusCodes: [201, 202],
});
t.pass();
await server.close();
});
test('should handle HTTP/2-only server with fallback', async t => {
const server = http2.createServer();
server.on('stream', (stream, headers) => {
if (headers[':method'] === 'HEAD') {
stream.respond({':status': 200});
stream.end();
}
});
await promisify(server.listen.bind(server))(0);
const {port} = server.address();
const result = await waitForLocalhost({port});
t.truthy(result);
t.true([4, 6].includes(result.ipVersion));
server.close();
});
test('should handle HTTP/2-only server with custom status codes', async t => {
const server = http2.createServer();
server.on('stream', (stream, headers) => {
if (headers[':method'] === 'HEAD') {
stream.respond({':status': 202});
stream.end();
}
});
await promisify(server.listen.bind(server))(0);
const {port} = server.address();
const result = await waitForLocalhost({
port,
statusCodes: [202],
});
t.truthy(result);
server.close();
});
test('should handle HTTP/2-only server with GET method', async t => {
const server = http2.createServer();
server.on('stream', (stream, headers) => {
if (headers[':method'] === 'GET') {
stream.respond({':status': 200});
stream.end('OK');
}
});
await promisify(server.listen.bind(server))(0);
const {port} = server.address();
const result = await waitForLocalhost({
port,
useGet: true,
});
t.truthy(result);
server.close();
});
test('should handle HTTP/2-only server with custom path', async t => {
const server = http2.createServer();
server.on('stream', (stream, headers) => {
if (headers[':path'] === '/health' && headers[':method'] === 'HEAD') {
stream.respond({':status': 200});
stream.end();
}
});
await promisify(server.listen.bind(server))(0);
const {port} = server.address();
const result = await waitForLocalhost({
port,
path: '/health',
});
t.truthy(result);
server.close();
});
test('should keep trying HTTP/1 while waiting for delayed server', async t => {
const reservation = http.createServer();
await promisify(reservation.listen.bind(reservation))(0, '127.0.0.1');
const {port} = reservation.address();
await promisify(reservation.close.bind(reservation))();
const server = http.createServer((request, response) => {
response.end();
});
const serverStarted = (async () => {
await delay(2000);
await promisify(server.listen.bind(server))(port, '127.0.0.1');
})();
try {
const result = await waitForLocalhost({
port,
signal: AbortSignal.timeout(5000),
});
t.is(result.ipVersion, 4);
} finally {
await serverStarted;
if (server.listening) {
await promisify(server.close.bind(server))();
}
}
});
test('should support AbortSignal.timeout()', async t => {
const startTime = Date.now();
try {
await waitForLocalhost({
port: 9999, // Non-existent server
signal: AbortSignal.timeout(500),
});
t.fail('Should have timed out');
} catch (error) {
const elapsed = Date.now() - startTime;
t.true(elapsed >= 500, 'Should timeout after 500ms');
t.true(elapsed < 700, 'Should timeout close to 500ms');
t.is(error.name, 'TimeoutError');
}
});
test('should not loop on error', async t => {
t.timeout(40_000);
try {
await waitForLocalhost({
port: 5879,
signal: AbortSignal.timeout(30_000),
path: '/json/list',
useGet: true,
});
t.fail('should have timed out');
} catch (error) {
t.is(error.name, 'TimeoutError');
}
});