fastify 5.0.0 → 5.2.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 (201) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/README.md +12 -7
  4. package/docs/Guides/Database.md +15 -15
  5. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  6. package/docs/Guides/Ecosystem.md +14 -15
  7. package/docs/Guides/Index.md +1 -1
  8. package/docs/Guides/Migration-Guide-V4.md +11 -11
  9. package/docs/Guides/Migration-Guide-V5.md +133 -9
  10. package/docs/Guides/Plugins-Guide.md +1 -1
  11. package/docs/Guides/Prototype-Poisoning.md +3 -3
  12. package/docs/Guides/Recommendations.md +9 -9
  13. package/docs/Guides/Serverless.md +5 -5
  14. package/docs/Guides/Testing.md +58 -57
  15. package/docs/Guides/Write-Plugin.md +2 -2
  16. package/docs/Guides/Write-Type-Provider.md +3 -3
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Decorators.md +2 -2
  19. package/docs/Reference/Errors.md +3 -3
  20. package/docs/Reference/Hooks.md +7 -7
  21. package/docs/Reference/LTS.md +8 -0
  22. package/docs/Reference/Logging.md +5 -4
  23. package/docs/Reference/Reply.md +55 -58
  24. package/docs/Reference/Request.md +49 -42
  25. package/docs/Reference/Routes.md +16 -13
  26. package/docs/Reference/Server.md +32 -28
  27. package/docs/Reference/TypeScript.md +9 -9
  28. package/docs/Reference/Validation-and-Serialization.md +5 -5
  29. package/examples/typescript-server.ts +1 -1
  30. package/fastify.d.ts +14 -5
  31. package/fastify.js +8 -6
  32. package/lib/contentTypeParser.js +9 -7
  33. package/lib/context.js +1 -2
  34. package/lib/error-handler.js +9 -9
  35. package/lib/errors.js +1 -1
  36. package/lib/fourOhFour.js +1 -1
  37. package/lib/hooks.js +4 -1
  38. package/lib/{logger.js → logger-factory.js} +70 -122
  39. package/lib/logger-pino.js +68 -0
  40. package/lib/pluginOverride.js +1 -1
  41. package/lib/pluginUtils.js +2 -2
  42. package/lib/reply.js +4 -5
  43. package/lib/request.js +16 -9
  44. package/lib/route.js +23 -22
  45. package/lib/validation.js +2 -2
  46. package/package.json +13 -15
  47. package/test/404s.test.js +675 -629
  48. package/test/500s.test.js +72 -63
  49. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
  50. package/test/als.test.js +48 -45
  51. package/test/async-await.test.js +148 -134
  52. package/test/async-dispose.test.js +4 -5
  53. package/test/async_hooks.test.js +30 -28
  54. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  55. package/test/buffer.test.js +9 -10
  56. package/test/build/error-serializer.test.js +3 -4
  57. package/test/build/version.test.js +2 -3
  58. package/test/build-certificate.js +1 -1
  59. package/test/bundler/README.md +5 -5
  60. package/test/bundler/esbuild/bundler-test.js +10 -9
  61. package/test/bundler/webpack/bundler-test.js +10 -9
  62. package/test/case-insensitive.test.js +31 -28
  63. package/test/chainable.test.js +4 -5
  64. package/test/check.test.js +8 -10
  65. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
  66. package/test/client-timeout.test.js +5 -5
  67. package/test/close-pipelining.test.js +6 -8
  68. package/test/conditional-pino.test.js +47 -0
  69. package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
  70. package/test/constrained-routes.test.js +243 -236
  71. package/test/content-length.test.js +53 -68
  72. package/test/content-parser.test.js +186 -158
  73. package/test/content-type.test.js +8 -9
  74. package/test/context-config.test.js +44 -54
  75. package/test/custom-http-server.test.js +16 -20
  76. package/test/custom-parser.5.test.js +32 -32
  77. package/test/diagnostics-channel/404.test.js +15 -15
  78. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  79. package/test/diagnostics-channel/async-request.test.js +24 -24
  80. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  81. package/test/diagnostics-channel/error-request.test.js +19 -19
  82. package/test/diagnostics-channel/error-status.test.js +8 -8
  83. package/test/diagnostics-channel/init.test.js +6 -7
  84. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  85. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  86. package/test/diagnostics-channel/sync-request.test.js +19 -19
  87. package/test/encapsulated-child-logger-factory.test.js +8 -8
  88. package/test/encapsulated-error-handler.test.js +20 -20
  89. package/test/esm/errorCodes.test.mjs +5 -5
  90. package/test/esm/esm.test.mjs +3 -3
  91. package/test/esm/named-exports.mjs +3 -3
  92. package/test/esm/other.mjs +2 -2
  93. package/test/fastify-instance.test.js +33 -34
  94. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  95. package/test/fluent-schema.test.js +33 -36
  96. package/test/handler-context.test.js +11 -11
  97. package/test/has-route.test.js +12 -15
  98. package/test/header-overflow.test.js +13 -12
  99. package/test/hooks.on-ready.test.js +2 -2
  100. package/test/hooks.test.js +25 -25
  101. package/test/http-methods/copy.test.js +22 -24
  102. package/test/http-methods/custom-http-methods.test.js +24 -21
  103. package/test/http-methods/get.test.js +97 -84
  104. package/test/http-methods/head.test.js +63 -57
  105. package/test/http-methods/lock.test.js +21 -20
  106. package/test/http-methods/mkcalendar.test.js +31 -27
  107. package/test/http-methods/mkcol.test.js +10 -10
  108. package/test/http-methods/move.test.js +11 -11
  109. package/test/http-methods/propfind.test.js +32 -27
  110. package/test/http-methods/proppatch.test.js +21 -19
  111. package/test/http-methods/report.test.js +32 -27
  112. package/test/http-methods/search.test.js +52 -47
  113. package/test/http-methods/trace.test.js +3 -4
  114. package/test/http-methods/unlock.test.js +10 -10
  115. package/test/http2/closing.test.js +50 -58
  116. package/test/http2/constraint.test.js +47 -50
  117. package/test/http2/head.test.js +18 -19
  118. package/test/http2/missing-http2-module.test.js +4 -5
  119. package/test/http2/plain.test.js +31 -31
  120. package/test/http2/secure-with-fallback.test.js +61 -61
  121. package/test/http2/secure.test.js +28 -31
  122. package/test/http2/unknown-http-method.test.js +13 -14
  123. package/test/https/custom-https-server.test.js +6 -7
  124. package/test/https/https.test.js +78 -78
  125. package/test/imports.test.js +5 -6
  126. package/test/internals/all.test.js +8 -11
  127. package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
  128. package/test/internals/context.test.js +9 -11
  129. package/test/internals/decorator.test.js +20 -21
  130. package/test/internals/errors.test.js +427 -427
  131. package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
  132. package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
  133. package/test/internals/hooks.test.js +31 -35
  134. package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
  135. package/test/internals/logger.test.js +28 -28
  136. package/test/internals/plugin.test.js +17 -18
  137. package/test/internals/reply-serialize.test.js +106 -106
  138. package/test/internals/reply.test.js +620 -585
  139. package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
  140. package/test/internals/request-validate.test.js +218 -221
  141. package/test/internals/request.test.js +225 -107
  142. package/test/internals/server.test.js +15 -12
  143. package/test/internals/validation.test.js +35 -36
  144. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  145. package/test/listen.5.test.js +9 -9
  146. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  147. package/test/middleware.test.js +4 -5
  148. package/test/noop-set.test.js +5 -5
  149. package/test/post-empty-body.test.js +18 -11
  150. package/test/pretty-print.test.js +59 -49
  151. package/test/proto-poisoning.test.js +42 -37
  152. package/test/reply-code.test.js +34 -32
  153. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  154. package/test/request-error.test.js +122 -0
  155. package/test/request-header-host.test.js +339 -0
  156. package/test/request-id.test.js +31 -25
  157. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  158. package/test/route.1.test.js +79 -72
  159. package/test/route.2.test.js +17 -16
  160. package/test/route.3.test.js +32 -27
  161. package/test/route.4.test.js +21 -25
  162. package/test/route.5.test.js +45 -64
  163. package/test/route.6.test.js +70 -89
  164. package/test/route.7.test.js +61 -65
  165. package/test/route.8.test.js +80 -18
  166. package/test/router-options.test.js +80 -77
  167. package/test/same-shape.test.js +5 -5
  168. package/test/schema-examples.test.js +72 -38
  169. package/test/serialize-response.test.js +9 -10
  170. package/test/server.test.js +75 -78
  171. package/test/set-error-handler.test.js +2 -3
  172. package/test/stream-serializers.test.js +10 -7
  173. package/test/sync-routes.test.js +18 -18
  174. package/test/test-reporter.mjs +68 -0
  175. package/test/trust-proxy.test.js +51 -45
  176. package/test/type-provider.test.js +8 -6
  177. package/test/types/content-type-parser.test-d.ts +1 -1
  178. package/test/types/fastify.test-d.ts +16 -4
  179. package/test/types/hooks.test-d.ts +2 -1
  180. package/test/types/instance.test-d.ts +13 -13
  181. package/test/types/logger.test-d.ts +2 -2
  182. package/test/types/plugin.test-d.ts +17 -9
  183. package/test/types/register.test-d.ts +22 -6
  184. package/test/types/reply.test-d.ts +1 -1
  185. package/test/types/route.test-d.ts +34 -4
  186. package/test/types/serverFactory.test-d.ts +1 -1
  187. package/test/types/type-provider.test-d.ts +1 -1
  188. package/test/url-rewriting.test.js +35 -38
  189. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  190. package/test/validation-error-handling.test.js +259 -285
  191. package/test/versioned-routes.test.js +126 -113
  192. package/test/web-api.test.js +48 -37
  193. package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
  194. package/types/hooks.d.ts +2 -1
  195. package/types/instance.d.ts +9 -2
  196. package/types/register.d.ts +12 -3
  197. package/types/reply.d.ts +1 -1
  198. package/types/request.d.ts +2 -6
  199. package/types/serverFactory.d.ts +3 -3
  200. package/types/utils.d.ts +13 -5
  201. package/test/types/import.js +0 -2
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Joi = require('joi')
5
5
  const Fastify = require('..')
