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.
Files changed (73) hide show
  1. package/.vscode/settings.json +22 -0
  2. package/LICENSE +1 -1
  3. package/PROJECT_CHARTER.md +7 -7
  4. package/README.md +24 -25
  5. package/SPONSORS.md +2 -0
  6. package/docs/Guides/Benchmarking.md +4 -4
  7. package/docs/Guides/Database.md +1 -1
  8. package/docs/Guides/Delay-Accepting-Requests.md +10 -10
  9. package/docs/Guides/Ecosystem.md +5 -1
  10. package/docs/Guides/Fluent-Schema.md +1 -1
  11. package/docs/Guides/Getting-Started.md +9 -5
  12. package/docs/Guides/Index.md +1 -1
  13. package/docs/Guides/Migration-Guide-V4.md +1 -1
  14. package/docs/Guides/Migration-Guide-V5.md +12 -2
  15. package/docs/Guides/Plugins-Guide.md +6 -6
  16. package/docs/Guides/Serverless.md +14 -48
  17. package/docs/Guides/Style-Guide.md +2 -2
  18. package/docs/Guides/Testing.md +2 -2
  19. package/docs/Guides/Write-Plugin.md +2 -3
  20. package/docs/Reference/ContentTypeParser.md +58 -78
  21. package/docs/Reference/Decorators.md +50 -60
  22. package/docs/Reference/Encapsulation.md +28 -33
  23. package/docs/Reference/Errors.md +50 -53
  24. package/docs/Reference/HTTP2.md +7 -7
  25. package/docs/Reference/Hooks.md +31 -30
  26. package/docs/Reference/LTS.md +10 -15
  27. package/docs/Reference/Lifecycle.md +19 -24
  28. package/docs/Reference/Logging.md +59 -56
  29. package/docs/Reference/Middleware.md +19 -19
  30. package/docs/Reference/Plugins.md +55 -71
  31. package/docs/Reference/Principles.md +25 -30
  32. package/docs/Reference/Reply.md +11 -10
  33. package/docs/Reference/Request.md +89 -98
  34. package/docs/Reference/Routes.md +108 -128
  35. package/docs/Reference/Server.md +18 -16
  36. package/docs/Reference/Type-Providers.md +19 -21
  37. package/docs/Reference/TypeScript.md +1 -18
  38. package/docs/Reference/Validation-and-Serialization.md +134 -159
  39. package/docs/Reference/Warnings.md +22 -25
  40. package/fastify.js +1 -1
  41. package/lib/contentTypeParser.js +7 -8
  42. package/lib/error-handler.js +14 -12
  43. package/lib/headRoute.js +4 -2
  44. package/lib/pluginUtils.js +4 -2
  45. package/lib/server.js +5 -0
  46. package/lib/validation.js +1 -1
  47. package/lib/warnings.js +9 -0
  48. package/lib/wrapThenable.js +8 -1
  49. package/package.json +10 -10
  50. package/test/build/error-serializer.test.js +2 -1
  51. package/test/bundler/esbuild/package.json +1 -1
  52. package/test/close.test.js +125 -108
  53. package/test/custom-parser-async.test.js +34 -36
  54. package/test/genReqId.test.js +125 -174
  55. package/test/has-route.test.js +1 -3
  56. package/test/internals/content-type-parser.test.js +1 -1
  57. package/test/issue-4959.test.js +84 -0
  58. package/test/listen.1.test.js +37 -34
  59. package/test/listen.2.test.js +47 -40
  60. package/test/listen.3.test.js +28 -32
  61. package/test/listen.4.test.js +61 -45
  62. package/test/listen.5.test.js +23 -0
  63. package/test/register.test.js +55 -50
  64. package/test/request-error.test.js +114 -94
  65. package/test/route-shorthand.test.js +36 -32
  66. package/test/server.test.js +0 -175
  67. package/test/stream.5.test.js +35 -33
  68. package/test/throw.test.js +87 -91
  69. package/test/toolkit.js +32 -0
  70. package/test/trust-proxy.test.js +23 -23
  71. package/test/types/instance.test-d.ts +1 -0
  72. package/test/upgrade.test.js +32 -30
  73. package/types/instance.d.ts +4 -0
@@ -2,12 +2,11 @@
2
2
 
