fastify 5.3.2 → 5.4.0

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 (103) hide show
  1. package/README.md +2 -0
  2. package/build/build-validation.js +2 -1
  3. package/docs/Guides/Delay-Accepting-Requests.md +3 -3
  4. package/docs/Guides/Ecosystem.md +16 -7
  5. package/docs/Guides/Serverless.md +28 -69
  6. package/docs/Reference/ContentTypeParser.md +1 -1
  7. package/docs/Reference/Errors.md +2 -4
  8. package/docs/Reference/Hooks.md +14 -14
  9. package/docs/Reference/Logging.md +3 -3
  10. package/docs/Reference/Middleware.md +1 -1
  11. package/docs/Reference/Reply.md +8 -8
  12. package/docs/Reference/Request.md +1 -1
  13. package/docs/Reference/Routes.md +3 -3
  14. package/docs/Reference/Server.md +40 -10
  15. package/docs/Reference/Validation-and-Serialization.md +1 -1
  16. package/eslint.config.js +17 -9
  17. package/fastify.d.ts +2 -1
  18. package/fastify.js +20 -4
  19. package/lib/configValidator.js +1 -1
  20. package/lib/decorate.js +2 -2
  21. package/lib/errors.js +6 -8
  22. package/lib/logger-factory.js +1 -1
  23. package/lib/logger-pino.js +2 -2
  24. package/lib/pluginOverride.js +3 -1
  25. package/lib/reply.js +9 -13
  26. package/lib/request.js +4 -11
  27. package/lib/server.js +30 -51
  28. package/lib/symbols.js +1 -0
  29. package/lib/warnings.js +8 -0
  30. package/package.json +11 -7
  31. package/test/404s.test.js +226 -325
  32. package/test/allow-unsafe-regex.test.js +19 -48
  33. package/test/als.test.js +28 -40
  34. package/test/async-await.test.js +11 -2
  35. package/test/body-limit.test.js +41 -65
  36. package/test/build-certificate.js +1 -1
  37. package/test/close-pipelining.test.js +5 -4
  38. package/test/custom-parser-async.test.js +17 -22
  39. package/test/decorator-namespace.test._js_ +3 -4
  40. package/test/decorator.test.js +422 -341
  41. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  42. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  43. package/test/helper.js +108 -70
  44. package/test/hooks-async.test.js +248 -218
  45. package/test/hooks.on-listen.test.js +255 -239
  46. package/test/hooks.on-ready.test.js +110 -92
  47. package/test/hooks.test.js +910 -769
  48. package/test/http-methods/lock.test.js +31 -31
  49. package/test/http-methods/mkcol.test.js +5 -9
  50. package/test/http-methods/proppatch.test.js +23 -29
  51. package/test/http-methods/report.test.js +44 -69
  52. package/test/http-methods/search.test.js +67 -82
  53. package/test/http2/closing.test.js +38 -20
  54. package/test/http2/secure-with-fallback.test.js +28 -27
  55. package/test/https/https.test.js +56 -53
  56. package/test/inject.test.js +114 -97
  57. package/test/input-validation.js +63 -53
  58. package/test/internals/errors.test.js +0 -10
  59. package/test/internals/handle-request.test.js +49 -66
  60. package/test/internals/hooks.test.js +17 -0
  61. package/test/issue-4959.test.js +14 -5
  62. package/test/listen.4.test.js +31 -43
  63. package/test/logger/response.test.js +19 -20
  64. package/test/nullable-validation.test.js +33 -46
  65. package/test/options.error-handler.test.js +1 -1
  66. package/test/options.test.js +1 -1
  67. package/test/output-validation.test.js +49 -72
  68. package/test/patch.error-handler.test.js +1 -1
  69. package/test/patch.test.js +1 -1
  70. package/test/plugin.1.test.js +71 -60
  71. package/test/plugin.2.test.js +104 -86
  72. package/test/plugin.3.test.js +56 -35
  73. package/test/plugin.4.test.js +124 -119
  74. package/test/promises.test.js +36 -30
  75. package/test/proto-poisoning.test.js +78 -97
  76. package/test/put.error-handler.test.js +1 -1
  77. package/test/put.test.js +1 -1
  78. package/test/reply-error.test.js +169 -148
  79. package/test/reply-trailers.test.js +119 -108
  80. package/test/request-error.test.js +0 -46
  81. package/test/route-hooks.test.js +112 -92
  82. package/test/route-prefix.test.js +194 -133
  83. package/test/schema-feature.test.js +309 -238
  84. package/test/schema-serialization.test.js +177 -154
  85. package/test/schema-special-usage.test.js +165 -132
  86. package/test/schema-validation.test.js +278 -199
  87. package/test/set-error-handler.test.js +58 -1
  88. package/test/skip-reply-send.test.js +64 -69
  89. package/test/stream.1.test.js +30 -27
  90. package/test/stream.2.test.js +20 -10
  91. package/test/stream.3.test.js +37 -31
  92. package/test/trust-proxy.test.js +32 -58
  93. package/test/types/errors.test-d.ts +0 -1
  94. package/test/types/fastify.test-d.ts +3 -0
  95. package/test/types/plugin.test-d.ts +1 -1
  96. package/test/types/register.test-d.ts +1 -1
  97. package/test/types/request.test-d.ts +1 -0
  98. package/test/url-rewriting.test.js +45 -62
  99. package/test/use-semicolon-delimiter.test.js +1 -1
  100. package/types/errors.d.ts +0 -1
  101. package/types/request.d.ts +1 -0
  102. package/.taprc +0 -7
  103. package/test/http2/missing-http2-module.test.js +0 -17
