fastify 3.27.4 → 4.0.0-alpha.3

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 (168) hide show
  1. package/.taprc +3 -0
  2. package/README.md +7 -7
  3. package/build/build-error-serializer.js +27 -0
  4. package/build/build-validation.js +47 -35
  5. package/docs/Guides/Database.md +320 -0
  6. package/docs/Guides/Ecosystem.md +9 -0
  7. package/docs/Guides/Getting-Started.md +7 -7
  8. package/docs/Guides/Plugins-Guide.md +1 -1
  9. package/docs/Guides/Serverless.md +3 -3
  10. package/docs/Guides/Testing.md +2 -2
  11. package/docs/Migration-Guide-V4.md +12 -0
  12. package/docs/Reference/ContentTypeParser.md +4 -0
  13. package/docs/Reference/Decorators.md +2 -2
  14. package/docs/Reference/Encapsulation.md +2 -2
  15. package/docs/Reference/Errors.md +51 -6
  16. package/docs/Reference/HTTP2.md +3 -3
  17. package/docs/Reference/Hooks.md +4 -7
  18. package/docs/Reference/LTS.md +5 -4
  19. package/docs/Reference/Plugins.md +3 -3
  20. package/docs/Reference/Reply.md +73 -22
  21. package/docs/Reference/Request.md +1 -3
  22. package/docs/Reference/Routes.md +22 -15
  23. package/docs/Reference/Server.md +69 -119
  24. package/docs/Reference/TypeScript.md +20 -22
  25. package/docs/Reference/Validation-and-Serialization.md +30 -55
  26. package/docs/Type-Providers.md +257 -0
  27. package/examples/asyncawait.js +1 -1
  28. package/examples/benchmark/hooks-benchmark-async-await.js +1 -1
  29. package/examples/benchmark/hooks-benchmark.js +1 -1
  30. package/examples/benchmark/simple.js +1 -1
  31. package/examples/hooks.js +2 -2
  32. package/examples/http2.js +1 -1
  33. package/examples/https.js +1 -1
  34. package/examples/parser.js +13 -3
  35. package/examples/route-prefix.js +1 -1
  36. package/examples/shared-schema.js +1 -1
  37. package/examples/simple-stream.js +18 -0
  38. package/examples/simple.js +1 -1
  39. package/examples/simple.mjs +1 -1
  40. package/examples/typescript-server.ts +1 -1
  41. package/examples/use-plugin.js +1 -1
  42. package/fastify.d.ts +34 -22
  43. package/fastify.js +40 -36
  44. package/lib/configValidator.js +902 -1023
  45. package/lib/contentTypeParser.js +6 -16
  46. package/lib/context.js +36 -10
  47. package/lib/decorate.js +3 -1
  48. package/lib/error-handler.js +158 -0
  49. package/lib/error-serializer.js +257 -0
  50. package/lib/errors.js +51 -9
  51. package/lib/fourOhFour.js +31 -20
  52. package/lib/handleRequest.js +10 -13
  53. package/lib/hooks.js +14 -9
  54. package/lib/pluginOverride.js +0 -3
  55. package/lib/pluginUtils.js +3 -2
  56. package/lib/reply.js +121 -175
  57. package/lib/request.js +13 -10
  58. package/lib/route.js +131 -138
  59. package/lib/schema-controller.js +2 -2
  60. package/lib/schemas.js +27 -1
  61. package/lib/server.js +242 -116
  62. package/lib/symbols.js +5 -3
  63. package/lib/validation.js +11 -9
  64. package/lib/warnings.js +4 -12
  65. package/lib/wrapThenable.js +4 -11
  66. package/package.json +37 -39
  67. package/test/404s.test.js +258 -125
  68. package/test/500s.test.js +3 -3
  69. package/test/als.test.js +1 -1
  70. package/test/async-await.test.js +20 -76
  71. package/test/bodyLimit.test.js +1 -1
  72. package/test/build-certificate.js +6 -7
  73. package/test/case-insensitive.test.js +4 -4
  74. package/test/close-pipelining.test.js +2 -2
  75. package/test/close.test.js +11 -11
  76. package/test/content-parser.test.js +32 -0
  77. package/test/context-config.test.js +52 -0
  78. package/test/custom-http-server.test.js +14 -7
  79. package/test/custom-parser-async.test.js +1 -66
  80. package/test/custom-parser.test.js +92 -159
  81. package/test/custom-querystring-parser.test.js +3 -3
  82. package/test/decorator.test.js +11 -13
  83. package/test/delete.test.js +6 -6
  84. package/test/encapsulated-error-handler.test.js +50 -0
  85. package/test/esm/index.test.js +0 -14
  86. package/test/fastify-instance.test.js +4 -4
  87. package/test/fluent-schema.test.js +4 -4
  88. package/test/genReqId.test.js +1 -1
  89. package/test/get.test.js +4 -4
  90. package/test/handler-context.test.js +2 -2
  91. package/test/head.test.js +1 -1
  92. package/test/helper.js +19 -4
  93. package/test/hooks-async.test.js +15 -48
  94. package/test/hooks.on-ready.test.js +10 -5
  95. package/test/hooks.test.js +78 -119
  96. package/test/http2/closing.test.js +10 -16
  97. package/test/http2/constraint.test.js +1 -1
  98. package/test/http2/head.test.js +1 -1
  99. package/test/http2/plain.test.js +1 -1
  100. package/test/http2/secure-with-fallback.test.js +1 -1
  101. package/test/http2/secure.test.js +1 -1
  102. package/test/http2/unknown-http-method.test.js +4 -10
  103. package/test/https/custom-https-server.test.js +12 -6
  104. package/test/https/https.test.js +1 -1
  105. package/test/input-validation.js +3 -3
  106. package/test/internals/handleRequest.test.js +6 -43
  107. package/test/internals/initialConfig.test.js +41 -12
  108. package/test/internals/logger.test.js +2 -2
  109. package/test/internals/reply.test.js +317 -48
  110. package/test/internals/request.test.js +13 -7
  111. package/test/internals/server.test.js +88 -0
  112. package/test/listen.deprecated.test.js +202 -0
  113. package/test/listen.test.js +140 -145
  114. package/test/logger.test.js +82 -42
  115. package/test/maxRequestsPerSocket.test.js +8 -6
  116. package/test/middleware.test.js +2 -25
  117. package/test/nullable-validation.test.js +53 -16
  118. package/test/output-validation.test.js +1 -1
  119. package/test/plugin.test.js +47 -21
  120. package/test/pretty-print.test.js +22 -10
  121. package/test/promises.test.js +1 -1
  122. package/test/proto-poisoning.test.js +6 -6
  123. package/test/register.test.js +3 -3
  124. package/test/reply-error.test.js +126 -15
  125. package/test/reply-trailers.test.js +270 -0
  126. package/test/request-error.test.js +3 -6
  127. package/test/route-hooks.test.js +18 -18
  128. package/test/route-prefix.test.js +2 -1
  129. package/test/route.test.js +206 -22
  130. package/test/router-options.test.js +2 -2
  131. package/test/schema-examples.test.js +11 -5
  132. package/test/schema-feature.test.js +25 -20
  133. package/test/schema-serialization.test.js +9 -9
  134. package/test/schema-special-usage.test.js +5 -153
  135. package/test/schema-validation.test.js +9 -9
  136. package/test/skip-reply-send.test.js +2 -2
  137. package/test/stream.test.js +82 -23
  138. package/test/throw.test.js +8 -5
  139. package/test/trust-proxy.test.js +6 -6
  140. package/test/type-provider.test.js +20 -0
  141. package/test/types/fastify.test-d.ts +10 -18
  142. package/test/types/hooks.test-d.ts +61 -5
  143. package/test/types/import.js +2 -0
  144. package/test/types/import.ts +1 -0
  145. package/test/types/instance.test-d.ts +68 -17
  146. package/test/types/logger.test-d.ts +44 -15
  147. package/test/types/reply.test-d.ts +2 -1
  148. package/test/types/request.test-d.ts +71 -1
  149. package/test/types/route.test-d.ts +8 -2
  150. package/test/types/schema.test-d.ts +2 -39
  151. package/test/types/type-provider.test-d.ts +424 -0
  152. package/test/url-rewriting.test.js +3 -3
  153. package/test/validation-error-handling.test.js +8 -8
  154. package/test/versioned-routes.test.js +30 -18
  155. package/test/wrapThenable.test.js +7 -6
  156. package/types/content-type-parser.d.ts +17 -8
  157. package/types/hooks.d.ts +182 -85
  158. package/types/instance.d.ts +286 -118
  159. package/types/logger.d.ts +18 -104
  160. package/types/plugin.d.ts +10 -4
  161. package/types/reply.d.ts +18 -12
  162. package/types/request.d.ts +13 -8
  163. package/types/route.d.ts +62 -34
  164. package/types/schema.d.ts +1 -1
  165. package/types/type-provider.d.ts +99 -0
  166. package/types/utils.d.ts +1 -1
  167. package/lib/schema-compilers.js +0 -12
  168. package/test/emit-warning.test.js +0 -166
