fastify 5.2.1 → 5.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +22 -0
- package/LICENSE +1 -1
- package/PROJECT_CHARTER.md +7 -7
- package/README.md +24 -25
- package/SPONSORS.md +2 -0
- package/docs/Guides/Benchmarking.md +4 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +10 -10
- package/docs/Guides/Ecosystem.md +5 -1
- package/docs/Guides/Fluent-Schema.md +1 -1
- package/docs/Guides/Getting-Started.md +9 -5
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +1 -1
- package/docs/Guides/Migration-Guide-V5.md +12 -2
- package/docs/Guides/Plugins-Guide.md +6 -6
- package/docs/Guides/Serverless.md +14 -48
- package/docs/Guides/Style-Guide.md +2 -2
- package/docs/Guides/Testing.md +2 -2
- package/docs/Guides/Write-Plugin.md +2 -3
- package/docs/Reference/ContentTypeParser.md +58 -78
- package/docs/Reference/Decorators.md +50 -60
- package/docs/Reference/Encapsulation.md +28 -33
- package/docs/Reference/Errors.md +50 -53
- package/docs/Reference/HTTP2.md +7 -7
- package/docs/Reference/Hooks.md +31 -30
- package/docs/Reference/LTS.md +10 -15
- package/docs/Reference/Lifecycle.md +19 -24
- package/docs/Reference/Logging.md +59 -56
- package/docs/Reference/Middleware.md +19 -19
- package/docs/Reference/Plugins.md +55 -71
- package/docs/Reference/Principles.md +25 -30
- package/docs/Reference/Reply.md +11 -10
- package/docs/Reference/Request.md +89 -98
- package/docs/Reference/Routes.md +108 -128
- package/docs/Reference/Server.md +18 -16
- package/docs/Reference/Type-Providers.md +19 -21
- package/docs/Reference/TypeScript.md +1 -18
- package/docs/Reference/Validation-and-Serialization.md +134 -159
- package/docs/Reference/Warnings.md +22 -25
- package/fastify.js +1 -1
- package/lib/contentTypeParser.js +7 -8
- package/lib/error-handler.js +14 -12
- package/lib/headRoute.js +4 -2
- package/lib/pluginUtils.js +4 -2
- package/lib/server.js +5 -0
- package/lib/validation.js +1 -1
- package/lib/warnings.js +9 -0
- package/lib/wrapThenable.js +8 -1
- package/package.json +10 -10
- package/test/build/error-serializer.test.js +2 -1
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close.test.js +125 -108
- package/test/custom-parser-async.test.js +34 -36
- package/test/genReqId.test.js +125 -174
- package/test/has-route.test.js +1 -3
- package/test/internals/content-type-parser.test.js +1 -1
- package/test/issue-4959.test.js +84 -0
- package/test/listen.1.test.js +37 -34
- package/test/listen.2.test.js +47 -40
- package/test/listen.3.test.js +28 -32
- package/test/listen.4.test.js +61 -45
- package/test/listen.5.test.js +23 -0
- package/test/register.test.js +55 -50
- package/test/request-error.test.js +114 -94
- package/test/route-shorthand.test.js +36 -32
- package/test/server.test.js +0 -175
- package/test/stream.5.test.js +35 -33
- package/test/throw.test.js +87 -91
- package/test/toolkit.js +32 -0
- package/test/trust-proxy.test.js +23 -23
- package/test/types/instance.test-d.ts +1 -0
- package/test/upgrade.test.js +32 -30
- package/types/instance.d.ts +4 -0
package/test/close.test.js
CHANGED
|
@@ -2,42 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
const net = require('node:net')
|
|
4
4
|
const http = require('node:http')
|
|
5
|
-
const { test } = require('
|
|
5
|
+
const { test } = require('node:test')
|
|
6
6
|
const Fastify = require('..')
|
|
7
7
|
const { Client } = require('undici')
|
|
8
8
|
const split = require('split2')
|
|
9
9
|
const { sleep } = require('./helper')
|
|
10
10
|
|
|
11
|
-
test('close callback', t => {
|
|
11
|
+
test('close callback', (t, testDone) => {
|
|
12
12
|
t.plan(7)
|
|
13
13
|
const fastify = Fastify()
|
|
14
14
|
fastify.addHook('onClose', onClose)
|
|
15
15
|
function onClose (instance, done) {
|
|
16
|
-
t.
|
|
17
|
-
t.
|
|
18
|
-
t.
|
|
19
|
-
t.
|
|
16
|
+
t.assert.ok(typeof fastify === typeof this)
|
|
17
|
+
t.assert.ok(typeof fastify === typeof instance)
|
|
18
|
+
t.assert.strictEqual(fastify, this)
|
|
19
|
+
t.assert.strictEqual(fastify, instance)
|
|
20
20
|
done()
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
fastify.listen({ port: 0 }, err => {
|
|
24
|
-
t.
|
|
24
|
+
t.assert.ifError(err)
|
|
25
25
|
|
|
26
26
|
fastify.close((err) => {
|
|
27
|
-
t.
|
|
28
|
-
t.ok('close callback')
|
|
27
|
+
t.assert.ifError(err)
|
|
28
|
+
t.assert.ok('close callback')
|
|
29
|
+
testDone()
|
|
29
30
|
})
|
|
30
31
|
})
|
|
31
32
|
})
|
|
32
33
|
|
|
33
|
-
test('inside register', t => {
|
|
34
|
+
test('inside register', (t, done) => {
|
|
34
35
|
t.plan(5)
|
|
35
36
|
const fastify = Fastify()
|
|
36
37
|
fastify.register(function (f, opts, done) {
|
|
37
38
|
f.addHook('onClose', onClose)
|
|
38
39
|
function onClose (instance, done) {
|
|
39
|
-
t.ok(instance.prototype === fastify.prototype)
|
|
40
|
-
t.
|
|
40
|
+
t.assert.ok(instance.prototype === fastify.prototype)
|
|
41
|
+
t.assert.strictEqual(instance, f)
|
|
41
42
|
done()
|
|
42
43
|
}
|
|
43
44
|
|
|
@@ -45,23 +46,24 @@ test('inside register', t => {
|
|
|
45
46
|
})
|
|
46
47
|
|
|
47
48
|
fastify.listen({ port: 0 }, err => {
|
|
48
|
-
t.
|
|
49
|
+
t.assert.ifError(err)
|
|
49
50
|
|
|
50
51
|
fastify.close((err) => {
|
|
51
|
-
t.
|
|
52
|
-
t.ok('close callback')
|
|
52
|
+
t.assert.ifError(err)
|
|
53
|
+
t.assert.ok('close callback')
|
|
54
|
+
done()
|
|
53
55
|
})
|
|
54
56
|
})
|
|
55
57
|
})
|
|
56
58
|
|
|
57
|
-
test('close order', t => {
|
|
59
|
+
test('close order', (t, done) => {
|
|
58
60
|
t.plan(5)
|
|
59
61
|
const fastify = Fastify()
|
|
60
62
|
const order = [1, 2, 3]
|
|
61
63
|
|
|
62
64
|
fastify.register(function (f, opts, done) {
|
|
63
65
|
f.addHook('onClose', (instance, done) => {
|
|
64
|
-
t.
|
|
66
|
+
t.assert.strictEqual(order.shift(), 1)
|
|
65
67
|
done()
|
|
66
68
|
})
|
|
67
69
|
|
|
@@ -69,16 +71,17 @@ test('close order', t => {
|
|
|
69
71
|
})
|
|
70
72
|
|
|
71
73
|
fastify.addHook('onClose', (instance, done) => {
|
|
72
|
-
t.
|
|
74
|
+
t.assert.strictEqual(order.shift(), 2)
|
|
73
75
|
done()
|
|
74
76
|
})
|
|
75
77
|
|
|
76
78
|
fastify.listen({ port: 0 }, err => {
|
|
77
|
-
t.
|
|
79
|
+
t.assert.ifError(err)
|
|
78
80
|
|
|
79
81
|
fastify.close((err) => {
|
|
80
|
-
t.
|
|
81
|
-
t.
|
|
82
|
+
t.assert.ifError(err)
|
|
83
|
+
t.assert.strictEqual(order.shift(), 3)
|
|
84
|
+
done()
|
|
82
85
|
})
|
|
83
86
|
})
|
|
84
87
|
})
|
|
@@ -90,37 +93,38 @@ test('close order - async', async t => {
|
|
|
90
93
|
|
|
91
94
|
fastify.register(function (f, opts, done) {
|
|
92
95
|
f.addHook('onClose', async instance => {
|
|
93
|
-
t.
|
|
96
|
+
t.assert.strictEqual(order.shift(), 1)
|
|
94
97
|
})
|
|
95
98
|
|
|
96
99
|
done()
|
|
97
100
|
})
|
|
98
101
|
|
|
99
102
|
fastify.addHook('onClose', () => {
|
|
100
|
-
t.
|
|
103
|
+
t.assert.strictEqual(order.shift(), 2)
|
|
101
104
|
})
|
|
102
105
|
|
|
103
106
|
await fastify.listen({ port: 0 })
|
|
104
107
|
await fastify.close()
|
|
105
108
|
|
|
106
|
-
t.
|
|
109
|
+
t.assert.strictEqual(order.shift(), 3)
|
|
107
110
|
})
|
|
108
111
|
|
|
109
|
-
test('should not throw an error if the server is not listening', t => {
|
|
112
|
+
test('should not throw an error if the server is not listening', (t, done) => {
|
|
110
113
|
t.plan(2)
|
|
111
114
|
const fastify = Fastify()
|
|
112
115
|
fastify.addHook('onClose', onClose)
|
|
113
116
|
function onClose (instance, done) {
|
|
114
|
-
t.
|
|
117
|
+
t.assert.ok(instance.prototype === fastify.prototype)
|
|
115
118
|
done()
|
|
116
119
|
}
|
|
117
120
|
|
|
118
121
|
fastify.close((err) => {
|
|
119
|
-
t.
|
|
122
|
+
t.assert.ifError(err)
|
|
123
|
+
done()
|
|
120
124
|
})
|
|
121
125
|
})
|
|
122
126
|
|
|
123
|
-
test('onClose should keep the context', t => {
|
|
127
|
+
test('onClose should keep the context', (t, done) => {
|
|
124
128
|
t.plan(4)
|
|
125
129
|
const fastify = Fastify()
|
|
126
130
|
fastify.register(plugin)
|
|
@@ -128,11 +132,11 @@ test('onClose should keep the context', t => {
|
|
|
128
132
|
function plugin (instance, opts, done) {
|
|
129
133
|
instance.decorate('test', true)
|
|
130
134
|
instance.addHook('onClose', onClose)
|
|
131
|
-
t.ok(instance.prototype === fastify.prototype)
|
|
135
|
+
t.assert.ok(instance.prototype === fastify.prototype)
|
|
132
136
|
|
|
133
137
|
function onClose (i, done) {
|
|
134
|
-
t.ok(i.test)
|
|
135
|
-
t.
|
|
138
|
+
t.assert.ok(i.test)
|
|
139
|
+
t.assert.strictEqual(i, instance)
|
|
136
140
|
done()
|
|
137
141
|
}
|
|
138
142
|
|
|
@@ -140,11 +144,12 @@ test('onClose should keep the context', t => {
|
|
|
140
144
|
}
|
|
141
145
|
|
|
142
146
|
fastify.close((err) => {
|
|
143
|
-
t.
|
|
147
|
+
t.assert.ifError(err)
|
|
148
|
+
done()
|
|
144
149
|
})
|
|
145
150
|
})
|
|
146
151
|
|
|
147
|
-
test('Should return error while closing (promise) - injection', t => {
|
|
152
|
+
test('Should return error while closing (promise) - injection', (t, done) => {
|
|
148
153
|
t.plan(4)
|
|
149
154
|
const fastify = Fastify()
|
|
150
155
|
|
|
@@ -158,8 +163,8 @@ test('Should return error while closing (promise) - injection', t => {
|
|
|
158
163
|
method: 'GET',
|
|
159
164
|
url: '/'
|
|
160
165
|
}, (err, res) => {
|
|
161
|
-
t.
|
|
162
|
-
t.
|
|
166
|
+
t.assert.ifError(err)
|
|
167
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
163
168
|
fastify.close()
|
|
164
169
|
|
|
165
170
|
process.nextTick(() => {
|
|
@@ -167,14 +172,15 @@ test('Should return error while closing (promise) - injection', t => {
|
|
|
167
172
|
method: 'GET',
|
|
168
173
|
url: '/'
|
|
169
174
|
}).catch(err => {
|
|
170
|
-
t.ok(err)
|
|
171
|
-
t.
|
|
175
|
+
t.assert.ok(err)
|
|
176
|
+
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
|
|
177
|
+
done()
|
|
172
178
|
})
|
|
173
179
|
}, 100)
|
|
174
180
|
})
|
|
175
181
|
})
|
|
176
182
|
|
|
177
|
-
test('Should return error while closing (callback) - injection', t => {
|
|
183
|
+
test('Should return error while closing (callback) - injection', (t, done) => {
|
|
178
184
|
t.plan(4)
|
|
179
185
|
const fastify = Fastify()
|
|
180
186
|
|
|
@@ -190,8 +196,8 @@ test('Should return error while closing (callback) - injection', t => {
|
|
|
190
196
|
method: 'GET',
|
|
191
197
|
url: '/'
|
|
192
198
|
}, (err, res) => {
|
|
193
|
-
t.
|
|
194
|
-
t.
|
|
199
|
+
t.assert.ifError(err)
|
|
200
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
195
201
|
fastify.close()
|
|
196
202
|
|
|
197
203
|
setTimeout(() => {
|
|
@@ -199,14 +205,15 @@ test('Should return error while closing (callback) - injection', t => {
|
|
|
199
205
|
method: 'GET',
|
|
200
206
|
url: '/'
|
|
201
207
|
}, (err, res) => {
|
|
202
|
-
t.ok(err)
|
|
203
|
-
t.
|
|
208
|
+
t.assert.ok(err)
|
|
209
|
+
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
|
|
210
|
+
done()
|
|
204
211
|
})
|
|
205
212
|
}, 100)
|
|
206
213
|
})
|
|
207
214
|
})
|
|
208
215
|
|
|
209
|
-
test('Current opened connection should NOT continue to work after closing and return "connection: close" header - return503OnClosing: false', t => {
|
|
216
|
+
test('Current opened connection should NOT continue to work after closing and return "connection: close" header - return503OnClosing: false', (t, done) => {
|
|
210
217
|
t.plan(4)
|
|
211
218
|
const fastify = Fastify({
|
|
212
219
|
return503OnClosing: false,
|
|
@@ -219,7 +226,7 @@ test('Current opened connection should NOT continue to work after closing and re
|
|
|
219
226
|
})
|
|
220
227
|
|
|
221
228
|
fastify.listen({ port: 0 }, err => {
|
|
222
|
-
t.
|
|
229
|
+
t.assert.ifError(err)
|
|
223
230
|
|
|
224
231
|
const port = fastify.server.address().port
|
|
225
232
|
const client = net.createConnection({ port }, () => {
|
|
@@ -232,12 +239,13 @@ test('Current opened connection should NOT continue to work after closing and re
|
|
|
232
239
|
})
|
|
233
240
|
|
|
234
241
|
client.on('close', function () {
|
|
235
|
-
t.
|
|
242
|
+
t.assert.ok(true)
|
|
243
|
+
done()
|
|
236
244
|
})
|
|
237
245
|
|
|
238
246
|
client.once('data', data => {
|
|
239
|
-
t.match(data.toString(), /Connection:\s*keep-alive/i)
|
|
240
|
-
t.match(data.toString(), /200 OK/i)
|
|
247
|
+
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
|
|
248
|
+
t.assert.match(data.toString(), /200 OK/i)
|
|
241
249
|
|
|
242
250
|
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
|
|
243
251
|
})
|
|
@@ -245,7 +253,7 @@ test('Current opened connection should NOT continue to work after closing and re
|
|
|
245
253
|
})
|
|
246
254
|
})
|
|
247
255
|
|
|
248
|
-
test('Current opened connection should not accept new incoming connections', t => {
|
|
256
|
+
test('Current opened connection should not accept new incoming connections', (t, done) => {
|
|
249
257
|
t.plan(3)
|
|
250
258
|
const fastify = Fastify({ forceCloseConnections: false })
|
|
251
259
|
fastify.get('/', (req, reply) => {
|
|
@@ -255,19 +263,20 @@ test('Current opened connection should not accept new incoming connections', t =
|
|
|
255
263
|
}, 250)
|
|
256
264
|
})
|
|
257
265
|
|
|
258
|
-
fastify.listen({ port: 0 }, err => {
|
|
259
|
-
t.
|
|
266
|
+
fastify.listen({ port: 0 }, async err => {
|
|
267
|
+
t.assert.ifError(err)
|
|
260
268
|
const instance = new Client('http://localhost:' + fastify.server.address().port)
|
|
261
|
-
instance.request({ path: '/', method: 'GET' })
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
instance.request({ path: '/', method: 'GET' })
|
|
265
|
-
|
|
266
|
-
|
|
269
|
+
let response = await instance.request({ path: '/', method: 'GET' })
|
|
270
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
271
|
+
|
|
272
|
+
response = await instance.request({ path: '/', method: 'GET' })
|
|
273
|
+
t.assert.strictEqual(response.statusCode, 503)
|
|
274
|
+
|
|
275
|
+
done()
|
|
267
276
|
})
|
|
268
277
|
})
|
|
269
278
|
|
|
270
|
-
test('rejected incoming connections should be logged', t => {
|
|
279
|
+
test('rejected incoming connections should be logged', (t, done) => {
|
|
271
280
|
t.plan(2)
|
|
272
281
|
const stream = split(JSON.parse)
|
|
273
282
|
const fastify = Fastify({
|
|
@@ -290,13 +299,14 @@ test('rejected incoming connections should be logged', t => {
|
|
|
290
299
|
})
|
|
291
300
|
|
|
292
301
|
fastify.listen({ port: 0 }, err => {
|
|
293
|
-
t.
|
|
302
|
+
t.assert.ifError(err)
|
|
294
303
|
const instance = new Client('http://localhost:' + fastify.server.address().port)
|
|
295
304
|
// initial request to trigger close
|
|
296
305
|
instance.request({ path: '/', method: 'GET' })
|
|
297
306
|
// subsequent request should be rejected
|
|
298
307
|
instance.request({ path: '/', method: 'GET' }).then(() => {
|
|
299
|
-
t.ok(messages.find(message => message.msg.includes('request aborted')))
|
|
308
|
+
t.assert.ok(messages.find(message => message.msg.includes('request aborted')))
|
|
309
|
+
done()
|
|
300
310
|
})
|
|
301
311
|
})
|
|
302
312
|
})
|
|
@@ -311,8 +321,8 @@ test('Cannot be reopened the closed server without listen callback', async t =>
|
|
|
311
321
|
try {
|
|
312
322
|
await fastify.listen({ port: 0 })
|
|
313
323
|
} catch (err) {
|
|
314
|
-
t.ok(err)
|
|
315
|
-
t.
|
|
324
|
+
t.assert.ok(err)
|
|
325
|
+
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
|
|
316
326
|
}
|
|
317
327
|
})
|
|
318
328
|
|
|
@@ -328,15 +338,15 @@ test('Cannot be reopened the closed server has listen callback', async t => {
|
|
|
328
338
|
reject(err)
|
|
329
339
|
})
|
|
330
340
|
}).catch(err => {
|
|
331
|
-
t.
|
|
332
|
-
t.ok(err)
|
|
341
|
+
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
|
|
342
|
+
t.assert.ok(err)
|
|
333
343
|
})
|
|
334
344
|
})
|
|
335
345
|
|
|
336
346
|
const server = http.createServer()
|
|
337
347
|
const noSupport = typeof server.closeAllConnections !== 'function'
|
|
338
348
|
|
|
339
|
-
test('shutsdown while keep-alive connections are active (non-async, native)', { skip: noSupport }, t => {
|
|
349
|
+
test('shutsdown while keep-alive connections are active (non-async, native)', { skip: noSupport }, (t, done) => {
|
|
340
350
|
t.plan(5)
|
|
341
351
|
|
|
342
352
|
const timeoutTime = 2 * 60 * 1000
|
|
@@ -350,31 +360,32 @@ test('shutsdown while keep-alive connections are active (non-async, native)', {
|
|
|
350
360
|
})
|
|
351
361
|
|
|
352
362
|
fastify.listen({ port: 0 }, (err, address) => {
|
|
353
|
-
t.
|
|
363
|
+
t.assert.ifError(err)
|
|
354
364
|
|
|
355
365
|
const client = new Client(
|
|
356
366
|
'http://localhost:' + fastify.server.address().port,
|
|
357
367
|
{ keepAliveTimeout: 1 * 60 * 1000 }
|
|
358
368
|
)
|
|
359
369
|
client.request({ path: '/', method: 'GET' }, (err, response) => {
|
|
360
|
-
t.
|
|
361
|
-
t.
|
|
370
|
+
t.assert.ifError(err)
|
|
371
|
+
t.assert.strictEqual(client.closed, false)
|
|
362
372
|
|
|
363
373
|
fastify.close((err) => {
|
|
364
|
-
t.
|
|
374
|
+
t.assert.ifError(err)
|
|
365
375
|
|
|
366
376
|
// Due to the nature of the way we reap these keep-alive connections,
|
|
367
377
|
// there hasn't been enough time before the server fully closed in order
|
|
368
378
|
// for the client to have seen the socket get destroyed. The mere fact
|
|
369
379
|
// that we have reached this callback is enough indication that the
|
|
370
380
|
// feature being tested works as designed.
|
|
371
|
-
t.
|
|
381
|
+
t.assert.strictEqual(client.closed, false)
|
|
382
|
+
done()
|
|
372
383
|
})
|
|
373
384
|
})
|
|
374
385
|
})
|
|
375
386
|
})
|
|
376
387
|
|
|
377
|
-
test('shutsdown while keep-alive connections are active (non-async, idle, native)', { skip: noSupport }, t => {
|
|
388
|
+
test('shutsdown while keep-alive connections are active (non-async, idle, native)', { skip: noSupport }, (t, done) => {
|
|
378
389
|
t.plan(5)
|
|
379
390
|
|
|
380
391
|
const timeoutTime = 2 * 60 * 1000
|
|
@@ -388,25 +399,27 @@ test('shutsdown while keep-alive connections are active (non-async, idle, native
|
|
|
388
399
|
})
|
|
389
400
|
|
|
390
401
|
fastify.listen({ port: 0 }, (err, address) => {
|
|
391
|
-
t.
|
|
402
|
+
t.assert.ifError(err)
|
|
392
403
|
|
|
393
404
|
const client = new Client(
|
|
394
405
|
'http://localhost:' + fastify.server.address().port,
|
|
395
406
|
{ keepAliveTimeout: 1 * 60 * 1000 }
|
|
396
407
|
)
|
|
397
408
|
client.request({ path: '/', method: 'GET' }, (err, response) => {
|
|
398
|
-
t.
|
|
399
|
-
t.
|
|
409
|
+
t.assert.ifError(err)
|
|
410
|
+
t.assert.strictEqual(client.closed, false)
|
|
400
411
|
|
|
401
412
|
fastify.close((err) => {
|
|
402
|
-
t.
|
|
413
|
+
t.assert.ifError(err)
|
|
403
414
|
|
|
404
415
|
// Due to the nature of the way we reap these keep-alive connections,
|
|
405
416
|
// there hasn't been enough time before the server fully closed in order
|
|
406
417
|
// for the client to have seen the socket get destroyed. The mere fact
|
|
407
418
|
// that we have reached this callback is enough indication that the
|
|
408
419
|
// feature being tested works as designed.
|
|
409
|
-
t.
|
|
420
|
+
t.assert.strictEqual(client.closed, false)
|
|
421
|
+
|
|
422
|
+
done()
|
|
410
423
|
})
|
|
411
424
|
})
|
|
412
425
|
})
|
|
@@ -434,9 +447,9 @@ test('triggers on-close hook in the right order with multiple bindings', async t
|
|
|
434
447
|
setTimeout(() => {
|
|
435
448
|
fastify.close(err => {
|
|
436
449
|
order.push(3)
|
|
437
|
-
t.
|
|
450
|
+
t.assert.deepEqual(order, expectedOrder)
|
|
438
451
|
|
|
439
|
-
if (err) t.
|
|
452
|
+
if (err) t.assert.ifError(err)
|
|
440
453
|
else resolve()
|
|
441
454
|
})
|
|
442
455
|
}, 2000)
|
|
@@ -479,20 +492,20 @@ test('triggers on-close hook in the right order with multiple bindings (forceClo
|
|
|
479
492
|
})
|
|
480
493
|
|
|
481
494
|
client.request({ path: '/', method: 'GET' })
|
|
482
|
-
.then((res) => res.body.json(), err => t.
|
|
495
|
+
.then((res) => res.body.json(), err => t.assert.ifError(err))
|
|
483
496
|
.then(json => {
|
|
484
|
-
t.
|
|
485
|
-
t.
|
|
486
|
-
}, err => t.
|
|
497
|
+
t.assert.deepEqual(json, expectedPayload, 'should payload match')
|
|
498
|
+
t.assert.ok(!client.closed, 'should client not be closed')
|
|
499
|
+
}, err => t.assert.ifError(err))
|
|
487
500
|
}
|
|
488
501
|
|
|
489
502
|
await new Promise((resolve, reject) => {
|
|
490
503
|
setTimeout(() => {
|
|
491
504
|
fastify.close(err => {
|
|
492
505
|
order.push(2)
|
|
493
|
-
t.
|
|
506
|
+
t.assert.deepEqual(order, expectedOrder)
|
|
494
507
|
|
|
495
|
-
if (err) t.
|
|
508
|
+
if (err) t.assert.ifError(err)
|
|
496
509
|
else resolve()
|
|
497
510
|
})
|
|
498
511
|
}, 2000)
|
|
@@ -535,27 +548,27 @@ test('triggers on-close hook in the right order with multiple bindings (forceClo
|
|
|
535
548
|
})
|
|
536
549
|
|
|
537
550
|
client.request({ path: '/', method: 'GET' })
|
|
538
|
-
.then((res) => res.body.json(), err => t.
|
|
551
|
+
.then((res) => res.body.json(), err => t.assert.ifError(err))
|
|
539
552
|
.then(json => {
|
|
540
|
-
t.
|
|
541
|
-
t.
|
|
542
|
-
}, err => t.
|
|
553
|
+
t.assert.deepEqual(json, expectedPayload, 'should payload match')
|
|
554
|
+
t.assert.ok(!client.closed, 'should client not be closed')
|
|
555
|
+
}, err => t.assert.ifError(err))
|
|
543
556
|
}
|
|
544
557
|
|
|
545
558
|
await new Promise((resolve, reject) => {
|
|
546
559
|
setTimeout(() => {
|
|
547
560
|
fastify.close(err => {
|
|
548
561
|
order.push(2)
|
|
549
|
-
t.
|
|
562
|
+
t.assert.deepEqual(order, expectedOrder)
|
|
550
563
|
|
|
551
|
-
if (err) t.
|
|
564
|
+
if (err) t.assert.ifError(err)
|
|
552
565
|
else resolve()
|
|
553
566
|
})
|
|
554
567
|
}, 2000)
|
|
555
568
|
})
|
|
556
569
|
})
|
|
557
570
|
|
|
558
|
-
test('shutsdown while keep-alive connections are active (non-async, custom)', t => {
|
|
571
|
+
test('shutsdown while keep-alive connections are active (non-async, custom)', (t, done) => {
|
|
559
572
|
t.plan(5)
|
|
560
573
|
|
|
561
574
|
const timeoutTime = 2 * 60 * 1000
|
|
@@ -578,53 +591,56 @@ test('shutsdown while keep-alive connections are active (non-async, custom)', t
|
|
|
578
591
|
})
|
|
579
592
|
|
|
580
593
|
fastify.listen({ port: 0 }, (err, address) => {
|
|
581
|
-
t.
|
|
594
|
+
t.assert.ifError(err)
|
|
582
595
|
|
|
583
596
|
const client = new Client(
|
|
584
597
|
'http://localhost:' + fastify.server.address().port,
|
|
585
598
|
{ keepAliveTimeout: 1 * 60 * 1000 }
|
|
586
599
|
)
|
|
587
600
|
client.request({ path: '/', method: 'GET' }, (err, response) => {
|
|
588
|
-
t.
|
|
589
|
-
t.
|
|
601
|
+
t.assert.ifError(err)
|
|
602
|
+
t.assert.strictEqual(client.closed, false)
|
|
590
603
|
|
|
591
604
|
fastify.close((err) => {
|
|
592
|
-
t.
|
|
605
|
+
t.assert.ifError(err)
|
|
593
606
|
|
|
594
607
|
// Due to the nature of the way we reap these keep-alive connections,
|
|
595
608
|
// there hasn't been enough time before the server fully closed in order
|
|
596
609
|
// for the client to have seen the socket get destroyed. The mere fact
|
|
597
610
|
// that we have reached this callback is enough indication that the
|
|
598
611
|
// feature being tested works as designed.
|
|
599
|
-
t.
|
|
612
|
+
t.assert.strictEqual(client.closed, false)
|
|
613
|
+
|
|
614
|
+
done()
|
|
600
615
|
})
|
|
601
616
|
})
|
|
602
617
|
})
|
|
603
618
|
})
|
|
604
619
|
|
|
605
|
-
test('preClose callback', t => {
|
|
620
|
+
test('preClose callback', (t, done) => {
|
|
606
621
|
t.plan(5)
|
|
607
622
|
const fastify = Fastify()
|
|
608
623
|
fastify.addHook('onClose', onClose)
|
|
609
624
|
let preCloseCalled = false
|
|
610
625
|
function onClose (instance, done) {
|
|
611
|
-
t.
|
|
626
|
+
t.assert.strictEqual(preCloseCalled, true)
|
|
612
627
|
done()
|
|
613
628
|
}
|
|
614
629
|
fastify.addHook('preClose', preClose)
|
|
615
630
|
|
|
616
631
|
function preClose (done) {
|
|
617
|
-
t.
|
|
632
|
+
t.assert.ok(typeof this === typeof fastify)
|
|
618
633
|
preCloseCalled = true
|
|
619
634
|
done()
|
|
620
635
|
}
|
|
621
636
|
|
|
622
637
|
fastify.listen({ port: 0 }, err => {
|
|
623
|
-
t.
|
|
638
|
+
t.assert.ifError(err)
|
|
624
639
|
|
|
625
640
|
fastify.close((err) => {
|
|
626
|
-
t.
|
|
627
|
-
t.ok('close callback')
|
|
641
|
+
t.assert.ifError(err)
|
|
642
|
+
t.assert.ok('close callback')
|
|
643
|
+
done()
|
|
628
644
|
})
|
|
629
645
|
})
|
|
630
646
|
})
|
|
@@ -635,13 +651,13 @@ test('preClose async', async t => {
|
|
|
635
651
|
fastify.addHook('onClose', onClose)
|
|
636
652
|
let preCloseCalled = false
|
|
637
653
|
async function onClose () {
|
|
638
|
-
t.
|
|
654
|
+
t.assert.strictEqual(preCloseCalled, true)
|
|
639
655
|
}
|
|
640
656
|
fastify.addHook('preClose', preClose)
|
|
641
657
|
|
|
642
658
|
async function preClose () {
|
|
643
659
|
preCloseCalled = true
|
|
644
|
-
t.
|
|
660
|
+
t.assert.ok(typeof this === typeof fastify)
|
|
645
661
|
}
|
|
646
662
|
|
|
647
663
|
await fastify.listen({ port: 0 })
|
|
@@ -649,13 +665,13 @@ test('preClose async', async t => {
|
|
|
649
665
|
await fastify.close()
|
|
650
666
|
})
|
|
651
667
|
|
|
652
|
-
test('preClose execution order', t => {
|
|
668
|
+
test('preClose execution order', (t, done) => {
|
|
653
669
|
t.plan(4)
|
|
654
670
|
const fastify = Fastify()
|
|
655
671
|
const order = []
|
|
656
672
|
fastify.addHook('onClose', onClose)
|
|
657
673
|
function onClose (instance, done) {
|
|
658
|
-
t.
|
|
674
|
+
t.assert.deepStrictEqual(order, [1, 2, 3])
|
|
659
675
|
done()
|
|
660
676
|
}
|
|
661
677
|
|
|
@@ -679,11 +695,12 @@ test('preClose execution order', t => {
|
|
|
679
695
|
})
|
|
680
696
|
|
|
681
697
|
fastify.listen({ port: 0 }, err => {
|
|
682
|
-
t.
|
|
698
|
+
t.assert.ifError(err)
|
|
683
699
|
|
|
684
700
|
fastify.close((err) => {
|
|
685
|
-
t.
|
|
686
|
-
t.ok('close callback')
|
|
701
|
+
t.assert.ifError(err)
|
|
702
|
+
t.assert.ok('close callback')
|
|
703
|
+
done()
|
|
687
704
|
})
|
|
688
705
|
})
|
|
689
706
|
})
|