6
6
 
@@ -19,59 +19,56 @@ function echoBody (req, reply) {
19
19
  reply.code(200).send(req.body.name)
20
20
  }
21
21
 
22
- test('should work with valid payload', t => {
23
- t.plan(3)
22
+ test('should work with valid payload', async (t) => {
23
+ t.plan(2)
24
24
 
25
25
  const fastify = Fastify()
26
26
 
27
27
  fastify.post('/', { schema }, echoBody)
28
28
 
29
- fastify.inject({
29
+ const response = await fastify.inject({
30
30
  method: 'POST',
31
31
  payload: {
32
32
  name: 'michelangelo',
33
33
  work: 'sculptor, painter, architect and poet'
34
34
  },
35
35
  url: '/'
36
- }, (err, res) => {
37
- t.error(err)
38
- t.same(res.payload, 'michelangelo')
39
- t.equal(res.statusCode, 200)
40
36
  })
37
+ t.assert.deepStrictEqual(response.payload, 'michelangelo')
38
+ t.assert.strictEqual(response.statusCode, 200)
41
39
  })
42
40
 
43
- test('should fail immediately with invalid payload', t => {
44
- t.plan(3)
41
+ test('should fail immediately with invalid payload', async (t) => {
42
+ t.plan(2)
45
43
 
46
44
  const fastify = Fastify()
47
45
 
48
46
  fastify.post('/', { schema }, echoBody)
49
47
 
50
- fastify.inject({
48
+ const response = await fastify.inject({
51
49
  method: 'POST',
52
50
  payload: {
53
51
  hello: 'michelangelo'
54
52
  },
55
53
  url: '/'
56
- }, (err, res) => {
57
- t.error(err)
58
- t.same(res.json(), {
59
- statusCode: 400,
60
- code: 'FST_ERR_VALIDATION',
61
- error: 'Bad Request',
62
- message: "body must have required property 'name'"
63
- })
64
- t.equal(res.statusCode, 400)
65
54
  })
55
+
56
+ t.assert.deepStrictEqual(response.json(), {
57
+ statusCode: 400,
58
+ code: 'FST_ERR_VALIDATION',
59
+ error: 'Bad Request',
60
+ message: "body must have required property 'name'"
61
+ })
62
+ t.assert.strictEqual(response.statusCode, 400)
66
63
  })
67
64
 
68
- test('should be able to use setErrorHandler specify custom validation error', t => {
69
- t.plan(3)
65
+ test('should be able to use setErrorHandler specify custom validation error', async (t) => {
66
+ t.plan(2)
70
67
 
71
68
  const fastify = Fastify()
72
69
 
73
70
  fastify.post('/', { schema }, function (req, reply) {
74
- t.fail('should not be here')
71
+ t.assert.fail('should not be here')
75
72
  reply.code(200).send(req.body.name)
76
73
  })
77
74
 
@@ -81,25 +78,24 @@ test('should be able to use setErrorHandler specify custom validation error', t
81
78
  }
82
79
  })
83
80
 
84
- fastify.inject({
81
+ const response = await fastify.inject({
85
82
  method: 'POST',
86
83
  payload: {
87
84
  hello: 'michelangelo'
88
85
  },
89
86
  url: '/'
90
- }, (err, res) => {
91
- t.error(err)
92
- t.same(JSON.parse(res.payload), {
93
- statusCode: 422,
94
- error: 'Unprocessable Entity',
95
- message: 'validation failed'
96
- })
97
- t.equal(res.statusCode, 422)
98
87
  })
88
+
89
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
90
+ statusCode: 422,
91
+ error: 'Unprocessable Entity',
92
+ message: 'validation failed'
93
+ })
94
+ t.assert.strictEqual(response.statusCode, 422)
99
95
  })
100
96
 
101
- test('validation error has 400 statusCode set', t => {
102
- t.plan(3)
97
+ test('validation error has 400 statusCode set', async (t) => {
98
+ t.plan(2)
103
99
 
104
100
  const fastify = Fastify()
105
101
 
@@ -114,23 +110,22 @@ test('validation error has 400 statusCode set', t => {
114
110
 
115
111
  fastify.post('/', { schema }, echoBody)
116
112
 
117
- fastify.inject({
113
+ const response = await fastify.inject({
118
114
  method: 'POST',
119
115
  payload: {
120
116
  hello: 'michelangelo'
121
117
  },
122
118
  url: '/'
123
- }, (err, res) => {
124
- t.error(err)
125
- t.same(res.json(), {
126
- statusCode: 400,
127
- message: "body must have required property 'name'"
128
- })
129
- t.equal(res.statusCode, 400)
130
119
  })
120
+
121
+ t.assert.deepStrictEqual(response.json(), {
122
+ statusCode: 400,
123
+ message: "body must have required property 'name'"
124
+ })
125
+ t.assert.strictEqual(response.statusCode, 400)
131
126
  })
132
127
 
133
- test('error inside custom error handler should have validationContext', t => {
128
+ test('error inside custom error handler should have validationContext', async (t) => {
134
129
  t.plan(1)
135
130
 
136
131
  const fastify = Fastify()
@@ -143,26 +138,26 @@ test('error inside custom error handler should have validationContext', t => {
143
138
  }
144
139
  }
145
140
  }, function (req, reply) {
146
- t.fail('should not be here')
141
+ t.assert.fail('should not be here')
147
142
  reply.code(200).send(req.body.name)
148
143
  })
149
144
 
150
145
  fastify.setErrorHandler(function (error, request, reply) {
151
- t.equal(error.validationContext, 'body')
146
+ t.assert.strictEqual(error.validationContext, 'body')
152
147
  reply.status(500).send(error)
153
148
  })
154
149
 
155
- fastify.inject({
150
+ await fastify.inject({
156
151
  method: 'POST',
157
152
  payload: {
158
153
  name: 'michelangelo',
159
154
  work: 'artist'
160
155
  },
161
156
  url: '/'
162
- }, () => {})
157
+ })
163
158
  })
164
159
 
165
- test('error inside custom error handler should have validationContext if specified by custom error handler', t => {
160
+ test('error inside custom error handler should have validationContext if specified by custom error handler', async (t) => {
166
161
  t.plan(1)
167
162
 
168
163
  const fastify = Fastify()
@@ -177,27 +172,27 @@ test('error inside custom error handler should have validationContext if specifi
177
172
  }
178
173
  }
179
174
  }, function (req, reply) {
180
- t.fail('should not be here')
175
+ t.assert.fail('should not be here')
181
176
  reply.code(200).send(req.body.name)
182
177
  })
183
178
 
184
179
  fastify.setErrorHandler(function (error, request, reply) {
185
- t.equal(error.validationContext, 'customContext')
180
+ t.assert.strictEqual(error.validationContext, 'customContext')
186
181
  reply.status(500).send(error)
187
182
  })
188
183
 
189
- fastify.inject({
184
+ await fastify.inject({
190
185
  method: 'POST',
191
186
  payload: {
192
187
  name: 'michelangelo',
193
188
  work: 'artist'
194
189
  },
195
190
  url: '/'
196
- }, () => {})
191
+ })
197
192
  })
198
193
 
199
- test('should be able to attach validation to request', t => {
200
- t.plan(3)
194
+ test('should be able to attach validation to request', async (t) => {
195
+ t.plan(2)
201
196
 
202
197
  const fastify = Fastify()
203
198
 
@@ -205,56 +200,53 @@ test('should be able to attach validation to request', t => {
205
200
  reply.code(400).send(req.validationError.validation)
206
201
  })
207
202
 
208
- fastify.inject({
203
+ const response = await fastify.inject({
209
204
  method: 'POST',
210
205
  payload: {
211
206
  hello: 'michelangelo'
212
207
  },
213
208
  url: '/'
214
- }, (err, res) => {
215
- t.error(err)
216
-
217
- t.same(res.json(), [{
218
- keyword: 'required',
219
- instancePath: '',
220
- schemaPath: '#/required',
221
- params: { missingProperty: 'name' },
222
- message: 'must have required property \'name\''
223
- }])
224
- t.equal(res.statusCode, 400)
225
209
  })
210
+
211
+ t.assert.deepStrictEqual(response.json(), [{
212
+ keyword: 'required',
213
+ instancePath: '',
214
+ schemaPath: '#/required',
215
+ params: { missingProperty: 'name' },
216
+ message: 'must have required property \'name\''
217
+ }])
218
+ t.assert.strictEqual(response.statusCode, 400)
226
219
  })
227
220
 
228
- test('should respect when attachValidation is explicitly set to false', t => {
229
- t.plan(3)
221
+ test('should respect when attachValidation is explicitly set to false', async (t) => {
222
+ t.plan(2)
230
223
 
231
224
  const fastify = Fastify()
232
225
 
233
226
  fastify.post('/', { schema, attachValidation: false }, function (req, reply) {
234
- t.fail('should not be here')
227
+ t.assert.fail('should not be here')
235
228
  reply.code(200).send(req.validationError.validation)
236
229
  })
237
230
 
238
- fastify.inject({
231
+ const response = await fastify.inject({
239
232
  method: 'POST',
240
233
  payload: {
241
234
  hello: 'michelangelo'
242
235
  },
243
236
  url: '/'
244
- }, (err, res) => {
245
- t.error(err)
246
- t.same(JSON.parse(res.payload), {
247
- statusCode: 400,
248
- code: 'FST_ERR_VALIDATION',
249
- error: 'Bad Request',
250
- message: "body must have required property 'name'"
251
- })
252
- t.equal(res.statusCode, 400)
253
237
  })
238
+
239
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
240
+ statusCode: 400,
241
+ code: 'FST_ERR_VALIDATION',
242
+ error: 'Bad Request',
243
+ message: "body must have required property 'name'"
244
+ })
245
+ t.assert.strictEqual(response.statusCode, 400)
254
246
  })
255
247
 
256
- test('Attached validation error should take precedence over setErrorHandler', t => {
257
- t.plan(3)
248
+ test('Attached validation error should take precedence over setErrorHandler', async (t) => {
249
+ t.plan(2)
258
250
 
259
251
  const fastify = Fastify()
260
252
 
@@ -263,26 +255,25 @@ test('Attached validation error should take precedence over setErrorHandler', t
263
255
  })
264
256
 
265
257
  fastify.setErrorHandler(function (error, request, reply) {
266
- t.fail('should not be here')
258
+ t.assert.fail('should not be here')
267
259
  if (error.validation) {
268
260
  reply.status(422).send(new Error('validation failed'))
269
261
  }
270
262
  })
271
263
 
272
- fastify.inject({
264
+ const response = await fastify.inject({
273
265
  method: 'POST',
274
266
  payload: {
275
267
  hello: 'michelangelo'
276
268
  },
277
269
  url: '/'
278
- }, (err, res) => {
279
- t.error(err)
280
- t.same(res.payload, "Attached: Error: body must have required property 'name'")
281
- t.equal(res.statusCode, 400)
282
270
  })
271
+
272
+ t.assert.deepStrictEqual(response.payload, "Attached: Error: body must have required property 'name'")
273
+ t.assert.strictEqual(response.statusCode, 400)
283
274
  })
284
275
 
285
- test('should handle response validation error', t => {
276
+ test('should handle response validation error', async (t) => {
286
277
  t.plan(2)
287
278
 
288
279
  const response = {
@@ -306,17 +297,17 @@ test('should handle response validation error', t => {
306
297
  }
307
298
  })
308
299
 
309
- fastify.inject({
300
+ const injectResponse = await fastify.inject({
310
301
  method: 'GET',
311
302
  payload: { },
312
303
  url: '/'
313
- }, (err, res) => {
314
- t.error(err)
315
- t.equal(res.payload, '{"statusCode":500,"error":"Internal Server Error","message":"\\"name\\" is required!"}')
316
304
  })
305
+
306
+ t.assert.strictEqual(injectResponse.statusCode, 500)
307
+ t.assert.strictEqual(injectResponse.payload, '{"statusCode":500,"error":"Internal Server Error","message":"\\"name\\" is required!"}')
317
308
  })
318
309
 
319
- test('should handle response validation error with promises', t => {
310
+ test('should handle response validation error with promises', async (t) => {
320
311
  t.plan(2)
321
312
 
322
313
  const response = {
@@ -336,17 +327,17 @@ test('should handle response validation error with promises', t => {
336
327
  return Promise.resolve({ work: 'actor' })
337
328
  })
338
329
 
339
- fastify.inject({
330
+ const injectResponse = await fastify.inject({
340
331
  method: 'GET',
341
332
  payload: { },
342
333
  url: '/'
343
- }, (err, res) => {
344
- t.error(err)
345
- t.equal(res.payload, '{"statusCode":500,"error":"Internal Server Error","message":"\\"name\\" is required!"}')
346
334
  })
335
+
336
+ t.assert.strictEqual(injectResponse.statusCode, 500)
337
+ t.assert.strictEqual(injectResponse.payload, '{"statusCode":500,"error":"Internal Server Error","message":"\\"name\\" is required!"}')
347
338
  })
348
339
 
349
- test('should return a defined output message parsing AJV errors', t => {
340
+ test('should return a defined output message parsing AJV errors', async (t) => {
350
341
  t.plan(2)
351
342
 
352
343
  const body = {
@@ -361,20 +352,20 @@ test('should return a defined output message parsing AJV errors', t => {
361
352
  const fastify = Fastify()
362
353
 
363
354
  fastify.post('/', { schema: { body } }, function (req, reply) {
364
- t.fail()
355
+ t.assert.fail()
365
356
  })
366
357
 
367
- fastify.inject({
358
+ const response = await fastify.inject({
368
359
  method: 'POST',
369
360
  payload: { },
370
361
  url: '/'
371
- }, (err, res) => {
372
- t.error(err)
373
- t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body must have required property \'name\'"}')
374
362
  })
363
+
364
+ t.assert.strictEqual(response.statusCode, 400)
365
+ t.assert.strictEqual(response.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body must have required property \'name\'"}')
375
366
  })
376
367
 
377
- test('should return a defined output message parsing JOI errors', t => {
368
+ test('should return a defined output message parsing JOI errors', async (t) => {
378
369
  t.plan(2)
379
370
 
380
371
  const body = Joi.object().keys({
@@ -391,20 +382,20 @@ test('should return a defined output message parsing JOI errors', t => {
391
382
  }
392
383
  },
393
384
  function (req, reply) {
394
- t.fail()
385
+ t.assert.fail()
395
386
  })
396
387
 
397
- fastify.inject({
388
+ const response = await fastify.inject({
398
389
  method: 'POST',
399
390
  payload: {},
400
391
  url: '/'
401
- }, (err, res) => {
402
- t.error(err)
403
- t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"\\"name\\" is required"}')
404
392
  })
393
+
394
+ t.assert.strictEqual(response.statusCode, 400)
395
+ t.assert.strictEqual(response.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"\\"name\\" is required"}')
405
396
  })
406
397
 
407
- test('should return a defined output message parsing JOI error details', t => {
398
+ test('should return a defined output message parsing JOI error details', async (t) => {
408
399
  t.plan(2)
409
400
 
410
401
  const body = Joi.object().keys({
@@ -424,118 +415,115 @@ test('should return a defined output message parsing JOI error details', t => {
424
415
  }
425
416
  },
426
417
  function (req, reply) {
427
- t.fail()
418
+ t.assert.fail()
428
419
  })
429
420
 
430
- fastify.inject({
421
+ const response = await fastify.inject({
431
422
  method: 'POST',
432
423
  payload: {},
433
424
  url: '/'
434
- }, (err, res) => {
435
- t.error(err)
436
- t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body \\"name\\" is required"}')
437
425
  })
426
+
427
+ t.assert.strictEqual(response.statusCode, 400)
428
+ t.assert.strictEqual(response.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body \\"name\\" is required"}')
438
429
  })
439
430
 
440
- test('the custom error formatter context must be the server instance', t => {
441
- t.plan(4)
431
+ test('the custom error formatter context must be the server instance', async (t) => {
432
+ t.plan(3)
442
433
 
443
434
  const fastify = Fastify()
444
435
 
445
436
  fastify.setSchemaErrorFormatter(function (errors, dataVar) {
446
- t.same(this, fastify)
437
+ t.assert.deepStrictEqual(this, fastify)
447
438
  return new Error('my error')
448
439
  })
449
440
 
450
441
  fastify.post('/', { schema }, echoBody)
451
442
 
452
- fastify.inject({
443
+ const response = await fastify.inject({
453
444
  method: 'POST',
454
445
  payload: {
455
446
  hello: 'michelangelo'
456
447
  },
457
448
  url: '/'
458
- }, (err, res) => {
459
- t.error(err)
460
- t.same(res.json(), {
461
- statusCode: 400,
462
- code: 'FST_ERR_VALIDATION',
463
- error: 'Bad Request',
464
- message: 'my error'
465
- })
466
- t.equal(res.statusCode, 400)
467
449
  })
450
+
451
+ t.assert.deepStrictEqual(response.json(), {
452
+ statusCode: 400,
453
+ code: 'FST_ERR_VALIDATION',
454
+ error: 'Bad Request',
455
+ message: 'my error'
456
+ })
457
+ t.assert.strictEqual(response.statusCode, 400)
468
458
  })
469
459
 
470
- test('the custom error formatter context must be the server instance in options', t => {
471
- t.plan(4)
460
+ test('the custom error formatter context must be the server instance in options', async (t) => {
461
+ t.plan(3)
472
462
 
473
463
  const fastify = Fastify({
474
464
  schemaErrorFormatter: function (errors, dataVar) {
475
- t.same(this, fastify)
465
+ t.assert.deepStrictEqual(this, fastify)
476
466
  return new Error('my error')
477
467
  }
478
468
  })
479
469
 
480
470
  fastify.post('/', { schema }, echoBody)
481
471
 
482
- fastify.inject({
472
+ const response = await fastify.inject({
483
473
  method: 'POST',
484
474
  payload: {
485
475
  hello: 'michelangelo'
486
476
  },
487
477
  url: '/'
488
- }, (err, res) => {
489
- t.error(err)
490
- t.same(res.json(), {
491
- statusCode: 400,
492
- code: 'FST_ERR_VALIDATION',
493
- error: 'Bad Request',
494
- message: 'my error'
495
- })
496
- t.equal(res.statusCode, 400)
497
478
  })
479
+
480
+ t.assert.deepStrictEqual(response.json(), {
481
+ statusCode: 400,
482
+ code: 'FST_ERR_VALIDATION',
483
+ error: 'Bad Request',
484
+ message: 'my error'
485
+ })
486
+ t.assert.strictEqual(response.statusCode, 400)
498
487
  })
499
488
 
500
- test('should call custom error formatter', t => {
501
- t.plan(9)
489
+ test('should call custom error formatter', async (t) => {
490
+ t.plan(8)
502
491
 
503
492
  const fastify = Fastify({
504
493
  schemaErrorFormatter: (errors, dataVar) => {
505
- t.equal(errors.length, 1)
506
- t.equal(errors[0].message, "must have required property 'name'")
507
- t.equal(errors[0].keyword, 'required')
508
- t.equal(errors[0].schemaPath, '#/required')
509
- t.same(errors[0].params, {
494
+ t.assert.strictEqual(errors.length, 1)
495
+ t.assert.strictEqual(errors[0].message, "must have required property 'name'")
496
+ t.assert.strictEqual(errors[0].keyword, 'required')
497
+ t.assert.strictEqual(errors[0].schemaPath, '#/required')
498
+ t.assert.deepStrictEqual(errors[0].params, {
510
499
  missingProperty: 'name'
511
500
  })
512
- t.equal(dataVar, 'body')
501
+ t.assert.strictEqual(dataVar, 'body')
513
502
  return new Error('my error')
514
503
  }
515
504
  })
516
505
 
517
506
  fastify.post('/', { schema }, echoBody)
518
507
 
519
- fastify.inject({
508
+ const response = await fastify.inject({
520
509
  method: 'POST',
521
510
  payload: {
522
511
  hello: 'michelangelo'
523
512
  },
524
513
  url: '/'
525
- }, (err, res) => {
526
- t.error(err)
527
- t.same(res.json(), {
528
- statusCode: 400,
529
- code: 'FST_ERR_VALIDATION',
530
- error: 'Bad Request',
531
- message: 'my error'
532
- })
533
- t.equal(res.statusCode, 400)
534
514
  })
515
+
516
+ t.assert.deepStrictEqual(response.json(), {
517
+ statusCode: 400,
518
+ code: 'FST_ERR_VALIDATION',
519
+ error: 'Bad Request',
520
+ message: 'my error'
521
+ })
522
+ t.assert.strictEqual(response.statusCode, 400)
535
523
  })
536
524
 
537
- test('should catch error inside formatter and return message', t => {
538
- t.plan(3)
525
+ test('should catch error inside formatter and return message', async (t) => {
526
+ t.plan(2)
539
527
 
540
528
  const fastify = Fastify({
541
529
  schemaErrorFormatter: (errors, dataVar) => {
@@ -545,25 +533,23 @@ test('should catch error inside formatter and return message', t => {
545
533
 
546
534
  fastify.post('/', { schema }, echoBody)
547
535
 
548
- fastify.inject({
536
+ const response = await fastify.inject({
549
537
  method: 'POST',
550
538
  payload: {
551
539
  hello: 'michelangelo'
552
540
  },
553
541
  url: '/'
554
- }, (err, res) => {
555
- t.error(err)
556
- t.same(res.json(), {
557
- statusCode: 500,
558
- error: 'Internal Server Error',
559
- message: 'abc'
560
- })
561
- t.equal(res.statusCode, 500)
562
- t.end()
563
542
  })
543
+
544
+ t.assert.deepStrictEqual(response.json(), {
545
+ statusCode: 500,
546
+ error: 'Internal Server Error',
547
+ message: 'abc'
548
+ })
549
+ t.assert.strictEqual(response.statusCode, 500)
564
550
  })
565
551
 
566
- test('cannot create a fastify instance with wrong type of errorFormatter', t => {
552
+ test('cannot create a fastify instance with wrong type of errorFormatter', async (t) => {
567
553
  t.plan(3)
568
554
 
569
555
  try {
@@ -573,7 +559,7 @@ test('cannot create a fastify instance with wrong type of errorFormatter', t =>
573
559
  }
574
560
  })
575
561
  } catch (err) {
576
- t.equal(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
562
+ t.assert.strictEqual(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
577
563
  }
578
564
 
579
565
  try {
@@ -581,19 +567,19 @@ test('cannot create a fastify instance with wrong type of errorFormatter', t =>
581
567
  schemaErrorFormatter: 500
582
568
  })
583
569
  } catch (err) {
584
- t.equal(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
570
+ t.assert.strictEqual(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
585
571
  }
586
572
 
587
573
  try {
588
574
  const fastify = Fastify()
589
575
  fastify.setSchemaErrorFormatter(500)
590
576
  } catch (err) {
591
- t.equal(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
577
+ t.assert.strictEqual(err.code, 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN')
592
578
  }
593
579
  })
594
580
 
595
- test('should register a route based schema error formatter', t => {
596
- t.plan(3)
581
+ test('should register a route based schema error formatter', async (t) => {
582
+ t.plan(2)
597
583
 
598
584
  const fastify = Fastify()
599
585
 
@@ -604,27 +590,25 @@ test('should register a route based schema error formatter', t => {
604
590
  }
605
591
  }, echoBody)
606
592
 
607
- fastify.inject({
593
+ const response = await fastify.inject({
608
594
  method: 'POST',
609
595
  payload: {
610
596
  hello: 'michelangelo'
611
597
  },
612
598
  url: '/'
613
- }, (err, res) => {
614
- t.error(err)
615
- t.same(res.json(), {
616
- statusCode: 400,
617
- code: 'FST_ERR_VALIDATION',
618
- error: 'Bad Request',
619
- message: 'abc'
620
- })
621
- t.equal(res.statusCode, 400)
622
- t.end()
623
599
  })
600
+
601
+ t.assert.deepStrictEqual(response.json(), {
602
+ statusCode: 400,
603
+ code: 'FST_ERR_VALIDATION',
604
+ error: 'Bad Request',
605
+ message: 'abc'
606
+ })
607
+ t.assert.strictEqual(response.statusCode, 400)
624
608
  })
625
609
 
626
- test('prefer route based error formatter over global one', t => {
627
- t.plan(9)
610
+ test('prefer route based error formatter over global one', async (t) => {
611
+ t.plan(6)
628
612
 
629
613
  const fastify = Fastify({
630
614
  schemaErrorFormatter: (errors, dataVar) => {
@@ -648,60 +632,57 @@ test('prefer route based error formatter over global one', t => {
648
632
 
649
633
  fastify.post('/test', { schema }, echoBody)
650
634
 
651
- fastify.inject({
635
+ const response1 = await fastify.inject({
652
636
  method: 'POST',
653
637
  payload: {
654
638
  hello: 'michelangelo'
655
639
  },
656
640
  url: '/'
657
- }, (err, res) => {
658
- t.error(err)
659
- t.same(res.json(), {
660
- statusCode: 400,
661
- code: 'FST_ERR_VALIDATION',
662
- error: 'Bad Request',
663
- message: '123'
664
- })
665
- t.equal(res.statusCode, 400)
666
641
  })
667
642
 
668
- fastify.inject({
643
+ t.assert.deepStrictEqual(response1.json(), {
644
+ statusCode: 400,
645
+ code: 'FST_ERR_VALIDATION',
646
+ error: 'Bad Request',
647
+ message: '123'
648
+ })
649
+ t.assert.strictEqual(response1.statusCode, 400)
650
+
651
+ const response2 = await fastify.inject({
669
652
  method: 'POST',
670
653
  payload: {
671
654
  hello: 'michelangelo'
672
655
  },
673
656
  url: '/abc'
674
- }, (err, res) => {
675
- t.error(err)
676
- t.same(res.json(), {
677
- statusCode: 400,
678
- code: 'FST_ERR_VALIDATION',
679
- error: 'Bad Request',
680
- message: 'abc'
681
- })
682
- t.equal(res.statusCode, 400)
683
657
  })
684
658
 
685
- fastify.inject({
659
+ t.assert.deepStrictEqual(response2.json(), {
660
+ statusCode: 400,
661
+ code: 'FST_ERR_VALIDATION',
662
+ error: 'Bad Request',
663
+ message: 'abc'
664
+ })
665
+ t.assert.strictEqual(response2.statusCode, 400)
666
+
667
+ const response3 = await fastify.inject({
686
668
  method: 'POST',
687
669
  payload: {
688
670
  hello: 'michelangelo'
689
671
  },
690
672
  url: '/test'
691
- }, (err, res) => {
692
- t.error(err)
693
- t.same(res.json(), {
694
- statusCode: 400,
695
- code: 'FST_ERR_VALIDATION',
696
- error: 'Bad Request',
697
- message: 'abc123'
698
- })
699
- t.equal(res.statusCode, 400)
700
673
  })
674
+
675
+ t.assert.deepStrictEqual(response3.json(), {
676
+ statusCode: 400,
677
+ code: 'FST_ERR_VALIDATION',
678
+ error: 'Bad Request',
679
+ message: 'abc123'
680
+ })
681
+ t.assert.strictEqual(response3.statusCode, 400)
701
682
  })
702
683
 
703
- test('adding schemaErrorFormatter', t => {
704
- t.plan(3)
684
+ test('adding schemaErrorFormatter', async (t) => {
685
+ t.plan(2)
705
686
 
706
687
  const fastify = Fastify()
707
688
 
@@ -711,27 +692,25 @@ test('adding schemaErrorFormatter', t => {
711
692
 
712
693
  fastify.post('/', { schema }, echoBody)
713
694
 
714
- fastify.inject({
695
+ const response = await fastify.inject({
715
696
  method: 'POST',
716
697
  payload: {
717
698
  hello: 'michelangelo'
718
699
  },
719
700
  url: '/'
720
- }, (err, res) => {
721
- t.error(err)
722
- t.same(res.json(), {
723
- statusCode: 400,
724
- code: 'FST_ERR_VALIDATION',
725
- error: 'Bad Request',
726
- message: 'abc'
727
- })
728
- t.equal(res.statusCode, 400)
729
- t.end()
730
701
  })
702
+
703
+ t.assert.deepStrictEqual(response.json(), {
704
+ statusCode: 400,
705
+ code: 'FST_ERR_VALIDATION',
706
+ error: 'Bad Request',
707
+ message: 'abc'
708
+ })
709
+ t.assert.strictEqual(response.statusCode, 400)
731
710
  })
732
711
 
733
- test('plugin override', t => {
734
- t.plan(15)
712
+ test('plugin override', async (t) => {
713
+ t.plan(10)
735
714
 
736
715
  const fastify = Fastify({
737
716
  schemaErrorFormatter: (errors, dataVar) => {
@@ -772,88 +751,83 @@ test('plugin override', t => {
772
751
  }
773
752
  }, echoBody)
774
753
 
775
- fastify.inject({
754
+ const response1 = await fastify.inject({
776
755
  method: 'POST',
777
756
  payload: {
778
757
  hello: 'michelangelo'
779
758
  },
780
759
  url: '/'
781
- }, (err, res) => {
782
- t.error(err)
783
- t.same(res.json(), {
784
- statusCode: 400,
785
- code: 'FST_ERR_VALIDATION',
786
- error: 'Bad Request',
787
- message: 'A'
788
- })
789
- t.equal(res.statusCode, 400)
790
760
  })
791
761
 
792
- fastify.inject({
762
+ t.assert.deepStrictEqual(response1.json(), {
763
+ statusCode: 400,
764
+ code: 'FST_ERR_VALIDATION',
765
+ error: 'Bad Request',
766
+ message: 'A'
767
+ })
768
+ t.assert.strictEqual(response1.statusCode, 400)
769
+
770
+ const response2 = await fastify.inject({
793
771
  method: 'POST',
794
772
  payload: {
795
773
  hello: 'michelangelo'
796
774
  },
797
775
  url: '/b'
798
- }, (err, res) => {
799
- t.error(err)
800
- t.same(res.json(), {
801
- statusCode: 400,
802
- code: 'FST_ERR_VALIDATION',
803
- error: 'Bad Request',
804
- message: 'B'
805
- })
806
- t.equal(res.statusCode, 400)
807
776
  })
808
777
 
809
- fastify.inject({
778
+ t.assert.deepStrictEqual(response2.json(), {
779
+ statusCode: 400,
780
+ code: 'FST_ERR_VALIDATION',
781
+ error: 'Bad Request',
782
+ message: 'B'
783
+ })
784
+ t.assert.strictEqual(response2.statusCode, 400)
785
+
786
+ const response3 = await fastify.inject({
810
787
  method: 'POST',
811
788
  payload: {
812
789
  hello: 'michelangelo'
813
790
  },
814
791
  url: '/c'
815
- }, (err, res) => {
816
- t.error(err)
817
- t.same(res.json(), {
818
- statusCode: 400,
819
- code: 'FST_ERR_VALIDATION',
820
- error: 'Bad Request',
821
- message: 'C'
822
- })
823
- t.equal(res.statusCode, 400)
824
792
  })
825
793
 
826
- fastify.inject({
794
+ t.assert.deepStrictEqual(response3.json(), {
795
+ statusCode: 400,
796
+ code: 'FST_ERR_VALIDATION',
797
+ error: 'Bad Request',
798
+ message: 'C'
799
+ })
800
+ t.assert.strictEqual(response3.statusCode, 400)
801
+
802
+ const response4 = await fastify.inject({
827
803
  method: 'POST',
828
804
  payload: {
829
805
  hello: 'michelangelo'
830
806
  },
831
807
  url: '/d'
832
- }, (err, res) => {
833
- t.error(err)
834
- t.same(res.json(), {
835
- statusCode: 400,
836
- code: 'FST_ERR_VALIDATION',
837
- error: 'Bad Request',
838
- message: 'D'
839
- })
840
- t.equal(res.statusCode, 400)
841
808
  })
842
809
 
843
- fastify.inject({
810
+ t.assert.deepStrictEqual(response4.json(), {
811
+ statusCode: 400,
812
+ code: 'FST_ERR_VALIDATION',
813
+ error: 'Bad Request',
814
+ message: 'D'
815
+ })
816
+ t.assert.strictEqual(response4.statusCode, 400)
817
+
818
+ const response5 = await fastify.inject({
844
819
  method: 'POST',
845
820
  payload: {
846
821
  hello: 'michelangelo'
847
822
  },
848
823
  url: '/stillC'
849
- }, (err, res) => {
850
- t.error(err)
851
- t.same(res.json(), {
852
- statusCode: 400,
853
- code: 'FST_ERR_VALIDATION',
854
- error: 'Bad Request',
855
- message: 'C'
856
- })
857
- t.equal(res.statusCode, 400)
858
824
  })
825
+
826
+ t.assert.deepStrictEqual(response5.json(), {
827
+ statusCode: 400,
828
+ code: 'FST_ERR_VALIDATION',
829
+ error: 'Bad Request',
830
+ message: 'C'
831
+ })
832
+ t.assert.strictEqual(response5.statusCode, 400)
859
833
  })