fastify 3.27.2 → 4.0.0-alpha.1

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 (116) hide show
  1. package/README.md +5 -4
  2. package/build/build-error-serializer.js +27 -0
  3. package/build/build-validation.js +47 -35
  4. package/docs/Migration-Guide-V4.md +12 -0
  5. package/docs/Reference/ContentTypeParser.md +4 -0
  6. package/docs/Reference/Errors.md +51 -6
  7. package/docs/Reference/Hooks.md +4 -7
  8. package/docs/Reference/LTS.md +5 -4
  9. package/docs/Reference/Reply.md +23 -22
  10. package/docs/Reference/Request.md +1 -3
  11. package/docs/Reference/Routes.md +17 -10
  12. package/docs/Reference/Server.md +48 -63
  13. package/docs/Reference/TypeScript.md +11 -13
  14. package/docs/Reference/Validation-and-Serialization.md +28 -53
  15. package/docs/Type-Providers.md +257 -0
  16. package/examples/hooks.js +1 -1
  17. package/examples/simple-stream.js +18 -0
  18. package/fastify.d.ts +34 -22
  19. package/fastify.js +37 -35
  20. package/lib/configValidator.js +902 -1023
  21. package/lib/contentTypeParser.js +6 -16
  22. package/lib/context.js +36 -10
  23. package/lib/decorate.js +3 -1
  24. package/lib/error-handler.js +158 -0
  25. package/lib/error-serializer.js +257 -0
  26. package/lib/errors.js +43 -9
  27. package/lib/fourOhFour.js +31 -20
  28. package/lib/handleRequest.js +10 -13
  29. package/lib/hooks.js +14 -9
  30. package/lib/pluginOverride.js +0 -3
  31. package/lib/pluginUtils.js +3 -2
  32. package/lib/reply.js +28 -157
  33. package/lib/request.js +13 -10
  34. package/lib/route.js +131 -138
  35. package/lib/schema-controller.js +2 -2
  36. package/lib/schemas.js +27 -1
  37. package/lib/server.js +219 -116
  38. package/lib/symbols.js +4 -3
  39. package/lib/validation.js +2 -1
  40. package/lib/warnings.js +2 -12
  41. package/lib/wrapThenable.js +4 -11
  42. package/package.json +31 -35
  43. package/test/404s.test.js +243 -110
  44. package/test/500s.test.js +2 -2
  45. package/test/async-await.test.js +13 -69
  46. package/test/content-parser.test.js +32 -0
  47. package/test/context-config.test.js +52 -0
  48. package/test/custom-http-server.test.js +14 -7
  49. package/test/custom-parser-async.test.js +0 -65
  50. package/test/custom-parser.test.js +54 -121
  51. package/test/decorator.test.js +1 -3
  52. package/test/delete.test.js +5 -5
  53. package/test/encapsulated-error-handler.test.js +50 -0
  54. package/test/esm/index.test.js +0 -14
  55. package/test/fastify-instance.test.js +4 -4
  56. package/test/fluent-schema.test.js +4 -4
  57. package/test/get.test.js +3 -3
  58. package/test/helper.js +18 -3
  59. package/test/hooks-async.test.js +14 -47
  60. package/test/hooks.on-ready.test.js +9 -4
  61. package/test/hooks.test.js +58 -99
  62. package/test/http2/closing.test.js +5 -11
  63. package/test/http2/unknown-http-method.test.js +3 -9
  64. package/test/https/custom-https-server.test.js +12 -6
  65. package/test/input-validation.js +2 -2
  66. package/test/internals/handleRequest.test.js +3 -40
  67. package/test/internals/initialConfig.test.js +33 -12
  68. package/test/internals/reply.test.js +245 -3
  69. package/test/internals/request.test.js +13 -7
  70. package/test/internals/server.test.js +88 -0
  71. package/test/listen.test.js +84 -1
  72. package/test/logger.test.js +80 -40
  73. package/test/maxRequestsPerSocket.test.js +6 -4
  74. package/test/middleware.test.js +2 -25
  75. package/test/nullable-validation.test.js +51 -14
  76. package/test/plugin.test.js +31 -5
  77. package/test/pretty-print.test.js +22 -10
  78. package/test/reply-error.test.js +123 -12
  79. package/test/request-error.test.js +2 -5
  80. package/test/route-hooks.test.js +17 -17
  81. package/test/route-prefix.test.js +2 -1
  82. package/test/route.test.js +204 -20
  83. package/test/router-options.test.js +1 -1
  84. package/test/schema-examples.test.js +11 -5
  85. package/test/schema-feature.test.js +24 -19
  86. package/test/schema-serialization.test.js +9 -9
  87. package/test/schema-special-usage.test.js +14 -81
  88. package/test/schema-validation.test.js +9 -9
  89. package/test/skip-reply-send.test.js +1 -1
  90. package/test/stream.test.js +23 -12
  91. package/test/throw.test.js +8 -5
  92. package/test/type-provider.test.js +20 -0
  93. package/test/types/fastify.test-d.ts +10 -18
  94. package/test/types/import.js +2 -0
  95. package/test/types/import.ts +1 -0
  96. package/test/types/instance.test-d.ts +35 -14
  97. package/test/types/logger.test-d.ts +44 -15
  98. package/test/types/route.test-d.ts +8 -2
  99. package/test/types/schema.test-d.ts +2 -39
  100. package/test/types/type-provider.test-d.ts +417 -0
  101. package/test/validation-error-handling.test.js +8 -8
  102. package/test/versioned-routes.test.js +28 -16
  103. package/test/wrapThenable.test.js +7 -6
  104. package/types/content-type-parser.d.ts +17 -8
  105. package/types/hooks.d.ts +102 -59
  106. package/types/instance.d.ts +124 -104
  107. package/types/logger.d.ts +18 -104
  108. package/types/plugin.d.ts +10 -4
  109. package/types/reply.d.ts +16 -11
  110. package/types/request.d.ts +10 -5
  111. package/types/route.d.ts +42 -31
  112. package/types/schema.d.ts +1 -1
  113. package/types/type-provider.d.ts +99 -0
  114. package/types/utils.d.ts +1 -1
  115. package/lib/schema-compilers.js +0 -12
  116. package/test/emit-warning.test.js +0 -166
