fastify 5.4.0 → 5.6.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 (98) hide show
  1. package/LICENSE +1 -1
  2. package/SECURITY.md +158 -2
  3. package/build/build-validation.js +19 -1
  4. package/docs/Guides/Delay-Accepting-Requests.md +8 -5
  5. package/docs/Guides/Ecosystem.md +11 -0
  6. package/docs/Guides/Migration-Guide-V5.md +6 -10
  7. package/docs/Guides/Recommendations.md +1 -1
  8. package/docs/Reference/Errors.md +3 -1
  9. package/docs/Reference/Hooks.md +2 -6
  10. package/docs/Reference/Lifecycle.md +2 -2
  11. package/docs/Reference/Request.md +1 -1
  12. package/docs/Reference/Routes.md +5 -5
  13. package/docs/Reference/Server.md +306 -179
  14. package/docs/Reference/TypeScript.md +3 -5
  15. package/docs/Reference/Validation-and-Serialization.md +55 -3
  16. package/docs/Reference/Warnings.md +2 -1
  17. package/fastify.d.ts +19 -2
  18. package/fastify.js +34 -33
  19. package/lib/configValidator.js +196 -28
  20. package/lib/contentTypeParser.js +41 -48
  21. package/lib/error-handler.js +3 -3
  22. package/lib/errors.js +5 -0
  23. package/lib/handleRequest.js +13 -17
  24. package/lib/promise.js +23 -0
  25. package/lib/reply.js +17 -19
  26. package/lib/route.js +37 -3
  27. package/lib/server.js +36 -35
  28. package/lib/warnings.js +11 -1
  29. package/package.json +7 -7
  30. package/test/async-await.test.js +81 -134
  31. package/test/async_hooks.test.js +18 -37
  32. package/test/body-limit.test.js +51 -0
  33. package/test/buffer.test.js +22 -0
  34. package/test/case-insensitive.test.js +44 -65
  35. package/test/check.test.js +17 -21
  36. package/test/close-pipelining.test.js +24 -15
  37. package/test/constrained-routes.test.js +231 -0
  38. package/test/custom-http-server.test.js +7 -15
  39. package/test/custom-parser.0.test.js +267 -348
  40. package/test/custom-parser.1.test.js +141 -191
  41. package/test/custom-parser.2.test.js +34 -44
  42. package/test/custom-parser.3.test.js +56 -104
  43. package/test/custom-parser.4.test.js +106 -144
  44. package/test/custom-parser.5.test.js +56 -75
  45. package/test/custom-querystring-parser.test.js +51 -77
  46. package/test/decorator.test.js +76 -259
  47. package/test/delete.test.js +101 -110
  48. package/test/diagnostics-channel/404.test.js +7 -15
  49. package/test/diagnostics-channel/async-request.test.js +8 -16
  50. package/test/diagnostics-channel/error-request.test.js +7 -15
  51. package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
  52. package/test/diagnostics-channel/sync-request.test.js +9 -16
  53. package/test/fastify-instance.test.js +1 -1
  54. package/test/header-overflow.test.js +18 -29
  55. package/test/helper.js +138 -134
  56. package/test/hooks-async.test.js +26 -32
  57. package/test/hooks.test.js +261 -447
  58. package/test/http-methods/copy.test.js +14 -19
  59. package/test/http-methods/get.test.js +131 -143
  60. package/test/http-methods/head.test.js +53 -84
  61. package/test/http-methods/mkcalendar.test.js +45 -72
  62. package/test/http-methods/move.test.js +6 -10
  63. package/test/http-methods/propfind.test.js +34 -44
  64. package/test/http-methods/unlock.test.js +5 -9
  65. package/test/http2/secure-with-fallback.test.js +3 -1
  66. package/test/https/custom-https-server.test.js +9 -13
  67. package/test/input-validation.js +139 -150
  68. package/test/internals/errors.test.js +50 -1
  69. package/test/internals/handle-request.test.js +29 -5
  70. package/test/internals/promise.test.js +63 -0
  71. package/test/internals/reply.test.js +277 -496
  72. package/test/plugin.1.test.js +40 -68
  73. package/test/plugin.2.test.js +40 -70
  74. package/test/plugin.3.test.js +25 -68
  75. package/test/promises.test.js +42 -63
  76. package/test/register.test.js +8 -18
  77. package/test/request-error.test.js +57 -100
  78. package/test/request-id.test.js +30 -49
  79. package/test/route-hooks.test.js +12 -16
  80. package/test/route-shorthand.test.js +9 -27
  81. package/test/route.1.test.js +74 -131
  82. package/test/route.8.test.js +9 -17
  83. package/test/router-options.test.js +450 -0
  84. package/test/schema-validation.test.js +30 -31
  85. package/test/server.test.js +143 -5
  86. package/test/stream.1.test.js +33 -50
  87. package/test/stream.4.test.js +18 -28
  88. package/test/stream.5.test.js +11 -19
  89. package/test/types/errors.test-d.ts +13 -1
  90. package/test/types/instance.test-d.ts +18 -1
  91. package/test/types/type-provider.test-d.ts +55 -0
  92. package/test/use-semicolon-delimiter.test.js +117 -59
  93. package/test/versioned-routes.test.js +39 -56
  94. package/types/errors.d.ts +11 -1
  95. package/types/hooks.d.ts +1 -1
  96. package/types/instance.d.ts +3 -1
  97. package/types/logger.d.ts +16 -14
  98. package/types/reply.d.ts +2 -2
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('node:test')
4
- const sget = require('simple-get').concat
5
4
  const stream = require('node:stream')
6
5
  const Fastify = require('..')
7
6
  const fp = require('fastify-plugin')
@@ -11,13 +10,13 @@ const symbols = require('../lib/symbols.js')
11
10
  const payload = { hello: 'world' }