3
3
  const { connect } = require('node:net')
4
4
  const sget = require('simple-get').concat
5
- const t = require('tap')
6
- const test = t.test
5
+ const { test } = require('node:test')
7
6
  const Fastify = require('..')
8
7
  const { kRequest } = require('../lib/symbols.js')
9
8
 
10
- test('default 400 on request error', t => {
9
+ test('default 400 on request error', (t, done) => {
11
10
  t.plan(4)
12
11
 
13
12
  const fastify = Fastify()
@@ -26,25 +25,26 @@ test('default 400 on request error', t => {
26
25
  text: '12345678901234567890123456789012345678901234567890'
27
26
  }
28
27
  }, (err, res) => {
29
- t.error(err)
30
- t.equal(res.statusCode, 400)
31
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
32
- t.same(JSON.parse(res.payload), {
28
+ t.assert.ifError(err)
29
+ t.assert.strictEqual(res.statusCode, 400)
30
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
31
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
33
32
  error: 'Bad Request',
34
33
  message: 'Simulated',
35
34
  statusCode: 400
36
35
  })
36
+ done()
37
37
  })
38
38
  })
39
39
 
40
- test('default 400 on request error with custom error handler', t => {
40
+ test('default 400 on request error with custom error handler', (t, done) => {
41
41
  t.plan(6)
42
42
 
43
43
  const fastify = Fastify()
44
44
 
45
45
  fastify.setErrorHandler(function (err, request, reply) {
46
- t.type(request, 'object')
47
- t.type(request, fastify[kRequest].parent)
46
+ t.assert.strictEqual(typeof request, 'object')
47
+ t.assert.strictEqual(request instanceof fastify[kRequest].parent, true)
48
48
  reply
49
49
  .code(err.statusCode)
50
50
  .type('application/json; charset=utf-8')
@@ -65,18 +65,19 @@ test('default 400 on request error with custom error handler', t => {
65
65
  text: '12345678901234567890123456789012345678901234567890'
66
66
  }
67
67
  }, (err, res) => {
68
- t.error(err)
69
- t.equal(res.statusCode, 400)
70
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
71
- t.same(JSON.parse(res.payload), {
68
+ t.assert.ifError(err)
69
+ t.assert.strictEqual(res.statusCode, 400)
70
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
71
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
72
72
  error: 'Bad Request',
73
73
  message: 'Simulated',
74
74
  statusCode: 400
75
75
  })
76
+ done()
76
77
  })
77
78
  })
78
79
 
79
- test('default clientError handler ignores ECONNRESET', t => {
80
+ test('default clientError handler ignores ECONNRESET', (t, done) => {
80
81
  t.plan(3)
81
82
 
82
83
  let logs = ''
@@ -107,8 +108,8 @@ test('default clientError handler ignores ECONNRESET', t => {
107
108
  })
108
109
 
109
110
  fastify.listen({ port: 0 }, function (err) {
110
- t.error(err)
111
- t.teardown(() => { fastify.close() })
111
+ t.assert.ifError(err)
112
+ t.after(() => fastify.close())
112
113
 
113
114
  const client = connect(fastify.server.address().port)
114
115
 
@@ -117,8 +118,9 @@ test('default clientError handler ignores ECONNRESET', t => {
117
118
  })
118
119
 
119
120
  client.on('end', () => {
120
- t.match(response, /^HTTP\/1.1 200 OK/)
121
- t.notMatch(logs, /ECONNRESET/)
121
+ t.assert.match(response, /^HTTP\/1.1 200 OK/)
122
+ t.assert.notEqual(logs, /ECONNRESET/)
123
+ done()
122
124
  })
123
125
 
124
126
  client.resume()
@@ -138,15 +140,15 @@ test('default clientError handler ignores sockets in destroyed state', t => {
138
140
  })
139
141
  fastify.server.on('clientError', () => {
140
142
  // this handler is called after default handler, so we can make sure end was not called
141
- t.pass()
143
+ t.assert.ok('end should not be called')
142
144
  })
143
145
  fastify.server.emit('clientError', new Error(), {
144
146
  destroyed: true,
145
147
  end () {
146
- t.fail('end should not be called')
148
+ t.assert.fail('end should not be called')
147
149
  },
148
150
  destroy () {
149
- t.fail('destroy should not be called')
151
+ t.assert.fail('destroy should not be called')
150
152
  }
151
153
  })
152
154
  })
@@ -164,13 +166,13 @@ test('default clientError handler destroys sockets in writable state', t => {
164
166
  writable: true,
165
167
  encrypted: true,
166
168
  end () {
167
- t.fail('end should not be called')
169
+ t.assert.fail('end should not be called')
168
170
  },
169
171
  destroy () {
170
- t.pass('destroy should be called')
172
+ t.assert.ok('destroy should be called')
171
173
  },
172
174
  write (response) {
173
- t.match(response, /^HTTP\/1.1 400 Bad Request/)
175
+ t.assert.match(response, /^HTTP\/1.1 400 Bad Request/)
174
176
  }
175
177
  })
176
178
  })
@@ -187,24 +189,24 @@ test('default clientError handler destroys http sockets in non-writable state',
187
189
  destroyed: false,
188
190
  writable: false,
189
191
  end () {
190
- t.fail('end should not be called')
192
+ t.assert.fail('end should not be called')
191
193
  },
192
194
  destroy () {
193
- t.pass('destroy should be called')
195
+ t.assert.ok('destroy should be called')
194
196
  },
195
197
  write (response) {
196
- t.fail('write should not be called')
198
+ t.assert.fail('write should not be called')
197
199
  }
198
200
  })
199
201
  })
200
202
 
201
- test('error handler binding', t => {
203
+ test('error handler binding', (t, done) => {
202
204
  t.plan(5)
203
205
 
204
206
  const fastify = Fastify()
205
207
 
206
208
  fastify.setErrorHandler(function (err, request, reply) {
207
- t.equal(this, fastify)
209
+ t.assert.strictEqual(this, fastify)
208
210
  reply
209
211
  .code(err.statusCode)
210
212
  .type('application/json; charset=utf-8')
@@ -225,30 +227,31 @@ test('error handler binding', t => {
225
227
  text: '12345678901234567890123456789012345678901234567890'
226
228
  }
227
229
  }, (err, res) => {
228
- t.error(err)
229
- t.equal(res.statusCode, 400)
230
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
231
- t.same(JSON.parse(res.payload), {
230
+ t.assert.ifError(err)
231
+ t.assert.strictEqual(res.statusCode, 400)
232
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
233
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
232
234
  error: 'Bad Request',
233
235
  message: 'Simulated',
234
236
  statusCode: 400
235
237
  })
238
+ done()
236
239
  })
237
240
  })
238
241
 
239
- test('encapsulated error handler binding', t => {
242
+ test('encapsulated error handler binding', (t, done) => {
240
243
  t.plan(7)
241
244
 
242
245
  const fastify = Fastify()
243
246
 
244
247
  fastify.register(function (app, opts, done) {
245
248
  app.decorate('hello', 'world')
246
- t.equal(app.hello, 'world')
249
+ t.assert.strictEqual(app.hello, 'world')
247
250
  app.post('/', function (req, reply) {
248
251
  reply.send({ hello: 'world' })
249
252
  })
250
253
  app.setErrorHandler(function (err, request, reply) {
251
- t.equal(this.hello, 'world')
254
+ t.assert.strictEqual(this.hello, 'world')
252
255
  reply
253
256
  .code(err.statusCode)
254
257
  .type('application/json; charset=utf-8')
@@ -267,19 +270,20 @@ test('encapsulated error handler binding', t => {
267
270
  text: '12345678901234567890123456789012345678901234567890'
268
271
  }
269
272
  }, (err, res) => {
270
- t.error(err)
271
- t.equal(res.statusCode, 400)
272
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
273
- t.same(res.json(), {
273
+ t.assert.ifError(err)
274
+ t.assert.strictEqual(res.statusCode, 400)
275
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
276
+ t.assert.deepStrictEqual(res.json(), {
274
277
  error: 'Bad Request',
275
278
  message: 'Simulated',
276
279
  statusCode: 400
277
280
  })
278
- t.equal(fastify.hello, undefined)
281
+ t.assert.strictEqual(fastify.hello, undefined)
282
+ done()
279
283
  })
280
284
  })
281
285
 
282
- test('default clientError replies with bad request on reused keep-alive connection', t => {
286
+ test('default clientError replies with bad request on reused keep-alive connection', (t, done) => {
283
287
  t.plan(2)
284
288
 
285
289
  let response = ''
@@ -294,7 +298,7 @@ test('default clientError replies with bad request on reused keep-alive connecti
294
298
  })
295
299
 
296
300
  fastify.listen({ port: 0 }, function (err) {
297
- t.error(err)
301
+ t.assert.ifError(err)
298
302
  fastify.server.unref()
299
303
 
300
304
  const client = connect(fastify.server.address().port)
@@ -304,7 +308,8 @@ test('default clientError replies with bad request on reused keep-alive connecti
304
308
  })
305
309
 
306
310
  client.on('end', () => {
307
- t.match(response, /^HTTP\/1.1 200 OK.*HTTP\/1.1 400 Bad Request/s)
311
+ t.assert.match(response, /^HTTP\/1.1 200 OK.*HTTP\/1.1 400 Bad Request/s)
312
+ done()
308
313
  })
309
314
 
310
315
  client.resume()
@@ -318,21 +323,21 @@ test('default clientError replies with bad request on reused keep-alive connecti
318
323
  })
319
324
  })
320
325
 
321
- test('request.routeOptions should be immutable', t => {
326
+ test('request.routeOptions should be immutable', (t, done) => {
322
327
  t.plan(14)
323
328
  const fastify = Fastify()
324
329
  const handler = function (req, res) {
325
- t.equal('POST', req.routeOptions.method)
326
- t.equal('/', req.routeOptions.url)
327
- t.throws(() => { req.routeOptions = null }, new TypeError('Cannot set property routeOptions of #<Request> which has only a getter'))
328
- t.throws(() => { req.routeOptions.method = 'INVALID' }, new TypeError('Cannot assign to read only property \'method\' of object \'#<Object>\''))
329
- t.throws(() => { req.routeOptions.url = '//' }, new TypeError('Cannot assign to read only property \'url\' of object \'#<Object>\''))
330
- t.throws(() => { req.routeOptions.bodyLimit = 0xDEADBEEF }, new TypeError('Cannot assign to read only property \'bodyLimit\' of object \'#<Object>\''))
331
- t.throws(() => { req.routeOptions.attachValidation = true }, new TypeError('Cannot assign to read only property \'attachValidation\' of object \'#<Object>\''))
332
- t.throws(() => { req.routeOptions.logLevel = 'invalid' }, new TypeError('Cannot assign to read only property \'logLevel\' of object \'#<Object>\''))
333
- t.throws(() => { req.routeOptions.version = '95.0.1' }, new TypeError('Cannot assign to read only property \'version\' of object \'#<Object>\''))
334
- t.throws(() => { req.routeOptions.prefixTrailingSlash = true }, new TypeError('Cannot assign to read only property \'prefixTrailingSlash\' of object \'#<Object>\''))
335
- t.throws(() => { req.routeOptions.newAttribute = {} }, new TypeError('Cannot add property newAttribute, object is not extensible'))
330
+ t.assert.strictEqual('POST', req.routeOptions.method)
331
+ t.assert.strictEqual('/', req.routeOptions.url)
332
+ t.assert.throws(() => { req.routeOptions = null }, new TypeError('Cannot set property routeOptions of #<Request> which has only a getter'))
333
+ t.assert.throws(() => { req.routeOptions.method = 'INVALID' }, new TypeError('Cannot assign to read only property \'method\' of object \'#<Object>\''))
334
+ t.assert.throws(() => { req.routeOptions.url = '//' }, new TypeError('Cannot assign to read only property \'url\' of object \'#<Object>\''))
335
+ t.assert.throws(() => { req.routeOptions.bodyLimit = 0xDEADBEEF }, new TypeError('Cannot assign to read only property \'bodyLimit\' of object \'#<Object>\''))
336
+ t.assert.throws(() => { req.routeOptions.attachValidation = true }, new TypeError('Cannot assign to read only property \'attachValidation\' of object \'#<Object>\''))
337
+ t.assert.throws(() => { req.routeOptions.logLevel = 'invalid' }, new TypeError('Cannot assign to read only property \'logLevel\' of object \'#<Object>\''))
338
+ t.assert.throws(() => { req.routeOptions.version = '95.0.1' }, new TypeError('Cannot assign to read only property \'version\' of object \'#<Object>\''))
339
+ t.assert.throws(() => { req.routeOptions.prefixTrailingSlash = true }, new TypeError('Cannot assign to read only property \'prefixTrailingSlash\' of object \'#<Object>\''))
340
+ t.assert.throws(() => { req.routeOptions.newAttribute = {} }, new TypeError('Cannot add property newAttribute, object is not extensible'))
336
341
 
337
342
  for (const key of Object.keys(req.routeOptions)) {
338
343
  if (typeof req.routeOptions[key] === 'object' && req.routeOptions[key] !== null) {
@@ -347,8 +352,8 @@ test('request.routeOptions should be immutable', t => {
347
352
  handler
348
353
  })
349
354
  fastify.listen({ port: 0 }, function (err) {
350
- t.error(err)
351
- t.teardown(() => { fastify.close() })
355
+ t.assert.ifError(err)
356
+ t.after(() => fastify.close())
352
357
 
353
358
  sget({
354
359
  method: 'POST',
@@ -357,17 +362,18 @@ test('request.routeOptions should be immutable', t => {
357
362
  body: [],
358
363
  json: true
359
364
  }, (err, response, body) => {
360
- t.error(err)
361
- t.equal(response.statusCode, 200)
365
+ t.assert.ifError(err)
366
+ t.assert.strictEqual(response.statusCode, 200)
367
+ done()
362
368
  })
363
369
  })
364
370
  })
365
371
 
366
- test('request.routeOptions.method is an uppercase string /1', t => {
372
+ test('request.routeOptions.method is an uppercase string /1', (t, done) => {
367
373
  t.plan(4)
368
374
  const fastify = Fastify()
369
375
  const handler = function (req, res) {
370
- t.equal('POST', req.routeOptions.method)
376
+ t.assert.strictEqual('POST', req.routeOptions.method)
371
377
  res.send({})
372
378
  }
373
379
 
@@ -376,8 +382,8 @@ test('request.routeOptions.method is an uppercase string /1', t => {
376
382
  handler
377
383
  })
378
384
  fastify.listen({ port: 0 }, function (err) {
379
- t.error(err)
380
- t.teardown(() => { fastify.close() })
385
+ t.assert.ifError(err)
386
+ t.after(() => fastify.close())
381
387
 
382
388
  sget({
383
389
  method: 'POST',
@@ -386,17 +392,18 @@ test('request.routeOptions.method is an uppercase string /1', t => {
386
392
  body: [],
387
393
  json: true
388
394
  }, (err, response, body) => {
389
- t.error(err)
390
- t.equal(response.statusCode, 200)
395
+ t.assert.ifError(err)
396
+ t.assert.strictEqual(response.statusCode, 200)
397
+ done()
391
398
  })
392
399
  })
393
400
  })
394
401
 
395
- test('request.routeOptions.method is an uppercase string /2', t => {
402
+ test('request.routeOptions.method is an uppercase string /2', (t, done) => {
396
403
  t.plan(4)
397
404
  const fastify = Fastify()
398
405
  const handler = function (req, res) {
399
- t.equal('POST', req.routeOptions.method)
406
+ t.assert.strictEqual('POST', req.routeOptions.method)
400
407
  res.send({})
401
408
  }
402
409
 
@@ -407,8 +414,8 @@ test('request.routeOptions.method is an uppercase string /2', t => {
407
414
  handler
408
415
  })
409
416
  fastify.listen({ port: 0 }, function (err) {
410
- t.error(err)
411
- t.teardown(() => { fastify.close() })
417
+ t.assert.ifError(err)
418
+ t.after(() => fastify.close())
412
419
 
413
420
  sget({
414
421
  method: 'POST',
@@ -417,17 +424,18 @@ test('request.routeOptions.method is an uppercase string /2', t => {
417
424
  body: [],
418
425
  json: true
419
426
  }, (err, response, body) => {
420
- t.error(err)
421
- t.equal(response.statusCode, 200)
427
+ t.assert.ifError(err)
428
+ t.assert.strictEqual(response.statusCode, 200)
429
+ done()
422
430
  })
423
431
  })
424
432
  })
425
433
 
426
- test('request.routeOptions.method is an uppercase string /3', t => {
434
+ test('request.routeOptions.method is an uppercase string /3', (t, done) => {
427
435
  t.plan(4)
428
436
  const fastify = Fastify()
429
437
  const handler = function (req, res) {
430
- t.equal('POST', req.routeOptions.method)
438
+ t.assert.strictEqual('POST', req.routeOptions.method)
431
439
  res.send({})
432
440
  }
433
441
 
@@ -438,8 +446,8 @@ test('request.routeOptions.method is an uppercase string /3', t => {
438
446
  handler
439
447
  })
440
448
  fastify.listen({ port: 0 }, function (err) {
441
- t.error(err)
442
- t.teardown(() => { fastify.close() })
449
+ t.assert.ifError(err)
450
+ t.after(() => fastify.close())
443
451
 
444
452
  sget({
445
453
  method: 'POST',
@@ -448,17 +456,18 @@ test('request.routeOptions.method is an uppercase string /3', t => {
448
456
  body: [],
449
457
  json: true
450
458
  }, (err, response, body) => {
451
- t.error(err)
452
- t.equal(response.statusCode, 200)
459
+ t.assert.ifError(err)
460
+ t.assert.strictEqual(response.statusCode, 200)
461
+ done()
453
462
  })
454
463
  })
455
464
  })
456
465
 
457
- test('request.routeOptions.method is an array with uppercase string', t => {
466
+ test('request.routeOptions.method is an array with uppercase string', (t, done) => {
458
467
  t.plan(4)
459
468
  const fastify = Fastify()
460
469
  const handler = function (req, res) {
461
- t.strictSame(['POST'], req.routeOptions.method)
470
+ t.assert.deepStrictEqual(['POST'], req.routeOptions.method)
462
471
  res.send({})
463
472
  }
464
473
 
@@ -469,8 +478,8 @@ test('request.routeOptions.method is an array with uppercase string', t => {
469
478
  handler
470
479
  })
471
480
  fastify.listen({ port: 0 }, function (err) {
472
- t.error(err)
473
- t.teardown(() => { fastify.close() })
481
+ t.assert.ifError(err)
482
+ t.after(() => fastify.close())
474
483
 
475
484
  sget({
476
485
  method: 'POST',
@@ -479,13 +488,14 @@ test('request.routeOptions.method is an array with uppercase string', t => {
479
488
  body: [],
480
489
  json: true
481
490
  }, (err, response, body) => {
482
- t.error(err)
483
- t.equal(response.statusCode, 200)
491
+ t.assert.ifError(err)
492
+ t.assert.strictEqual(response.statusCode, 200)
493
+ done()
484
494
  })
485
495
  })
486
496
  })
487
497
 
488
- test('test request.routeOptions.version', t => {
498
+ test('test request.routeOptions.version', (t, done) => {
489
499
  t.plan(7)
490
500
  const fastify = Fastify()
491
501
 
@@ -494,7 +504,7 @@ test('test request.routeOptions.version', t => {
494
504
  url: '/version',
495
505
  constraints: { version: '1.2.0' },
496
506
  handler: function (request, reply) {
497
- t.equal('1.2.0', request.routeOptions.version)
507
+ t.assert.strictEqual('1.2.0', request.routeOptions.version)
498
508
  reply.send({})
499
509
  }
500
510
  })
@@ -503,13 +513,21 @@ test('test request.routeOptions.version', t => {
503
513
  method: 'POST',
504
514
  url: '/version-undefined',
505
515
  handler: function (request, reply) {
506
- t.equal(undefined, request.routeOptions.version)
516
+ t.assert.strictEqual(undefined, request.routeOptions.version)
507
517
  reply.send({})
508
518
  }
509
519
  })
510
520
  fastify.listen({ port: 0 }, function (err) {
511
- t.error(err)
512
- t.teardown(() => { fastify.close() })
521
+ t.assert.ifError(err)
522
+ t.after(() => fastify.close())
523
+
524
+ let pending = 2
525
+
526
+ function completed () {
527
+ if (--pending === 0) {
528
+ done()
529
+ }
530
+ }
513
531
 
514
532
  sget({
515
533
  method: 'POST',
@@ -518,8 +536,9 @@ test('test request.routeOptions.version', t => {
518
536
  body: [],
519
537
  json: true
520
538
  }, (err, response, body) => {
521
- t.error(err)
522
- t.equal(response.statusCode, 200)
539
+ t.assert.ifError(err)
540
+ t.assert.strictEqual(response.statusCode, 200)
541
+ completed()
523
542
  })
524
543
 
525
544
  sget({
@@ -529,8 +548,9 @@ test('test request.routeOptions.version', t => {
529
548
  body: [],
530
549
  json: true
531
550
  }, (err, response, body) => {
532
- t.error(err)
533
- t.equal(response.statusCode, 200)
551
+ t.assert.ifError(err)
552
+ t.assert.strictEqual(response.statusCode, 200)
553
+ completed()
534
554
  })
535
555
  })
536
556
  })
@@ -1,62 +1,66 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { describe, test } = require('node:test')
5
4
  const sget = require('simple-get').concat
6
- const Fastify = require('../fastify')
5
+ const Fastify = require('..')
7
6
 
8
- test('route-shorthand', t => {
7
+ describe('route-shorthand', () => {
9
8
  const methodsReader = new Fastify()
10
9
  const supportedMethods = methodsReader.supportedMethods
11
10
 
12
- t.plan(supportedMethods.length + 1)
13
- const test = t.test
14
-
15
11
  for (const method of supportedMethods) {
16
- test(`route-shorthand - ${method.toLowerCase()}`, t => {
17
- t.plan(3)
12
+ test(`route-shorthand - ${method.toLowerCase()}`, async (t) => {
13
+ t.plan(2)
18
14
  const fastify = new Fastify()
19
- fastify[method.toLowerCase()]('/', function (req, reply) {
20
- t.equal(req.method, method)
15
+ fastify[method.toLowerCase()]('/', (req, reply) => {
16
+ t.assert.strictEqual(req.method, method)
21
17
  reply.send()
22
18
  })
23
- fastify.listen({ port: 0 }, function (err) {
24
- if (err) t.error(err)
25
- t.teardown(() => { fastify.close() })
19
+ await fastify.listen({ port: 0 })
20
+ t.after(() => fastify.close())
21
+
22
+ await new Promise((resolve, reject) => {
26
23
  sget({
27
24
  method,
28
- url: 'http://localhost:' + fastify.server.address().port
25
+ url: `http://localhost:${fastify.server.address().port}`
29
26
  }, (err, response, body) => {
30
- t.error(err)
31
- t.equal(response.statusCode, 200)
27
+ if (err) {
28
+ t.assert.ifError(err)
29
+ return reject(err)
30
+ }
31
+ t.assert.strictEqual(response.statusCode, 200)
32
+ resolve()
32
33
  })
33
34
  })
34
35
  })
35
36
  }
36
37
 
37
- test('route-shorthand - all', t => {
38
- t.plan(3 * supportedMethods.length)
38
+ test('route-shorthand - all', async (t) => {
39
+ t.plan(2 * supportedMethods.length)
39
40
  const fastify = new Fastify()
40
41
  let currentMethod = ''
41
42
  fastify.all('/', function (req, reply) {
42
- t.equal(req.method, currentMethod)
43
+ t.assert.strictEqual(req.method, currentMethod)
43
44
  reply.send()
44
45
  })
45
- fastify.listen({ port: 0 }, async function (err) {
46
- if (err) t.error(err)
47
- t.teardown(() => { fastify.close() })
48
- for (const method of supportedMethods) {
49
- currentMethod = method
50
- await new Promise(resolve => sget({
46
+ await fastify.listen({ port: 0 })
47
+ t.after(() => fastify.close())
48
+
49
+ for (const method of supportedMethods) {
50
+ currentMethod = method
51
+ await new Promise((resolve, reject) => {
52
+ sget({
51
53
  method,
52
- url: 'http://localhost:' + fastify.server.address().port
54
+ url: `http://localhost:${fastify.server.address().port}`
53
55
  }, (err, response, body) => {
54
- t.error(err)
55
- t.equal(response.statusCode, 200)
56
+ if (err) {
57
+ t.assert.ifError(err)
58
+ return reject(err)
59
+ }
60
+ t.assert.strictEqual(response.statusCode, 200)
56
61
  resolve()
57
62
  })
58
- )
59
- }
60
- })
63
+ })
64
+ }
61
65
  })
62
66
  })