@@ -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()
@@ -195,7 +128,7 @@ test('contentTypeParser should handle multiple custom parsers', t => {
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'
@@ -247,7 +180,7 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
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'
@@ -290,7 +223,7 @@ test('contentTypeParser should handle errors', t => {
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'
@@ -351,7 +284,7 @@ test('contentTypeParser should support encapsulation, second try', t => {
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'
@@ -384,7 +317,7 @@ test('contentTypeParser shouldn\'t support request with undefined "Content-Type"
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
@@ -457,7 +390,7 @@ test('catch all content type parser', t => {
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'
@@ -511,7 +444,7 @@ test('catch all content type parser should not interfere with other conte type p
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'
@@ -566,7 +499,7 @@ test('\'*\' catch undefined Content-Type requests', t => {
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)
@@ -619,7 +552,7 @@ test('Can override the default json parser', t => {
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'
@@ -653,7 +586,7 @@ test('Can override the default plain text parser', t => {
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'
@@ -691,7 +624,7 @@ test('Can override the default json parser in a plugin', t => {
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'
@@ -776,7 +709,7 @@ test('Should get the body as string', t => {
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'
@@ -803,7 +736,7 @@ test('Should return defined body with no custom parser defined and content type
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'
@@ -830,7 +763,7 @@ test('Should have typeof body object with no custom parser defined, no body defi
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
  }
@@ -856,7 +789,7 @@ test('Should have typeof body object with no custom parser defined, null body an
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'
@@ -883,7 +816,7 @@ test('Should have typeof body object with no custom parser defined, undefined bo
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'
@@ -922,7 +855,7 @@ test('Should get the body as string', t => {
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'
@@ -961,7 +894,7 @@ test('Should get the body as buffer', t => {
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'
@@ -1000,7 +933,7 @@ test('Should get the body as buffer', t => {
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'
@@ -1037,7 +970,7 @@ test('Should parse empty bodies as a string', t => {
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',
@@ -1083,7 +1016,7 @@ test('Should parse empty bodies as a buffer', t => {
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'
@@ -1117,7 +1050,7 @@ test('The charset should not interfere with the content type handling', t => {
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'
@@ -1167,7 +1100,7 @@ test('Should allow defining the bodyLimit per parser', t => {
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'
@@ -1208,7 +1141,7 @@ test('route bodyLimit should take precedence over a custom parser bodyLimit', t
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) => {
@@ -1240,7 +1173,7 @@ test('should be able to use default parser for extra content type', t => {
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'
@@ -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)
@@ -1419,7 +1353,7 @@ test('catch all content type parser should not interfere with content type parse
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)
@@ -1486,7 +1420,7 @@ test('should prefer string content types over RegExp ones', t => {
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', () => {})
@@ -1543,7 +1477,7 @@ test('removeContentTypeParser should support arrays of content types to remove',
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
  })
@@ -1612,7 +1545,7 @@ test('removeContentTypeParser should support encapsulation', t => {
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'
@@ -1662,7 +1595,7 @@ test('removeAllContentTypeParsers should support encapsulation', t => {
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'
@@ -1739,7 +1672,7 @@ test('should be able to override the default json parser after removeAllContentT
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'
@@ -1776,7 +1709,7 @@ test('should be able to override the default plain text parser after removeAllCo
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'
@@ -1813,7 +1746,7 @@ test('should be able to add a custom content type parser after removeAllContentT
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'
@@ -913,8 +913,7 @@ test('plugin required decorators', async t => {
913
913
  })
914
914
  },
915
915
  {
916
- name: 'custom-plugin-one',
917
- fastify: '3.x'
916
+ name: 'custom-plugin-one'
918
917
  }
919
918
  )
920
919
 
@@ -924,7 +923,6 @@ test('plugin required decorators', async t => {
924
923
  },
925
924
  {
926
925
  name: 'custom-plugin-two',
927
- fastify: '3.x',
928
926
  dependencies: ['custom-plugin-one'],
929
927
  decorators: {
930
928
  request: ['someThing']
@@ -197,7 +197,7 @@ fastify.listen(0, err => {
197
197
  t.equal(response.statusCode, 400)
198
198
  t.same(JSON.parse(body), {
199
199
  error: 'Bad Request',
200
- message: 'params.test should be integer',
200
+ message: 'params/test must be integer',
201
201
  statusCode: 400
202
202
  })
203
203
  })
@@ -232,7 +232,7 @@ fastify.listen(0, err => {
232
232
  t.equal(response.statusCode, 400)
233
233
  t.same(JSON.parse(body), {
234
234
  error: 'Bad Request',
235
- message: "headers['x-test'] should be number",
235
+ message: 'headers/x-test must be number',
236
236
  statusCode: 400
237
237
  })
238
238
  })
@@ -261,7 +261,7 @@ fastify.listen(0, err => {
261
261
  t.equal(response.statusCode, 400)
262
262
  t.same(JSON.parse(body), {
263
263
  error: 'Bad Request',
264
- message: 'querystring.hello should be integer',
264
+ message: 'querystring/hello must be integer',
265
265
  statusCode: 400
266
266
  })
267
267
  })
@@ -302,7 +302,7 @@ test('shorthand - delete with application/json Content-Type header and without b
302
302
  t.plan(4)
303
303
  const fastify = require('..')()
304
304
  fastify.delete('/', {}, (req, reply) => {
305
- t.equal(req.body, null)
305
+ t.equal(req.body, undefined)
306
306
  reply.send(req.body)
307
307
  })
308
308
  fastify.inject({
@@ -313,6 +313,6 @@ test('shorthand - delete with application/json Content-Type header and without b
313
313
  }, (err, response) => {
314
314
  t.error(err)
315
315
  t.equal(response.statusCode, 200)
316
- t.same(JSON.parse(response.payload), null)
316
+ t.same(response.payload.toString(), '')
317
317
  })
318
318
  })
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('..')
5
+
6
+ test('encapuslates an error handler', async t => {
7
+ t.plan(3)
8
+
9
+ const fastify = Fastify()
10
+ fastify.register(async function (fastify) {
11
+ fastify.setErrorHandler(async function a (err) {
12
+ t.equal(err.message, 'kaboom')
13
+ throw new Error('caught')
14
+ })
15
+ fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
16
+ })
17
+
18
+ fastify.setErrorHandler(async function b (err) {
19
+ t.equal(err.message, 'caught')
20
+ throw new Error('wrapped')
21
+ })
22
+
23
+ const res = await fastify.inject('/encapsulated')
24
+ t.equal(res.json().message, 'wrapped')
25
+ })
26
+
27
+ test('onError hook nested', async t => {
28
+ t.plan(4)
29
+
30
+ const fastify = Fastify()
31
+ fastify.register(async function (fastify) {
32
+ fastify.setErrorHandler(async function a (err) {
33
+ t.equal(err.message, 'kaboom')
34
+ throw new Error('caught')
35
+ })
36
+ fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
37
+ })
38
+
39
+ fastify.setErrorHandler(async function b (err) {
40
+ t.equal(err.message, 'caught')
41
+ throw new Error('wrapped')
42
+ })
43
+
44
+ fastify.addHook('onError', async function (request, reply, err) {
45
+ t.equal(err.message, 'kaboom')
46
+ })
47
+
48
+ const res = await fastify.inject('/encapsulated')
49
+ t.equal(res.json().message, 'wrapped')
50
+ })
@@ -3,20 +3,6 @@
3
3
  const t = require('tap')
4
4
  const semver = require('semver')
5
5
 
6
- if (semver.lt(process.versions.node, '13.3.0')) {
7
- t.skip('Skip esm because Node version <= 13.3.0')
8
- } else {
9
- // Node v8 throw a `SyntaxError: Unexpected token import`
10
- // even if this branch is never touch in the code,
11
- // by using `eval` we can avoid this issue.
12
- // eslint-disable-next-line
13
- new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
14
- process.nextTick(() => {
15
- throw err
16
- })
17
- })
18
- }
19
-
20
6
  if (semver.lt(process.versions.node, '14.13.0')) {
21
7
  t.skip('Skip named exports because Node version < 14.13.0')
22
8
  } else {