12
11
  const proxyquire = require('proxyquire')
13
12
  const { connect } = require('node:net')
14
- const { sleep, getServerUrl } = require('./helper')
13
+ const { sleep } = require('./helper')
15
14
  const { waitForCb } = require('./toolkit.js')
16
15
 
17
16
  process.removeAllListeners('warning')
18
17
 
19
- test('hooks', (t, testDone) => {
20
- t.plan(49)
18
+ test('hooks', async t => {
19
+ t.plan(48)
21
20
  const fastify = Fastify({ exposeHeadRoutes: false })
22
21
 
23
22
  try {
@@ -148,39 +147,23 @@ test('hooks', (t, testDone) => {
148
147
  reply.code(200).send(payload)
149
148
  })
150
149
 
151
- fastify.listen({ port: 0 }, err => {
152
- t.assert.ifError(err)
153
- t.after(() => { fastify.close() })
150
+ const fastifyServer = await fastify.listen({ port: 0 })
151
+ t.after(() => { fastify.close() })
154
152
 
155
- const completion = waitForCb({ steps: 3 })
156
- sget({
157
- method: 'GET',
158
- url: 'http://127.0.0.1:' + fastify.server.address().port
159
- }, (err, response, body) => {
160
- t.assert.ifError(err)
161
- t.assert.strictEqual(response.statusCode, 200)
162
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
163
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
164
- completion.stepIn()
165
- })
166
- sget({
167
- method: 'HEAD',
168
- url: 'http://127.0.0.1:' + fastify.server.address().port
169
- }, (err, response, body) => {
170
- t.assert.ifError(err)
171
- t.assert.strictEqual(response.statusCode, 500)
172
- completion.stepIn()
173
- })
174
- sget({
175
- method: 'DELETE',
176
- url: 'http://127.0.0.1:' + fastify.server.address().port
177
- }, (err, response, body) => {
178
- t.assert.ifError(err)
179
- t.assert.strictEqual(response.statusCode, 500)
180
- completion.stepIn()
181
- })
182
- completion.patience.then(testDone)
183
- })
153
+ const getResult = await fetch(fastifyServer)
154
+ t.assert.ok(getResult.ok)
155
+ t.assert.strictEqual(getResult.status, 200)
156
+ const getBody = await getResult.text()
157
+ t.assert.strictEqual(getResult.headers.get('content-length'), '' + getBody.length)
158
+ t.assert.deepStrictEqual(JSON.parse(getBody), { hello: 'world' })
159
+
160
+ const headResult = await fetch(fastifyServer, { method: 'HEAD' })
161
+ t.assert.ok(!headResult.ok)
162
+ t.assert.strictEqual(headResult.status, 500)
163
+
164
+ const deleteResult = await fetch(fastifyServer, { method: 'DELETE' })
165
+ t.assert.ok(!deleteResult.ok)
166
+ t.assert.strictEqual(deleteResult.status, 500)
184
167
  })
185
168
 
186
169
  test('onRequest hook should support encapsulation / 1', (t, testDone) => {
@@ -237,8 +220,8 @@ test('onRequest hook should support encapsulation / 2', (t, testDone) => {
237
220
  })
238
221
  })
239
222
 
240
- test('onRequest hook should support encapsulation / 3', (t, testDone) => {
241
- t.plan(20)
223
+ test('onRequest hook should support encapsulation / 3', async t => {
224
+ t.plan(19)
242
225
  const fastify = Fastify()
243
226
  fastify.decorate('hello', 'world')
244
227
 
@@ -276,37 +259,26 @@ test('onRequest hook should support encapsulation / 3', (t, testDone) => {
276
259
  done()
277
260
  })
278
261
 
279
- fastify.listen({ port: 0 }, err => {
280
- t.assert.ifError(err)
281
- t.after(() => { fastify.close() })
262
+ const fastifyServer = await fastify.listen({ port: 0 })
263
+ t.after(() => { fastify.close() })
282
264
 
283
- const completion = waitForCb({ steps: 2 })
284
- sget({
285
- method: 'GET',
286
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
287
- }, (err, response, body) => {
288
- t.assert.ifError(err)
289
- t.assert.strictEqual(response.statusCode, 200)
290
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
291
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
292
- completion.stepIn()
293
- })
294
- sget({
295
- method: 'GET',
296
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
297
- }, (err, response, body) => {
298
- t.assert.ifError(err)
299
- t.assert.strictEqual(response.statusCode, 200)
300
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
301
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
302
- completion.stepIn()
303
- })
304
- completion.patience.then(testDone)
305
- })
265
+ const firstResult = await fetch(fastifyServer + '/first', { method: 'GET' })
266
+ t.assert.ok(firstResult.ok)
267
+ t.assert.strictEqual(firstResult.status, 200)
268
+ const firstBody = await firstResult.text()
269
+ t.assert.strictEqual(firstResult.headers.get('content-length'), '' + firstBody.length)
270
+ t.assert.deepStrictEqual(JSON.parse(firstBody), { hello: 'world' })
271
+
272
+ const secondResult = await fetch(fastifyServer + '/second', { method: 'GET' })
273
+ t.assert.ok(secondResult.ok)
274
+ t.assert.strictEqual(secondResult.status, 200)
275
+ const secondBody = await secondResult.text()
276
+ t.assert.strictEqual(secondResult.headers.get('content-length'), '' + secondBody.length)
277
+ t.assert.deepStrictEqual(JSON.parse(secondBody), { hello: 'world' })
306
278
  })
307
279
 
308
- test('preHandler hook should support encapsulation / 5', (t, testDone) => {
309
- t.plan(17)
280
+ test('preHandler hook should support encapsulation / 5', async t => {
281
+ t.plan(16)
310
282
  const fastify = Fastify()
311
283
  t.after(() => { fastify.close() })
312
284
  fastify.decorate('hello', 'world')
@@ -341,32 +313,21 @@ test('preHandler hook should support encapsulation / 5', (t, testDone) => {
341
313
  done()
342
314
  })
343
315
 
344
- fastify.listen({ port: 0 }, err => {
345
- t.assert.ifError(err)
316
+ const fastifyServer = await fastify.listen({ port: 0 })
346
317
 
347
- const completion = waitForCb({ steps: 2 })
348
- sget({
349
- method: 'GET',
350
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
351
- }, (err, response, body) => {
352
- t.assert.ifError(err)
353
- t.assert.strictEqual(response.statusCode, 200)
354
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
355
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
356
- completion.stepIn()
357
- })
358
- sget({
359
- method: 'GET',
360
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
361
- }, (err, response, body) => {
362
- t.assert.ifError(err)
363
- t.assert.strictEqual(response.statusCode, 200)
364
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
365
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
366
- completion.stepIn()
367
- })
368
- completion.patience.then(testDone)
369
- })
318
+ const firstResult = await fetch(fastifyServer + '/first')
319
+ t.assert.ok(firstResult.ok)
320
+ t.assert.strictEqual(firstResult.status, 200)
321
+ const firstBody = await firstResult.text()
322
+ t.assert.strictEqual(firstResult.headers.get('content-length'), '' + firstBody.length)
323
+ t.assert.deepStrictEqual(JSON.parse(firstBody), { hello: 'world' })
324
+
325
+ const secondResult = await fetch(fastifyServer + '/second')
326
+ t.assert.ok(secondResult.ok)
327
+ t.assert.strictEqual(secondResult.status, 200)
328
+ const secondBody = await secondResult.text()
329
+ t.assert.strictEqual(secondResult.headers.get('content-length'), '' + secondBody.length)
330
+ t.assert.deepStrictEqual(JSON.parse(secondBody), { hello: 'world' })
370
331
  })
371
332
 
372
333
  test('onRoute hook should be called / 1', (t, testDone) => {
@@ -750,8 +711,8 @@ test('onRoute hook should be called once when prefixTrailingSlash', (t, testDone
750
711
  })
751
712
  })
752
713
 
753
- test('onRoute hook should able to change the route url', (t, testDone) => {
754
- t.plan(5)
714
+ test('onRoute hook should able to change the route url', async t => {
715
+ t.plan(4)
755
716
 
756
717
  const fastify = Fastify({ exposeHeadRoutes: false })
757
718
  t.after(() => { fastify.close() })
@@ -769,19 +730,12 @@ test('onRoute hook should able to change the route url', (t, testDone) => {
769
730
  done()
770
731
  })
771
732
 
772
- fastify.listen({ port: 0 }, err => {
773
- t.assert.ifError(err)
733
+ const fastifyServer = await fastify.listen({ port: 0 })
774
734
 
775
- sget({
776
- method: 'GET',
777
- url: getServerUrl(fastify) + encodeURI('/foo')
778
- }, (err, response, body) => {
779
- t.assert.ifError(err)
780
- t.assert.strictEqual(response.statusCode, 200)
781
- t.assert.strictEqual(body.toString(), 'here /foo')
782
- testDone()
783
- })
784
- })
735
+ const result = await fetch(fastifyServer + encodeURI('/foo'))
736
+ t.assert.ok(result.ok)
737
+ t.assert.strictEqual(result.status, 200)
738
+ t.assert.strictEqual(await result.text(), 'here /foo')
785
739
  })
786
740
 
787
741
  test('onRoute hook that throws should be caught', (t, testDone) => {
@@ -951,8 +905,8 @@ test('onResponse hook should support encapsulation / 2', (t, testDone) => {
951
905
  })
952
906
  })
953
907
 
954
- test('onResponse hook should support encapsulation / 3', (t, testDone) => {
955
- t.plan(16)
908
+ test('onResponse hook should support encapsulation / 3', async t => {
909
+ t.plan(15)
956
910
  const fastify = Fastify()
957
911
  t.after(() => { fastify.close() })
958
912
  fastify.decorate('hello', 'world')
@@ -983,32 +937,21 @@ test('onResponse hook should support encapsulation / 3', (t, testDone) => {
983
937
  done()
984
938
  })
985
939
 
986
- fastify.listen({ port: 0 }, err => {
987
- t.assert.ifError(err)
940
+ const fastifyServer = await fastify.listen({ port: 0 })
988
941
 
989
- const completion = waitForCb({ steps: 2 })
990
- sget({
991
- method: 'GET',
992
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
993
- }, (err, response, body) => {
994
- t.assert.ifError(err)
995
- t.assert.strictEqual(response.statusCode, 200)
996
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
997
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
998
- completion.stepIn()
999
- })
1000
- sget({
1001
- method: 'GET',
1002
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
1003
- }, (err, response, body) => {
1004
- t.assert.ifError(err)
1005
- t.assert.strictEqual(response.statusCode, 200)
1006
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
1007
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
1008
- completion.stepIn()
1009
- })
1010
- completion.patience.then(testDone)
1011
- })
942
+ const firstResult = await fetch(fastifyServer + '/first', { method: 'GET' })
943
+ t.assert.ok(firstResult.ok)
944
+ t.assert.strictEqual(firstResult.status, 200)
945
+ const firstBody = await firstResult.text()
946
+ t.assert.strictEqual(firstResult.headers.get('content-length'), '' + firstBody.length)
947
+ t.assert.deepStrictEqual(JSON.parse(firstBody), { hello: 'world' })
948
+
949
+ const secondResult = await fetch(fastifyServer + '/second')
950
+ t.assert.ok(secondResult.ok)
951
+ t.assert.strictEqual(secondResult.status, 200)
952
+ const secondBody = await secondResult.text()
953
+ t.assert.strictEqual(secondResult.headers.get('content-length'), '' + secondBody.length)
954
+ t.assert.deepStrictEqual(JSON.parse(secondBody), { hello: 'world' })
1012
955
  })
1013
956
 
1014
957
  test('onSend hook should support encapsulation / 1', (t, testDone) => {
@@ -1032,8 +975,8 @@ test('onSend hook should support encapsulation / 1', (t, testDone) => {
1032
975
  })
1033
976
  })
1034
977
 
1035
- test('onSend hook should support encapsulation / 2', (t, testDone) => {
1036
- t.plan(16)
978
+ test('onSend hook should support encapsulation / 2', async t => {
979
+ t.plan(15)
1037
980
  const fastify = Fastify()
1038
981
  t.after(() => { fastify.close() })
1039
982
  fastify.decorate('hello', 'world')
@@ -1064,33 +1007,21 @@ test('onSend hook should support encapsulation / 2', (t, testDone) => {
1064
1007
  done()
1065
1008
  })
1066
1009
 
1067
- fastify.listen({ port: 0 }, err => {
1068
- t.assert.ifError(err)
1010
+ const fastifyServer = await fastify.listen({ port: 0 })
1069
1011
 
1070
- const completion = waitForCb({ steps: 2 })
1071
- sget({
1072
- method: 'GET',
1073
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
1074
- }, (err, response, body) => {
1075
- t.assert.ifError(err)
1076
- t.assert.strictEqual(response.statusCode, 200)
1077
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
1078
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
1079
- completion.stepIn()
1080
- })
1012
+ const firstResult = await fetch(fastifyServer + '/first')
1013
+ t.assert.ok(firstResult.ok)
1014
+ t.assert.strictEqual(firstResult.status, 200)
1015
+ const firstBody = await firstResult.text()
1016
+ t.assert.strictEqual(firstResult.headers.get('content-length'), '' + firstBody.length)
1017
+ t.assert.deepStrictEqual(JSON.parse(firstBody), { hello: 'world' })
1081
1018
 
1082
- sget({
1083
- method: 'GET',
1084
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
1085
- }, (err, response, body) => {
1086
- t.assert.ifError(err)
1087
- t.assert.strictEqual(response.statusCode, 200)
1088
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
1089
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
1090
- completion.stepIn()
1091
- })
1092
- completion.patience.then(testDone)
1093
- })
1019
+ const secondResult = await fetch(fastifyServer + '/second')
1020
+ t.assert.ok(secondResult.ok)
1021
+ t.assert.strictEqual(secondResult.status, 200)
1022
+ const secondBody = await secondResult.text()
1023
+ t.assert.strictEqual(secondResult.headers.get('content-length'), '' + secondBody.length)
1024
+ t.assert.deepStrictEqual(JSON.parse(secondBody), { hello: 'world' })
1094
1025
  })
1095
1026
 
1096
1027
  test('onSend hook is called after payload is serialized and headers are set', (t, testDone) => {
@@ -1312,8 +1243,8 @@ test('clear payload', (t, testDone) => {
1312
1243
  })
1313
1244
  })
1314
1245
 
1315
- test('onSend hook throws', (t, testDone) => {
1316
- t.plan(11)
1246
+ test('onSend hook throws', async t => {
1247
+ t.plan(10)
1317
1248
  const Fastify = proxyquire('..', {
1318
1249
  './lib/schemas.js': {
1319
1250
  getSchemaSerializer: (param1, param2, param3) => {
@@ -1372,46 +1303,26 @@ test('onSend hook throws', (t, testDone) => {
1372
1303
  reply.send({ hello: 'world' })
1373
1304
  })
1374
1305
 
1375
- fastify.listen({ port: 0 }, err => {
1376
- t.assert.ifError(err)
1306
+ const fastifyServer = await fastify.listen({ port: 0 })
1377
1307
 
1378
- const completion = waitForCb({ steps: 4 })
1379
- sget({
1380
- method: 'GET',
1381
- url: 'http://127.0.0.1:' + fastify.server.address().port
1382
- }, (err, response, body) => {
1383
- t.assert.ifError(err)
1384
- t.assert.strictEqual(response.statusCode, 200)
1385
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
1386
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
1387
- completion.stepIn()
1388
- })
1389
- sget({
1390
- method: 'POST',
1391
- url: 'http://127.0.0.1:' + fastify.server.address().port
1392
- }, (err, response, body) => {
1393
- t.assert.ifError(err)
1394
- t.assert.strictEqual(response.statusCode, 500)
1395
- completion.stepIn()
1396
- })
1397
- sget({
1398
- method: 'DELETE',
1399
- url: 'http://127.0.0.1:' + fastify.server.address().port
1400
- }, (err, response, body) => {
1401
- t.assert.ifError(err)
1402
- t.assert.strictEqual(response.statusCode, 500)
1403
- completion.stepIn()
1404
- })
1405
- sget({
1406
- method: 'PUT',
1407
- url: 'http://127.0.0.1:' + fastify.server.address().port
1408
- }, (err, response, body) => {
1409
- t.assert.ifError(err)
1410
- t.assert.strictEqual(response.statusCode, 500)
1411
- completion.stepIn()
1412
- })
1413
- completion.patience.then(testDone)
1414
- })
1308
+ const getResult = await fetch(fastifyServer)
1309
+ t.assert.ok(getResult.ok)
1310
+ t.assert.strictEqual(getResult.status, 200)
1311
+ const getBody = await getResult.text()
1312
+ t.assert.strictEqual(getResult.headers.get('content-length'), '' + getBody.length)
1313
+ t.assert.deepStrictEqual(JSON.parse(getBody), { hello: 'world' })
1314
+
1315
+ const postResult = await fetch(fastifyServer, { method: 'POST' })
1316
+ t.assert.ok(!postResult.ok)
1317
+ t.assert.strictEqual(postResult.status, 500)
1318
+
1319
+ const deleteResult = await fetch(fastifyServer, { method: 'DELETE' })
1320
+ t.assert.ok(!deleteResult.ok)
1321
+ t.assert.strictEqual(deleteResult.status, 500)
1322
+
1323
+ const putResult = await fetch(fastifyServer, { method: 'PUT' })
1324
+ t.assert.ok(!putResult.ok)
1325
+ t.assert.strictEqual(putResult.status, 500)
1415
1326
  })
1416
1327
 
1417
1328
  test('onSend hook should receive valid request and reply objects if onRequest hook fails', (t, testDone) => {
@@ -2501,8 +2412,8 @@ test('preValidation hook should support encapsulation / 2', (t, testDone) => {
2501
2412
  })
2502
2413
  })
2503
2414
 
2504
- test('preValidation hook should support encapsulation / 3', (t, testDone) => {
2505
- t.plan(20)
2415
+ test('preValidation hook should support encapsulation / 3', async t => {
2416
+ t.plan(19)
2506
2417
  const fastify = Fastify()
2507
2418
  t.after(() => { fastify.close() })
2508
2419
  fastify.decorate('hello', 'world')
@@ -2541,32 +2452,21 @@ test('preValidation hook should support encapsulation / 3', (t, testDone) => {
2541
2452
  done()
2542
2453
  })
2543
2454
 
2544
- fastify.listen({ port: 0 }, err => {
2545
- t.assert.ifError(err)
2455
+ const fastifyServer = await fastify.listen({ port: 0 })
2546
2456
 
2547
- const completion = waitForCb({ steps: 2 })
2548
- sget({
2549
- method: 'GET',
2550
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
2551
- }, (err, response, body) => {
2552
- t.assert.ifError(err)
2553
- t.assert.strictEqual(response.statusCode, 200)
2554
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
2555
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
2556
- completion.stepIn()
2557
- })
2558
- sget({
2559
- method: 'GET',
2560
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
2561
- }, (err, response, body) => {
2562
- t.assert.ifError(err)
2563
- t.assert.strictEqual(response.statusCode, 200)
2564
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
2565
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
2566
- completion.stepIn()
2567
- })
2568
- completion.patience.then(testDone)
2569
- })
2457
+ const result1 = await fetch(fastifyServer + '/first')
2458
+ t.assert.ok(result1.ok)
2459
+ t.assert.strictEqual(result1.status, 200)
2460
+ const body1 = await result1.text()
2461
+ t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
2462
+ t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' })
2463
+
2464
+ const result2 = await fetch(fastifyServer + '/second')
2465
+ t.assert.ok(result2.ok)
2466
+ t.assert.strictEqual(result2.status, 200)
2467
+ const body2 = await result2.text()
2468
+ t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
2469
+ t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'world' })
2570
2470
  })
2571
2471
 
2572
2472
  test('onError hook', (t, testDone) => {
@@ -2669,8 +2569,8 @@ test('onError hook with setErrorHandler', (t, testDone) => {
2669
2569
  })
2670
2570
  })
2671
2571
 
2672
- test('preParsing hook should run before parsing and be able to modify the payload', (t, testDone) => {
2673
- t.plan(5)
2572
+ test('preParsing hook should run before parsing and be able to modify the payload', async t => {
2573
+ t.plan(4)
2674
2574
  const fastify = Fastify()
2675
2575
  t.after(() => { fastify.close() })
2676
2576
 
@@ -2690,26 +2590,22 @@ test('preParsing hook should run before parsing and be able to modify the payloa
2690
2590
  }
2691
2591
  })
2692
2592
 
2693
- fastify.listen({ port: 0 }, err => {
2694
- t.assert.ifError(err)
2593
+ const fastifyServer = await fastify.listen({ port: 0 })
2695
2594
 
2696
- sget({
2697
- method: 'POST',
2698
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first',
2699
- body: { hello: 'world' },
2700
- json: true
2701
- }, (err, response, body) => {
2702
- t.assert.ifError(err)
2703
- t.assert.strictEqual(response.statusCode, 200)
2704
- t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
2705
- t.assert.deepStrictEqual(body, { hello: 'another world' })
2706
- testDone()
2707
- })
2595
+ const result = await fetch(fastifyServer + '/first', {
2596
+ method: 'POST',
2597
+ body: JSON.stringify({ hello: 'world' }),
2598
+ headers: { 'Content-Type': 'application/json' }
2708
2599
  })
2600
+ t.assert.ok(result.ok)
2601
+ t.assert.strictEqual(result.status, 200)
2602
+ const body = await result.text()
2603
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
2604
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'another world' })
2709
2605
  })
2710
2606
 
2711
- test('preParsing hooks should run in the order in which they are defined', (t, testDone) => {
2712
- t.plan(5)
2607
+ test('preParsing hooks should run in the order in which they are defined', async t => {
2608
+ t.plan(4)
2713
2609
  const fastify = Fastify()
2714
2610
  t.after(() => { fastify.close() })
2715
2611
 
@@ -2734,26 +2630,22 @@ test('preParsing hooks should run in the order in which they are defined', (t, t
2734
2630
  }
2735
2631
  })
2736
2632
 
2737
- fastify.listen({ port: 0 }, err => {
2738
- t.assert.ifError(err)
2633
+ const fastifyServer = await fastify.listen({ port: 0 })
2739
2634
 
2740
- sget({
2741
- method: 'POST',
2742
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first',
2743
- body: { hello: 'world' },
2744
- json: true
2745
- }, (err, response, body) => {
2746
- t.assert.ifError(err)
2747
- t.assert.strictEqual(response.statusCode, 200)
2748
- t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
2749
- t.assert.deepStrictEqual(body, { hello: 'another world' })
2750
- testDone()
2751
- })
2635
+ const result = await fetch(fastifyServer + '/first', {
2636
+ method: 'POST',
2637
+ body: JSON.stringify({ hello: 'world' }),
2638
+ headers: { 'Content-Type': 'application/json' }
2752
2639
  })
2640
+ t.assert.ok(result.ok)
2641
+ t.assert.strictEqual(result.status, 200)
2642
+ const body = await result.text()
2643
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
2644
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'another world' })
2753
2645
  })
2754
2646
 
2755
- test('preParsing hooks should support encapsulation', (t, testDone) => {
2756
- t.plan(9)
2647
+ test('preParsing hooks should support encapsulation', async t => {
2648
+ t.plan(8)
2757
2649
  const fastify = Fastify()
2758
2650
  t.after(() => { fastify.close() })
2759
2651
 
@@ -2785,36 +2677,29 @@ test('preParsing hooks should support encapsulation', (t, testDone) => {
2785
2677
  done()
2786
2678
  })
2787
2679
 
2788
- fastify.listen({ port: 0 }, err => {
2789
- t.assert.ifError(err)
2680
+ const fastifyServer = await fastify.listen({ port: 0 })
2790
2681
 
2791
- const completion = waitForCb({ steps: 2 })
2792
- sget({
2793
- method: 'POST',
2794
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first',
2795
- body: { hello: 'world' },
2796
- json: true
2797
- }, (err, response, body) => {
2798
- t.assert.ifError(err)
2799
- t.assert.strictEqual(response.statusCode, 200)
2800
- t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
2801
- t.assert.deepStrictEqual(body, { hello: 'another world' })
2802
- completion.stepIn()
2803
- })
2804
- sget({
2805
- method: 'POST',
2806
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second',
2807
- body: { hello: 'world' },
2808
- json: true
2809
- }, (err, response, body) => {
2810
- t.assert.ifError(err)
2811
- t.assert.strictEqual(response.statusCode, 200)
2812
- t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
2813
- t.assert.deepStrictEqual(body, { hello: 'encapsulated world' })
2814
- completion.stepIn()
2815
- })
2816
- completion.patience.then(testDone)
2682
+ const result1 = await fetch(fastifyServer + '/first', {
2683
+ method: 'POST',
2684
+ body: JSON.stringify({ hello: 'world' }),
2685
+ headers: { 'Content-Type': 'application/json' }
2817
2686
  })
2687
+ t.assert.ok(result1.ok)
2688
+ t.assert.strictEqual(result1.status, 200)
2689
+ const body1 = await result1.text()
2690
+ t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
2691
+ t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'another world' })
2692
+
2693
+ const result2 = await fetch(fastifyServer + '/second', {
2694
+ method: 'POST',
2695
+ body: JSON.stringify({ hello: 'world' }),
2696
+ headers: { 'Content-Type': 'application/json' }
2697
+ })
2698
+ t.assert.ok(result2.ok)
2699
+ t.assert.strictEqual(result2.status, 200)
2700
+ const body2 = await result2.text()
2701
+ t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
2702
+ t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'encapsulated world' })
2818
2703
  })
2819
2704
 
2820
2705
  test('preParsing hook should support encapsulation / 1', (t, testDone) => {
@@ -2870,8 +2755,8 @@ test('preParsing hook should support encapsulation / 2', (t, testDone) => {
2870
2755
  })
2871
2756
  })
2872
2757
 
2873
- test('preParsing hook should support encapsulation / 3', (t, testDone) => {
2874
- t.plan(20)
2758
+ test('preParsing hook should support encapsulation / 3', async t => {
2759
+ t.plan(19)
2875
2760
  const fastify = Fastify()
2876
2761
  t.after(() => { fastify.close() })
2877
2762
  fastify.decorate('hello', 'world')
@@ -2910,36 +2795,25 @@ test('preParsing hook should support encapsulation / 3', (t, testDone) => {
2910
2795
  done()
2911
2796
  })
2912
2797
 
2913
- fastify.listen({ port: 0 }, err => {
2914
- t.assert.ifError(err)
2798
+ const fastifyServer = await fastify.listen({ port: 0 })
2915
2799
 
2916
- const completion = waitForCb({ steps: 2 })
2917
- sget({
2918
- method: 'GET',
2919
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
2920
- }, (err, response, body) => {
2921
- t.assert.ifError(err)
2922
- t.assert.strictEqual(response.statusCode, 200)
2923
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
2924
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
2925
- completion.stepIn()
2926
- })
2927
- sget({
2928
- method: 'GET',
2929
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
2930
- }, (err, response, body) => {
2931
- t.assert.ifError(err)
2932
- t.assert.strictEqual(response.statusCode, 200)
2933
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
2934
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
2935
- completion.stepIn()
2936
- })
2937
- completion.patience.then(testDone)
2938
- })
2800
+ const result1 = await fetch(fastifyServer + '/first')
2801
+ t.assert.ok(result1.ok)
2802
+ t.assert.strictEqual(result1.status, 200)
2803
+ const body1 = await result1.text()
2804
+ t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
2805
+ t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' })
2806
+
2807
+ const result2 = await fetch(fastifyServer + '/second')
2808
+ t.assert.ok(result2.ok)
2809
+ t.assert.strictEqual(result2.status, 200)
2810
+ const body2 = await result2.text()
2811
+ t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
2812
+ t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'world' })
2939
2813
  })
2940
2814
 
2941
- test('preSerialization hook should run before serialization and be able to modify the payload', (t, testDone) => {
2942
- t.plan(5)
2815
+ test('preSerialization hook should run before serialization and be able to modify the payload', async t => {
2816
+ t.plan(4)
2943
2817
  const fastify = Fastify()
2944
2818
  t.after(() => { fastify.close() })
2945
2819
 
@@ -2975,23 +2849,18 @@ test('preSerialization hook should run before serialization and be able to modif
2975
2849
  }
2976
2850
  })
2977
2851
 
2978
- fastify.listen({ port: 0 }, err => {
2979
- t.assert.ifError(err)
2852
+ const fastifyServer = await fastify.listen({ port: 0 })
2980
2853
 
2981
- sget({
2982
- method: 'GET',
2983
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
2984
- }, (err, response, body) => {
2985
- t.assert.ifError(err)
2986
- t.assert.strictEqual(response.statusCode, 200)
2987
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
2988
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world1', world: 'ok' })
2989
- testDone()
2990
- })
2991
- })
2854
+ const result = await fetch(fastifyServer + '/first')
2855
+ t.assert.ok(result.ok)
2856
+ t.assert.strictEqual(result.status, 200)
2857
+ const body = await result.text()
2858
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
2859
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world1', world: 'ok' })
2992
2860
  })
2993
2861
 
2994
- test('preSerialization hook should be able to throw errors which are validated against schema response', (t, testDone) => {
2862
+ test('preSerialization hook should be able to throw errors which are validated against schema response', async t => {
2863
+ t.plan(5)
2995
2864
  const fastify = Fastify()
2996
2865
  t.after(() => { fastify.close() })
2997
2866
 
@@ -3027,24 +2896,18 @@ test('preSerialization hook should be able to throw errors which are validated a
3027
2896
  }
3028
2897
  })
3029
2898
 
3030
- fastify.listen({ port: 0 }, err => {
3031
- t.assert.ifError(err)
2899
+ const fastifyServer = await fastify.listen({ port: 0 })
3032
2900
 
3033
- sget({
3034
- method: 'GET',
3035
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
3036
- }, (err, response, body) => {
3037
- t.assert.ifError(err)
3038
- t.assert.strictEqual(response.statusCode, 500)
3039
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
3040
- t.assert.deepStrictEqual(JSON.parse(body), { world: 'error' })
3041
- testDone()
3042
- })
3043
- })
2901
+ const result = await fetch(fastifyServer + '/first')
2902
+ t.assert.ok(!result.ok)
2903
+ t.assert.strictEqual(result.status, 500)
2904
+ const body = await result.text()
2905
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
2906
+ t.assert.deepStrictEqual(JSON.parse(body), { world: 'error' })
3044
2907
  })
3045
2908
 
3046
- test('preSerialization hook which returned error should still run onError hooks', (t, testDone) => {
3047
- t.plan(4)
2909
+ test('preSerialization hook which returned error should still run onError hooks', async t => {
2910
+ t.plan(3)
3048
2911
  const fastify = Fastify()
3049
2912
  t.after(() => { fastify.close() })
3050
2913
 
@@ -3061,22 +2924,15 @@ test('preSerialization hook which returned error should still run onError hooks'
3061
2924
  reply.send({ hello: 'world' })
3062
2925
  })
3063
2926
 
3064
- fastify.listen({ port: 0 }, err => {
3065
- t.assert.ifError(err)
2927
+ const fastifyServer = await fastify.listen({ port: 0 })
3066
2928
 
3067
- sget({
3068
- method: 'GET',
3069
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
3070
- }, (err, response, body) => {
3071
- t.assert.ifError(err)
3072
- t.assert.strictEqual(response.statusCode, 500)
3073
- testDone()
3074
- })
3075
- })
2929
+ const result = await fetch(fastifyServer + '/first')
2930
+ t.assert.ok(!result.ok)
2931
+ t.assert.strictEqual(result.status, 500)
3076
2932
  })
3077
2933
 
3078
- test('preSerialization hooks should run in the order in which they are defined', (t, testDone) => {
3079
- t.plan(5)
2934
+ test('preSerialization hooks should run in the order in which they are defined', async t => {
2935
+ t.plan(4)
3080
2936
  const fastify = Fastify()
3081
2937
  t.after(() => { fastify.close() })
3082
2938
 
@@ -3093,27 +2949,21 @@ test('preSerialization hooks should run in the order in which they are defined',
3093
2949
  })
3094
2950
 
3095
2951
  fastify.get('/first', (req, reply) => {
3096
- reply.send(payload)
2952
+ reply.send({ hello: 'world' })
3097
2953
  })
3098
2954
 
3099
- fastify.listen({ port: 0 }, err => {
3100
- t.assert.ifError(err)
2955
+ const fastifyServer = await fastify.listen({ port: 0 })
3101
2956
 
3102
- sget({
3103
- method: 'GET',
3104
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
3105
- }, (err, response, body) => {
3106
- t.assert.ifError(err)
3107
- t.assert.strictEqual(response.statusCode, 200)
3108
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
3109
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world21' })
3110
- testDone()
3111
- })
3112
- })
2957
+ const result = await fetch(fastifyServer + '/first')
2958
+ t.assert.ok(result.ok)
2959
+ t.assert.strictEqual(result.status, 200)
2960
+ const body = await result.text()
2961
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
2962
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world21' })
3113
2963
  })
3114
2964
 
3115
- test('preSerialization hooks should support encapsulation', (t, testDone) => {
3116
- t.plan(9)
2965
+ test('preSerialization hooks should support encapsulation', async t => {
2966
+ t.plan(8)
3117
2967
  const fastify = Fastify()
3118
2968
  t.after(() => { fastify.close() })
3119
2969
 
@@ -3141,32 +2991,21 @@ test('preSerialization hooks should support encapsulation', (t, testDone) => {
3141
2991
  done()
3142
2992
  })
3143
2993
 
3144
- fastify.listen({ port: 0 }, err => {
3145
- t.assert.ifError(err)
2994
+ const fastifyServer = await fastify.listen({ port: 0 })
3146
2995
 
3147
- const completion = waitForCb({ steps: 2 })
3148
- sget({
3149
- method: 'GET',
3150
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
3151
- }, (err, response, body) => {
3152
- t.assert.ifError(err)
3153
- t.assert.strictEqual(response.statusCode, 200)
3154
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
3155
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world1' })
3156
- completion.stepIn()
3157
- })
3158
- sget({
3159
- method: 'GET',
3160
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
3161
- }, (err, response, body) => {
3162
- t.assert.ifError(err)
3163
- t.assert.strictEqual(response.statusCode, 200)
3164
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
3165
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world12' })
3166
- completion.stepIn()
3167
- })
3168
- completion.patience.then(testDone)
3169
- })
2996
+ const result1 = await fetch(fastifyServer + '/first')
2997
+ t.assert.ok(result1.ok)
2998
+ t.assert.strictEqual(result1.status, 200)
2999
+ const body1 = await result1.text()
3000
+ t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
3001
+ t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world1' })
3002
+
3003
+ const result2 = await fetch(fastifyServer + '/second')
3004
+ t.assert.ok(result2.ok)
3005
+ t.assert.strictEqual(result2.status, 200)
3006
+ const body2 = await result2.text()
3007
+ t.assert.strictEqual(result2.headers.get('content-length'), '' + body2.length)
3008
+ t.assert.deepStrictEqual(JSON.parse(body2), { hello: 'world12' })
3170
3009
  })
3171
3010
 
3172
3011
  test('onRegister hook should be called / 1', (t, testDone) => {
@@ -3378,8 +3217,8 @@ test('reply.send should throw if undefined error is thrown at onSend hook', (t,
3378
3217
  })
3379
3218
  })
3380
3219
 
3381
- test('onTimeout should be triggered', (t, testDone) => {
3382
- t.plan(6)
3220
+ test('onTimeout should be triggered', async t => {
3221
+ t.plan(4)
3383
3222
  const fastify = Fastify({ connectionTimeout: 500 })
3384
3223
  t.after(() => { fastify.close() })
3385
3224
 
@@ -3396,32 +3235,17 @@ test('onTimeout should be triggered', (t, testDone) => {
3396
3235
  return reply
3397
3236
  })
3398
3237
 
3399
- fastify.listen({ port: 0 }, (err, address) => {
3400
- t.assert.ifError(err)
3238
+ const fastifyServer = await fastify.listen({ port: 0 })
3401
3239
 
3402
- const completion = waitForCb({ steps: 2 })
3403
- sget({
3404
- method: 'GET',
3405
- url: address
3406
- }, (err, response, body) => {
3407
- t.assert.ifError(err)
3408
- t.assert.strictEqual(response.statusCode, 200)
3409
- completion.stepIn()
3410
- })
3411
- sget({
3412
- method: 'GET',
3413
- url: `${address}/timeout`
3414
- }, (err, response, body) => {
3415
- t.assert.ok(err, Error)
3416
- t.assert.strictEqual(err.message, 'socket hang up')
3417
- completion.stepIn()
3418
- })
3419
- completion.patience.then(testDone)
3420
- })
3240
+ const result1 = await fetch(fastifyServer)
3241
+ t.assert.ok(result1.ok)
3242
+ t.assert.strictEqual(result1.status, 200)
3243
+
3244
+ await t.assert.rejects(() => fetch(fastifyServer + '/timeout'))
3421
3245
  })
3422
3246
 
3423
- test('onTimeout should be triggered and socket _meta is set', (t, testDone) => {
3424
- t.plan(6)
3247
+ test('onTimeout should be triggered and socket _meta is set', async t => {
3248
+ t.plan(4)
3425
3249
  const fastify = Fastify({ connectionTimeout: 500 })
3426
3250
  t.after(() => { fastify.close() })
3427
3251
 
@@ -3439,28 +3263,18 @@ test('onTimeout should be triggered and socket _meta is set', (t, testDone) => {
3439
3263
  return reply
3440
3264
  })
3441
3265
 
3442
- fastify.listen({ port: 0 }, (err, address) => {
3443
- t.assert.ifError(err)
3266
+ const fastifyServer = await fastify.listen({ port: 0 })
3444
3267
 
3445
- const completion = waitForCb({ steps: 2 })
3446
- sget({
3447
- method: 'GET',
3448
- url: address
3449
- }, (err, response, body) => {
3450
- t.assert.ifError(err)
3451
- t.assert.strictEqual(response.statusCode, 200)
3452
- completion.stepIn()
3453
- })
3454
- sget({
3455
- method: 'GET',
3456
- url: `${address}/timeout`
3457
- }, (err, response, body) => {
3458
- t.assert.ok(err, Error)
3459
- t.assert.strictEqual(err.message, 'socket hang up')
3460
- completion.stepIn()
3461
- })
3462
- completion.patience.then(testDone)
3463
- })
3268
+ const result1 = await fetch(fastifyServer)
3269
+ t.assert.ok(result1.ok)
3270
+ t.assert.strictEqual(result1.status, 200)
3271
+
3272
+ try {
3273
+ await fetch(fastifyServer + '/timeout')
3274
+ t.fail('Should have thrown an error')
3275
+ } catch (err) {
3276
+ t.assert.ok(err instanceof Error)
3277
+ }
3464
3278
  })
3465
3279
 
3466
3280
  test('registering invalid hooks should throw an error', async t => {