fastify 5.3.3 → 5.5.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 (137) hide show
  1. package/.vscode/settings.json +15 -15
  2. package/LICENSE +1 -1
  3. package/README.md +2 -0
  4. package/SECURITY.md +158 -2
  5. package/build/build-validation.js +20 -1
  6. package/docs/Guides/Delay-Accepting-Requests.md +8 -5
  7. package/docs/Guides/Ecosystem.md +20 -5
  8. package/docs/Guides/Migration-Guide-V5.md +6 -10
  9. package/docs/Guides/Recommendations.md +1 -1
  10. package/docs/Reference/ContentTypeParser.md +1 -1
  11. package/docs/Reference/Errors.md +5 -3
  12. package/docs/Reference/Hooks.md +16 -20
  13. package/docs/Reference/Lifecycle.md +2 -2
  14. package/docs/Reference/Logging.md +3 -3
  15. package/docs/Reference/Middleware.md +1 -1
  16. package/docs/Reference/Reply.md +8 -8
  17. package/docs/Reference/Request.md +2 -2
  18. package/docs/Reference/Routes.md +7 -6
  19. package/docs/Reference/Server.md +341 -200
  20. package/docs/Reference/TypeScript.md +1 -3
  21. package/docs/Reference/Validation-and-Serialization.md +56 -4
  22. package/docs/Reference/Warnings.md +2 -1
  23. package/fastify.d.ts +4 -3
  24. package/fastify.js +47 -34
  25. package/lib/configValidator.js +196 -28
  26. package/lib/contentTypeParser.js +41 -48
  27. package/lib/error-handler.js +3 -3
  28. package/lib/errors.js +11 -0
  29. package/lib/handleRequest.js +13 -17
  30. package/lib/pluginOverride.js +3 -1
  31. package/lib/promise.js +23 -0
  32. package/lib/reply.js +24 -30
  33. package/lib/request.js +3 -10
  34. package/lib/route.js +37 -3
  35. package/lib/server.js +36 -35
  36. package/lib/symbols.js +1 -0
  37. package/lib/warnings.js +19 -1
  38. package/package.json +14 -10
  39. package/test/404s.test.js +226 -325
  40. package/test/allow-unsafe-regex.test.js +19 -48
  41. package/test/als.test.js +28 -40
  42. package/test/async-await.test.js +84 -128
  43. package/test/async_hooks.test.js +18 -37
  44. package/test/body-limit.test.js +90 -63
  45. package/test/buffer.test.js +22 -0
  46. package/test/build-certificate.js +1 -1
  47. package/test/case-insensitive.test.js +44 -65
  48. package/test/check.test.js +17 -21
  49. package/test/close-pipelining.test.js +24 -15
  50. package/test/constrained-routes.test.js +231 -0
  51. package/test/custom-http-server.test.js +7 -15
  52. package/test/custom-parser-async.test.js +17 -22
  53. package/test/custom-parser.0.test.js +267 -348
  54. package/test/custom-parser.1.test.js +141 -191
  55. package/test/custom-parser.2.test.js +34 -44
  56. package/test/custom-parser.3.test.js +56 -104
  57. package/test/custom-parser.4.test.js +106 -144
  58. package/test/custom-parser.5.test.js +56 -75
  59. package/test/custom-querystring-parser.test.js +51 -77
  60. package/test/decorator-namespace.test._js_ +3 -4
  61. package/test/decorator.test.js +76 -259
  62. package/test/delete.test.js +101 -110
  63. package/test/diagnostics-channel/404.test.js +7 -15
  64. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  65. package/test/diagnostics-channel/async-request.test.js +8 -16
  66. package/test/diagnostics-channel/error-request.test.js +7 -15
  67. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  68. package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
  69. package/test/diagnostics-channel/sync-request.test.js +9 -16
  70. package/test/fastify-instance.test.js +1 -1
  71. package/test/header-overflow.test.js +18 -29
  72. package/test/helper.js +139 -135
  73. package/test/hooks-async.test.js +259 -235
  74. package/test/hooks.test.js +951 -996
  75. package/test/http-methods/copy.test.js +14 -19
  76. package/test/http-methods/get.test.js +131 -143
  77. package/test/http-methods/head.test.js +53 -84
  78. package/test/http-methods/lock.test.js +31 -31
  79. package/test/http-methods/mkcalendar.test.js +45 -72
  80. package/test/http-methods/mkcol.test.js +5 -9
  81. package/test/http-methods/move.test.js +6 -10
  82. package/test/http-methods/propfind.test.js +34 -44
  83. package/test/http-methods/proppatch.test.js +23 -29
  84. package/test/http-methods/report.test.js +44 -69
  85. package/test/http-methods/search.test.js +67 -82
  86. package/test/http-methods/unlock.test.js +5 -9
  87. package/test/http2/closing.test.js +38 -20
  88. package/test/http2/secure-with-fallback.test.js +31 -28
  89. package/test/https/custom-https-server.test.js +9 -13
  90. package/test/https/https.test.js +56 -53
  91. package/test/input-validation.js +139 -150
  92. package/test/internals/errors.test.js +50 -1
  93. package/test/internals/handle-request.test.js +72 -65
  94. package/test/internals/promise.test.js +63 -0
  95. package/test/internals/reply.test.js +277 -496
  96. package/test/issue-4959.test.js +12 -3
  97. package/test/listen.4.test.js +31 -43
  98. package/test/nullable-validation.test.js +33 -46
  99. package/test/output-validation.test.js +24 -26
  100. package/test/plugin.1.test.js +40 -68
  101. package/test/plugin.2.test.js +108 -120
  102. package/test/plugin.3.test.js +50 -72
  103. package/test/plugin.4.test.js +124 -119
  104. package/test/promises.test.js +42 -63
  105. package/test/proto-poisoning.test.js +78 -97
  106. package/test/register.test.js +8 -18
  107. package/test/request-error.test.js +57 -146
  108. package/test/request-id.test.js +30 -49
  109. package/test/route-hooks.test.js +117 -101
  110. package/test/route-prefix.test.js +194 -133
  111. package/test/route-shorthand.test.js +9 -27
  112. package/test/route.1.test.js +74 -131
  113. package/test/route.8.test.js +9 -17
  114. package/test/router-options.test.js +450 -0
  115. package/test/schema-serialization.test.js +177 -154
  116. package/test/schema-special-usage.test.js +165 -132
  117. package/test/schema-validation.test.js +254 -218
  118. package/test/server.test.js +143 -5
  119. package/test/set-error-handler.test.js +58 -1
  120. package/test/skip-reply-send.test.js +64 -69
  121. package/test/stream.1.test.js +33 -50
  122. package/test/stream.4.test.js +18 -28
  123. package/test/stream.5.test.js +11 -19
  124. package/test/trust-proxy.test.js +32 -58
  125. package/test/types/errors.test-d.ts +13 -1
  126. package/test/types/fastify.test-d.ts +3 -0
  127. package/test/types/request.test-d.ts +1 -0
  128. package/test/types/type-provider.test-d.ts +55 -0
  129. package/test/url-rewriting.test.js +45 -62
  130. package/test/use-semicolon-delimiter.test.js +117 -59
  131. package/test/versioned-routes.test.js +39 -56
  132. package/types/errors.d.ts +11 -1
  133. package/types/hooks.d.ts +1 -1
  134. package/types/instance.d.ts +1 -1
  135. package/types/reply.d.ts +2 -2
  136. package/types/request.d.ts +1 -0
  137. package/.taprc +0 -7
