-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
http-server.integration.ts
359 lines (323 loc) · 11.5 KB
/
http-server.integration.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/http-server
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
expect,
givenHttpServerConfig,
httpGetAsync,
httpsGetAsync,
skipOnTravis,
supertest,
} from '@loopback/testlab';
import {EventEmitter} from 'events';
import fs from 'fs';
import {Agent, IncomingMessage, Server, ServerResponse} from 'http';
import os from 'os';
import pEvent from 'p-event';
import path from 'path';
import {HttpOptions, HttpServer, HttpsOptions} from '../../';
import {HttpServerOptions} from '../../http-server';
describe('HttpServer (integration)', () => {
let server: HttpServer | undefined;
afterEach(stopServer);
skipOnTravis(it, 'formats IPv6 url correctly', async () => {
server = new HttpServer(dummyRequestHandler, {
host: '::1',
} as HttpOptions);
await server.start();
expect(getAddressFamily(server)).to.equal('IPv6');
const response = await httpGetAsync(server.url);
expect(response.statusCode).to.equal(200);
});
it('starts server', async () => {
const serverOptions = givenHttpServerConfig();
server = new HttpServer(dummyRequestHandler, serverOptions);
await server.start();
await supertest(server.url)
.get('/')
.expect(200);
});
it('stops server', async () => {
const serverOptions = givenHttpServerConfig();
server = new HttpServer(dummyRequestHandler, serverOptions);
await server.start();
await server.stop();
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
});
it('stops server with grace period and inflight request', async () => {
const serverOptions = givenHttpServerConfig() as HttpServerOptions;
serverOptions.gracePeriodForClose = 1000;
const {emitter, deferredRequestHandler} = createDeferredRequestHandler();
server = new HttpServer(deferredRequestHandler, serverOptions);
await server.start();
const agent = new Agent({keepAlive: true});
// Send a request with keep-alive
const req = httpGetAsync(server.url, agent);
// Wait until the request is accepted by the server
await pEvent(server.server, 'request');
// Stop the server
const stop = server.stop();
// Now notify the request to finish in next cycle with setImmediate
setImmediate(() => {
emitter.emit('finish');
});
// The in-flight task can finish before the grace period
await req;
// Wait until the server is stopped
await stop;
// No more new connections are accepted
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
});
it('stops server with shorter grace period and inflight request', async () => {
const serverOptions = givenHttpServerConfig() as HttpServerOptions;
serverOptions.gracePeriodForClose = 10;
const {deferredRequestHandler} = createDeferredRequestHandler();
server = new HttpServer(deferredRequestHandler, serverOptions);
await server.start();
const agent = new Agent({keepAlive: true});
// Send a request with keep-alive
const req = httpGetAsync(server.url, agent);
// Wait until the request is accepted by the server
await pEvent(server.server, 'request');
// Stop the server
await server.stop();
// No more new connections are accepted
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
// We never send `finish` to the pending request
// The inflight request is aborted as it takes longer than the grace period
await expect(req).to.be.rejectedWith(/socket hang up/);
});
it('exports original port', async () => {
server = new HttpServer(dummyRequestHandler, {port: 0});
expect(server)
.to.have.property('port')
.which.is.equal(0);
});
it('exports reported port', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('port')
.which.is.a.Number()
.which.is.greaterThan(0);
});
it('does not permanently bind to the initial port', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
const port = server.port;
await server.stop();
await server.start();
expect(server)
.to.have.property('port')
.which.is.a.Number()
.which.is.not.equal(port);
});
it('exports original host', async () => {
server = new HttpServer(dummyRequestHandler);
expect(server)
.to.have.property('host')
.which.is.equal(undefined);
});
it('exports reported host', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('host')
.which.is.a.String();
});
it('exports protocol', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('protocol')
.which.is.a.String()
.match(/http|https/);
});
it('exports url', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('url')
.which.is.a.String()
.match(/http|https\:\/\//);
});
it('exports address', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('address')
.which.is.an.Object();
});
it('exports server before start', async () => {
server = new HttpServer(dummyRequestHandler);
expect(server.server).to.be.instanceOf(Server);
});
it('stops server before start', async () => {
server = new HttpServer(dummyRequestHandler);
await server.stop();
});
it('resets address when server is stopped', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server)
.to.have.property('address')
.which.is.an.Object();
await server.stop();
expect(server.address).to.be.undefined();
});
it('exports listening', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
expect(server.listening).to.be.true();
await server.stop();
expect(server.listening).to.be.false();
});
it('reports error when the server cannot be started', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
const port = server.port;
const anotherServer = new HttpServer(dummyRequestHandler, {
port: port,
});
await expect(anotherServer.start()).to.be.rejectedWith(/EADDRINUSE/);
});
it('supports HTTP over IPv4', async () => {
server = new HttpServer(dummyRequestHandler, {host: '127.0.0.1'});
await server.start();
expect(getAddressFamily(server)).to.equal('IPv4');
const response = await httpGetAsync(server.url);
expect(response.statusCode).to.equal(200);
});
skipOnTravis(it, 'supports HTTP over IPv6', async () => {
server = new HttpServer(dummyRequestHandler, {host: '::1'});
await server.start();
expect(getAddressFamily(server)).to.equal('IPv6');
const response = await httpGetAsync(server.url);
expect(response.statusCode).to.equal(200);
});
it('supports HTTPS protocol with key and certificate files', async () => {
const serverOptions = givenHttpServerConfig();
const httpsServer: HttpServer = givenHttpsServer(serverOptions);
await httpsServer.start();
const response = await httpsGetAsync(httpsServer.url);
expect(response.statusCode).to.equal(200);
});
it('supports HTTPS protocol with a pfx file', async () => {
const httpsServer: HttpServer = givenHttpsServer({usePfx: true});
await httpsServer.start();
const response = await httpsGetAsync(httpsServer.url);
expect(response.statusCode).to.equal(200);
});
skipOnTravis(it, 'handles IPv6 loopback address in HTTPS', async () => {
const httpsServer: HttpServer = givenHttpsServer({
host: '::1',
});
await httpsServer.start();
expect(getAddressFamily(httpsServer)).to.equal('IPv6');
const response = await httpsGetAsync(httpsServer.url);
expect(response.statusCode).to.equal(200);
});
it('converts host from [::] to [::1] in url', async () => {
// Safari on MacOS does not support http://[::]:3000/
server = new HttpServer(dummyRequestHandler, {host: '::'});
await server.start();
expect(server.url).to.equal(`http://[::1]:${server.port}`);
});
it('converts host from 0.0.0.0 to 127.0.0.1 in url', async () => {
// Windows does not support http://0.0.0.0:3000/
server = new HttpServer(dummyRequestHandler, {host: '0.0.0.0'});
await server.start();
expect(server.url).to.equal(`http://127.0.0.1:${server.port}`);
});
it('supports HTTP over unix socket', async () => {
if (os.platform() === 'win32') return;
const socketPath = path.join(os.tmpdir(), 'test.sock');
server = new HttpServer(dummyRequestHandler, {
path: socketPath,
});
await server.start();
expect(getAddressFamily(server)).to.equal('ipc');
expect(server.address).to.eql(socketPath);
expect(server.host).to.be.undefined();
expect(server.port).to.eql(0);
expect(server.url).to.eql('http+unix://' + encodeURIComponent(socketPath));
await supertest(server.url)
.get('/')
.expect(200);
});
it('supports HTTP over Windows named pipe', async () => {
if (os.platform() !== 'win32') return;
const namedPipe = path.join('\\\\?\\pipe', process.cwd(), 'test.pipe');
server = new HttpServer(dummyRequestHandler, {
path: namedPipe,
});
await server.start();
expect(getAddressFamily(server)).to.equal('ipc');
expect(server.url).to.eql(namedPipe);
});
it('rejects invalid named pipe on Windows', async () => {
if (os.platform() !== 'win32') return;
const namedPipe = 'test.pipe';
expect(() => {
server = new HttpServer(dummyRequestHandler, {
path: namedPipe,
});
}).to.throw(/Named pipe test\.pipe does NOT start with/);
});
function getAddressFamily(httpServer: HttpServer) {
if (!httpServer || !httpServer.address) return undefined;
if (typeof httpServer.address === 'string') {
return 'ipc';
}
return httpServer.address.family;
}
function dummyRequestHandler(
req: IncomingMessage,
res: ServerResponse,
): void {
res.end();
}
/**
* Create a request handler to simulate long-running requests. The request
* is only processed once the emitter receives `finish` signal.
*/
function createDeferredRequestHandler() {
const emitter = new EventEmitter();
function deferredRequestHandler(
req: IncomingMessage,
res: ServerResponse,
): void {
emitter.on('finish', () => res.end());
}
return {emitter, deferredRequestHandler};
}
async function stopServer() {
if (!server) return;
await server.stop();
}
function givenHttpsServer({
usePfx,
host,
}: {
usePfx?: boolean;
host?: string;
}): HttpServer {
const options = givenHttpServerConfig<HttpsOptions>({
protocol: 'https',
host,
});
const certDir = path.resolve(__dirname, '../../../fixtures');
if (usePfx) {
const pfxPath = path.join(certDir, 'pfx.pfx');
options.pfx = fs.readFileSync(pfxPath);
options.passphrase = 'loopback4';
} else {
const keyPath = path.join(certDir, 'key.pem');
const certPath = path.join(certDir, 'cert.pem');
options.key = fs.readFileSync(keyPath);
options.cert = fs.readFileSync(certPath);
}
return new HttpServer(dummyRequestHandler, options);
}
});