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,7 +1,6 @@
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('..')
6
5
  const keys = require('../lib/symbols')
7
6
  const { FST_ERR_CTP_ALREADY_PRESENT, FST_ERR_CTP_INVALID_TYPE, FST_ERR_CTP_INVALID_MEDIA_TYPE } = require('../lib/errors')
@@ -10,21 +9,22 @@ const first = function (req, payload, done) {}
10
9
  const second = function (req, payload, done) {}
11
10
  const third = function (req, payload, done) {}
12
11
 
13
- test('hasContentTypeParser', t => {
14
- test('should know about internal parsers', t => {
12
+ test('hasContentTypeParser', async t => {
13
+ await t.test('should know about internal parsers', (t, done) => {
15
14
  t.plan(5)
16
15
 
17
16
  const fastify = Fastify()
18
17
  fastify.ready(err => {
19
- t.error(err)
20
- t.ok(fastify.hasContentTypeParser('application/json'))
21
- t.ok(fastify.hasContentTypeParser('text/plain'))
22
- t.ok(fastify.hasContentTypeParser(' text/plain '))
23
- t.notOk(fastify.hasContentTypeParser('application/jsoff'))
18
+ t.assert.ifError(err)
19
+ t.assert.ok(fastify.hasContentTypeParser('application/json'))
20
+ t.assert.ok(fastify.hasContentTypeParser('text/plain'))
21
+ t.assert.ok(fastify.hasContentTypeParser(' text/plain '))
22
+ t.assert.ok(!fastify.hasContentTypeParser('application/jsoff'))
23
+ done()
24
24
  })
25
25
  })
26
26
 
27
- test('should only work with string and RegExp', t => {
27
+ await t.test('should only work with string and RegExp', t => {
28
28
  t.plan(8)
29
29
 
30
30
  const fastify = Fastify()
@@ -32,21 +32,22 @@ test('hasContentTypeParser', t => {
32
32
  fastify.addContentTypeParser(/^application\/.+\+xml/, first)
33
33
  fastify.addContentTypeParser('image/gif', first)
34
34
 
35
- t.ok(fastify.hasContentTypeParser('application/json'))
36
- t.ok(fastify.hasContentTypeParser(/^image\/.*/))
37
- t.ok(fastify.hasContentTypeParser(/^application\/.+\+xml/))
38
- t.ok(fastify.hasContentTypeParser('image/gif'))
39
- t.notOk(fastify.hasContentTypeParser(/^image\/.+\+xml/))
40
- t.notOk(fastify.hasContentTypeParser('image/png'))
41
- t.notOk(fastify.hasContentTypeParser('*'))
42
- t.throws(() => fastify.hasContentTypeParser(123), FST_ERR_CTP_INVALID_TYPE)
35
+ t.assert.ok(fastify.hasContentTypeParser('application/json'))
36
+ t.assert.ok(fastify.hasContentTypeParser(/^image\/.*/))
37
+ t.assert.ok(fastify.hasContentTypeParser(/^application\/.+\+xml/))
38
+ t.assert.ok(fastify.hasContentTypeParser('image/gif'))
39
+ t.assert.ok(!fastify.hasContentTypeParser(/^image\/.+\+xml/))
40
+ t.assert.ok(!fastify.hasContentTypeParser('image/png'))
41
+ t.assert.ok(!fastify.hasContentTypeParser('*'))
42
+ t.assert.throws(
43
+ () => fastify.hasContentTypeParser(123),
44
+ FST_ERR_CTP_INVALID_TYPE
45
+ )
43
46
  })
44
-
45
- t.end()
46
47
  })
47
48
 
48
- test('getParser', t => {
49
- test('should return matching parser', t => {
49
+ test('getParser', async t => {
50
+ await t.test('should return matching parser', t => {
50
51
  t.plan(6)
51
52
 
52
53
  const fastify = Fastify()
@@ -55,45 +56,62 @@ test('getParser', t => {
55
56
  fastify.addContentTypeParser(/^application\/.+\+xml/, second)
56
57
  fastify.addContentTypeParser('text/html', third)
57
58
 
58
- t.equal(fastify[keys.kContentTypeParser].getParser('application/t+xml').fn, second)
59
- t.equal(fastify[keys.kContentTypeParser].getParser('image/png').fn, first)
60
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html').fn, third)
61
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html; charset=utf-8').fn, third)
62
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html ; charset=utf-8').fn, third)
63
- t.equal(fastify[keys.kContentTypeParser].getParser('text/htmlINVALID')?.fn, undefined)
59
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('application/t+xml').fn, second)
60
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('image/png').fn, first)
61
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html').fn, third)
62
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html; charset=utf-8').fn, third)
63
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html ; charset=utf-8').fn, third)
64
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/htmlINVALID')?.fn, undefined)
64
65
  })
65
66
 
66
- test('should return matching parser with caching /1', t => {
67
+ await t.test('should return matching parser with caching /1', t => {
67
68
  t.plan(6)
68
69
 
69
70
  const fastify = Fastify()
70
71
 
71
72
  fastify.addContentTypeParser('text/html', first)
72
73
 
73
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html').fn, first)
74
- t.equal(fastify[keys.kContentTypeParser].cache.size, 0)
75
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html ').fn, first)
76
- t.equal(fastify[keys.kContentTypeParser].cache.size, 1)
77
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html ').fn, first)
78
- t.equal(fastify[keys.kContentTypeParser].cache.size, 1)
74
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html').fn, first)
75
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 0)
76
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html ').fn, first)
77
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 1)
78
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html ').fn, first)
79
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 1)
79
80
  })
80
81
 
81
- test('should return matching parser with caching /2', t => {
82
+ await t.test('should return matching parser with caching /2', t => {
83
+ t.plan(8)
84
+
85
+ const fastify = Fastify()
86
+
87
+ fastify.addContentTypeParser('text/html', first)
88
+
89
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html').fn, first)
90
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 0)
91
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/HTML').fn, first)
92
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 1)
93
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('TEXT/html').fn, first)
94
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 2)
95
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('TEXT/html').fn, first)
96
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 2)
97
+ })
98
+
99
+ await t.test('should return matching parser with caching /3', t => {
82
100
  t.plan(6)
83
101
 
84
102
  const fastify = Fastify()
85
103
 
86
104
  fastify.addContentTypeParser(/^text\/html(;\s*charset=[^;]+)?$/, first)
87
105
 
88
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html').fn, first)
89
- t.equal(fastify[keys.kContentTypeParser].cache.size, 1)
90
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html;charset=utf-8').fn, first)
91
- t.equal(fastify[keys.kContentTypeParser].cache.size, 2)
92
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html;charset=utf-8').fn, first)
93
- t.equal(fastify[keys.kContentTypeParser].cache.size, 2)
106
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html').fn, first)
107
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 1)
108
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html;charset=utf-8').fn, first)
109
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 2)
110
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html;charset=utf-8').fn, first)
111
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].cache.size, 2)
94
112
  })
95
113
 
96
- test('should prefer content type parser with string value', t => {
114
+ await t.test('should prefer content type parser with string value', t => {
97
115
  t.plan(2)
98
116
 
99
117
  const fastify = Fastify()
@@ -101,11 +119,11 @@ test('getParser', t => {
101
119
  fastify.addContentTypeParser(/^image\/.*/, first)
102
120
  fastify.addContentTypeParser('image/gif', second)
103
121
 
104
- t.equal(fastify[keys.kContentTypeParser].getParser('image/gif').fn, second)
105
- t.equal(fastify[keys.kContentTypeParser].getParser('image/png').fn, first)
122
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('image/gif').fn, second)
123
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('image/png').fn, first)
106
124
  })
107
125
 
108
- test('should return parser that catches all if no other is set', t => {
126
+ await t.test('should return parser that catches all if no other is set', t => {
109
127
  t.plan(3)
110
128
 
111
129
  const fastify = Fastify()
@@ -113,12 +131,12 @@ test('getParser', t => {
113
131
  fastify.addContentTypeParser('*', first)
114
132
  fastify.addContentTypeParser(/^text\/.*/, second)
115
133
 
116
- t.equal(fastify[keys.kContentTypeParser].getParser('image/gif').fn, first)
117
- t.equal(fastify[keys.kContentTypeParser].getParser('text/html').fn, second)
118
- t.equal(fastify[keys.kContentTypeParser].getParser('text').fn, first)
134
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('image/gif').fn, first)
135
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text/html').fn, second)
136
+ t.assert.strictEqual(fastify[keys.kContentTypeParser].getParser('text').fn, first)
119
137
  })
120
138
 
121
- test('should return undefined if no matching parser exist', t => {
139
+ await t.test('should return undefined if no matching parser exist', t => {
122
140
  t.plan(2)
123
141
 
124
142
  const fastify = Fastify()
@@ -126,15 +144,13 @@ test('getParser', t => {
126
144
  fastify.addContentTypeParser(/^weirdType\/.+/, first)
127
145
  fastify.addContentTypeParser('application/javascript', first)
128
146
 
129
- t.notOk(fastify[keys.kContentTypeParser].getParser('application/xml'))
130
- t.notOk(fastify[keys.kContentTypeParser].getParser('weirdType/'))
147
+ t.assert.ok(!fastify[keys.kContentTypeParser].getParser('application/xml'))
148
+ t.assert.ok(!fastify[keys.kContentTypeParser].getParser('weirdType/'))
131
149
  })
132
-
133
- t.end()
134
150
  })
135
151
 
136
- test('existingParser', t => {
137
- test('returns always false for "*"', t => {
152
+ test('existingParser', async t => {
153
+ await t.test('returns always false for "*"', t => {
138
154
  t.plan(2)
139
155
 
140
156
  const fastify = Fastify()
@@ -143,14 +159,14 @@ test('existingParser', t => {
143
159
  fastify.addContentTypeParser(/^application\/.+\+xml/, first)
144
160
  fastify.addContentTypeParser('text/html', first)
145
161
 
146
- t.notOk(fastify[keys.kContentTypeParser].existingParser('*'))
162
+ t.assert.ok(!fastify[keys.kContentTypeParser].existingParser('*'))
147
163
 
148
164
  fastify.addContentTypeParser('*', first)
149
165
 
150
- t.notOk(fastify[keys.kContentTypeParser].existingParser('*'))
166
+ t.assert.ok(!fastify[keys.kContentTypeParser].existingParser('*'))
151
167
  })
152
168
 
153
- test('let you override the default parser once', t => {
169
+ await t.test('let you override the default parser once', t => {
154
170
  t.plan(2)
155
171
 
156
172
  const fastify = Fastify()
@@ -158,15 +174,13 @@ test('existingParser', t => {
158
174
  fastify.addContentTypeParser('application/json', first)
159
175
  fastify.addContentTypeParser('text/plain', first)
160
176
 
161
- t.throws(
177
+ t.assert.throws(
162
178
  () => fastify.addContentTypeParser('application/json', first),
163
- FST_ERR_CTP_ALREADY_PRESENT,
164
- "Content type parser 'application/json' already present"
179
+ FST_ERR_CTP_ALREADY_PRESENT
165
180
  )
166
- t.throws(
181
+ t.assert.throws(
167
182
  () => fastify.addContentTypeParser('text/plain', first),
168
- FST_ERR_CTP_ALREADY_PRESENT,
169
- "Content type parser 'text/plain' already present"
183
+ FST_ERR_CTP_ALREADY_PRESENT
170
184
  )
171
185
  })
172
186
 
@@ -177,49 +191,47 @@ test('existingParser', t => {
177
191
  fastify.addContentTypeParser(/^application\/.+\+xml/, first)
178
192
  fastify.addContentTypeParser('text/html', first)
179
193
 
180
- t.ok(contentTypeParser.existingParser(/^image\/.*/))
181
- t.ok(contentTypeParser.existingParser('text/html'))
182
- t.ok(contentTypeParser.existingParser(/^application\/.+\+xml/))
183
- t.notOk(contentTypeParser.existingParser('application/json'))
184
- t.notOk(contentTypeParser.existingParser('text/plain'))
185
- t.notOk(contentTypeParser.existingParser('image/png'))
186
- t.notOk(contentTypeParser.existingParser(/^application\/.+\+json/))
187
-
188
- t.end()
194
+ t.assert.ok(contentTypeParser.existingParser(/^image\/.*/))
195
+ t.assert.ok(contentTypeParser.existingParser('text/html'))
196
+ t.assert.ok(contentTypeParser.existingParser(/^application\/.+\+xml/))
197
+ t.assert.ok(!contentTypeParser.existingParser('application/json'))
198
+ t.assert.ok(!contentTypeParser.existingParser('text/plain'))
199
+ t.assert.ok(!contentTypeParser.existingParser('image/png'))
200
+ t.assert.ok(!contentTypeParser.existingParser(/^application\/.+\+json/))
189
201
  })
190
202
 
191
- test('add', t => {
192
- test('should only accept string and RegExp', t => {
203
+ test('add', async t => {
204
+ await t.test('should only accept string and RegExp', t => {
193
205
  t.plan(4)
194
206
 
195
207
  const fastify = Fastify()
196
208
  const contentTypeParser = fastify[keys.kContentTypeParser]
197
209
 
198
- t.error(contentTypeParser.add('test', {}, first))
199
- t.error(contentTypeParser.add(/test/, {}, first))
200
- t.throws(
210
+ t.assert.ifError(contentTypeParser.add('test', {}, first))
211
+ t.assert.ifError(contentTypeParser.add(/test/, {}, first))
212
+ t.assert.throws(
201
213
  () => contentTypeParser.add({}, {}, first),
202
214
  FST_ERR_CTP_INVALID_TYPE,
203
215
  'The content type should be a string or a RegExp'
204
216
  )
205
- t.throws(
217
+ t.assert.throws(
206
218
  () => contentTypeParser.add(1, {}, first),
207
219
  FST_ERR_CTP_INVALID_TYPE,
208
220
  'The content type should be a string or a RegExp'
209
221
  )
210
222
  })
211
223
 
212
- test('should set "*" as parser that catches all', t => {
224
+ await t.test('should set "*" as parser that catches all', t => {
213
225
  t.plan(1)
214
226
 
215
227
  const fastify = Fastify()
216
228
  const contentTypeParser = fastify[keys.kContentTypeParser]
217
229
 
218
230
  contentTypeParser.add('*', {}, first)
219
- t.equal(contentTypeParser.customParsers.get('').fn, first)
231
+ t.assert.strictEqual(contentTypeParser.customParsers.get('').fn, first)
220
232
  })
221
233
 
222
- test('should lowercase contentTypeParser name', async t => {
234
+ await t.test('should lowercase contentTypeParser name', async t => {
223
235
  t.plan(1)
224
236
  const fastify = Fastify()
225
237
  fastify.addContentTypeParser('text/html', function (req, done) {
@@ -230,11 +242,11 @@ test('add', t => {
230
242
  done()
231
243
  })
232
244
  } catch (err) {
233
- t.same(err.message, FST_ERR_CTP_ALREADY_PRESENT('text/html').message)
245
+ t.assert.strictEqual(err.message, FST_ERR_CTP_ALREADY_PRESENT('text/html').message)
234
246
  }
235
247
  })
236
248
 
237
- test('should trim contentTypeParser name', async t => {
249
+ await t.test('should trim contentTypeParser name', async t => {
238
250
  t.plan(1)
239
251
  const fastify = Fastify()
240
252
  fastify.addContentTypeParser('text/html', function (req, done) {
@@ -245,14 +257,12 @@ test('add', t => {
245
257
  done()
246
258
  })
247
259
  } catch (err) {
248
- t.same(err.message, FST_ERR_CTP_ALREADY_PRESENT('text/html').message)
260
+ t.assert.strictEqual(err.message, FST_ERR_CTP_ALREADY_PRESENT('text/html').message)
249
261
  }
250
262
  })
251
-
252
- t.end()
253
263
  })
254
264
 
255
- test('non-Error thrown from content parser is properly handled', t => {
265
+ test('non-Error thrown from content parser is properly handled', (t, done) => {
256
266
  t.plan(3)
257
267
 
258
268
  const fastify = Fastify()
@@ -268,7 +278,7 @@ test('non-Error thrown from content parser is properly handled', t => {
268
278
  })
269
279
 
270
280
  fastify.setErrorHandler((err, req, res) => {
271
- t.equal(err, throwable)
281
+ t.assert.strictEqual(err, throwable)
272
282
 
273
283
  res.send(payload)
274
284
  })
@@ -279,12 +289,13 @@ test('non-Error thrown from content parser is properly handled', t => {
279
289
  headers: { 'Content-Type': 'text/test' },
280
290
  body: 'some text'
281
291
  }, (err, res) => {
282
- t.error(err)
283
- t.equal(res.payload, payload)
292
+ t.assert.ifError(err)
293
+ t.assert.strictEqual(res.payload, payload)
294
+ done()
284
295
  })
285
296
  })
286
297
 
287
- test('Error thrown 415 from content type is null and make post request to server', t => {
298
+ test('Error thrown 415 from content type is null and make post request to server', (t, done) => {
288
299
  t.plan(3)
289
300
 
290
301
  const fastify = Fastify()
@@ -298,28 +309,29 @@ test('Error thrown 415 from content type is null and make post request to server
298
309
  url: '/',
299
310
  body: 'some text'
300
311
  }, (err, res) => {
301
- t.error(err)
302
- t.equal(res.statusCode, 415)
303
- t.equal(JSON.parse(res.body).message, errMsg)
312
+ t.assert.ifError(err)
313
+ t.assert.strictEqual(res.statusCode, 415)
314
+ t.assert.strictEqual(JSON.parse(res.body).message, errMsg)
315
+ done()
304
316
  })
305
317
  })
306
318
 
307
- test('remove', t => {
308
- test('should remove default parser', t => {
319
+ test('remove', async t => {
320
+ await t.test('should remove default parser', t => {
309
321
  t.plan(6)
310
322
 
311
323
  const fastify = Fastify()
312
324
  const contentTypeParser = fastify[keys.kContentTypeParser]
313
325
 
314
- t.ok(contentTypeParser.remove('application/json'))
315
- t.notOk(contentTypeParser.customParsers['application/json'])
316
- t.notOk(contentTypeParser.parserList.find(parser => parser === 'application/json'))
317
- t.ok(contentTypeParser.remove(' text/plain '))
318
- t.notOk(contentTypeParser.customParsers['text/plain'])
319
- t.notOk(contentTypeParser.parserList.find(parser => parser === 'text/plain'))
326
+ t.assert.ok(contentTypeParser.remove('application/json'))
327
+ t.assert.ok(!contentTypeParser.customParsers['application/json'])
328
+ t.assert.ok(!contentTypeParser.parserList.find(parser => parser === 'application/json'))
329
+ t.assert.ok(contentTypeParser.remove(' text/plain '))
330
+ t.assert.ok(!contentTypeParser.customParsers['text/plain'])
331
+ t.assert.ok(!contentTypeParser.parserList.find(parser => parser === 'text/plain'))
320
332
  })
321
333
 
322
- test('should remove RegExp parser', t => {
334
+ await t.test('should remove RegExp parser', t => {
323
335
  t.plan(3)
324
336
 
325
337
  const fastify = Fastify()
@@ -327,39 +339,37 @@ test('remove', t => {
327
339
 
328
340
  const contentTypeParser = fastify[keys.kContentTypeParser]
329
341
 
330
- t.ok(contentTypeParser.remove(/^text\/*/))
331
- t.notOk(contentTypeParser.customParsers[/^text\/*/])
332
- t.notOk(contentTypeParser.parserRegExpList.find(parser => parser.toString() === /^text\/*/.toString()))
342
+ t.assert.ok(contentTypeParser.remove(/^text\/*/))
343
+ t.assert.ok(!contentTypeParser.customParsers[/^text\/*/])
344
+ t.assert.ok(!contentTypeParser.parserRegExpList.find(parser => parser.toString() === /^text\/*/.toString()))
333
345
  })
334
346
 
335
- test('should throw an error if content type is neither string nor RegExp', t => {
347
+ await t.test('should throw an error if content type is neither string nor RegExp', t => {
336
348
  t.plan(1)
337
349
 
338
350
  const fastify = Fastify()
339
351
 
340
- t.throws(() => fastify[keys.kContentTypeParser].remove(12), FST_ERR_CTP_INVALID_TYPE)
352
+ t.assert.throws(() => fastify[keys.kContentTypeParser].remove(12), FST_ERR_CTP_INVALID_TYPE)
341
353
  })
342
354
 
343
- test('should return false if content type does not exist', t => {
355
+ await t.test('should return false if content type does not exist', t => {
344
356
  t.plan(1)
345
357
 
346
358
  const fastify = Fastify()
347
359
 
348
- t.notOk(fastify[keys.kContentTypeParser].remove('image/png'))
360
+ t.assert.ok(!fastify[keys.kContentTypeParser].remove('image/png'))
349
361
  })
350
362
 
351
- test('should not remove any content type parser if content type does not exist', t => {
363
+ await t.test('should not remove any content type parser if content type does not exist', t => {
352
364
  t.plan(2)
353
365
 
354
366
  const fastify = Fastify()
355
367
 
356
368
  const contentTypeParser = fastify[keys.kContentTypeParser]
357
369
 
358
- t.notOk(contentTypeParser.remove('image/png'))
359
- t.same(contentTypeParser.customParsers.size, 2)
370
+ t.assert.ok(!contentTypeParser.remove('image/png'))
371
+ t.assert.strictEqual(contentTypeParser.customParsers.size, 2)
360
372
  })
361
-
362
- t.end()
363
373
  })
364
374
 
365
375
  test('remove all should remove all existing parsers and reset cache', t => {
@@ -374,10 +384,10 @@ test('remove all should remove all existing parsers and reset cache', t => {
374
384
  contentTypeParser.getParser('application/xml') // fill cache with one entry
375
385
  contentTypeParser.removeAll()
376
386
 
377
- t.same(contentTypeParser.cache.size, 0)
378
- t.same(contentTypeParser.parserList.length, 0)
379
- t.same(contentTypeParser.parserRegExpList.length, 0)
380
- t.same(Object.keys(contentTypeParser.customParsers).length, 0)
387
+ t.assert.strictEqual(contentTypeParser.cache.size, 0)
388
+ t.assert.strictEqual(contentTypeParser.parserList.length, 0)
389
+ t.assert.strictEqual(contentTypeParser.parserRegExpList.length, 0)
390
+ t.assert.strictEqual(Object.keys(contentTypeParser.customParsers).length, 0)
381
391
  })
382
392
 
383
393
  test('Safeguard against malicious content-type / 1', async t => {
@@ -400,7 +410,7 @@ test('Safeguard against malicious content-type / 1', async t => {
400
410
  body: ''
401
411
  })
402
412
 
403
- t.same(response.statusCode, 415)
413
+ t.assert.strictEqual(response.statusCode, 415)
404
414
  }
405
415
  })
406
416
 
@@ -422,7 +432,7 @@ test('Safeguard against malicious content-type / 2', async t => {
422
432
  body: ''
423
433
  })
424
434
 
425
- t.same(response.statusCode, 415)
435
+ t.assert.strictEqual(response.statusCode, 415)
426
436
  })
427
437
 
428
438
  test('Safeguard against malicious content-type / 3', async t => {
@@ -443,7 +453,7 @@ test('Safeguard against malicious content-type / 3', async t => {
443
453
  body: ''
444
454
  })
445
455
 
446
- t.same(response.statusCode, 415)
456
+ t.assert.strictEqual(response.statusCode, 415)
447
457
  })
448
458
 
449
459
  test('Safeguard against content-type spoofing - string', async t => {
@@ -452,11 +462,11 @@ test('Safeguard against content-type spoofing - string', async t => {
452
462
  const fastify = Fastify()
453
463
  fastify.removeAllContentTypeParsers()
454
464
  fastify.addContentTypeParser('text/plain', function (request, body, done) {
455
- t.pass('should be called')
465
+ t.assert.ok('should be called')
456
466
  done(null, body)
457
467
  })
458
468
  fastify.addContentTypeParser('application/json', function (request, body, done) {
459
- t.fail('shouldn\'t be called')
469
+ t.assert.fail('shouldn\'t be called')
460
470
  done(null, body)
461
471
  })
462
472
 
@@ -474,24 +484,42 @@ test('Safeguard against content-type spoofing - string', async t => {
474
484
  })
475
485
  })
476
486
 
477
- test('Warning against improper content-type - regexp', t => {
478
- t.plan(2)
479
-
480
- const fastify = Fastify()
487
+ test('Warning against improper content-type - regexp', async t => {
488
+ await t.test('improper regex - text plain', (t, done) => {
489
+ t.plan(2)
490
+ const fastify = Fastify()
481
491
 
482
- process.on('warning', onWarning)
483
- function onWarning (warning) {
484
- t.equal(warning.name, 'FastifySecurity')
485
- t.equal(warning.code, 'FSTSEC001')
486
- }
487
- t.teardown(() => process.removeListener('warning', onWarning))
492
+ process.on('warning', onWarning)
493
+ function onWarning (warning) {
494
+ t.assert.strictEqual(warning.name, 'FastifySecurity')
495
+ t.assert.strictEqual(warning.code, 'FSTSEC001')
496
+ done()
497
+ }
498
+ t.after(() => process.removeListener('warning', onWarning))
488
499
 
489
- fastify.removeAllContentTypeParsers()
490
- fastify.addContentTypeParser(/text\/plain/, function (request, body, done) {
491
- done(null, body)
500
+ fastify.removeAllContentTypeParsers()
501
+ fastify.addContentTypeParser(/text\/plain/, function (request, body, done) {
502
+ done(null, body)
503
+ })
492
504
  })
493
- fastify.addContentTypeParser(/application\/json/, function (request, body, done) {
494
- done(null, body)
505
+
506
+ await t.test('improper regex - application json', (t, done) => {
507
+ t.plan(2)
508
+ const fastify = Fastify()
509
+
510
+ process.on('warning', onWarning)
511
+ function onWarning (warning) {
512
+ t.assert.strictEqual(warning.name, 'FastifySecurity')
513
+ t.assert.strictEqual(warning.code, 'FSTSEC001')
514
+ done()
515
+ }
516
+ t.after(() => process.removeListener('warning', onWarning))
517
+
518
+ fastify.removeAllContentTypeParsers()
519
+
520
+ fastify.addContentTypeParser(/application\/json/, function (request, body, done) {
521
+ done(null, body)
522
+ })
495
523
  })
496
524
  })
497
525
 
@@ -501,11 +529,11 @@ test('content-type match parameters - string 1', async t => {
501
529
  const fastify = Fastify()
502
530
  fastify.removeAllContentTypeParsers()
503
531
  fastify.addContentTypeParser('text/plain; charset=utf8', function (request, body, done) {
504
- t.fail('shouldn\'t be called')
532
+ t.assert.fail('shouldn\'t be called')
505
533
  done(null, body)
506
534
  })
507
535
  fastify.addContentTypeParser('application/json; charset=utf8', function (request, body, done) {
508
- t.pass('should be called')
536
+ t.assert.ok('should be called')
509
537
  done(null, body)
510
538
  })
511
539
 
@@ -529,7 +557,7 @@ test('content-type match parameters - regexp', async t => {
529
557
  const fastify = Fastify()
530
558
  fastify.removeAllContentTypeParsers()
531
559
  fastify.addContentTypeParser(/application\/json; charset=utf8/, function (request, body, done) {
532
- t.pass('should be called')
560
+ t.assert.ok('should be called')
533
561
  done(null, body)
534
562
  })
535
563
 
@@ -553,7 +581,7 @@ test('content-type fail when parameters not match - string 1', async t => {
553
581
  const fastify = Fastify()
554
582
  fastify.removeAllContentTypeParsers()
555
583
  fastify.addContentTypeParser('application/json; charset=utf8; foo=bar', function (request, body, done) {
556
- t.fail('shouldn\'t be called')
584
+ t.assert.fail('shouldn\'t be called')
557
585
  done(null, body)
558
586
  })
559
587
 
@@ -570,7 +598,7 @@ test('content-type fail when parameters not match - string 1', async t => {
570
598
  body: ''
571
599
  })
572
600
 
573
- t.same(response.statusCode, 415)
601
+ t.assert.strictEqual(response.statusCode, 415)
574
602
  })
575
603
 
576
604
  test('content-type fail when parameters not match - string 2', async t => {
@@ -579,7 +607,7 @@ test('content-type fail when parameters not match - string 2', async t => {
579
607
  const fastify = Fastify()
580
608
  fastify.removeAllContentTypeParsers()
581
609
  fastify.addContentTypeParser('application/json; charset=utf8; foo=bar', function (request, body, done) {
582
- t.fail('shouldn\'t be called')
610
+ t.assert.fail('shouldn\'t be called')
583
611
  done(null, body)
584
612
  })
585
613
 
@@ -596,7 +624,7 @@ test('content-type fail when parameters not match - string 2', async t => {
596
624
  body: ''
597
625
  })
598
626
 
599
- t.same(response.statusCode, 415)
627
+ t.assert.strictEqual(response.statusCode, 415)
600
628
  })
601
629
 
602
630
  test('content-type fail when parameters not match - regexp', async t => {
@@ -605,7 +633,7 @@ test('content-type fail when parameters not match - regexp', async t => {
605
633
  const fastify = Fastify()
606
634
  fastify.removeAllContentTypeParsers()
607
635
  fastify.addContentTypeParser(/application\/json; charset=utf8; foo=bar/, function (request, body, done) {
608
- t.fail('shouldn\'t be called')
636
+ t.assert.fail('shouldn\'t be called')
609
637
  done(null, body)
610
638
  })
611
639
 
@@ -622,7 +650,7 @@ test('content-type fail when parameters not match - regexp', async t => {
622
650
  body: ''
623
651
  })
624
652
 
625
- t.same(response.statusCode, 415)
653
+ t.assert.strictEqual(response.statusCode, 415)
626
654
  })
627
655
 
628
656
  // Refs: https://github.com/fastify/fastify/issues/4495
@@ -650,9 +678,9 @@ test('content-type regexp list should be cloned when plugin override', async t =
650
678
  payload: 'jpeg',
651
679
  headers: { 'content-type': 'image/jpeg' }
652
680
  })
653
- t.same(statusCode, 200)
654
- t.same(headers['content-type'], 'image/jpeg')
655
- t.same(payload, 'jpeg')
681
+ t.assert.strictEqual(statusCode, 200)
682
+ t.assert.strictEqual(headers['content-type'], 'image/jpeg')
683
+ t.assert.strictEqual(payload, 'jpeg')
656
684
  }
657
685
 
658
686
  {
@@ -662,9 +690,9 @@ test('content-type regexp list should be cloned when plugin override', async t =
662
690
  payload: 'png',
663
691
  headers: { 'content-type': 'image/png' }
664
692
  })
665
- t.same(statusCode, 200)
666
- t.same(headers['content-type'], 'image/png')
667
- t.same(payload, 'png')
693
+ t.assert.strictEqual(statusCode, 200)
694
+ t.assert.strictEqual(headers['content-type'], 'image/png')
695
+ t.assert.strictEqual(payload, 'png')
668
696
  }
669
697
  })
670
698
 
@@ -674,7 +702,7 @@ test('edge case content-type - ;', async t => {
674
702
  const fastify = Fastify()
675
703
  fastify.removeAllContentTypeParsers()
676
704
  fastify.addContentTypeParser(';', function (request, body, done) {
677
- t.fail('should not be called')
705
+ t.assert.fail('should not be called')
678
706
  done(null, body)
679
707
  })
680
708
 
@@ -700,5 +728,5 @@ test('edge case content-type - ;', async t => {
700
728
  body: ''
701
729
  })
702
730
 
703
- t.pass('end')
731
+ t.assert.ok('end')
704
732
  })