package/test/404s.test.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const { test } = require('node:test')
4
4
  const fp = require('fastify-plugin')
5
- const sget = require('simple-get').concat
6
5
  const errors = require('http-errors')
7
6
  const split = require('split2')
8
7
  const Fastify = require('..')
@@ -21,50 +20,36 @@ test('default 404', async t => {
21
20
 
22
21
  await fastify.listen({ port: 0 })
23
22
 
24
- await t.test('unsupported method', (t, done) => {
23
+ await t.test('unsupported method', async (t) => {
25
24
  t.plan(3)
26
- sget({
27
- method: 'PUT',
28
- url: getServerUrl(fastify),
29
- body: {},
30
- json: true
31
- }, (err, response, body) => {
32
- t.assert.ifError(err)
33
- t.assert.strictEqual(response.statusCode, 404)
34
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
35
- done()
25
+ const result = await fetch(getServerUrl(fastify), {
26
+ method: 'PUT'
36
27
  })
28
+
29
+ t.assert.ok(!result.ok)
30
+ t.assert.strictEqual(result.status, 404)
31
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
37
32
  })
38
33
 
39
34
  // Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
40
- await t.test('framework-unsupported method', (t, done) => {
35
+ await t.test('framework-unsupported method', async (t) => {
41
36
  t.plan(3)
42
- sget({
43
- method: 'PROPFIND',
44
- url: getServerUrl(fastify),
45
- body: {},
46
- json: true
47
- }, (err, response, body) => {
48
- t.assert.ifError(err)
49
- t.assert.strictEqual(response.statusCode, 404)
50
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
51
- done()
37
+ const result = await fetch(getServerUrl(fastify), {
38
+ method: 'PROPFIND'
52
39
  })
40
+
41
+ t.assert.ok(!result.ok)
42
+ t.assert.strictEqual(result.status, 404)
43
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
53
44
  })
54
45
 
55
- await t.test('unsupported route', (t, done) => {
46
+ await t.test('unsupported route', async (t) => {
56
47
  t.plan(3)
57
- sget({
58
- method: 'GET',
59
- url: getServerUrl(fastify) + '/notSupported',
60
- body: {},
61
- json: true
62
- }, (err, response, body) => {
63
- t.assert.ifError(err)
64
- t.assert.strictEqual(response.statusCode, 404)
65
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
66
- done()
67
- })
48
+ const response = await fetch(getServerUrl(fastify) + '/notSupported')
49
+
50
+ t.assert.ok(!response.ok)
51
+ t.assert.strictEqual(response.status, 404)
52
+ t.assert.strictEqual(response.headers.get('content-type'), 'application/json; charset=utf-8')
68
53
  })
69
54
 
70
55
  await t.test('using post method and multipart/formdata', async t => {
@@ -109,81 +94,65 @@ test('customized 404', async t => {
109
94
 
110
95
  await fastify.listen({ port: 0 })
111
96
 
112
- await t.test('unsupported method', (t, done) => {
97
+ await t.test('unsupported method', async (t) => {
113
98
  t.plan(3)
114
- sget({
99
+ const response = await fetch(getServerUrl(fastify), {
115
100
  method: 'PUT',
116
- url: getServerUrl(fastify),
117
101
  body: JSON.stringify({ hello: 'world' }),
118
102
  headers: { 'Content-Type': 'application/json' }
119
- }, (err, response, body) => {
120
- t.assert.ifError(err)
121
- t.assert.strictEqual(response.statusCode, 404)
122
- t.assert.strictEqual(body.toString(), 'this was not found')
123
- done()
124
103
  })
104
+
105
+ t.assert.ok(!response.ok)
106
+ t.assert.strictEqual(response.status, 404)
107
+ t.assert.strictEqual(await response.text(), 'this was not found')
125
108
  })
126
109
 
127
- await t.test('framework-unsupported method', (t, done) => {
110
+ await t.test('framework-unsupported method', async (t) => {
128
111
  t.plan(3)
129
- sget({
112
+ const response = await fetch(getServerUrl(fastify), {
130
113
  method: 'PROPFIND',
131
- url: getServerUrl(fastify),
132
114
  body: JSON.stringify({ hello: 'world' }),
133
115
  headers: { 'Content-Type': 'application/json' }
134
- }, (err, response, body) => {
135
- t.assert.ifError(err)
136
- t.assert.strictEqual(response.statusCode, 404)
137
- t.assert.strictEqual(body.toString(), 'this was not found')
138
- done()
139
116
  })
117
+
118
+ t.assert.ok(!response.ok)
119
+ t.assert.strictEqual(response.status, 404)
120
+ t.assert.strictEqual(await response.text(), 'this was not found')
140
121
  })
141
122
 
142
- await t.test('unsupported route', (t, done) => {
123
+ await t.test('unsupported route', async (t) => {
143
124
  t.plan(3)
144
- sget({
145
- method: 'GET',
146
- url: getServerUrl(fastify) + '/notSupported'
147
- }, (err, response, body) => {
148
- t.assert.ifError(err)
149
- t.assert.strictEqual(response.statusCode, 404)
150
- t.assert.strictEqual(body.toString(), 'this was not found')
151
- done()
152
- })
125
+ const response = await fetch(getServerUrl(fastify) + '/notSupported')
126
+
127
+ t.assert.ok(!response.ok)
128
+ t.assert.strictEqual(response.status, 404)
129
+ t.assert.strictEqual(await response.text(), 'this was not found')
153
130
  })
154
131
 
155
- await t.test('with error object', (t, done) => {
132
+ await t.test('with error object', async (t) => {
156
133
  t.plan(3)
157
- sget({
158
- method: 'GET',
159
- url: getServerUrl(fastify) + '/with-error'
160
- }, (err, response, body) => {
161
- t.assert.ifError(err)
162
- t.assert.strictEqual(response.statusCode, 404)
163
- t.assert.deepStrictEqual(JSON.parse(body), {
164
- error: 'Not Found',
165
- message: 'Not Found',
166
- statusCode: 404
167
- })
168
- done()
134
+ const response = await fetch(getServerUrl(fastify) + '/with-error')
135
+
136
+ t.assert.ok(!response.ok)
137
+ t.assert.strictEqual(response.status, 404)
138
+ t.assert.deepStrictEqual(await response.json(), {
139
+ error: 'Not Found',
140
+ message: 'Not Found',
141
+ statusCode: 404
169
142
  })
170
143
  })
171
144
 
172
- await t.test('error object with headers property', (t, done) => {
145
+ await t.test('error object with headers property', async (t) => {
173
146
  t.plan(4)
174
- sget({
175
- method: 'GET',
176
- url: getServerUrl(fastify) + '/with-error-custom-header'
177
- }, (err, response, body) => {
178
- t.assert.ifError(err)
179
- t.assert.strictEqual(response.statusCode, 404)
180
- t.assert.strictEqual(response.headers['x-foo'], 'bar')
181
- t.assert.deepStrictEqual(JSON.parse(body), {
182
- error: 'Not Found',
183
- message: 'Not Found',
184
- statusCode: 404
185
- })
186
- done()
147
+ const response = await fetch(getServerUrl(fastify) + '/with-error-custom-header')
148
+
149
+ t.assert.ok(!response.ok)
150
+ t.assert.strictEqual(response.status, 404)
151
+ t.assert.strictEqual(response.headers.get('x-foo'), 'bar')
152
+ t.assert.deepStrictEqual(await response.json(), {
153
+ error: 'Not Found',
154
+ message: 'Not Found',
155
+ statusCode: 404
187
156
  })
188
157
  })
189
158
  })
@@ -201,18 +170,14 @@ test('custom header in notFound handler', async t => {
201
170
 
202
171
  await fastify.listen({ port: 0 })
203
172
 
204
- await t.test('not found with custom header', (t, done) => {
173
+ await t.test('not found with custom header', async (t) => {
205
174
  t.plan(4)
206
- sget({
207
- method: 'GET',
208
- url: getServerUrl(fastify) + '/notSupported'
209
- }, (err, response, body) => {
210
- t.assert.ifError(err)
211
- t.assert.strictEqual(response.statusCode, 404)
212
- t.assert.strictEqual(response.headers['x-foo'], 'bar')
213
- t.assert.strictEqual(body.toString(), 'this was not found')
214
- done()
215
- })
175
+ const response = await fetch(getServerUrl(fastify) + '/notSupported')
176
+
177
+ t.assert.ok(!response.ok)
178
+ t.assert.strictEqual(response.status, 404)
179
+ t.assert.strictEqual(response.headers.get('x-foo'), 'bar')
180
+ t.assert.strictEqual(await response.text(), 'this was not found')
216
181
  })
217
182
  })
218
183
 
@@ -390,176 +355,144 @@ test('encapsulated 404', async t => {
390
355
 
391
356
  await fastify.listen({ port: 0 })
392
357
 
393
- await t.test('root unsupported method', (t, done) => {
358
+ await t.test('root unsupported method', async (t) => {
394
359
  t.plan(3)
395
- sget({
360
+ const response = await fetch(getServerUrl(fastify), {
396
361
  method: 'PUT',
397
- url: getServerUrl(fastify),
398
362
  body: JSON.stringify({ hello: 'world' }),
399
363
  headers: { 'Content-Type': 'application/json' }
400
- }, (err, response, body) => {
401
- t.assert.ifError(err)
402
- t.assert.strictEqual(response.statusCode, 404)
403
- t.assert.strictEqual(body.toString(), 'this was not found')
404
- done()
405
364
  })
365
+
366
+ t.assert.ok(!response.ok)
367
+ t.assert.strictEqual(response.status, 404)
368
+ t.assert.strictEqual(await response.text(), 'this was not found')
406
369
  })
407
370
 
408
- await t.test('root framework-unsupported method', (t, done) => {
371
+ await t.test('root framework-unsupported method', async (t) => {
409
372
  t.plan(3)
410
- sget({
373
+ const response = await fetch(getServerUrl(fastify), {
411
374
  method: 'PROPFIND',
412
- url: getServerUrl(fastify),
413
375
  body: JSON.stringify({ hello: 'world' }),
414
376
  headers: { 'Content-Type': 'application/json' }
415
- }, (err, response, body) => {
416
- t.assert.ifError(err)
417
- t.assert.strictEqual(response.statusCode, 404)
418
- t.assert.strictEqual(body.toString(), 'this was not found')
419
- done()
420
377
  })
378
+
379
+ t.assert.ok(!response.ok)
380
+ t.assert.strictEqual(response.status, 404)
381
+ t.assert.strictEqual(await response.text(), 'this was not found')
421
382
  })
422
383
 
423
- await t.test('root unsupported route', (t, done) => {
384
+ await t.test('root unsupported route', async (t) => {
424
385
  t.plan(3)
425
- sget({
426
- method: 'GET',
427
- url: getServerUrl(fastify) + '/notSupported'
428
- }, (err, response, body) => {
429
- t.assert.ifError(err)
430
- t.assert.strictEqual(response.statusCode, 404)
431
- t.assert.strictEqual(body.toString(), 'this was not found')
432
- done()
433
- })
386
+ const response = await fetch(getServerUrl(fastify) + '/notSupported')
387
+
388
+ t.assert.ok(!response.ok)
389
+ t.assert.strictEqual(response.status, 404)
390
+ t.assert.strictEqual(await response.text(), 'this was not found')
434
391
  })
435
392
 
436
- await t.test('unsupported method', (t, done) => {
393
+ await t.test('unsupported method', async (t) => {
437
394
  t.plan(3)
438
- sget({
395
+ const response = await fetch(getServerUrl(fastify) + '/test', {
439
396
  method: 'PUT',
440
- url: getServerUrl(fastify) + '/test',
441
397
  body: JSON.stringify({ hello: 'world' }),
442
398
  headers: { 'Content-Type': 'application/json' }
443
- }, (err, response, body) => {
444
- t.assert.ifError(err)
445
- t.assert.strictEqual(response.statusCode, 404)
446
- t.assert.strictEqual(body.toString(), 'this was not found 2')
447
- done()
448
399
  })
400
+
401
+ t.assert.ok(!response.ok)
402
+ t.assert.strictEqual(response.status, 404)
403
+ t.assert.strictEqual(await response.text(), 'this was not found 2')
449
404
  })
450
405
 
451
- await t.test('framework-unsupported method', (t, done) => {
406
+ await t.test('framework-unsupported method', async (t) => {
452
407
  t.plan(3)
453
- sget({
408
+ const response = await fetch(getServerUrl(fastify) + '/test', {
454
409
  method: 'PROPFIND',
455
- url: getServerUrl(fastify) + '/test',
456
410
  body: JSON.stringify({ hello: 'world' }),
457
411
  headers: { 'Content-Type': 'application/json' }
458
- }, (err, response, body) => {
459
- t.assert.ifError(err)
460
- t.assert.strictEqual(response.statusCode, 404)
461
- t.assert.strictEqual(body.toString(), 'this was not found 2')
462
- done()
463
412
  })
413
+
414
+ t.assert.ok(!response.ok)
415
+ t.assert.strictEqual(response.status, 404)
416
+ t.assert.strictEqual(await response.text(), 'this was not found 2')
464
417
  })
465
418
 
466
- await t.test('unsupported route', (t, done) => {
419
+ await t.test('unsupported route', async (t) => {
467
420
  t.plan(3)
468
- sget({
469
- method: 'GET',
470
- url: getServerUrl(fastify) + '/test/notSupported'
471
- }, (err, response, body) => {
472
- t.assert.ifError(err)
473
- t.assert.strictEqual(response.statusCode, 404)
474
- t.assert.strictEqual(body.toString(), 'this was not found 2')
475
- done()
476
- })
421
+ const response = await fetch(getServerUrl(fastify) + '/test/notSupported')
422
+
423
+ t.assert.ok(!response.ok)
424
+ t.assert.strictEqual(response.status, 404)
425
+ t.assert.strictEqual(await response.text(), 'this was not found 2')
477
426
  })
478
427
 
479
- await t.test('unsupported method 2', (t, done) => {
428
+ await t.test('unsupported method 2', async (t) => {
480
429
  t.plan(3)
481
- sget({
430
+ const response = await fetch(getServerUrl(fastify) + '/test2', {
482
431
  method: 'PUT',
483
- url: getServerUrl(fastify) + '/test2',
484
432
  body: JSON.stringify({ hello: 'world' }),
485
433
  headers: { 'Content-Type': 'application/json' }
486
- }, (err, response, body) => {
487
- t.assert.ifError(err)
488
- t.assert.strictEqual(response.statusCode, 404)
489
- t.assert.strictEqual(body.toString(), 'this was not found 3')
490
- done()
491
434
  })
435
+
436
+ t.assert.ok(!response.ok)
437
+ t.assert.strictEqual(response.status, 404)
438
+ t.assert.strictEqual(await response.text(), 'this was not found 3')
492
439
  })
493
440
 
494
- await t.test('framework-unsupported method 2', (t, done) => {
441
+ await t.test('framework-unsupported method 2', async (t) => {
495
442
  t.plan(3)
496
- sget({
443
+ const response = await fetch(getServerUrl(fastify) + '/test2', {
497
444
  method: 'PROPFIND',
498
- url: getServerUrl(fastify) + '/test2',
499
445
  body: JSON.stringify({ hello: 'world' }),
500
446
  headers: { 'Content-Type': 'application/json' }
501
- }, (err, response, body) => {
502
- t.assert.ifError(err)
503
- t.assert.strictEqual(response.statusCode, 404)
504
- t.assert.strictEqual(body.toString(), 'this was not found 3')
505
- done()
506
447
  })
448
+
449
+ t.assert.ok(!response.ok)
450
+ t.assert.strictEqual(response.status, 404)
451
+ t.assert.strictEqual(await response.text(), 'this was not found 3')
507
452
  })
508
453
 
509
- await t.test('unsupported route 2', (t, done) => {
454
+ await t.test('unsupported route 2', async (t) => {
510
455
  t.plan(3)
511
- sget({
512
- method: 'GET',
513
- url: getServerUrl(fastify) + '/test2/notSupported'
514
- }, (err, response, body) => {
515
- t.assert.ifError(err)
516
- t.assert.strictEqual(response.statusCode, 404)
517
- t.assert.strictEqual(body.toString(), 'this was not found 3')
518
- done()
519
- })
456
+ const response = await fetch(getServerUrl(fastify) + '/test2/notSupported')
457
+
458
+ t.assert.ok(!response.ok)
459
+ t.assert.strictEqual(response.status, 404)
460
+ t.assert.strictEqual(await response.text(), 'this was not found 3')
520
461
  })
521
462
 
522
- await t.test('unsupported method 3', (t, done) => {
463
+ await t.test('unsupported method 3', async (t) => {
523
464
  t.plan(3)
524
- sget({
465
+ const response = await fetch(getServerUrl(fastify) + '/test3/', {
525
466
  method: 'PUT',
526
- url: getServerUrl(fastify) + '/test3/',
527
467
  body: JSON.stringify({ hello: 'world' }),
528
468
  headers: { 'Content-Type': 'application/json' }
529
- }, (err, response, body) => {
530
- t.assert.ifError(err)
531
- t.assert.strictEqual(response.statusCode, 404)
532
- t.assert.strictEqual(body.toString(), 'this was not found 4')
533
- done()
534
469
  })
470
+
471
+ t.assert.ok(!response.ok)
472
+ t.assert.strictEqual(response.status, 404)
473
+ t.assert.strictEqual(await response.text(), 'this was not found 4')
535
474
  })
536
475
 
537
- await t.test('framework-unsupported method 3', (t, done) => {
476
+ await t.test('framework-unsupported method 3', async (t) => {
538
477
  t.plan(3)
539
- sget({
478
+ const response = await fetch(getServerUrl(fastify) + '/test3/', {
540
479
  method: 'PROPFIND',
541
- url: getServerUrl(fastify) + '/test3/',
542
480
  body: JSON.stringify({ hello: 'world' }),
543
481
  headers: { 'Content-Type': 'application/json' }
544
- }, (err, response, body) => {
545
- t.assert.ifError(err)
546
- t.assert.strictEqual(response.statusCode, 404)
547
- t.assert.strictEqual(body.toString(), 'this was not found 4')
548
- done()
549
482
  })
483
+
484
+ t.assert.ok(!response.ok)
485
+ t.assert.strictEqual(response.status, 404)
486
+ t.assert.strictEqual(await response.text(), 'this was not found 4')
550
487
  })
551
488
 
552
- await t.test('unsupported route 3', (t, done) => {
489
+ await t.test('unsupported route 3', async (t) => {
553
490
  t.plan(3)
554
- sget({
555
- method: 'GET',
556
- url: getServerUrl(fastify) + '/test3/notSupported'
557
- }, (err, response, body) => {
558
- t.assert.ifError(err)
559
- t.assert.strictEqual(response.statusCode, 404)
560
- t.assert.strictEqual(body.toString(), 'this was not found 4')
561
- done()
562
- })
491
+ const response = await fetch(getServerUrl(fastify) + '/test3/notSupported')
492
+
493
+ t.assert.ok(!response.ok)
494
+ t.assert.strictEqual(response.status, 404)
495
+ t.assert.strictEqual(await response.text(), 'this was not found 4')
563
496
  })
564
497
  })
565
498
 
@@ -682,8 +615,8 @@ test('encapsulated custom 404 without - prefix hook and handler context', (t, do
682
615
  })
683
616
  })
684
617
 
685
- test('run hooks on default 404', (t, done) => {
686
- t.plan(7)
618
+ test('run hooks on default 404', async t => {
619
+ t.plan(6)
687
620
 
688
621
  const fastify = Fastify()
689
622
 
@@ -713,20 +646,16 @@ test('run hooks on default 404', (t, done) => {
713
646
 
714
647
  t.after(() => { fastify.close() })
715
648
 
716
- fastify.listen({ port: 0 }, err => {
717
- t.assert.ifError(err)
649
+ await fastify.listen({ port: 0 })
718
650
 
719
- sget({
720
- method: 'PUT',
721
- url: getServerUrl(fastify),
722
- body: JSON.stringify({ hello: 'world' }),
723
- headers: { 'Content-Type': 'application/json' }
724
- }, (err, response, body) => {
725
- t.assert.ifError(err)
726
- t.assert.strictEqual(response.statusCode, 404)
727
- done()
728
- })
651
+ const response = await fetch(getServerUrl(fastify), {
652
+ method: 'PUT',
653
+ body: JSON.stringify({ hello: 'world' }),
654
+ headers: { 'Content-Type': 'application/json' }
729
655
  })
656
+
657
+ t.assert.ok(!response.ok)
658
+ t.assert.strictEqual(response.status, 404)
730
659
  })
731
660
 
732
661
  test('run non-encapsulated plugin hooks on default 404', (t, done) => {
@@ -822,8 +751,8 @@ test('run non-encapsulated plugin hooks on custom 404', (t, done) => {
822
751
  })
823
752
  })
824
753
 
825
- test('run hook with encapsulated 404', (t, done) => {
826
- t.plan(11)
754
+ test('run hook with encapsulated 404', async t => {
755
+ t.plan(10)
827
756
 
828
757
  const fastify = Fastify()
829
758
 
@@ -877,24 +806,20 @@ test('run hook with encapsulated 404', (t, done) => {
877
806
 
878
807
  t.after(() => { fastify.close() })
879
808
 
880
- fastify.listen({ port: 0 }, err => {
881
- t.assert.ifError(err)
809
+ await fastify.listen({ port: 0 })
882
810
 
883
- sget({
884
- method: 'PUT',
885
- url: getServerUrl(fastify) + '/test',
886
- body: JSON.stringify({ hello: 'world' }),
887
- headers: { 'Content-Type': 'application/json' }
888
- }, (err, response, body) => {
889
- t.assert.ifError(err)
890
- t.assert.strictEqual(response.statusCode, 404)
891
- done()
892
- })
811
+ const response = await fetch(getServerUrl(fastify) + '/test', {
812
+ method: 'PUT',
813
+ body: JSON.stringify({ hello: 'world' }),
814
+ headers: { 'Content-Type': 'application/json' }
893
815
  })
816
+
817
+ t.assert.ok(!response.ok)
818
+ t.assert.strictEqual(response.status, 404)
894
819
  })
895
820
 
896
- test('run hook with encapsulated 404 and framework-unsupported method', (t, done) => {
897
- t.plan(11)
821
+ test('run hook with encapsulated 404 and framework-unsupported method', async t => {
822
+ t.plan(10)
898
823
 
899
824
  const fastify = Fastify()
900
825
 
@@ -948,24 +873,20 @@ test('run hook with encapsulated 404 and framework-unsupported method', (t, done
948
873
 
949
874
  t.after(() => { fastify.close() })
950
875
 
951
- fastify.listen({ port: 0 }, err => {
952
- t.assert.ifError(err)
876
+ await fastify.listen({ port: 0 })
953
877
 
954
- sget({
955
- method: 'PROPFIND',
956
- url: getServerUrl(fastify) + '/test',
957
- body: JSON.stringify({ hello: 'world' }),
958
- headers: { 'Content-Type': 'application/json' }
959
- }, (err, response, body) => {
960
- t.assert.ifError(err)
961
- t.assert.strictEqual(response.statusCode, 404)
962
- done()
963
- })
878
+ const response = await fetch(getServerUrl(fastify) + '/test', {
879
+ method: 'PROPFIND',
880
+ body: JSON.stringify({ hello: 'world' }),
881
+ headers: { 'Content-Type': 'application/json' }
964
882
  })
883
+
884
+ t.assert.ok(!response.ok)
885
+ t.assert.strictEqual(response.status, 404)
965
886
  })
966
887
 
967
- test('hooks check 404', (t, done) => {
968
- t.plan(13)
888
+ test('hooks check 404', async t => {
889
+ t.plan(12)
969
890
 
970
891
  const fastify = Fastify()
971
892
 
@@ -989,28 +910,21 @@ test('hooks check 404', (t, done) => {
989
910
 
990
911
  t.after(() => { fastify.close() })
991
912
 
992
- fastify.listen({ port: 0 }, err => {
993
- t.assert.ifError(err)
994
-
995
- sget({
996
- method: 'PUT',
997
- url: getServerUrl(fastify) + '?foo=asd',
998
- body: JSON.stringify({ hello: 'world' }),
999
- headers: { 'Content-Type': 'application/json' }
1000
- }, (err, response, body) => {
1001
- t.assert.ifError(err)
1002
- t.assert.strictEqual(response.statusCode, 404)
1003
- })
913
+ await fastify.listen({ port: 0 })
1004
914
 
1005
- sget({
1006
- method: 'GET',
1007
- url: getServerUrl(fastify) + '/notSupported?foo=asd'
1008
- }, (err, response, body) => {
1009
- t.assert.ifError(err)
1010
- t.assert.strictEqual(response.statusCode, 404)
1011
- done()
1012
- })
915
+ const response1 = await fetch(getServerUrl(fastify) + '?foo=asd', {
916
+ method: 'PUT',
917
+ body: JSON.stringify({ hello: 'world' }),
918
+ headers: { 'Content-Type': 'application/json' }
1013
919
  })
920
+
921
+ t.assert.ok(!response1.ok)
922
+ t.assert.strictEqual(response1.status, 404)
923
+
924
+ const response2 = await fetch(getServerUrl(fastify) + '/notSupported?foo=asd')
925
+
926
+ t.assert.ok(!response2.ok)
927
+ t.assert.strictEqual(response2.status, 404)
1014
928
  })
1015
929
 
1016
930
  test('setNotFoundHandler should not suppress duplicated routes checking', t => {
@@ -1080,8 +994,8 @@ test('log debug for 404', async t => {
1080
994
  })
1081
995
  })
1082
996
 
1083
- test('Unknown method', (t, done) => {
1084
- t.plan(5)
997
+ test('Unknown method', async t => {
998
+ t.plan(4)
1085
999
 
1086
1000
  const fastify = Fastify()
1087
1001
 
@@ -1091,34 +1005,31 @@ test('Unknown method', (t, done) => {
1091
1005
 
1092
1006
  t.after(() => { fastify.close() })
1093
1007
 
1094
- fastify.listen({ port: 0 }, err => {
1095
- t.assert.ifError(err)
1008
+ await fastify.listen({ port: 0 })
1096
1009
 
1097
- const handler = () => {}
1098
- // See https://github.com/fastify/light-my-request/pull/20
1099
- t.assert.throws(() => fastify.inject({
1100
- method: 'UNKNOWN_METHOD',
1101
- url: '/'
1102
- }, handler), Error)
1103
-
1104
- sget({
1105
- method: 'UNKNOWN_METHOD',
1106
- url: getServerUrl(fastify)
1107
- }, (err, response, body) => {
1108
- t.assert.ifError(err)
1109
- t.assert.strictEqual(response.statusCode, 400)
1110
- t.assert.deepStrictEqual(JSON.parse(body), {
1111
- error: 'Bad Request',
1112
- message: 'Client Error',
1113
- statusCode: 400
1114
- })
1115
- done()
1116
- })
1010
+ const handler = () => {}
1011
+ // See https://github.com/fastify/light-my-request/pull/20
1012
+ t.assert.throws(() => fastify.inject({
1013
+ method: 'UNKNOWN_METHOD',
1014
+ url: '/'
1015
+ }, handler), Error)
1016
+
1017
+ const response = await fetch(getServerUrl(fastify), {
1018
+ method: 'UNKNOWN_METHOD'
1019
+ })
1020
+
1021
+ t.assert.ok(!response.ok)
1022
+ t.assert.strictEqual(response.status, 400)
1023
+
1024
+ t.assert.deepStrictEqual(await response.json(), {
1025
+ error: 'Bad Request',
1026
+ message: 'Client Error',
1027
+ statusCode: 400
1117
1028
  })
1118
1029
  })
1119
1030
 
1120
- test('recognizes errors from the http-errors module', (t, done) => {
1121
- t.plan(5)
1031
+ test('recognizes errors from the http-errors module', async t => {
1032
+ t.plan(4)
1122
1033
 
1123
1034
  const fastify = Fastify()
1124
1035
 
@@ -1128,27 +1039,23 @@ test('recognizes errors from the http-errors module', (t, done) => {
1128
1039
 
1129
1040
  t.after(() => { fastify.close() })
1130
1041
 
1131
- fastify.listen({ port: 0 }, err => {
1042
+ await fastify.listen({ port: 0 })
1043
+
1044
+ fastify.inject({
1045
+ method: 'GET',
1046
+ url: '/'
1047
+ }, (err, res) => {
1132
1048
  t.assert.ifError(err)
1049
+ t.assert.strictEqual(res.statusCode, 404)
1050
+ })
1133
1051
 
1134
- fastify.inject({
1135
- method: 'GET',
1136
- url: '/'
1137
- }, (err, res) => {
1138
- t.assert.ifError(err)
1139
- t.assert.strictEqual(res.statusCode, 404)
1140
-
1141
- sget(getServerUrl(fastify), (err, response, body) => {
1142
- t.assert.ifError(err)
1143
- const obj = JSON.parse(body.toString())
1144
- t.assert.deepStrictEqual(obj, {
1145
- error: 'Not Found',
1146
- message: 'Not Found',
1147
- statusCode: 404
1148
- })
1149
- done()
1150
- })
1151
- })
1052
+ const response = await fetch(getServerUrl(fastify))
1053
+
1054
+ t.assert.ok(!response.ok)
1055
+ t.assert.deepStrictEqual(await response.json(), {
1056
+ error: 'Not Found',
1057
+ message: 'Not Found',
1058
+ statusCode: 404
1152
1059
  })
1153
1060
  })
1154
1061
 
@@ -1262,8 +1169,8 @@ test('cannot set notFoundHandler after binding', (t, done) => {
1262
1169
  })
1263
1170
  })
1264
1171
 
1265
- test('404 inside onSend', (t, done) => {
1266
- t.plan(3)
1172
+ test('404 inside onSend', async t => {
1173
+ t.plan(2)
1267
1174
 
1268
1175
  const fastify = Fastify()
1269
1176
 
@@ -1284,18 +1191,12 @@ test('404 inside onSend', (t, done) => {
1284
1191
 
1285
1192
  t.after(() => { fastify.close() })
1286
1193
 
1287
- fastify.listen({ port: 0 }, err => {
1288
- t.assert.ifError(err)
1194
+ await fastify.listen({ port: 0 })
1289
1195
 
1290
- sget({
1291
- method: 'GET',
1292
- url: getServerUrl(fastify)
1293
- }, (err, response, body) => {
1294
- t.assert.ifError(err)
1295
- t.assert.strictEqual(response.statusCode, 404)
1296
- done()
1297
- })
1298
- })
1196
+ const response = await fetch(getServerUrl(fastify))
1197
+
1198
+ t.assert.ok(!response.ok)
1199
+ t.assert.strictEqual(response.status, 404)
1299
1200
  })
1300
1201
 
1301
1202
  // https://github.com/fastify/fastify/issues/868