@@ -60,7 +60,7 @@ test('contentTypeParser should add a custom parser', t => {
60
60
  })
61
61
  })
62
62
 
63
- fastify.listen(0, err => {
63
+ fastify.listen({ port: 0 }, err => {
64
64
  t.error(err)
65
65
 
66
66
  t.teardown(() => fastify.close())
@@ -101,73 +101,6 @@ test('contentTypeParser should add a custom parser', t => {
101
101
  })
102
102
  })
103
103
 
104
- test('contentTypeParser should add a custom parser - deprecated syntax', t => {
105
- t.plan(5)
106
- const fastify = Fastify()
107
-
108
- process.on('warning', onWarning)
109
- function onWarning (warning) {
110
- t.equal(warning.name, 'FastifyDeprecation')
111
- t.equal(warning.code, 'FSTDEP003')
112
- }
113
-
114
- fastify.post('/', (req, reply) => {
115
- reply.send(req.body)
116
- })
117
-
118
- fastify.options('/', (req, reply) => {
119
- reply.send(req.body)
120
- })
121
-
122
- fastify.addContentTypeParser('application/jsoff', function (req, done) {
123
- jsonParser(req, function (err, body) {
124
- done(err, body)
125
- })
126
- })
127
-
128
- fastify.listen(0, err => {
129
- t.error(err)
130
-
131
- t.teardown(() => fastify.close())
132
-
133
- t.test('in POST', t => {
134
- t.plan(3)
135
-
136
- sget({
137
- method: 'POST',
138
- url: 'http://localhost:' + fastify.server.address().port,
139
- body: '{"hello":"world"}',
140
- headers: {
141
- 'Content-Type': 'application/jsoff'
142
- }
143
- }, (err, response, body) => {
144
- t.error(err)
145
- t.equal(response.statusCode, 200)
146
- t.same(body.toString(), JSON.stringify({ hello: 'world' }))
147
- })
148
- })
149
-
150
- t.test('in OPTIONS', t => {
151
- t.plan(3)
152
-
153
- sget({
154
- method: 'OPTIONS',
155
- url: 'http://localhost:' + fastify.server.address().port,
156
- body: '{"hello":"world"}',
157
- headers: {
158
- 'Content-Type': 'application/jsoff'
159
- }
160
- }, (err, response, body) => {
161
- t.error(err)
162
- t.equal(response.statusCode, 200)
163
- t.same(body.toString(), JSON.stringify({ hello: 'world' }))
164
- })
165
-
166
- process.removeListener('warning', onWarning)
167
- })
168
- })
169
- })
170
-
171
104
  test('contentTypeParser should handle multiple custom parsers', t => {
172
105
  t.plan(7)
173
106
  const fastify = Fastify()
@@ -189,13 +122,13 @@ test('contentTypeParser should handle multiple custom parsers', t => {
189
122
  fastify.addContentTypeParser('application/jsoff', customParser)
190
123
  fastify.addContentTypeParser('application/ffosj', customParser)
191
124
 
192
- fastify.listen(0, err => {
125
+ fastify.listen({ port: 0 }, err => {
193
126
  t.error(err)
194
127
  fastify.server.unref()
195
128
 
196
129
  sget({
197
130
  method: 'POST',
198
- url: 'http://localhost:' + fastify.server.address().port,
131
+ url: getUrl(fastify),
199
132
  body: '{"hello":"world"}',
200
133
  headers: {
201
134
  'Content-Type': 'application/jsoff'
@@ -208,7 +141,7 @@ test('contentTypeParser should handle multiple custom parsers', t => {
208
141
 
209
142
  sget({
210
143
  method: 'POST',
211
- url: 'http://localhost:' + fastify.server.address().port + '/hello',
144
+ url: getUrl(fastify) + '/hello',
212
145
  body: '{"hello":"world"}',
213
146
  headers: {
214
147
  'Content-Type': 'application/ffosj'
@@ -241,13 +174,13 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
241
174
 
242
175
  fastify.addContentTypeParser(['application/jsoff', 'application/ffosj'], customParser)
243
176
 
244
- fastify.listen(0, err => {
177
+ fastify.listen({ port: 0 }, err => {
245
178
  t.error(err)
246
179
  fastify.server.unref()
247
180
 
248
181
  sget({
249
182
  method: 'POST',
250
- url: 'http://localhost:' + fastify.server.address().port,
183
+ url: getUrl(fastify),
251
184
  body: '{"hello":"world"}',
252
185
  headers: {
253
186
  'Content-Type': 'application/jsoff'
@@ -260,7 +193,7 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
260
193
 
261
194
  sget({
262
195
  method: 'POST',
263
- url: 'http://localhost:' + fastify.server.address().port + '/hello',
196
+ url: getUrl(fastify) + '/hello',
264
197
  body: '{"hello":"world"}',
265
198
  headers: {
266
199
  'Content-Type': 'application/ffosj'
@@ -285,12 +218,12 @@ test('contentTypeParser should handle errors', t => {
285
218
  done(new Error('kaboom!'), {})
286
219
  })
287
220
 
288
- fastify.listen(0, err => {
221
+ fastify.listen({ port: 0 }, err => {
289
222
  t.error(err)
290
223
 
291
224
  sget({
292
225
  method: 'POST',
293
- url: 'http://localhost:' + fastify.server.address().port,
226
+ url: getUrl(fastify),
294
227
  body: '{"hello":"world"}',
295
228
  headers: {
296
229
  'Content-Type': 'application/jsoff'
@@ -346,12 +279,12 @@ test('contentTypeParser should support encapsulation, second try', t => {
346
279
  done()
347
280
  })
348
281
 
349
- fastify.listen(0, err => {
282
+ fastify.listen({ port: 0 }, err => {
350
283
  t.error(err)
351
284
 
352
285
  sget({
353
286
  method: 'POST',
354
- url: 'http://localhost:' + fastify.server.address().port,
287
+ url: getUrl(fastify),
355
288
  body: '{"hello":"world"}',
356
289
  headers: {
357
290
  'Content-Type': 'application/jsoff'
@@ -379,12 +312,12 @@ test('contentTypeParser shouldn\'t support request with undefined "Content-Type"
379
312
  })
380
313
  })
381
314
 
382
- fastify.listen(0, err => {
315
+ fastify.listen({ port: 0 }, err => {
383
316
  t.error(err)
384
317
 
385
318
  sget({
386
319
  method: 'POST',
387
- url: 'http://localhost:' + fastify.server.address().port,
320
+ url: getUrl(fastify),
388
321
  body: 'unknown content type!',
389
322
  headers: {
390
323
  // 'Content-Type': undefined
@@ -452,12 +385,12 @@ test('catch all content type parser', t => {
452
385
  })
453
386
  })
454
387
 
455
- fastify.listen(0, err => {
388
+ fastify.listen({ port: 0 }, err => {
456
389
  t.error(err)
457
390
 
458
391
  sget({
459
392
  method: 'POST',
460
- url: 'http://localhost:' + fastify.server.address().port,
393
+ url: getUrl(fastify),
461
394
  body: 'hello',
462
395
  headers: {
463
396
  'Content-Type': 'application/jsoff'
@@ -469,7 +402,7 @@ test('catch all content type parser', t => {
469
402
 
470
403
  sget({
471
404
  method: 'POST',
472
- url: 'http://localhost:' + fastify.server.address().port,
405
+ url: getUrl(fastify),
473
406
  body: 'hello',
474
407
  headers: {
475
408
  'Content-Type': 'very-weird-content-type'
@@ -506,12 +439,12 @@ test('catch all content type parser should not interfere with other conte type p
506
439
  })
507
440
  })
508
441
 
509
- fastify.listen(0, err => {
442
+ fastify.listen({ port: 0 }, err => {
510
443
  t.error(err)
511
444
 
512
445
  sget({
513
446
  method: 'POST',
514
- url: 'http://localhost:' + fastify.server.address().port,
447
+ url: getUrl(fastify),
515
448
  body: '{"hello":"world"}',
516
449
  headers: {
517
450
  'Content-Type': 'application/jsoff'
@@ -523,7 +456,7 @@ test('catch all content type parser should not interfere with other conte type p
523
456
 
524
457
  sget({
525
458
  method: 'POST',
526
- url: 'http://localhost:' + fastify.server.address().port,
459
+ url: getUrl(fastify),
527
460
  body: 'hello',
528
461
  headers: {
529
462
  'Content-Type': 'very-weird-content-type'
@@ -559,14 +492,14 @@ test('\'*\' catch undefined Content-Type requests', t => {
559
492
  res.type('text/plain').send(req.body)
560
493
  })
561
494
 
562
- fastify.listen(0, function (err) {
495
+ fastify.listen({ port: 0 }, function (err) {
563
496
  t.error(err)
564
497
 
565
498
  const fileStream = fs.createReadStream(__filename)
566
499
 
567
500
  sget({
568
501
  method: 'POST',
569
- url: 'http://localhost:' + fastify.server.address().port + '/',
502
+ url: getUrl(fastify) + '/',
570
503
  body: fileStream
571
504
  }, (err, response, body) => {
572
505
  t.error(err)
@@ -587,7 +520,7 @@ test('cannot add custom parser after binding', t => {
587
520
  res.type('text/plain').send(req.body)
588
521
  })
589
522
 
590
- fastify.listen(0, function (err) {
523
+ fastify.listen({ port: 0 }, function (err) {
591
524
  t.error(err)
592
525
 
593
526
  try {
@@ -614,12 +547,12 @@ test('Can override the default json parser', t => {
614
547
  })
615
548
  })
616
549
 
617
- fastify.listen(0, err => {
550
+ fastify.listen({ port: 0 }, err => {
618
551
  t.error(err)
619
552
 
620
553
  sget({
621
554
  method: 'POST',
622
- url: 'http://localhost:' + fastify.server.address().port,
555
+ url: getUrl(fastify),
623
556
  body: '{"hello":"world"}',
624
557
  headers: {
625
558
  'Content-Type': 'application/json'
@@ -648,12 +581,12 @@ test('Can override the default plain text parser', t => {
648
581
  })
649
582
  })
650
583
 
651
- fastify.listen(0, err => {
584
+ fastify.listen({ port: 0 }, err => {
652
585
  t.error(err)
653
586
 
654
587
  sget({
655
588
  method: 'POST',
656
- url: 'http://localhost:' + fastify.server.address().port,
589
+ url: getUrl(fastify),
657
590
  body: 'hello world',
658
591
  headers: {
659
592
  'Content-Type': 'text/plain'
@@ -686,12 +619,12 @@ test('Can override the default json parser in a plugin', t => {
686
619
  done()
687
620
  })
688
621
 
689
- fastify.listen(0, err => {
622
+ fastify.listen({ port: 0 }, err => {
690
623
  t.error(err)
691
624
 
692
625
  sget({
693
626
  method: 'POST',
694
- url: 'http://localhost:' + fastify.server.address().port,
627
+ url: getUrl(fastify),
695
628
  body: '{"hello":"world"}',
696
629
  headers: {
697
630
  'Content-Type': 'application/json'
@@ -771,12 +704,12 @@ test('Should get the body as string', t => {
771
704
  }
772
705
  })
773
706
 
774
- fastify.listen(0, err => {
707
+ fastify.listen({ port: 0 }, err => {
775
708
  t.error(err)
776
709
 
777
710
  sget({
778
711
  method: 'POST',
779
- url: 'http://localhost:' + fastify.server.address().port,
712
+ url: getUrl(fastify),
780
713
  body: '{"hello":"world"}',
781
714
  headers: {
782
715
  'Content-Type': 'application/json'
@@ -798,12 +731,12 @@ test('Should return defined body with no custom parser defined and content type
798
731
  reply.send(req.body)
799
732
  })
800
733
 
801
- fastify.listen(0, err => {
734
+ fastify.listen({ port: 0 }, err => {
802
735
  t.error(err)
803
736
 
804
737
  sget({
805
738
  method: 'POST',
806
- url: 'http://localhost:' + fastify.server.address().port,
739
+ url: getUrl(fastify),
807
740
  body: 'hello world',
808
741
  headers: {
809
742
  'Content-Type': 'text/plain'
@@ -825,12 +758,12 @@ test('Should have typeof body object with no custom parser defined, no body defi
825
758
  reply.send(req.body)
826
759
  })
827
760
 
828
- fastify.listen(0, err => {
761
+ fastify.listen({ port: 0 }, err => {
829
762
  t.error(err)
830
763
 
831
764
  sget({
832
765
  method: 'POST',
833
- url: 'http://localhost:' + fastify.server.address().port,
766
+ url: getUrl(fastify),
834
767
  headers: {
835
768
  'Content-Type': 'text/plain'
836
769
  }
@@ -851,12 +784,12 @@ test('Should have typeof body object with no custom parser defined, null body an
851
784
  reply.send(req.body)
852
785
  })
853
786
 
854
- fastify.listen(0, err => {
787
+ fastify.listen({ port: 0 }, err => {
855
788
  t.error(err)
856
789
 
857
790
  sget({
858
791
  method: 'POST',
859
- url: 'http://localhost:' + fastify.server.address().port,
792
+ url: getUrl(fastify),
860
793
  body: null,
861
794
  headers: {
862
795
  'Content-Type': 'text/plain'
@@ -878,12 +811,12 @@ test('Should have typeof body object with no custom parser defined, undefined bo
878
811
  reply.send(req.body)
879
812
  })
880
813
 
881
- fastify.listen(0, err => {
814
+ fastify.listen({ port: 0 }, err => {
882
815
  t.error(err)
883
816
 
884
817
  sget({
885
818
  method: 'POST',
886
- url: 'http://localhost:' + fastify.server.address().port,
819
+ url: getUrl(fastify),
887
820
  body: undefined,
888
821
  headers: {
889
822
  'Content-Type': 'text/plain'
@@ -917,12 +850,12 @@ test('Should get the body as string', t => {
917
850
  }
918
851
  })
919
852
 
920
- fastify.listen(0, err => {
853
+ fastify.listen({ port: 0 }, err => {
921
854
  t.error(err)
922
855
 
923
856
  sget({
924
857
  method: 'POST',
925
- url: 'http://localhost:' + fastify.server.address().port,
858
+ url: getUrl(fastify),
926
859
  body: 'hello world',
927
860
  headers: {
928
861
  'Content-Type': 'text/plain'
@@ -956,12 +889,12 @@ test('Should get the body as buffer', t => {
956
889
  }
957
890
  })
958
891
 
959
- fastify.listen(0, err => {
892
+ fastify.listen({ port: 0 }, err => {
960
893
  t.error(err)
961
894
 
962
895
  sget({
963
896
  method: 'POST',
964
- url: 'http://localhost:' + fastify.server.address().port,
897
+ url: getUrl(fastify),
965
898
  body: '{"hello":"world"}',
966
899
  headers: {
967
900
  'Content-Type': 'application/json'
@@ -995,12 +928,12 @@ test('Should get the body as buffer', t => {
995
928
  }
996
929
  })
997
930
 
998
- fastify.listen(0, err => {
931
+ fastify.listen({ port: 0 }, err => {
999
932
  t.error(err)
1000
933
 
1001
934
  sget({
1002
935
  method: 'POST',
1003
- url: 'http://localhost:' + fastify.server.address().port,
936
+ url: getUrl(fastify),
1004
937
  body: 'hello world',
1005
938
  headers: {
1006
939
  'Content-Type': 'text/plain'
@@ -1031,13 +964,13 @@ test('Should parse empty bodies as a string', t => {
1031
964
  }
1032
965
  })
1033
966
 
1034
- fastify.listen(0, err => {
967
+ fastify.listen({ port: 0 }, err => {
1035
968
  t.error(err)
1036
969
  fastify.server.unref()
1037
970
 
1038
971
  sget({
1039
972
  method: 'POST',
1040
- url: 'http://localhost:' + fastify.server.address().port,
973
+ url: getUrl(fastify),
1041
974
  body: '',
1042
975
  headers: {
1043
976
  'Content-Type': 'text/plain'
@@ -1050,7 +983,7 @@ test('Should parse empty bodies as a string', t => {
1050
983
 
1051
984
  sget({
1052
985
  method: 'DELETE',
1053
- url: 'http://localhost:' + fastify.server.address().port,
986
+ url: getUrl(fastify),
1054
987
  body: '',
1055
988
  headers: {
1056
989
  'Content-Type': 'text/plain',
@@ -1078,12 +1011,12 @@ test('Should parse empty bodies as a buffer', t => {
1078
1011
  done(null, body)
1079
1012
  })
1080
1013
 
1081
- fastify.listen(0, err => {
1014
+ fastify.listen({ port: 0 }, err => {
1082
1015
  t.error(err)
1083
1016
 
1084
1017
  sget({
1085
1018
  method: 'POST',
1086
- url: 'http://localhost:' + fastify.server.address().port,
1019
+ url: getUrl(fastify),
1087
1020
  body: '',
1088
1021
  headers: {
1089
1022
  'Content-Type': 'text/plain'
@@ -1112,12 +1045,12 @@ test('The charset should not interfere with the content type handling', t => {
1112
1045
  })
1113
1046
  })
1114
1047
 
1115
- fastify.listen(0, err => {
1048
+ fastify.listen({ port: 0 }, err => {
1116
1049
  t.error(err)
1117
1050
 
1118
1051
  sget({
1119
1052
  method: 'POST',
1120
- url: 'http://localhost:' + fastify.server.address().port,
1053
+ url: getUrl(fastify),
1121
1054
  body: '{"hello":"world"}',
1122
1055
  headers: {
1123
1056
  'Content-Type': 'application/json charset=utf-8'
@@ -1162,12 +1095,12 @@ test('Should allow defining the bodyLimit per parser', t => {
1162
1095
  }
1163
1096
  )
1164
1097
 
1165
- fastify.listen(0, err => {
1098
+ fastify.listen({ port: 0 }, err => {
1166
1099
  t.error(err)
1167
1100
 
1168
1101
  sget({
1169
1102
  method: 'POST',
1170
- url: 'http://localhost:' + fastify.server.address().port,
1103
+ url: getUrl(fastify),
1171
1104
  body: '1234567890',
1172
1105
  headers: {
1173
1106
  'Content-Type': 'x/foo'
@@ -1203,12 +1136,12 @@ test('route bodyLimit should take precedence over a custom parser bodyLimit', t
1203
1136
  }
1204
1137
  )
1205
1138
 
1206
- fastify.listen(0, err => {
1139
+ fastify.listen({ port: 0 }, err => {
1207
1140
  t.error(err)
1208
1141
 
1209
1142
  sget({
1210
1143
  method: 'POST',
1211
- url: 'http://localhost:' + fastify.server.address().port,
1144
+ url: getUrl(fastify),
1212
1145
  body: '1234567890',
1213
1146
  headers: { 'Content-Type': 'x/foo' }
1214
1147
  }, (err, response, body) => {
@@ -1235,12 +1168,12 @@ test('should be able to use default parser for extra content type', t => {
1235
1168
 
1236
1169
  fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))
1237
1170
 
1238
- fastify.listen(0, err => {
1171
+ fastify.listen({ port: 0 }, err => {
1239
1172
  t.error(err)
1240
1173
 
1241
1174
  sget({
1242
1175
  method: 'POST',
1243
- url: 'http://localhost:' + fastify.server.address().port,
1176
+ url: getUrl(fastify),
1244
1177
  body: '{"hello":"world"}',
1245
1178
  headers: {
1246
1179
  'Content-Type': 'text/json'
@@ -1273,7 +1206,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
1273
1206
  })
1274
1207
  })
1275
1208
 
1276
- fastify.listen(0, err => {
1209
+ fastify.listen({ port: 0 }, err => {
1277
1210
  t.error(err)
1278
1211
 
1279
1212
  t.teardown(() => fastify.close())
@@ -1283,7 +1216,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
1283
1216
 
1284
1217
  sget({
1285
1218
  method: 'POST',
1286
- url: 'http://localhost:' + fastify.server.address().port,
1219
+ url: getUrl(fastify),
1287
1220
  body: '{"hello":"world"}',
1288
1221
  headers: {
1289
1222
  'Content-Type': 'application/vnd.test+json'
@@ -1300,7 +1233,7 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
1300
1233
 
1301
1234
  sget({
1302
1235
  method: 'OPTIONS',
1303
- url: 'http://localhost:' + fastify.server.address().port,
1236
+ url: getUrl(fastify),
1304
1237
  body: '{"hello":"world"}',
1305
1238
  headers: {
1306
1239
  'Content-Type': 'weird-content-type+json'
@@ -1346,8 +1279,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
1346
1279
  {
1347
1280
  const response = await fastify.inject({
1348
1281
  method: 'POST',
1349
- path: '/',
1350
- payload: '{"hello":"world"}',
1282
+ url: '/',
1283
+ body: '{"hello":"world"}',
1351
1284
  headers: {
1352
1285
  'Content-Type': 'application/vnd.hello+json'
1353
1286
  }
@@ -1359,8 +1292,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
1359
1292
  {
1360
1293
  const response = await fastify.inject({
1361
1294
  method: 'POST',
1362
- path: '/',
1363
- payload: '{"hello":"world"}',
1295
+ url: '/',
1296
+ body: '{"hello":"world"}',
1364
1297
  headers: {
1365
1298
  'Content-Type': 'application/test+xml'
1366
1299
  }
@@ -1387,6 +1320,7 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
1387
1320
  test('catch all content type parser should not interfere with content type parser', t => {
1388
1321
  t.plan(10)
1389
1322
  const fastify = Fastify()
1323
+ t.teardown(fastify.close.bind(fastify))
1390
1324
 
1391
1325
  fastify.post('/', (req, reply) => {
1392
1326
  reply.send(req.body)
@@ -1414,12 +1348,12 @@ test('catch all content type parser should not interfere with content type parse
1414
1348
  })
1415
1349
  })
1416
1350
 
1417
- fastify.listen(0, err => {
1351
+ fastify.listen({ port: 0 }, err => {
1418
1352
  t.error(err)
1419
1353
 
1420
1354
  sget({
1421
1355
  method: 'POST',
1422
- url: 'http://localhost:' + fastify.server.address().port,
1356
+ url: getUrl(fastify),
1423
1357
  body: '{"myKey":"myValue"}',
1424
1358
  headers: {
1425
1359
  'Content-Type': 'application/json'
@@ -1432,7 +1366,7 @@ test('catch all content type parser should not interfere with content type parse
1432
1366
 
1433
1367
  sget({
1434
1368
  method: 'POST',
1435
- url: 'http://localhost:' + fastify.server.address().port,
1369
+ url: getUrl(fastify),
1436
1370
  body: 'body',
1437
1371
  headers: {
1438
1372
  'Content-Type': 'very-weird-content-type'
@@ -1445,7 +1379,7 @@ test('catch all content type parser should not interfere with content type parse
1445
1379
 
1446
1380
  sget({
1447
1381
  method: 'POST',
1448
- url: 'http://localhost:' + fastify.server.address().port,
1382
+ url: getUrl(fastify),
1449
1383
  body: 'my text',
1450
1384
  headers: {
1451
1385
  'Content-Type': 'text/html'
@@ -1454,7 +1388,6 @@ test('catch all content type parser should not interfere with content type parse
1454
1388
  t.error(err)
1455
1389
  t.equal(response.statusCode, 200)
1456
1390
  t.same(body.toString(), 'my texthtml')
1457
- fastify.close()
1458
1391
  })
1459
1392
  })
1460
1393
  })
@@ -1462,6 +1395,7 @@ test('catch all content type parser should not interfere with content type parse
1462
1395
  test('should prefer string content types over RegExp ones', t => {
1463
1396
  t.plan(7)
1464
1397
  const fastify = Fastify()
1398
+ t.teardown(fastify.close.bind(fastify))
1465
1399
 
1466
1400
  fastify.post('/', (req, reply) => {
1467
1401
  reply.send(req.body)
@@ -1481,12 +1415,12 @@ test('should prefer string content types over RegExp ones', t => {
1481
1415
  })
1482
1416
  })
1483
1417
 
1484
- fastify.listen(0, err => {
1418
+ fastify.listen({ port: 0 }, err => {
1485
1419
  t.error(err)
1486
1420
 
1487
1421
  sget({
1488
1422
  method: 'POST',
1489
- url: 'http://localhost:' + fastify.server.address().port,
1423
+ url: getUrl(fastify),
1490
1424
  body: '{"k1":"myValue", "k2": "myValue"}',
1491
1425
  headers: {
1492
1426
  'Content-Type': 'application/json'
@@ -1499,7 +1433,7 @@ test('should prefer string content types over RegExp ones', t => {
1499
1433
 
1500
1434
  sget({
1501
1435
  method: 'POST',
1502
- url: 'http://localhost:' + fastify.server.address().port,
1436
+ url: getUrl(fastify),
1503
1437
  body: 'javascript',
1504
1438
  headers: {
1505
1439
  'Content-Type': 'application/javascript'
@@ -1508,7 +1442,6 @@ test('should prefer string content types over RegExp ones', t => {
1508
1442
  t.error(err)
1509
1443
  t.equal(response.statusCode, 200)
1510
1444
  t.same(body.toString(), 'javascript')
1511
- fastify.close()
1512
1445
  })
1513
1446
  })
1514
1447
  })
@@ -1517,6 +1450,7 @@ test('removeContentTypeParser should support arrays of content types to remove',
1517
1450
  t.plan(8)
1518
1451
 
1519
1452
  const fastify = Fastify()
1453
+ t.teardown(fastify.close.bind(fastify))
1520
1454
 
1521
1455
  fastify.addContentTypeParser('application/xml', function (req, payload, done) {
1522
1456
  payload.on('data', () => {})
@@ -1538,12 +1472,12 @@ test('removeContentTypeParser should support arrays of content types to remove',
1538
1472
  reply.send(req.body)
1539
1473
  })
1540
1474
 
1541
- fastify.listen(0, err => {
1475
+ fastify.listen({ port: 0 }, err => {
1542
1476
  t.error(err)
1543
1477
 
1544
1478
  sget({
1545
1479
  method: 'POST',
1546
- url: 'http://localhost:' + fastify.server.address().port,
1480
+ url: getUrl(fastify),
1547
1481
  body: '<?xml version="1.0">',
1548
1482
  headers: {
1549
1483
  'Content-Type': 'application/xml'
@@ -1556,7 +1490,7 @@ test('removeContentTypeParser should support arrays of content types to remove',
1556
1490
 
1557
1491
  sget({
1558
1492
  method: 'POST',
1559
- url: 'http://localhost:' + fastify.server.address().port,
1493
+ url: getUrl(fastify),
1560
1494
  body: '',
1561
1495
  headers: {
1562
1496
  'Content-Type': 'image/png'
@@ -1568,7 +1502,7 @@ test('removeContentTypeParser should support arrays of content types to remove',
1568
1502
 
1569
1503
  sget({
1570
1504
  method: 'POST',
1571
- url: 'http://localhost:' + fastify.server.address().port,
1505
+ url: getUrl(fastify),
1572
1506
  body: '{test: "test"}',
1573
1507
  headers: {
1574
1508
  'Content-Type': 'application/json'
@@ -1576,7 +1510,6 @@ test('removeContentTypeParser should support arrays of content types to remove',
1576
1510
  }, (err, response, body) => {
1577
1511
  t.error(err)
1578
1512
  t.equal(response.statusCode, 415)
1579
- fastify.close()
1580
1513
  })
1581
1514
  })
1582
1515
  })
@@ -1607,12 +1540,12 @@ test('removeContentTypeParser should support encapsulation', t => {
1607
1540
  done()
1608
1541
  })
1609
1542
 
1610
- fastify.listen(0, err => {
1543
+ fastify.listen({ port: 0 }, err => {
1611
1544
  t.error(err)
1612
1545
 
1613
1546
  sget({
1614
1547
  method: 'POST',
1615
- url: 'http://localhost:' + fastify.server.address().port + '/encapsulated',
1548
+ url: getUrl(fastify) + '/encapsulated',
1616
1549
  body: '<?xml version="1.0">',
1617
1550
  headers: {
1618
1551
  'Content-Type': 'application/xml'
@@ -1624,7 +1557,7 @@ test('removeContentTypeParser should support encapsulation', t => {
1624
1557
 
1625
1558
  sget({
1626
1559
  method: 'POST',
1627
- url: 'http://localhost:' + fastify.server.address().port,
1560
+ url: getUrl(fastify),
1628
1561
  body: '<?xml version="1.0">',
1629
1562
  headers: {
1630
1563
  'Content-Type': 'application/xml'
@@ -1657,12 +1590,12 @@ test('removeAllContentTypeParsers should support encapsulation', t => {
1657
1590
  done()
1658
1591
  })
1659
1592
 
1660
- fastify.listen(0, err => {
1593
+ fastify.listen({ port: 0 }, err => {
1661
1594
  t.error(err)
1662
1595
 
1663
1596
  sget({
1664
1597
  method: 'POST',
1665
- url: 'http://localhost:' + fastify.server.address().port + '/encapsulated',
1598
+ url: getUrl(fastify) + '/encapsulated',
1666
1599
  body: '{}',
1667
1600
  headers: {
1668
1601
  'Content-Type': 'application/json'
@@ -1674,7 +1607,7 @@ test('removeAllContentTypeParsers should support encapsulation', t => {
1674
1607
 
1675
1608
  sget({
1676
1609
  method: 'POST',
1677
- url: 'http://localhost:' + fastify.server.address().port,
1610
+ url: getUrl(fastify),
1678
1611
  body: '{"test":1}',
1679
1612
  headers: {
1680
1613
  'Content-Type': 'application/json'
@@ -1695,7 +1628,7 @@ test('cannot remove all content type parsers after binding', t => {
1695
1628
 
1696
1629
  t.teardown(fastify.close.bind(fastify))
1697
1630
 
1698
- fastify.listen(0, function (err) {
1631
+ fastify.listen({ port: 0 }, function (err) {
1699
1632
  t.error(err)
1700
1633
 
1701
1634
  t.throws(() => fastify.removeAllContentTypeParsers())
@@ -1709,7 +1642,7 @@ test('cannot remove content type parsers after binding', t => {
1709
1642
 
1710
1643
  t.teardown(fastify.close.bind(fastify))
1711
1644
 
1712
- fastify.listen(0, function (err) {
1645
+ fastify.listen({ port: 0 }, function (err) {
1713
1646
  t.error(err)
1714
1647
 
1715
1648
  t.throws(() => fastify.removeContentTypeParser('application/json'))
@@ -1734,12 +1667,12 @@ test('should be able to override the default json parser after removeAllContentT
1734
1667
  })
1735
1668
  })
1736
1669
 
1737
- fastify.listen(0, err => {
1670
+ fastify.listen({ port: 0 }, err => {
1738
1671
  t.error(err)
1739
1672
 
1740
1673
  sget({
1741
1674
  method: 'POST',
1742
- url: 'http://localhost:' + fastify.server.address().port,
1675
+ url: getUrl(fastify),
1743
1676
  body: '{"hello":"world"}',
1744
1677
  headers: {
1745
1678
  'Content-Type': 'application/json'
@@ -1771,12 +1704,12 @@ test('should be able to override the default plain text parser after removeAllCo
1771
1704
  })
1772
1705
  })
1773
1706
 
1774
- fastify.listen(0, err => {
1707
+ fastify.listen({ port: 0 }, err => {
1775
1708
  t.error(err)
1776
1709
 
1777
1710
  sget({
1778
1711
  method: 'POST',
1779
- url: 'http://localhost:' + fastify.server.address().port,
1712
+ url: getUrl(fastify),
1780
1713
  body: 'hello world',
1781
1714
  headers: {
1782
1715
  'Content-Type': 'text/plain'
@@ -1808,12 +1741,12 @@ test('should be able to add a custom content type parser after removeAllContentT
1808
1741
  })
1809
1742
  })
1810
1743
 
1811
- fastify.listen(0, err => {
1744
+ fastify.listen({ port: 0 }, err => {
1812
1745
  t.error(err)
1813
1746
 
1814
1747
  sget({
1815
1748
  method: 'POST',
1816
- url: 'http://localhost:' + fastify.server.address().port,
1749
+ url: getUrl(fastify),
1817
1750
  body: '{"hello":"world"}',
1818
1751
  headers: {
1819
1752
  'Content-Type': 'application/jsoff'