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,11 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../fastify')
6
5
 
7
- test('Should register a host constrained route', t => {
8
- t.plan(7)
6
+ test('Should register a host constrained route', async t => {
7
+ t.plan(4)
9
8
  const fastify = Fastify()
10
9
 
11
10
  fastify.route({
@@ -17,40 +16,41 @@ test('Should register a host constrained route', t => {
17
16
  }
18
17
  })
19
18
 
20
- fastify.inject({
21
- method: 'GET',
22
- url: '/',
23
- headers: {
24
- host: 'fastify.dev'
25
- }
26
- }, (err, res) => {
27
- t.error(err)
28
- t.same(JSON.parse(res.payload), { hello: 'world' })
29
- t.equal(res.statusCode, 200)
30
- })
19
+ {
20
+ const res = await fastify.inject({
21
+ method: 'GET',
22
+ url: '/',
23
+ headers: {
24
+ host: 'fastify.dev'
25
+ }
26
+ })
27
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
28
+ t.assert.strictEqual(res.statusCode, 200)
29
+ }
31
30
 
32
- fastify.inject({
33
- method: 'GET',
34
- url: '/',
35
- headers: {
36
- host: 'example.com'
37
- }
38
- }, (err, res) => {
39
- t.error(err)
40
- t.equal(res.statusCode, 404)
41
- })
31
+ {
32
+ const res = await fastify.inject({
33
+ method: 'GET',
34
+ url: '/',
35
+ headers: {
36
+ host: 'example.com'
37
+ }
38
+ })
42
39
 
43
- fastify.inject({
44
- method: 'GET',
45
- url: '/'
46
- }, (err, res) => {
47
- t.error(err)
48
- t.equal(res.statusCode, 404)
49
- })
40
+ t.assert.strictEqual(res.statusCode, 404)
41
+ }
42
+
43
+ {
44
+ const res = await fastify.inject({
45
+ method: 'GET',
46
+ url: '/'
47
+ })
48
+ t.assert.strictEqual(res.statusCode, 404)
49
+ }
50
50
  })
51
51
 
52
- test('Should register the same route with host constraints', t => {
53
- t.plan(8)
52
+ test('Should register the same route with host constraints', async t => {
53
+ t.plan(5)
54
54
  const fastify = Fastify()
55
55
 
56
56
  fastify.route({
@@ -71,44 +71,45 @@ test('Should register the same route with host constraints', t => {
71
71
  }
72
72
  })
73
73
 
74
- fastify.inject({
75
- method: 'GET',
76
- url: '/',
77
- headers: {
78
- host: 'fastify.dev'
79
- }
80
- }, (err, res) => {
81
- t.error(err)
82
- t.equal(res.statusCode, 200)
83
- t.equal(res.payload, 'fastify.dev')
84
- })
74
+ {
75
+ const res = await fastify.inject({
76
+ method: 'GET',
77
+ url: '/',
78
+ headers: {
79
+ host: 'fastify.dev'
80
+ }
81
+ })
82
+ t.assert.strictEqual(res.statusCode, 200)
83
+ t.assert.strictEqual(res.payload, 'fastify.dev')
84
+ }
85
85
 
86
- fastify.inject({
87
- method: 'GET',
88
- url: '/',
89
- headers: {
90
- host: 'example.com'
91
- }
92
- }, (err, res) => {
93
- t.error(err)
94
- t.equal(res.statusCode, 200)
95
- t.equal(res.payload, 'example.com')
96
- })
86
+ {
87
+ const res = await fastify.inject({
88
+ method: 'GET',
89
+ url: '/',
90
+ headers: {
91
+ host: 'example.com'
92
+ }
93
+ })
97
94
 
98
- fastify.inject({
99
- method: 'GET',
100
- url: '/',
101
- headers: {
102
- host: 'fancy.ca'
103
- }
104
- }, (err, res) => {
105
- t.error(err)
106
- t.equal(res.statusCode, 404)
107
- })
95
+ t.assert.strictEqual(res.statusCode, 200)
96
+ t.assert.strictEqual(res.payload, 'example.com')
97
+ }
98
+
99
+ {
100
+ const res = await fastify.inject({
101
+ method: 'GET',
102
+ url: '/',
103
+ headers: {
104
+ host: 'fancy.ca'
105
+ }
106
+ })
107
+ t.assert.strictEqual(res.statusCode, 404)
108
+ }
108
109
  })
109
110
 
110
- test('Should allow registering custom constrained routes', t => {
111
- t.plan(8)
111
+ test('Should allow registering custom constrained routes', async t => {
112
+ t.plan(5)
112
113
 
113
114
  const constraint = {
114
115
  name: 'secret',
@@ -145,44 +146,44 @@ test('Should allow registering custom constrained routes', t => {
145
146
  }
146
147
  })
147
148
 
148
- fastify.inject({
149
- method: 'GET',
150
- url: '/',
151
- headers: {
152
- 'X-Secret': 'alpha'
153
- }
154
- }, (err, res) => {
155
- t.error(err)
156
- t.same(JSON.parse(res.payload), { hello: 'from alpha' })
157
- t.equal(res.statusCode, 200)
158
- })
149
+ {
150
+ const res = await fastify.inject({
151
+ method: 'GET',
152
+ url: '/',
153
+ headers: {
154
+ 'X-Secret': 'alpha'
155
+ }
156
+ })
157
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from alpha' })
158
+ t.assert.strictEqual(res.statusCode, 200)
159
+ }
159
160
 
160
- fastify.inject({
161
- method: 'GET',
162
- url: '/',
163
- headers: {
164
- 'X-Secret': 'beta'
165
- }
166
- }, (err, res) => {
167
- t.error(err)
168
- t.same(JSON.parse(res.payload), { hello: 'from beta' })
169
- t.equal(res.statusCode, 200)
170
- })
161
+ {
162
+ const res = await fastify.inject({
163
+ method: 'GET',
164
+ url: '/',
165
+ headers: {
166
+ 'X-Secret': 'beta'
167
+ }
168
+ })
169
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from beta' })
170
+ t.assert.strictEqual(res.statusCode, 200)
171
+ }
171
172
 
172
- fastify.inject({
173
- method: 'GET',
174
- url: '/',
175
- headers: {
176
- 'X-Secret': 'gamma'
177
- }
178
- }, (err, res) => {
179
- t.error(err)
180
- t.equal(res.statusCode, 404)
181
- })
173
+ {
174
+ const res = await fastify.inject({
175
+ method: 'GET',
176
+ url: '/',
177
+ headers: {
178
+ 'X-Secret': 'gamma'
179
+ }
180
+ })
181
+ t.assert.strictEqual(res.statusCode, 404)
182
+ }
182
183
  })
183
184
 
184
- test('Should allow registering custom constrained routes outside constructor', t => {
185
- t.plan(8)
185
+ test('Should allow registering custom constrained routes outside constructor', async t => {
186
+ t.plan(5)
186
187
 
187
188
  const constraint = {
188
189
  name: 'secret',
@@ -220,43 +221,44 @@ test('Should allow registering custom constrained routes outside constructor', t
220
221
  }
221
222
  })
222
223
 
223
- fastify.inject({
224
- method: 'GET',
225
- url: '/',
226
- headers: {
227
- 'X-Secret': 'alpha'
228
- }
229
- }, (err, res) => {
230
- t.error(err)
231
- t.same(JSON.parse(res.payload), { hello: 'from alpha' })
232
- t.equal(res.statusCode, 200)
233
- })
224
+ {
225
+ const res = await fastify.inject({
226
+ method: 'GET',
227
+ url: '/',
228
+ headers: {
229
+ 'X-Secret': 'alpha'
230
+ }
231
+ })
234
232
 
235
- fastify.inject({
236
- method: 'GET',
237
- url: '/',
238
- headers: {
239
- 'X-Secret': 'beta'
240
- }
241
- }, (err, res) => {
242
- t.error(err)
243
- t.same(JSON.parse(res.payload), { hello: 'from beta' })
244
- t.equal(res.statusCode, 200)
245
- })
233
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from alpha' })
234
+ t.assert.strictEqual(res.statusCode, 200)
235
+ }
246
236
 
247
- fastify.inject({
248
- method: 'GET',
249
- url: '/',
250
- headers: {
251
- 'X-Secret': 'gamma'
252
- }
253
- }, (err, res) => {
254
- t.error(err)
255
- t.equal(res.statusCode, 404)
256
- })
237
+ {
238
+ const res = await fastify.inject({
239
+ method: 'GET',
240
+ url: '/',
241
+ headers: {
242
+ 'X-Secret': 'beta'
243
+ }
244
+ })
245
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from beta' })
246
+ t.assert.strictEqual(res.statusCode, 200)
247
+ }
248
+
249
+ {
250
+ const res = await fastify.inject({
251
+ method: 'GET',
252
+ url: '/',
253
+ headers: {
254
+ 'X-Secret': 'gamma'
255
+ }
256
+ })
257
+ t.assert.strictEqual(res.statusCode, 404)
258
+ }
257
259
  })
258
260
 
259
- test('Custom constrained routes registered also for HEAD method generated by fastify', t => {
261
+ test('Custom constrained routes registered also for HEAD method generated by fastify', (t, done) => {
260
262
  t.plan(3)
261
263
 
262
264
  const constraint = {
@@ -292,13 +294,14 @@ test('Custom constrained routes registered also for HEAD method generated by fas
292
294
  'X-Secret': 'mySecret'
293
295
  }
294
296
  }, (err, res) => {
295
- t.error(err)
296
- t.same(res.headers['content-length'], '31')
297
- t.equal(res.statusCode, 200)
297
+ t.assert.ifError(err)
298
+ t.assert.deepStrictEqual(res.headers['content-length'], '31')
299
+ t.assert.strictEqual(res.statusCode, 200)
300
+ done()
298
301
  })
299
302
  })
300
303
 
301
- test('Custom constrained routes registered with addConstraintStrategy apply also for HEAD method generated by fastify', t => {
304
+ test('Custom constrained routes registered with addConstraintStrategy apply also for HEAD method generated by fastify', (t, done) => {
302
305
  t.plan(3)
303
306
 
304
307
  const constraint = {
@@ -335,13 +338,14 @@ test('Custom constrained routes registered with addConstraintStrategy apply also
335
338
  'X-Secret': 'mySecret'
336
339
  }
337
340
  }, (err, res) => {
338
- t.error(err)
339
- t.same(res.headers['content-length'], '31')
340
- t.equal(res.statusCode, 200)
341
+ t.assert.ifError(err)
342
+ t.assert.deepStrictEqual(res.headers['content-length'], '31')
343
+ t.assert.strictEqual(res.statusCode, 200)
344
+ done()
341
345
  })
342
346
  })
343
347
 
344
- test('Add a constraint strategy after fastify instance was started', t => {
348
+ test('Add a constraint strategy after fastify instance was started', (t, done) => {
345
349
  t.plan(4)
346
350
 
347
351
  const constraint = {
@@ -371,14 +375,14 @@ test('Add a constraint strategy after fastify instance was started', t => {
371
375
  method: 'GET',
372
376
  url: '/'
373
377
  }, (err, res) => {
374
- t.error(err)
375
- t.same(res.payload, 'ok')
376
- t.equal(res.statusCode, 200)
378
+ t.assert.ifError(err)
379
+ t.assert.deepStrictEqual(res.payload, 'ok')
380
+ t.assert.strictEqual(res.statusCode, 200)
377
381
 
378
- t.throws(
379
- () => fastify.addConstraintStrategy(constraint),
380
- 'Cannot add constraint strategy when fastify instance is already started!'
382
+ t.assert.throws(
383
+ () => fastify.addConstraintStrategy(constraint)
381
384
  )
385
+ done()
382
386
  })
383
387
  })
384
388
 
@@ -403,9 +407,9 @@ test('Add a constraint strategy should throw an error if there already exist cus
403
407
  const fastify = Fastify()
404
408
 
405
409
  fastify.addConstraintStrategy(constraint)
406
- t.throws(
410
+ t.assert.throws(
407
411
  () => fastify.addConstraintStrategy(constraint),
408
- 'There already exists a custom constraint with the name secret.'
412
+ /^Error: There already exists a custom constraint with the name secret.$/
409
413
  )
410
414
  })
411
415
 
@@ -430,7 +434,7 @@ test('Add a constraint strategy shouldn\'t throw an error if default constraint
430
434
  const fastify = Fastify()
431
435
  fastify.addConstraintStrategy(constraint)
432
436
 
433
- t.pass()
437
+ t.assert.ok(true)
434
438
  })
435
439
 
436
440
  test('Add a constraint strategy should throw an error if default constraint with the same name is used', t => {
@@ -462,9 +466,9 @@ test('Add a constraint strategy should throw an error if default constraint with
462
466
  }
463
467
  })
464
468
 
465
- t.throws(
469
+ t.assert.throws(
466
470
  () => fastify.addConstraintStrategy(constraint),
467
- 'There already exists a route with version constraint.'
471
+ /^Error: There already exists a route with version constraint.$/
468
472
  )
469
473
  })
470
474
 
@@ -473,8 +477,8 @@ test('The hasConstraintStrategy should return false for default constraints unti
473
477
 
474
478
  const fastify = Fastify()
475
479
 
476
- t.equal(fastify.hasConstraintStrategy('version'), false)
477
- t.equal(fastify.hasConstraintStrategy('host'), false)
480
+ t.assert.strictEqual(fastify.hasConstraintStrategy('version'), false)
481
+ t.assert.strictEqual(fastify.hasConstraintStrategy('host'), false)
478
482
 
479
483
  fastify.route({
480
484
  method: 'GET',
@@ -485,8 +489,8 @@ test('The hasConstraintStrategy should return false for default constraints unti
485
489
  }
486
490
  })
487
491
 
488
- t.equal(fastify.hasConstraintStrategy('version'), false)
489
- t.equal(fastify.hasConstraintStrategy('host'), true)
492
+ t.assert.strictEqual(fastify.hasConstraintStrategy('version'), false)
493
+ t.assert.strictEqual(fastify.hasConstraintStrategy('host'), true)
490
494
 
491
495
  fastify.route({
492
496
  method: 'GET',
@@ -497,8 +501,8 @@ test('The hasConstraintStrategy should return false for default constraints unti
497
501
  }
498
502
  })
499
503
 
500
- t.equal(fastify.hasConstraintStrategy('version'), true)
501
- t.equal(fastify.hasConstraintStrategy('host'), true)
504
+ t.assert.strictEqual(fastify.hasConstraintStrategy('version'), true)
505
+ t.assert.strictEqual(fastify.hasConstraintStrategy('host'), true)
502
506
  })
503
507
 
504
508
  test('The hasConstraintStrategy should return true if there already exist a custom constraint with the same name', t => {
@@ -521,13 +525,13 @@ test('The hasConstraintStrategy should return true if there already exist a cust
521
525
 
522
526
  const fastify = Fastify()
523
527
 
524
- t.equal(fastify.hasConstraintStrategy('secret'), false)
528
+ t.assert.strictEqual(fastify.hasConstraintStrategy('secret'), false)
525
529
  fastify.addConstraintStrategy(constraint)
526
- t.equal(fastify.hasConstraintStrategy('secret'), true)
530
+ t.assert.strictEqual(fastify.hasConstraintStrategy('secret'), true)
527
531
  })
528
532
 
529
- test('Should allow registering an unconstrained route after a constrained route', t => {
530
- t.plan(6)
533
+ test('Should allow registering an unconstrained route after a constrained route', async t => {
534
+ t.plan(4)
531
535
  const fastify = Fastify()
532
536
 
533
537
  fastify.route({
@@ -547,32 +551,32 @@ test('Should allow registering an unconstrained route after a constrained route'
547
551
  }
548
552
  })
549
553
 
550
- fastify.inject({
551
- method: 'GET',
552
- url: '/',
553
- headers: {
554
- host: 'fastify.dev'
555
- }
556
- }, (err, res) => {
557
- t.error(err)
558
- t.same(JSON.parse(res.payload), { hello: 'from fastify.dev' })
559
- t.equal(res.statusCode, 200)
560
- })
554
+ {
555
+ const res = await fastify.inject({
556
+ method: 'GET',
557
+ url: '/',
558
+ headers: {
559
+ host: 'fastify.dev'
560
+ }
561
+ })
562
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from fastify.dev' })
563
+ t.assert.strictEqual(res.statusCode, 200)
564
+ }
561
565
 
562
- fastify.inject({
563
- method: 'GET',
564
- url: '/',
565
- headers: {
566
- host: 'example.com'
567
- }
568
- }, (err, res) => {
569
- t.error(err)
570
- t.same(JSON.parse(res.payload), { hello: 'from any other domain' })
571
- t.equal(res.statusCode, 200)
572
- })
566
+ {
567
+ const res = await fastify.inject({
568
+ method: 'GET',
569
+ url: '/',
570
+ headers: {
571
+ host: 'example.com'
572
+ }
573
+ })
574
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from any other domain' })
575
+ t.assert.strictEqual(res.statusCode, 200)
576
+ }
573
577
  })
574
578
 
575
- test('Should allow registering constrained routes in a prefixed plugin', t => {
579
+ test('Should allow registering constrained routes in a prefixed plugin', (t, done) => {
576
580
  t.plan(3)
577
581
 
578
582
  const fastify = Fastify()
@@ -595,13 +599,14 @@ test('Should allow registering constrained routes in a prefixed plugin', t => {
595
599
  host: 'fastify.dev'
596
600
  }
597
601
  }, (err, res) => {
598
- t.error(err)
599
- t.same(JSON.parse(res.payload), { ok: true })
600
- t.equal(res.statusCode, 200)
602
+ t.assert.ifError(err)
603
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { ok: true })
604
+ t.assert.strictEqual(res.statusCode, 200)
605
+ done()
601
606
  })
602
607
  })
603
608
 
604
- test('Should allow registering a constrained GET route after a constrained HEAD route', t => {
609
+ test('Should allow registering a constrained GET route after a constrained HEAD route', (t, done) => {
605
610
  t.plan(3)
606
611
  const fastify = Fastify()
607
612
 
@@ -631,13 +636,14 @@ test('Should allow registering a constrained GET route after a constrained HEAD
631
636
  host: 'fastify.dev'
632
637
  }
633
638
  }, (err, res) => {
634
- t.error(err)
635
- t.same(res.payload, 'custom HEAD response')
636
- t.equal(res.statusCode, 200)
639
+ t.assert.ifError(err)
640
+ t.assert.deepStrictEqual(res.payload, 'custom HEAD response')
641
+ t.assert.strictEqual(res.statusCode, 200)
642
+ done()
637
643
  })
638
644
  })
639
645
 
640
- test('Should allow registering a constrained GET route after an unconstrained HEAD route', t => {
646
+ test('Should allow registering a constrained GET route after an unconstrained HEAD route', (t, done) => {
641
647
  t.plan(3)
642
648
  const fastify = Fastify()
643
649
 
@@ -667,9 +673,10 @@ test('Should allow registering a constrained GET route after an unconstrained HE
667
673
  host: 'fastify.dev'
668
674
  }
669
675
  }, (err, res) => {
670
- t.error(err)
671
- t.same(res.headers['content-length'], '41')
672
- t.equal(res.statusCode, 200)
676
+ t.assert.ifError(err)
677
+ t.assert.deepStrictEqual(res.headers['content-length'], '41')
678
+ t.assert.strictEqual(res.statusCode, 200)
679
+ done()
673
680
  })
674
681
  })
675
682
 
@@ -702,7 +709,7 @@ test('Will not try to re-createprefixed HEAD route if it already exists and expo
702
709
 
703
710
  await fastify.ready()
704
711
 
705
- t.ok(true)
712
+ t.assert.ok(true)
706
713
  })
707
714
 
708
715
  test('allows separate constrained and unconstrained HEAD routes', async (t) => {
@@ -744,7 +751,7 @@ test('allows separate constrained and unconstrained HEAD routes', async (t) => {
744
751
 
745
752
  await fastify.ready()
746
753
 
747
- t.ok(true)
754
+ t.assert.ok(true)
748
755
  })
749
756
 
750
757
  test('allow async constraints', async (t) => {
@@ -787,17 +794,17 @@ test('allow async constraints', async (t) => {
787
794
 
788
795
  {
789
796
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'alpha' } })
790
- t.same(JSON.parse(payload), { hello: 'from alpha' })
791
- t.equal(statusCode, 200)
797
+ t.assert.deepStrictEqual(JSON.parse(payload), { hello: 'from alpha' })
798
+ t.assert.strictEqual(statusCode, 200)
792
799
  }
793
800
  {
794
801
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'beta' } })
795
- t.same(JSON.parse(payload), { hello: 'from beta' })
796
- t.equal(statusCode, 200)
802
+ t.assert.deepStrictEqual(JSON.parse(payload), { hello: 'from beta' })
803
+ t.assert.strictEqual(statusCode, 200)
797
804
  }
798
805
  {
799
806
  const { statusCode } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'gamma' } })
800
- t.equal(statusCode, 404)
807
+ t.assert.strictEqual(statusCode, 404)
801
808
  }
802
809
  })
803
810
 
@@ -841,28 +848,28 @@ test('error in async constraints', async (t) => {
841
848
 
842
849
  {
843
850
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'alpha' } })
844
- t.same(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
845
- t.equal(statusCode, 500)
851
+ t.assert.deepStrictEqual(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
852
+ t.assert.strictEqual(statusCode, 500)
846
853
  }
847
854
  {
848
855
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'beta' } })
849
- t.same(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
850
- t.equal(statusCode, 500)
856
+ t.assert.deepStrictEqual(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
857
+ t.assert.strictEqual(statusCode, 500)
851
858
  }
852
859
  {
853
860
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/', headers: { 'X-Secret': 'gamma' } })
854
- t.same(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
855
- t.equal(statusCode, 500)
861
+ t.assert.deepStrictEqual(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
862
+ t.assert.strictEqual(statusCode, 500)
856
863
  }
857
864
  {
858
865
  const { statusCode, payload } = await fastify.inject({ method: 'GET', path: '/' })
859
- t.same(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
860
- t.equal(statusCode, 500)
866
+ t.assert.deepStrictEqual(JSON.parse(payload), { error: 'Internal Server Error', message: 'Unexpected error from async constraint', statusCode: 500 })
867
+ t.assert.strictEqual(statusCode, 500)
861
868
  }
862
869
  })
863
870
 
864
- test('Allow regex constraints in routes', t => {
865
- t.plan(5)
871
+ test('Allow regex constraints in routes', async t => {
872
+ t.plan(3)
866
873
 
867
874
  const fastify = Fastify()
868
875
 
@@ -875,26 +882,26 @@ test('Allow regex constraints in routes', t => {
875
882
  }
876
883
  })
877
884
 
878
- fastify.inject({
879
- method: 'GET',
880
- url: '/',
881
- headers: {
882
- host: 'dev.fastify.dev'
883
- }
884
- }, (err, res) => {
885
- t.error(err)
886
- t.same(JSON.parse(res.payload), { hello: 'from fastify dev domain' })
887
- t.equal(res.statusCode, 200)
888
- })
885
+ {
886
+ const res = await fastify.inject({
887
+ method: 'GET',
888
+ url: '/',
889
+ headers: {
890
+ host: 'dev.fastify.dev'
891
+ }
892
+ })
893
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'from fastify dev domain' })
894
+ t.assert.strictEqual(res.statusCode, 200)
895
+ }
889
896
 
890
- fastify.inject({
891
- method: 'GET',
892
- url: '/',
893
- headers: {
894
- host: 'google.com'
895
- }
896
- }, (err, res) => {
897
- t.error(err)
898
- t.equal(res.statusCode, 404)
899
- })
897
+ {
898
+ const res = await fastify.inject({
899
+ method: 'GET',
900
+ url: '/',
901
+ headers: {
902
+ host: 'google.com'
903
+ }
904
+ })
905
+ t.assert.strictEqual(res.statusCode, 404)
906
+ }
900
907
  })