@@ -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 http = require('node:http')
6
5
  const NotFound = require('http-errors').NotFound
7
6
  const Request = require('../../lib/request')
@@ -19,18 +18,17 @@ const {
19
18
  const fs = require('node:fs')
20
19
  const path = require('node:path')
21
20
 
22
- const agent = new http.Agent({ keepAlive: false })
23
-
24
- const doGet = function (url) {
25
- return new Promise((resolve, reject) => {
26
- sget({ method: 'GET', url, followRedirects: false, agent }, (err, response, body) => {
27
- if (err) {
28
- reject(err)
29
- } else {
30
- resolve({ response, body })
31
- }
32
- })
21
+ const doGet = async function (url) {
22
+ const result = await fetch(url, {
23
+ method: 'GET',
24
+ redirect: 'manual',
25
+ keepAlive: false
33
26
  })
27
+
28
+ return {
29
+ response: result,
30
+ body: await result.json().catch(() => undefined)
31
+ }
34
32
  }
35
33
 
36
34
  test('Once called, Reply should return an object with methods', t => {
@@ -279,65 +277,44 @@ test('within an instance', async t => {
279
277
  done()
280
278
  })
281
279
 
282
- await fastify.listen({ port: 0 })
280
+ const fastifyServer = await fastify.listen({ port: 0 })
283
281
 
284
- await t.test('custom serializer should be used', (t, done) => {
282
+ await t.test('custom serializer should be used', async t => {
285
283
  t.plan(3)
286
- sget({
287
- method: 'GET',
288
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/custom-serializer'
289
- }, (err, response, body) => {
290
- t.assert.ifError(err)
291
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
292
- t.assert.deepStrictEqual(body.toString(), 'hello=world!')
293
- done()
294
- })
284
+ const result = await fetch(fastifyServer + '/custom-serializer')
285
+ t.assert.ok(result.ok)
286
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
287
+ t.assert.deepStrictEqual(await result.text(), 'hello=world!')
295
288
  })
296
289
 
297
- await t.test('status code and content-type should be correct', (t, done) => {
298
- t.plan(4)
299
- sget({
300
- method: 'GET',
301
- url: 'http://127.0.0.1:' + fastify.server.address().port
302
- }, (err, response, body) => {
303
- t.assert.ifError(err)
304
- t.assert.strictEqual(response.statusCode, 200)
305
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
306
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
307
- done()
308
- })
290
+ await t.test('status code and content-type should be correct', async t => {
291
+ t.plan(3)
292
+ const result = await fetch(fastifyServer)
293
+ t.assert.ok(result.ok)
294
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
295
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
309
296
  })
310
297
 
311
- await t.test('auto status code should be 200', (t, done) => {
298
+ await t.test('auto status code should be 200', async t => {
312
299
  t.plan(3)
313
- sget({
314
- method: 'GET',
315
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-status-code'
316
- }, (err, response, body) => {
317
- t.assert.ifError(err)
318
- t.assert.strictEqual(response.statusCode, 200)
319
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
320
- done()
321
- })
300
+ const result = await fetch(fastifyServer + '/auto-status-code')
301
+ t.assert.ok(result.ok)
302
+ t.assert.strictEqual(result.status, 200)
303
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
322
304
  })
323
305
 
324
- await t.test('auto type should be text/plain', (t, done) => {
306
+ await t.test('auto type should be text/plain', async t => {
325
307
  t.plan(3)
326
- sget({
327
- method: 'GET',
328
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-type'
329
- }, (err, response, body) => {
330
- t.assert.ifError(err)
331
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
332
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
333
- done()
334
- })
308
+ const result = await fetch(fastifyServer + '/auto-type')
309
+ t.assert.ok(result.ok)
310
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
311
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
335
312
  })
336
313
 
337
314
  await t.test('redirect to `/` - 1', (t, done) => {
338
315
  t.plan(1)
339
316
 
340
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect', function (response) {
317
+ http.get(fastifyServer + '/redirect', function (response) {
341
318
  t.assert.strictEqual(response.statusCode, 302)
342
319
  done()
343
320
  })
@@ -346,43 +323,33 @@ test('within an instance', async t => {
346
323
  await t.test('redirect to `/` - 2', (t, done) => {
347
324
  t.plan(1)
348
325
 
349
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code', function (response) {
326
+ http.get(fastifyServer + '/redirect-code', function (response) {
350
327
  t.assert.strictEqual(response.statusCode, 301)
351
328
  done()
352
329
  })
353
330
  })
354
331
 
355
- await t.test('redirect to `/` - 3', (t, done) => {
332
+ await t.test('redirect to `/` - 3', async t => {
356
333
  t.plan(4)
357
- sget({
358
- method: 'GET',
359
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect'
360
- }, (err, response, body) => {
361
- t.assert.ifError(err)
362
- t.assert.strictEqual(response.statusCode, 200)
363
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
364
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
365
- done()
366
- })
334
+ const result = await fetch(fastifyServer + '/redirect')
335
+ t.assert.ok(result.ok)
336
+ t.assert.strictEqual(result.status, 200)
337
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
338
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
367
339
  })
368
340
 
369
- await t.test('redirect to `/` - 4', (t, done) => {
341
+ await t.test('redirect to `/` - 4', async t => {
370
342
  t.plan(4)
371
- sget({
372
- method: 'GET',
373
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code'
374
- }, (err, response, body) => {
375
- t.assert.ifError(err)
376
- t.assert.strictEqual(response.statusCode, 200)
377
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
378
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
379
- done()
380
- })
343
+ const result = await fetch(fastifyServer + '/redirect-code')
344
+ t.assert.ok(result.ok)
345
+ t.assert.strictEqual(result.status, 200)
346
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
347
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
381
348
  })
382
349
 
383
350
  await t.test('redirect to `/` - 5', (t, done) => {
384
351
  t.plan(3)
385
- const url = 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-onsend'
352
+ const url = fastifyServer + '/redirect-onsend'
386
353
  http.get(url, (response) => {
387
354
  t.assert.strictEqual(response.headers['x-onsend'], 'yes')
388
355
  t.assert.strictEqual(response.headers['content-length'], '0')
@@ -391,38 +358,28 @@ test('within an instance', async t => {
391
358
  })
392
359
  })
393
360
 
394
- await t.test('redirect to `/` - 6', (t, done) => {
361
+ await t.test('redirect to `/` - 6', async t => {
395
362
  t.plan(4)
396
- sget({
397
- method: 'GET',
398
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call'
399
- }, (err, response, body) => {
400
- t.assert.ifError(err)
401
- t.assert.strictEqual(response.statusCode, 200)
402
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
403
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
404
- done()
405
- })
363
+ const result = await fetch(fastifyServer + '/redirect-code-before-call')
364
+ t.assert.ok(result.ok)
365
+ t.assert.strictEqual(result.status, 200)
366
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
367
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
406
368
  })
407
369
 
408
- await t.test('redirect to `/` - 7', (t, done) => {
370
+ await t.test('redirect to `/` - 7', async t => {
409
371
  t.plan(4)
410
- sget({
411
- method: 'GET',
412
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite'
413
- }, (err, response, body) => {
414
- t.assert.ifError(err)
415
- t.assert.strictEqual(response.statusCode, 200)
416
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
417
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
418
- done()
419
- })
372
+ const result = await fetch(fastifyServer + '/redirect-code-before-call-overwrite')
373
+ t.assert.ok(result.ok)
374
+ t.assert.strictEqual(result.status, 200)
375
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
376
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
420
377
  })
421
378
 
422
379
  await t.test('redirect to `/` - 8', (t, done) => {
423
380
  t.plan(1)
424
381
 
425
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call', function (response) {
382
+ http.get(fastifyServer + '/redirect-code-before-call', function (response) {
426
383
  t.assert.strictEqual(response.statusCode, 307)
427
384
  done()
428
385
  })
@@ -431,7 +388,7 @@ test('within an instance', async t => {
431
388
  await t.test('redirect to `/` - 9', (t, done) => {
432
389
  t.plan(1)
433
390
 
434
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite', function (response) {
391
+ http.get(fastifyServer + '/redirect-code-before-call-overwrite', function (response) {
435
392
  t.assert.strictEqual(response.statusCode, 302)
436
393
  done()
437
394
  })
@@ -440,15 +397,15 @@ test('within an instance', async t => {
440
397
  await t.test('redirect with async function to `/` - 10', (t, done) => {
441
398
  t.plan(1)
442
399
 
443
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-async', function (response) {
400
+ http.get(fastifyServer + '/redirect-async', function (response) {
444
401
  t.assert.strictEqual(response.statusCode, 302)
445
402
  done()
446
403
  })
447
404
  })
448
405
  })
449
406
 
450
- test('buffer without content type should send a application/octet-stream and raw buffer', (t, done) => {
451
- t.plan(4)
407
+ test('buffer without content type should send a application/octet-stream and raw buffer', async t => {
408
+ t.plan(3)
452
409
 
453
410
  const fastify = Fastify()
454
411
 
@@ -456,20 +413,13 @@ test('buffer without content type should send a application/octet-stream and raw
456
413
  reply.send(Buffer.alloc(1024))
457
414
  })
458
415
 
459
- fastify.listen({ port: 0 }, err => {
460
- t.assert.ifError(err)
461
- t.after(() => fastify.close())
416
+ const fastifyServer = await fastify.listen({ port: 0 })
417
+ t.after(() => fastify.close())
462
418
 
463
- sget({
464
- method: 'GET',
465
- url: 'http://127.0.0.1:' + fastify.server.address().port
466
- }, (err, response, body) => {
467
- t.assert.ifError(err)
468
- t.assert.strictEqual(response.headers['content-type'], 'application/octet-stream')
469
- t.assert.deepStrictEqual(body, Buffer.alloc(1024))
470
- done()
471
- })
472
- })
419
+ const result = await fetch(fastifyServer)
420
+ t.assert.ok(result.ok)
421
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/octet-stream')
422
+ t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), Buffer.alloc(1024))
473
423
  })
474
424
 
475
425
  test('Uint8Array without content type should send a application/octet-stream and raw buffer', (t, done) => {
@@ -545,8 +495,8 @@ test('TypedArray with content type should not send application/octet-stream', (t
545
495
  })
546
496
  })
547
497
  })
548
- test('buffer with content type should not send application/octet-stream', (t, done) => {
549
- t.plan(4)
498
+ test('buffer with content type should not send application/octet-stream', async t => {
499
+ t.plan(3)
550
500
 
551
501
  const fastify = Fastify()
552
502
 
@@ -555,24 +505,17 @@ test('buffer with content type should not send application/octet-stream', (t, do
555
505
  reply.send(Buffer.alloc(1024))
556
506
  })
557
507
 
558
- fastify.listen({ port: 0 }, err => {
559
- t.assert.ifError(err)
560
- t.after(() => fastify.close())
508
+ const fastifyServer = await fastify.listen({ port: 0 })
509
+ t.after(() => fastify.close())
561
510
 
562
- sget({
563
- method: 'GET',
564
- url: 'http://127.0.0.1:' + fastify.server.address().port
565
- }, (err, response, body) => {
566
- t.assert.ifError(err)
567
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
568
- t.assert.deepStrictEqual(body, Buffer.alloc(1024))
569
- done()
570
- })
571
- })
511
+ const result = await fetch(fastifyServer)
512
+ t.assert.ok(result.ok)
513
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
514
+ t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), Buffer.alloc(1024))
572
515
  })
573
516
 
574
- test('stream with content type should not send application/octet-stream', (t, done) => {
575
- t.plan(4)
517
+ test('stream with content type should not send application/octet-stream', async t => {
518
+ t.plan(3)
576
519
 
577
520
  const fastify = Fastify()
578
521
 
@@ -584,23 +527,17 @@ test('stream with content type should not send application/octet-stream', (t, do
584
527
  reply.header('Content-Type', 'text/plain').send(stream)
585
528
  })
586
529
 
587
- fastify.listen({ port: 0 }, err => {
588
- t.assert.ifError(err)
589
- t.after(() => fastify.close())
590
- sget({
591
- method: 'GET',
592
- url: 'http://127.0.0.1:' + fastify.server.address().port
593
- }, (err, response, body) => {
594
- t.assert.ifError(err)
595
- t.assert.strictEqual(response.headers['content-type'], 'text/plain')
596
- t.assert.deepStrictEqual(body, buf)
597
- done()
598
- })
599
- })
530
+ const fastifyServer = await fastify.listen({ port: 0 })
531
+ t.after(() => fastify.close())
532
+
533
+ const result = await fetch(fastifyServer)
534
+ t.assert.ok(result.ok)
535
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
536
+ t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
600
537
  })
601
538
 
602
- test('stream without content type should not send application/octet-stream', (t, done) => {
603
- t.plan(4)
539
+ test('stream without content type should not send application/octet-stream', async t => {
540
+ t.plan(3)
604
541
 
605
542
  const fastify = Fastify()
606
543
 
@@ -611,23 +548,17 @@ test('stream without content type should not send application/octet-stream', (t,
611
548
  reply.send(stream)
612
549
  })
613
550
 
614
- fastify.listen({ port: 0 }, err => {
615
- t.assert.ifError(err)
616
- t.after(() => fastify.close())
617
- sget({
618
- method: 'GET',
619
- url: 'http://127.0.0.1:' + fastify.server.address().port
620
- }, (err, response, body) => {
621
- t.assert.ifError(err)
622
- t.assert.strictEqual(response.headers['content-type'], undefined)
623
- t.assert.deepStrictEqual(body, buf)
624
- done()
625
- })
626
- })
551
+ const fastifyServer = await fastify.listen({ port: 0 })
552
+ t.after(() => fastify.close())
553
+
554
+ const result = await fetch(fastifyServer)
555
+ t.assert.ok(result.ok)
556
+ t.assert.strictEqual(result.headers.get('content-type'), null)
557
+ t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
627
558
  })
628
559
 
629
- test('stream using reply.raw.writeHead should return customize headers', (t, done) => {
630
- t.plan(6)
560
+ test('stream using reply.raw.writeHead should return customize headers', async t => {
561
+ t.plan(5)
631
562
 
632
563
  const fastify = Fastify()
633
564
  const fs = require('node:fs')
@@ -647,24 +578,18 @@ test('stream using reply.raw.writeHead should return customize headers', (t, don
647
578
  reply.send(stream)
648
579
  })
649
580
 
650
- fastify.listen({ port: 0 }, err => {
651
- t.assert.ifError(err)
652
- t.after(() => fastify.close())
653
- sget({
654
- method: 'GET',
655
- url: 'http://127.0.0.1:' + fastify.server.address().port
656
- }, (err, response, body) => {
657
- t.assert.ifError(err)
658
- t.assert.strictEqual(response.headers.location, '/')
659
- t.assert.strictEqual(response.headers['Content-Type'], undefined)
660
- t.assert.deepStrictEqual(body, buf)
661
- done()
662
- })
663
- })
581
+ const fastifyServer = await fastify.listen({ port: 0 })
582
+ t.after(() => fastify.close())
583
+
584
+ const result = await fetch(fastifyServer)
585
+ t.assert.ok(result.ok)
586
+ t.assert.strictEqual(result.headers.get('location'), '/')
587
+ t.assert.strictEqual(result.headers.get('content-type'), null)
588
+ t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
664
589
  })
665
590
 
666
- test('plain string without content type should send a text/plain', (t, done) => {
667
- t.plan(4)
591
+ test('plain string without content type should send a text/plain', async t => {
592
+ t.plan(3)
668
593
 
669
594
  const fastify = Fastify()
670
595
 
@@ -672,24 +597,17 @@ test('plain string without content type should send a text/plain', (t, done) =>
672
597
  reply.send('hello world!')
673
598
  })
674
599
 
675
- fastify.listen({ port: 0 }, err => {
676
- t.assert.ifError(err)
677
- t.after(() => fastify.close())
600
+ const fastifyServer = await fastify.listen({ port: 0 })
601
+ t.after(() => fastify.close())
678
602
 
679
- sget({
680
- method: 'GET',
681
- url: 'http://127.0.0.1:' + fastify.server.address().port
682
- }, (err, response, body) => {
683
- t.assert.ifError(err)
684
- t.assert.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
685
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
686
- done()
687
- })
688
- })
603
+ const result = await fetch(fastifyServer)
604
+ t.assert.ok(result.ok)
605
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/plain; charset=utf-8')
606
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
689
607
  })
690
608
 
691
- test('plain string with content type should be sent unmodified', (t, done) => {
692
- t.plan(4)
609
+ test('plain string with content type should be sent unmodified', async t => {
610
+ t.plan(3)
693
611
 
694
612
  const fastify = Fastify()
695
613
 
@@ -697,24 +615,17 @@ test('plain string with content type should be sent unmodified', (t, done) => {
697
615
  reply.type('text/css').send('hello world!')
698
616
  })
699
617
 
700
- fastify.listen({ port: 0 }, err => {
701
- t.assert.ifError(err)
702
- t.after(() => fastify.close())
618
+ const fastifyServer = await fastify.listen({ port: 0 })
619
+ t.after(() => fastify.close())
703
620
 
704
- sget({
705
- method: 'GET',
706
- url: 'http://127.0.0.1:' + fastify.server.address().port
707
- }, (err, response, body) => {
708
- t.assert.ifError(err)
709
- t.assert.strictEqual(response.headers['content-type'], 'text/css')
710
- t.assert.deepStrictEqual(body.toString(), 'hello world!')
711
- done()
712
- })
713
- })
621
+ const result = await fetch(fastifyServer)
622
+ t.assert.ok(result.ok)
623
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/css')
624
+ t.assert.deepStrictEqual(await result.text(), 'hello world!')
714
625
  })
715
626
 
716
- test('plain string with content type and custom serializer should be serialized', (t, done) => {
717
- t.plan(4)
627
+ test('plain string with content type and custom serializer should be serialized', async t => {
628
+ t.plan(3)
718
629
 
719
630
  const fastify = Fastify()
720
631
 
@@ -725,24 +636,17 @@ test('plain string with content type and custom serializer should be serialized'
725
636
  .send('hello world!')
726
637
  })
727
638
 
728
- fastify.listen({ port: 0 }, err => {
729
- t.assert.ifError(err)
730
- t.after(() => fastify.close())
639
+ const fastifyServer = await fastify.listen({ port: 0 })
640
+ t.after(() => fastify.close())
731
641
 
732
- sget({
733
- method: 'GET',
734
- url: 'http://127.0.0.1:' + fastify.server.address().port
735
- }, (err, response, body) => {
736
- t.assert.ifError(err)
737
- t.assert.strictEqual(response.headers['content-type'], 'text/css')
738
- t.assert.deepStrictEqual(body.toString(), 'serialized')
739
- done()
740
- })
741
- })
642
+ const result = await fetch(fastifyServer)
643
+ t.assert.ok(result.ok)
644
+ t.assert.strictEqual(result.headers.get('content-type'), 'text/css')
645
+ t.assert.deepStrictEqual(await result.text(), 'serialized')
742
646
  })
743
647
 
744
- test('plain string with content type application/json should NOT be serialized as json', (t, done) => {
745
- t.plan(4)
648
+ test('plain string with content type application/json should NOT be serialized as json', async t => {
649
+ t.plan(3)
746
650
 
747
651
  const fastify = Fastify()
748
652
 
@@ -750,24 +654,17 @@ test('plain string with content type application/json should NOT be serialized a
750
654
  reply.type('application/json').send('{"key": "hello world!"}')
751
655
  })
752
656
 
753
- fastify.listen({ port: 0 }, err => {
754
- t.assert.ifError(err)
755
- t.after(() => fastify.close())
657
+ const fastifyServer = await fastify.listen({ port: 0 })
658
+ t.after(() => fastify.close())
756
659
 
757
- sget({
758
- method: 'GET',
759
- url: 'http://127.0.0.1:' + fastify.server.address().port
760
- }, (err, response, body) => {
761
- t.assert.ifError(err)
762
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
763
- t.assert.deepStrictEqual(body.toString(), '{"key": "hello world!"}')
764
- done()
765
- })
766
- })
660
+ const result = await fetch(fastifyServer)
661
+ t.assert.ok(result.ok)
662
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
663
+ t.assert.deepStrictEqual(await result.text(), '{"key": "hello world!"}')
767
664
  })
768
665
 
769
666
  test('plain string with custom json content type should NOT be serialized as json', async t => {
770
- t.plan(12)
667
+ t.plan(18)
771
668
 
772
669
  const fastify = Fastify()
773
670
 
@@ -806,27 +703,18 @@ test('plain string with custom json content type should NOT be serialized as jso
806
703
  })
807
704
  })
808
705
 
809
- await fastify.listen({ port: 0 })
706
+ const fastifyServer = await fastify.listen({ port: 0 })
810
707
 
811
- await Promise.all(Object.keys(customSamples).map(path => {
812
- return new Promise((resolve, reject) => {
813
- sget({
814
- method: 'GET',
815
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/' + path
816
- }, (err, response, body) => {
817
- if (err) {
818
- reject(err)
819
- }
820
- t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
821
- t.assert.deepStrictEqual(body.toString(), customSamples[path].sample)
822
- resolve()
823
- })
824
- })
708
+ await Promise.all(Object.keys(customSamples).map(async path => {
709
+ const result = await fetch(fastifyServer + '/' + path)
710
+ t.assert.ok(result.ok)
711
+ t.assert.strictEqual(result.headers.get('content-type'), customSamples[path].mimeType + '; charset=utf-8')
712
+ t.assert.deepStrictEqual(await result.text(), customSamples[path].sample)
825
713
  }))
826
714
  })
827
715
 
828
- test('non-string with content type application/json SHOULD be serialized as json', (t, done) => {
829
- t.plan(4)
716
+ test('non-string with content type application/json SHOULD be serialized as json', async t => {
717
+ t.plan(3)
830
718
 
831
719
  const fastify = Fastify()
832
720
 
@@ -834,24 +722,17 @@ test('non-string with content type application/json SHOULD be serialized as json
834
722
  reply.type('application/json').send({ key: 'hello world!' })
835
723
  })
836
724
 
837
- fastify.listen({ port: 0 }, err => {
838
- t.assert.ifError(err)
839
- t.after(() => fastify.close())
725
+ const fastifyServer = await fastify.listen({ port: 0 })
726
+ t.after(() => fastify.close())
840
727
 
841
- sget({
842
- method: 'GET',
843
- url: 'http://127.0.0.1:' + fastify.server.address().port
844
- }, (err, response, body) => {
845
- t.assert.ifError(err)
846
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
847
- t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
848
- done()
849
- })
850
- })
728
+ const result = await fetch(fastifyServer)
729
+ t.assert.ok(result.ok)
730
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
731
+ t.assert.deepStrictEqual(await result.text(), JSON.stringify({ key: 'hello world!' }))
851
732
  })
852
733
 
853
- test('non-string with custom json\'s content-type SHOULD be serialized as json', (t, done) => {
854
- t.plan(4)
734
+ test('non-string with custom json\'s content-type SHOULD be serialized as json', async t => {
735
+ t.plan(3)
855
736
 
856
737
  const fastify = Fastify()
857
738
 
@@ -859,24 +740,17 @@ test('non-string with custom json\'s content-type SHOULD be serialized as json',
859
740
  reply.type('application/json; version=2; ').send({ key: 'hello world!' })
860
741
  })
861
742
 
862
- fastify.listen({ port: 0 }, err => {
863
- t.assert.ifError(err)
864
- t.after(() => fastify.close())
743
+ const fastifyServer = await fastify.listen({ port: 0 })
744
+ t.after(() => fastify.close())
865
745
 
866
- sget({
867
- method: 'GET',
868
- url: 'http://127.0.0.1:' + fastify.server.address().port
869
- }, (err, response, body) => {
870
- t.assert.ifError(err)
871
- t.assert.strictEqual(response.headers['content-type'], 'application/json; version=2; charset=utf-8')
872
- t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
873
- done()
874
- })
875
- })
746
+ const result = await fetch(fastifyServer)
747
+ t.assert.ok(result.ok)
748
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; version=2; charset=utf-8')
749
+ t.assert.deepStrictEqual(await result.text(), JSON.stringify({ key: 'hello world!' }))
876
750
  })
877
751
 
878
752
  test('non-string with custom json content type SHOULD be serialized as json', async t => {
879
- t.plan(10)
753
+ t.plan(15)
880
754
 
881
755
  const fastify = Fastify()
882
756
  t.after(() => fastify.close())
@@ -910,22 +784,13 @@ test('non-string with custom json content type SHOULD be serialized as json', as
910
784
  })
911
785
  })
912
786
 
913
- await fastify.listen({ port: 0 })
787
+ const fastifyServer = await fastify.listen({ port: 0 })
914
788
 
915
- await Promise.all(Object.keys(customSamples).map(path => {
916
- return new Promise((resolve, reject) => {
917
- sget({
918
- method: 'GET',
919
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/' + path
920
- }, (err, response, body) => {
921
- if (err) {
922
- reject(err)
923
- }
924
- t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
925
- t.assert.deepStrictEqual(body.toString(), JSON.stringify(customSamples[path].sample))
926
- resolve()
927
- })
928
- })
789
+ await Promise.all(Object.keys(customSamples).map(async path => {
790
+ const result = await fetch(fastifyServer + '/' + path)
791
+ t.assert.ok(result.ok)
792
+ t.assert.strictEqual(result.headers.get('content-type'), customSamples[path].mimeType + '; charset=utf-8')
793
+ t.assert.deepStrictEqual(await result.text(), JSON.stringify(customSamples[path].sample))
929
794
  }))
930
795
  })
931
796
 
@@ -963,8 +828,8 @@ test('error object with a content type that is not application/json should work'
963
828
  }
964
829
  })
965
830
 
966
- test('undefined payload should be sent as-is', (t, done) => {
967
- t.plan(6)
831
+ test('undefined payload should be sent as-is', async t => {
832
+ t.plan(5)
968
833
 
969
834
  const fastify = Fastify()
970
835
 
@@ -977,25 +842,19 @@ test('undefined payload should be sent as-is', (t, done) => {
977
842
  reply.code(204).send()
978
843
  })
979
844
 
980
- fastify.listen({ port: 0 }, err => {
981
- t.assert.ifError(err)
982
- t.after(() => fastify.close())
845
+ const fastifyServer = await fastify.listen({ port: 0 })
846
+ t.after(() => fastify.close())
983
847
 
984
- sget({
985
- method: 'GET',
986
- url: `http://127.0.0.1:${fastify.server.address().port}`
987
- }, (err, response, body) => {
988
- t.assert.ifError(err)
989
- t.assert.strictEqual(response.headers['content-type'], undefined)
990
- t.assert.strictEqual(response.headers['content-length'], undefined)
991
- t.assert.strictEqual(body.length, 0)
992
- done()
993
- })
994
- })
848
+ const result = await fetch(fastifyServer)
849
+ t.assert.ok(result.ok)
850
+ t.assert.strictEqual(result.headers.get('content-type'), null)
851
+ t.assert.strictEqual(result.headers.get('content-length'), null)
852
+ const body = await result.text()
853
+ t.assert.strictEqual(body.length, 0)
995
854
  })
996
855
 
997
856
  test('for HEAD method, no body should be sent but content-length should be', async t => {
998
- t.plan(8)
857
+ t.plan(10)
999
858
 
1000
859
  const fastify = Fastify()
1001
860
  t.after(() => fastify.close())
@@ -1024,37 +883,23 @@ test('for HEAD method, no body should be sent but content-length should be', asy
1024
883
  reply.code(200).send(null)
1025
884
  })
1026
885
 
1027
- await fastify.listen({ port: 0 })
886
+ const fastifyServer = await fastify.listen({ port: 0 })
1028
887
 
1029
- const promise1 = new Promise((resolve, reject) => {
1030
- sget({
1031
- method: 'HEAD',
1032
- url: `http://127.0.0.1:${fastify.server.address().port}`
1033
- }, (err, response, body) => {
1034
- if (err) {
1035
- reject(err)
1036
- }
1037
- t.assert.strictEqual(response.headers['content-type'], contentType)
1038
- t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
1039
- t.assert.strictEqual(body.length, 0)
1040
- resolve()
1041
- })
1042
- })
888
+ const promise1 = (async () => {
889
+ const result = await fetch(fastifyServer, { method: 'HEAD' })
890
+ t.assert.ok(result.ok)
891
+ t.assert.strictEqual(result.headers.get('content-type'), contentType)
892
+ t.assert.strictEqual(result.headers.get('content-length'), bodySize.toString())
893
+ t.assert.strictEqual((await result.text()).length, 0)
894
+ })()
1043
895
 
1044
- const promise2 = new Promise((resolve, reject) => {
1045
- sget({
1046
- method: 'HEAD',
1047
- url: `http://127.0.0.1:${fastify.server.address().port}/with/null`
1048
- }, (err, response, body) => {
1049
- if (err) {
1050
- reject(err)
1051
- }
1052
- t.assert.strictEqual(response.headers['content-type'], contentType)
1053
- t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
1054
- t.assert.strictEqual(body.length, 0)
1055
- resolve()
1056
- })
1057
- })
896
+ const promise2 = (async () => {
897
+ const result = await fetch(fastifyServer + '/with/null', { method: 'HEAD' })
898
+ t.assert.ok(result.ok)
899
+ t.assert.strictEqual(result.headers.get('content-type'), contentType)
900
+ t.assert.strictEqual(result.headers.get('content-length'), bodySize.toString())
901
+ t.assert.strictEqual((await result.text()).length, 0)
902
+ })()
1058
903
 
1059
904
  await Promise.all([promise1, promise2])
1060
905
  })
@@ -1081,51 +926,35 @@ test('reply.send(new NotFound()) should not invoke the 404 handler', async t =>
1081
926
  done()
1082
927
  }, { prefix: '/prefixed' })
1083
928
 
1084
- await fastify.listen({ port: 0 })
929
+ const fastifyServer = await fastify.listen({ port: 0 })
1085
930
 
1086
- const promise1 = new Promise((resolve, reject) => {
1087
- sget({
1088
- method: 'GET',
1089
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/not-found'
1090
- }, (err, response, body) => {
1091
- if (err) {
1092
- reject(err)
1093
- }
1094
- t.assert.strictEqual(response.statusCode, 404)
1095
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
1096
- t.assert.deepStrictEqual(JSON.parse(body.toString()), {
1097
- statusCode: 404,
1098
- error: 'Not Found',
1099
- message: 'Not Found'
1100
- })
1101
- resolve()
931
+ const promise1 = (async () => {
932
+ const result = await fetch(`${fastifyServer}/not-found`)
933
+ t.assert.strictEqual(result.status, 404)
934
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
935
+ t.assert.deepStrictEqual(JSON.parse(await result.text()), {
936
+ statusCode: 404,
937
+ error: 'Not Found',
938
+ message: 'Not Found'
1102
939
  })
1103
- })
940
+ })()
1104
941
 
1105
- const promise2 = new Promise((resolve, reject) => {
1106
- sget({
1107
- method: 'GET',
1108
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/prefixed/not-found'
1109
- }, (err, response, body) => {
1110
- if (err) {
1111
- reject(err)
1112
- }
1113
- t.assert.strictEqual(response.statusCode, 404)
1114
- t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
1115
- t.assert.deepStrictEqual(JSON.parse(body.toString()), {
1116
- statusCode: 404,
1117
- error: 'Not Found',
1118
- message: 'Not Found'
1119
- })
1120
- resolve()
942
+ const promise2 = (async () => {
943
+ const result = await fetch(`${fastifyServer}/prefixed/not-found`)
944
+ t.assert.strictEqual(result.status, 404)
945
+ t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
946
+ t.assert.deepStrictEqual(JSON.parse(await result.text()), {
947
+ statusCode: 404,
948
+ error: 'Not Found',
949
+ message: 'Not Found'
1121
950
  })
1122
- })
951
+ })()
1123
952
 
1124
953
  await Promise.all([promise1, promise2])
1125
954
  })
1126
955
 
1127
- test('reply can set multiple instances of same header', (t, done) => {
1128
- t.plan(4)
956
+ test('reply can set multiple instances of same header', async t => {
957
+ t.plan(3)
1129
958
 
1130
959
  const fastify = require('../../')()
1131
960
 
@@ -1136,24 +965,17 @@ test('reply can set multiple instances of same header', (t, done) => {
1136
965
  .send({})
1137
966
  })
1138
967
 
1139
- fastify.listen({ port: 0 }, err => {
1140
- t.assert.ifError(err)
1141
- t.after(() => fastify.close())
968
+ const fastifyServer = await fastify.listen({ port: 0 })
969
+ t.after(() => fastify.close())
1142
970
 
1143
- sget({
1144
- method: 'GET',
1145
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1146
- }, (err, response, body) => {
1147
- t.assert.ifError(err)
1148
- t.assert.ok(response.headers['set-cookie'])
1149
- t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two'])
1150
- done()
1151
- })
1152
- })
971
+ const result = await fetch(`${fastifyServer}/headers`)
972
+ t.assert.ok(result.ok)
973
+ t.assert.ok(result.headers.get('set-cookie'))
974
+ t.assert.deepStrictEqual(result.headers.getSetCookie(), ['one', 'two'])
1153
975
  })
1154
976
 
1155
- test('reply.hasHeader returns correct values', (t, done) => {
1156
- t.plan(3)
977
+ test('reply.hasHeader returns correct values', async t => {
978
+ t.plan(2)
1157
979
 
1158
980
  const fastify = require('../../')()
1159
981
 
@@ -1164,20 +986,14 @@ test('reply.hasHeader returns correct values', (t, done) => {
1164
986
  reply.send()
1165
987
  })
1166
988
 
1167
- fastify.listen({ port: 0 }, err => {
1168
- t.assert.ifError(err)
1169
- t.after(() => fastify.close())
1170
- sget({
1171
- method: 'GET',
1172
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1173
- }, () => {
1174
- done()
1175
- })
1176
- })
989
+ const fastifyServer = await fastify.listen({ port: 0 })
990
+ t.after(() => fastify.close())
991
+
992
+ await fetch(`${fastifyServer}/headers`)
1177
993
  })
1178
994
 
1179
- test('reply.getHeader returns correct values', (t, done) => {
1180
- t.plan(5)
995
+ test('reply.getHeader returns correct values', async t => {
996
+ t.plan(4)
1181
997
 
1182
998
  const fastify = require('../../')()
1183
999
 
@@ -1198,16 +1014,10 @@ test('reply.getHeader returns correct values', (t, done) => {
1198
1014
  reply.send()
1199
1015
  })
1200
1016
 
1201
- fastify.listen({ port: 0 }, err => {
1202
- t.assert.ifError(err)
1203
- t.after(() => fastify.close())
1204
- sget({
1205
- method: 'GET',
1206
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1207
- }, () => {
1208
- done()
1209
- })
1210
- })
1017
+ const fastifyServer = await fastify.listen({ port: 0 })
1018
+ t.after(() => fastify.close())
1019
+
1020
+ await fetch(`${fastifyServer}/headers`)
1211
1021
  })
1212
1022
 
1213
1023
  test('reply.getHeader returns raw header if there is not in the reply headers', (t) => {
@@ -1254,8 +1064,8 @@ test('reply.getHeaders returns correct values', (t, done) => {
1254
1064
  })
1255
1065
  })
1256
1066
 
1257
- test('reply.removeHeader can remove the value', (t, done) => {
1258
- t.plan(4)
1067
+ test('reply.removeHeader can remove the value', async t => {
1068
+ t.plan(3)
1259
1069
 
1260
1070
  const fastify = require('../../')()
1261
1071
 
@@ -1271,20 +1081,13 @@ test('reply.removeHeader can remove the value', (t, done) => {
1271
1081
  reply.send()
1272
1082
  })
1273
1083
 
1274
- fastify.listen({ port: 0 }, err => {
1275
- t.assert.ifError(err)
1276
- t.after(() => fastify.close())
1277
- sget({
1278
- method: 'GET',
1279
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1280
- }, () => {
1281
- done()
1282
- })
1283
- })
1084
+ const fastifyServer = await fastify.listen({ port: 0 })
1085
+
1086
+ await fetch(`${fastifyServer}/headers`)
1284
1087
  })
1285
1088
 
1286
- test('reply.header can reset the value', (t, done) => {
1287
- t.plan(2)
1089
+ test('reply.header can reset the value', async t => {
1090
+ t.plan(1)
1288
1091
 
1289
1092
  const fastify = require('../../')()
1290
1093
 
@@ -1298,21 +1101,14 @@ test('reply.header can reset the value', (t, done) => {
1298
1101
  reply.send()
1299
1102
  })
1300
1103
 
1301
- fastify.listen({ port: 0 }, err => {
1302
- t.assert.ifError(err)
1303
- t.after(() => fastify.close())
1304
- sget({
1305
- method: 'GET',
1306
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1307
- }, () => {
1308
- done()
1309
- })
1310
- })
1104
+ const fastifyServer = await fastify.listen({ port: 0 })
1105
+
1106
+ await fetch(`${fastifyServer}/headers`)
1311
1107
  })
1312
1108
 
1313
1109
  // https://github.com/fastify/fastify/issues/3030
1314
- test('reply.hasHeader computes raw and fastify headers', (t, done) => {
1315
- t.plan(3)
1110
+ test('reply.hasHeader computes raw and fastify headers', async t => {
1111
+ t.plan(2)
1316
1112
 
1317
1113
  const fastify = require('../../')()
1318
1114
 
@@ -1327,16 +1123,10 @@ test('reply.hasHeader computes raw and fastify headers', (t, done) => {
1327
1123
  reply.send()
1328
1124
  })
1329
1125
 
1330
- fastify.listen({ port: 0 }, err => {
1331
- t.assert.ifError(err)
1332
- t.after(() => fastify.close())
1333
- sget({
1334
- method: 'GET',
1335
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1336
- }, () => {
1337
- done()
1338
- })
1339
- })
1126
+ const fastifyServer = await fastify.listen({ port: 0 })
1127
+ t.after(() => fastify.close())
1128
+
1129
+ await fetch(`${fastifyServer}/headers`)
1340
1130
  })
1341
1131
 
1342
1132
  test('Reply should handle JSON content type with a charset', async t => {
@@ -1485,7 +1275,7 @@ test('.statusCode is getter and setter', (t, done) => {
1485
1275
  })
1486
1276
 
1487
1277
  test('reply.header setting multiple cookies as multiple Set-Cookie headers', async t => {
1488
- t.plan(4)
1278
+ t.plan(5)
1489
1279
 
1490
1280
  const fastify = require('../../')()
1491
1281
  t.after(() => fastify.close())
@@ -1499,21 +1289,12 @@ test('reply.header setting multiple cookies as multiple Set-Cookie headers', asy
1499
1289
  .send({})
1500
1290
  })
1501
1291
 
1502
- await fastify.listen({ port: 0 })
1292
+ const fastifyServer = await fastify.listen({ port: 0 })
1503
1293
 
1504
- await new Promise((resolve, reject) => {
1505
- sget({
1506
- method: 'GET',
1507
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1508
- }, (err, response, body) => {
1509
- if (err) {
1510
- reject(err)
1511
- }
1512
- t.assert.ok(response.headers['set-cookie'])
1513
- t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
1514
- resolve()
1515
- })
1516
- })
1294
+ const result = await fetch(`${fastifyServer}/headers`)
1295
+ t.assert.ok(result.ok)
1296
+ t.assert.ok(result.headers.get('set-cookie'))
1297
+ t.assert.deepStrictEqual(result.headers.getSetCookie(), ['one', 'two', 'three', 'four', 'five', 'six'])
1517
1298
 
1518
1299
  const response = await fastify.inject('/headers')
1519
1300
  t.assert.ok(response.headers['set-cookie'])
@@ -1937,12 +1718,12 @@ test('redirect to an invalid URL should not crash the server', async t => {
1937
1718
  }
1938
1719
  })
1939
1720
 
1940
- await fastify.listen({ port: 0 })
1721
+ const fastifyServer = await fastify.listen({ port: 0 })
1941
1722
 
1942
1723
  {
1943
- const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=1`)
1944
- t.assert.strictEqual(response.statusCode, 500)
1945
- t.assert.deepStrictEqual(JSON.parse(body), {
1724
+ const { response, body } = await doGet(`${fastifyServer}/redirect?useCase=1`)
1725
+ t.assert.strictEqual(response.status, 500)
1726
+ t.assert.deepStrictEqual(body, {
1946
1727
  statusCode: 500,
1947
1728
  code: 'ERR_INVALID_CHAR',
1948
1729
  error: 'Internal Server Error',
@@ -1950,15 +1731,15 @@ test('redirect to an invalid URL should not crash the server', async t => {
1950
1731
  })
1951
1732
  }
1952
1733
  {
1953
- const { response } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=2`)
1954
- t.assert.strictEqual(response.statusCode, 302)
1955
- t.assert.strictEqual(response.headers.location, '/?key=a%E2%80%99b')
1734
+ const { response } = await doGet(`${fastifyServer}/redirect?useCase=2`)
1735
+ t.assert.strictEqual(response.status, 302)
1736
+ t.assert.strictEqual(response.headers.get('location'), '/?key=a%E2%80%99b')
1956
1737
  }
1957
1738
 
1958
1739
  {
1959
- const { response } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=3`)
1960
- t.assert.strictEqual(response.statusCode, 302)
1961
- t.assert.strictEqual(response.headers.location, '/?key=ab')
1740
+ const { response } = await doGet(`${fastifyServer}/redirect?useCase=3`)
1741
+ t.assert.strictEqual(response.status, 302)
1742
+ t.assert.strictEqual(response.headers.get('location'), '/?key=ab')
1962
1743
  }
1963
1744
 
1964
1745
  await fastify.close()
@@ -1983,11 +1764,11 @@ test('invalid response headers should not crash the server', async t => {
1983
1764
  }
1984
1765
  })
1985
1766
 
1986
- await fastify.listen({ port: 0 })
1767
+ const fastifyServer = await fastify.listen({ port: 0 })
1987
1768
 
1988
- const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
1989
- t.assert.strictEqual(response.statusCode, 500)
1990
- t.assert.deepStrictEqual(JSON.parse(body), {
1769
+ const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
1770
+ t.assert.strictEqual(response.status, 500)
1771
+ t.assert.deepStrictEqual(body, {
1991
1772
  statusCode: 500,
1992
1773
  code: 'ERR_INVALID_CHAR',
1993
1774
  error: 'Internal Server Error',
@@ -2012,11 +1793,11 @@ test('invalid response headers when sending back an error', async t => {
2012
1793
  }
2013
1794
  })
2014
1795
 
2015
- await fastify.listen({ port: 0 })
1796
+ const fastifyServer = await fastify.listen({ port: 0 })
2016
1797
 
2017
- const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
2018
- t.assert.strictEqual(response.statusCode, 500)
2019
- t.assert.deepStrictEqual(JSON.parse(body), {
1798
+ const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
1799
+ t.assert.strictEqual(response.status, 500)
1800
+ t.assert.deepStrictEqual(body, {
2020
1801
  statusCode: 500,
2021
1802
  code: 'ERR_INVALID_CHAR',
2022
1803
  error: 'Internal Server Error',
@@ -2046,11 +1827,11 @@ test('invalid response headers and custom error handler', async t => {
2046
1827
  reply.status(500).send({ ops: true })
2047
1828
  })
2048
1829
 
2049
- await fastify.listen({ port: 0 })
1830
+ const fastifyServer = await fastify.listen({ port: 0 })
2050
1831
 
2051
- const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
2052
- t.assert.strictEqual(response.statusCode, 500)
2053
- t.assert.deepStrictEqual(JSON.parse(body), {
1832
+ const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
1833
+ t.assert.strictEqual(response.status, 500)
1834
+ t.assert.deepStrictEqual(body, {
2054
1835
  statusCode: 500,
2055
1836
  code: 'ERR_INVALID_CHAR',
2056
1837
  error: 'Internal Server Error',