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
package/test/404s.test.js CHANGED
@@ -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 fp = require('fastify-plugin')
6
5
  const sget = require('simple-get').concat
7
6
  const errors = require('http-errors')
@@ -9,84 +8,83 @@ const split = require('split2')
9
8
  const Fastify = require('..')
10
9
  const { getServerUrl } = require('./helper')
11
10
 
12
- test('default 404', t => {
13
- t.plan(5)
11
+ test('default 404', async t => {
12
+ t.plan(4)
14
13
 
15
- const test = t.test
16
14
  const fastify = Fastify()
17
15
 
18
16
  fastify.get('/', function (req, reply) {
19
17
  reply.send({ hello: 'world' })
20
18
  })
21
19
 
22
- t.teardown(fastify.close.bind(fastify))
20
+ t.after(() => { fastify.close() })
23
21
 
24
- fastify.listen({ port: 0 }, err => {
25
- t.error(err)
26
-
27
- test('unsupported method', t => {
28
- t.plan(3)
29
- sget({
30
- method: 'PUT',
31
- url: getServerUrl(fastify),
32
- body: {},
33
- json: true
34
- }, (err, response, body) => {
35
- t.error(err)
36
- t.equal(response.statusCode, 404)
37
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
38
- })
22
+ await fastify.listen({ port: 0 })
23
+
24
+ await t.test('unsupported method', (t, done) => {
25
+ t.plan(3)
26
+ sget({
27
+ method: 'PUT',
28
+ url: getServerUrl(fastify),
29
+ body: {},
30
+ json: true
31
+ }, (err, response, body) => {
32
+ t.assert.ifError(err)
33
+ t.assert.strictEqual(response.statusCode, 404)
34
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
35
+ done()
39
36
  })
37
+ })
40
38
 
41
- // Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
42
- test('framework-unsupported method', t => {
43
- t.plan(3)
44
- sget({
45
- method: 'PROPFIND',
46
- url: getServerUrl(fastify),
47
- body: {},
48
- json: true
49
- }, (err, response, body) => {
50
- t.error(err)
51
- t.equal(response.statusCode, 404)
52
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
53
- })
39
+ // Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
40
+ await t.test('framework-unsupported method', (t, done) => {
41
+ t.plan(3)
42
+ sget({
43
+ method: 'PROPFIND',
44
+ url: getServerUrl(fastify),
45
+ body: {},
46
+ json: true
47
+ }, (err, response, body) => {
48
+ t.assert.ifError(err)
49
+ t.assert.strictEqual(response.statusCode, 404)
50
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
51
+ done()
54
52
  })
53
+ })
55
54
 
56
- test('unsupported route', t => {
57
- t.plan(3)
58
- sget({
59
- method: 'GET',
60
- url: getServerUrl(fastify) + '/notSupported',
61
- body: {},
62
- json: true
63
- }, (err, response, body) => {
64
- t.error(err)
65
- t.equal(response.statusCode, 404)
66
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
67
- })
55
+ await t.test('unsupported route', (t, done) => {
56
+ t.plan(3)
57
+ sget({
58
+ method: 'GET',
59
+ url: getServerUrl(fastify) + '/notSupported',
60
+ body: {},
61
+ json: true
62
+ }, (err, response, body) => {
63
+ t.assert.ifError(err)
64
+ t.assert.strictEqual(response.statusCode, 404)
65
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
66
+ done()
68
67
  })
68
+ })
69
69
 
70
- test('using post method and multipart/formdata', async t => {
71
- t.plan(3)
72
- const form = new FormData()
73
- form.set('test-field', 'just some field')
70
+ await t.test('using post method and multipart/formdata', async t => {
71
+ t.plan(3)
72
+ const form = new FormData()
73
+ form.set('test-field', 'just some field')
74
74
 
75
- const response = await fetch(getServerUrl(fastify) + '/notSupported', {
76
- method: 'POST',
77
- body: form
78
- })
79
- t.equal(response.status, 404)
80
- t.equal(response.statusText, 'Not Found')
81
- t.equal(response.headers.get('content-type'), 'application/json; charset=utf-8')
75
+ const response = await fetch(getServerUrl(fastify) + '/notSupported', {
76
+ method: 'POST',
77
+ body: form
82
78
  })
79
+ t.assert.strictEqual(response.status, 404)
80
+ t.assert.strictEqual(response.statusText, 'Not Found')
81
+ t.assert.strictEqual(response.headers.get('content-type'), 'application/json; charset=utf-8')
83
82
  })
84
83
  })
85
84
 
86
- test('customized 404', t => {
87
- t.plan(6)
85
+ test('customized 404', async t => {
86
+ t.plan(5)
88
87
 
89
- const test = t.test
90
88
  const fastify = Fastify()
91
89
 
92
90
  fastify.get('/', function (req, reply) {
@@ -107,120 +105,121 @@ test('customized 404', t => {
107
105
  reply.code(404).send('this was not found')
108
106
  })
109
107
 
110
- t.teardown(fastify.close.bind(fastify))
108
+ t.after(() => { fastify.close() })
111
109
 
112
- fastify.listen({ port: 0 }, err => {
113
- t.error(err)
114
-
115
- test('unsupported method', t => {
116
- t.plan(3)
117
- sget({
118
- method: 'PUT',
119
- url: getServerUrl(fastify),
120
- body: JSON.stringify({ hello: 'world' }),
121
- headers: { 'Content-Type': 'application/json' }
122
- }, (err, response, body) => {
123
- t.error(err)
124
- t.equal(response.statusCode, 404)
125
- t.equal(body.toString(), 'this was not found')
126
- })
110
+ await fastify.listen({ port: 0 })
111
+
112
+ await t.test('unsupported method', (t, done) => {
113
+ t.plan(3)
114
+ sget({
115
+ method: 'PUT',
116
+ url: getServerUrl(fastify),
117
+ body: JSON.stringify({ hello: 'world' }),
118
+ headers: { 'Content-Type': 'application/json' }
119
+ }, (err, response, body) => {
120
+ t.assert.ifError(err)
121
+ t.assert.strictEqual(response.statusCode, 404)
122
+ t.assert.strictEqual(body.toString(), 'this was not found')
123
+ done()
127
124
  })
125
+ })
128
126
 
129
- test('framework-unsupported method', t => {
130
- t.plan(3)
131
- sget({
132
- method: 'PROPFIND',
133
- url: getServerUrl(fastify),
134
- body: JSON.stringify({ hello: 'world' }),
135
- headers: { 'Content-Type': 'application/json' }
136
- }, (err, response, body) => {
137
- t.error(err)
138
- t.equal(response.statusCode, 404)
139
- t.equal(body.toString(), 'this was not found')
140
- })
127
+ await t.test('framework-unsupported method', (t, done) => {
128
+ t.plan(3)
129
+ sget({
130
+ method: 'PROPFIND',
131
+ url: getServerUrl(fastify),
132
+ body: JSON.stringify({ hello: 'world' }),
133
+ headers: { 'Content-Type': 'application/json' }
134
+ }, (err, response, body) => {
135
+ t.assert.ifError(err)
136
+ t.assert.strictEqual(response.statusCode, 404)
137
+ t.assert.strictEqual(body.toString(), 'this was not found')
138
+ done()
141
139
  })
140
+ })
142
141
 
143
- test('unsupported route', t => {
144
- t.plan(3)
145
- sget({
146
- method: 'GET',
147
- url: getServerUrl(fastify) + '/notSupported'
148
- }, (err, response, body) => {
149
- t.error(err)
150
- t.equal(response.statusCode, 404)
151
- t.equal(body.toString(), 'this was not found')
152
- })
142
+ await t.test('unsupported route', (t, done) => {
143
+ t.plan(3)
144
+ sget({
145
+ method: 'GET',
146
+ url: getServerUrl(fastify) + '/notSupported'
147
+ }, (err, response, body) => {
148
+ t.assert.ifError(err)
149
+ t.assert.strictEqual(response.statusCode, 404)
150
+ t.assert.strictEqual(body.toString(), 'this was not found')
151
+ done()
153
152
  })
153
+ })
154
154
 
155
- test('with error object', t => {
156
- t.plan(3)
157
- sget({
158
- method: 'GET',
159
- url: getServerUrl(fastify) + '/with-error'
160
- }, (err, response, body) => {
161
- t.error(err)
162
- t.equal(response.statusCode, 404)
163
- t.same(JSON.parse(body), {
164
- error: 'Not Found',
165
- message: 'Not Found',
166
- statusCode: 404
167
- })
155
+ await t.test('with error object', (t, done) => {
156
+ t.plan(3)
157
+ sget({
158
+ method: 'GET',
159
+ url: getServerUrl(fastify) + '/with-error'
160
+ }, (err, response, body) => {
161
+ t.assert.ifError(err)
162
+ t.assert.strictEqual(response.statusCode, 404)
163
+ t.assert.deepStrictEqual(JSON.parse(body), {
164
+ error: 'Not Found',
165
+ message: 'Not Found',
166
+ statusCode: 404
168
167
  })
168
+ done()
169
169
  })
170
+ })
170
171
 
171
- test('error object with headers property', t => {
172
- t.plan(4)
173
- sget({
174
- method: 'GET',
175
- url: getServerUrl(fastify) + '/with-error-custom-header'
176
- }, (err, response, body) => {
177
- t.error(err)
178
- t.equal(response.statusCode, 404)
179
- t.equal(response.headers['x-foo'], 'bar')
180
- t.same(JSON.parse(body), {
181
- error: 'Not Found',
182
- message: 'Not Found',
183
- statusCode: 404
184
- })
172
+ await t.test('error object with headers property', (t, done) => {
173
+ t.plan(4)
174
+ sget({
175
+ method: 'GET',
176
+ url: getServerUrl(fastify) + '/with-error-custom-header'
177
+ }, (err, response, body) => {
178
+ t.assert.ifError(err)
179
+ t.assert.strictEqual(response.statusCode, 404)
180
+ t.assert.strictEqual(response.headers['x-foo'], 'bar')
181
+ t.assert.deepStrictEqual(JSON.parse(body), {
182
+ error: 'Not Found',
183
+ message: 'Not Found',
184
+ statusCode: 404
185
185
  })
186
+ done()
186
187
  })
187
188
  })
188
189
  })
189
190
 
190
- test('custom header in notFound handler', t => {
191
- t.plan(2)
191
+ test('custom header in notFound handler', async t => {
192
+ t.plan(1)
192
193
 
193
- const test = t.test
194
194
  const fastify = Fastify()
195
195
 
196
196
  fastify.setNotFoundHandler(function (req, reply) {
197
197
  reply.code(404).header('x-foo', 'bar').send('this was not found')
198
198
  })
199
199
 
200
- t.teardown(fastify.close.bind(fastify))
200
+ t.after(() => { fastify.close() })
201
201
 
202
- fastify.listen({ port: 0 }, err => {
203
- t.error(err)
204
-
205
- test('not found with custom header', t => {
206
- t.plan(4)
207
- sget({
208
- method: 'GET',
209
- url: getServerUrl(fastify) + '/notSupported'
210
- }, (err, response, body) => {
211
- t.error(err)
212
- t.equal(response.statusCode, 404)
213
- t.equal(response.headers['x-foo'], 'bar')
214
- t.equal(body.toString(), 'this was not found')
215
- })
202
+ await fastify.listen({ port: 0 })
203
+
204
+ await t.test('not found with custom header', (t, done) => {
205
+ t.plan(4)
206
+ sget({
207
+ method: 'GET',
208
+ url: getServerUrl(fastify) + '/notSupported'
209
+ }, (err, response, body) => {
210
+ t.assert.ifError(err)
211
+ t.assert.strictEqual(response.statusCode, 404)
212
+ t.assert.strictEqual(response.headers['x-foo'], 'bar')
213
+ t.assert.strictEqual(body.toString(), 'this was not found')
214
+ done()
216
215
  })
217
216
  })
218
217
  })
219
218
 
220
- test('setting a custom 404 handler multiple times is an error', t => {
219
+ test('setting a custom 404 handler multiple times is an error', async t => {
221
220
  t.plan(5)
222
221
 
223
- t.test('at the root level', t => {
222
+ await t.test('at the root level', t => {
224
223
  t.plan(2)
225
224
 
226
225
  const fastify = Fastify()
@@ -229,14 +228,14 @@ test('setting a custom 404 handler multiple times is an error', t => {
229
228
 
230
229
  try {
231
230
  fastify.setNotFoundHandler(() => {})
232
- t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
231
+ t.assert.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
233
232
  } catch (err) {
234
- t.type(err, Error)
235
- t.equal(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
233
+ t.assert.ok(err instanceof Error)
234
+ t.assert.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
236
235
  }
237
236
  })
238
237
 
239
- t.test('at the plugin level', t => {
238
+ await t.test('at the plugin level', (t, done) => {
240
239
  t.plan(3)
241
240
 
242
241
  const fastify = Fastify()
@@ -246,22 +245,23 @@ test('setting a custom 404 handler multiple times is an error', t => {
246
245
 
247
246
  try {
248
247
  instance.setNotFoundHandler(() => {})
249
- t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
248
+ t.assert.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
250
249
  } catch (err) {
251
- t.type(err, Error)
252
- t.equal(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
250
+ t.assert.ok(err instanceof Error)
251
+ t.assert.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
253
252
  }
254
253
 
255
254
  done()
256
255
  }, { prefix: '/prefix' })
257
256
 
258
257
  fastify.listen({ port: 0 }, err => {
259
- t.error(err)
258
+ t.assert.ifError(err)
260
259
  fastify.close()
260
+ done()
261
261
  })
262
262
  })
263
263
 
264
- t.test('at multiple levels', t => {
264
+ await t.test('at multiple levels', (t, done) => {
265
265
  t.plan(3)
266
266
 
267
267
  const fastify = Fastify()
@@ -269,10 +269,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
269
269
  fastify.register((instance, options, done) => {
270
270
  try {
271
271
  instance.setNotFoundHandler(() => {})
272
- t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
272
+ t.assert.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
273
273
  } catch (err) {
274
- t.type(err, Error)
275
- t.equal(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
274
+ t.assert.ok(err instanceof Error)
275
+ t.assert.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
276
276
  }
277
277
  done()
278
278
  })
@@ -280,12 +280,13 @@ test('setting a custom 404 handler multiple times is an error', t => {
280
280
  fastify.setNotFoundHandler(() => {})
281
281
 
282
282
  fastify.listen({ port: 0 }, err => {
283
- t.error(err)
283
+ t.assert.ifError(err)
284
284
  fastify.close()
285
+ done()
285
286
  })
286
287
  })
287
288
 
288
- t.test('at multiple levels / 2', t => {
289
+ await t.test('at multiple levels / 2', (t, done) => {
289
290
  t.plan(3)
290
291
 
291
292
  const fastify = Fastify()
@@ -296,10 +297,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
296
297
  instance.register((instance2, options, done) => {
297
298
  try {
298
299
  instance2.setNotFoundHandler(() => {})
299
- t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
300
+ t.assert.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
300
301
  } catch (err) {
301
- t.type(err, Error)
302
- t.equal(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
302
+ t.assert.ok(err instanceof Error)
303
+ t.assert.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
303
304
  }
304
305
  done()
305
306
  })
@@ -310,12 +311,13 @@ test('setting a custom 404 handler multiple times is an error', t => {
310
311
  fastify.setNotFoundHandler(() => {})
311
312
 
312
313
  fastify.listen({ port: 0 }, err => {
313
- t.error(err)
314
+ t.assert.ifError(err)
314
315
  fastify.close()
316
+ done()
315
317
  })
316
318
  })
317
319
 
318
- t.test('in separate plugins at the same level', t => {
320
+ await t.test('in separate plugins at the same level', (t, done) => {
319
321
  t.plan(3)
320
322
 
321
323
  const fastify = Fastify()
@@ -329,10 +331,10 @@ test('setting a custom 404 handler multiple times is an error', t => {
329
331
  instance.register((instance2B, options, done) => {
330
332
  try {
331
333
  instance2B.setNotFoundHandler(() => {})
332
- t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
334
+ t.assert.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
333
335
  } catch (err) {
334
- t.type(err, Error)
335
- t.equal(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
336
+ t.assert.ok(err instanceof Error)
337
+ t.assert.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
336
338
  }
337
339
  done()
338
340
  })
@@ -343,16 +345,16 @@ test('setting a custom 404 handler multiple times is an error', t => {
343
345
  fastify.setNotFoundHandler(() => {})
344
346
 
345
347
  fastify.listen({ port: 0 }, err => {
346
- t.error(err)
348
+ t.assert.ifError(err)
347
349
  fastify.close()
350
+ done()
348
351
  })
349
352
  })
350
353
  })
351
354
 
352
- test('encapsulated 404', t => {
353
- t.plan(13)
355
+ test('encapsulated 404', async t => {
356
+ t.plan(12)
354
357
 
355
- const test = t.test
356
358
  const fastify = Fastify()
357
359
 
358
360
  fastify.get('/', function (req, reply) {
@@ -384,199 +386,209 @@ test('encapsulated 404', t => {
384
386
  done()
385
387
  }, { prefix: '/test3/' })
386
388
 
387
- t.teardown(fastify.close.bind(fastify))
389
+ t.after(() => { fastify.close() })
388
390
 
389
- fastify.listen({ port: 0 }, err => {
390
- t.error(err)
391
-
392
- test('root unsupported method', t => {
393
- t.plan(3)
394
- sget({
395
- method: 'PUT',
396
- url: getServerUrl(fastify),
397
- body: JSON.stringify({ hello: 'world' }),
398
- headers: { 'Content-Type': 'application/json' }
399
- }, (err, response, body) => {
400
- t.error(err)
401
- t.equal(response.statusCode, 404)
402
- t.equal(body.toString(), 'this was not found')
403
- })
391
+ await fastify.listen({ port: 0 })
392
+
393
+ await t.test('root unsupported method', (t, done) => {
394
+ t.plan(3)
395
+ sget({
396
+ method: 'PUT',
397
+ url: getServerUrl(fastify),
398
+ body: JSON.stringify({ hello: 'world' }),
399
+ headers: { 'Content-Type': 'application/json' }
400
+ }, (err, response, body) => {
401
+ t.assert.ifError(err)
402
+ t.assert.strictEqual(response.statusCode, 404)
403
+ t.assert.strictEqual(body.toString(), 'this was not found')
404
+ done()
404
405
  })
406
+ })
405
407
 
406
- test('root framework-unsupported method', t => {
407
- t.plan(3)
408
- sget({
409
- method: 'PROPFIND',
410
- url: getServerUrl(fastify),
411
- body: JSON.stringify({ hello: 'world' }),
412
- headers: { 'Content-Type': 'application/json' }
413
- }, (err, response, body) => {
414
- t.error(err)
415
- t.equal(response.statusCode, 404)
416
- t.equal(body.toString(), 'this was not found')
417
- })
408
+ await t.test('root framework-unsupported method', (t, done) => {
409
+ t.plan(3)
410
+ sget({
411
+ method: 'PROPFIND',
412
+ url: getServerUrl(fastify),
413
+ body: JSON.stringify({ hello: 'world' }),
414
+ headers: { 'Content-Type': 'application/json' }
415
+ }, (err, response, body) => {
416
+ t.assert.ifError(err)
417
+ t.assert.strictEqual(response.statusCode, 404)
418
+ t.assert.strictEqual(body.toString(), 'this was not found')
419
+ done()
418
420
  })
421
+ })
419
422
 
420
- test('root unsupported route', t => {
421
- t.plan(3)
422
- sget({
423
- method: 'GET',
424
- url: getServerUrl(fastify) + '/notSupported'
425
- }, (err, response, body) => {
426
- t.error(err)
427
- t.equal(response.statusCode, 404)
428
- t.equal(body.toString(), 'this was not found')
429
- })
423
+ await t.test('root unsupported route', (t, done) => {
424
+ t.plan(3)
425
+ sget({
426
+ method: 'GET',
427
+ url: getServerUrl(fastify) + '/notSupported'
428
+ }, (err, response, body) => {
429
+ t.assert.ifError(err)
430
+ t.assert.strictEqual(response.statusCode, 404)
431
+ t.assert.strictEqual(body.toString(), 'this was not found')
432
+ done()
430
433
  })
434
+ })
431
435
 
432
- test('unsupported method', t => {
433
- t.plan(3)
434
- sget({
435
- method: 'PUT',
436
- url: getServerUrl(fastify) + '/test',
437
- body: JSON.stringify({ hello: 'world' }),
438
- headers: { 'Content-Type': 'application/json' }
439
- }, (err, response, body) => {
440
- t.error(err)
441
- t.equal(response.statusCode, 404)
442
- t.equal(body.toString(), 'this was not found 2')
443
- })
436
+ await t.test('unsupported method', (t, done) => {
437
+ t.plan(3)
438
+ sget({
439
+ method: 'PUT',
440
+ url: getServerUrl(fastify) + '/test',
441
+ body: JSON.stringify({ hello: 'world' }),
442
+ headers: { 'Content-Type': 'application/json' }
443
+ }, (err, response, body) => {
444
+ t.assert.ifError(err)
445
+ t.assert.strictEqual(response.statusCode, 404)
446
+ t.assert.strictEqual(body.toString(), 'this was not found 2')
447
+ done()
444
448
  })
449
+ })
445
450
 
446
- test('framework-unsupported method', t => {
447
- t.plan(3)
448
- sget({
449
- method: 'PROPFIND',
450
- url: getServerUrl(fastify) + '/test',
451
- body: JSON.stringify({ hello: 'world' }),
452
- headers: { 'Content-Type': 'application/json' }
453
- }, (err, response, body) => {
454
- t.error(err)
455
- t.equal(response.statusCode, 404)
456
- t.equal(body.toString(), 'this was not found 2')
457
- })
451
+ await t.test('framework-unsupported method', (t, done) => {
452
+ t.plan(3)
453
+ sget({
454
+ method: 'PROPFIND',
455
+ url: getServerUrl(fastify) + '/test',
456
+ body: JSON.stringify({ hello: 'world' }),
457
+ headers: { 'Content-Type': 'application/json' }
458
+ }, (err, response, body) => {
459
+ t.assert.ifError(err)
460
+ t.assert.strictEqual(response.statusCode, 404)
461
+ t.assert.strictEqual(body.toString(), 'this was not found 2')
462
+ done()
458
463
  })
464
+ })
459
465
 
460
- test('unsupported route', t => {
461
- t.plan(3)
462
- sget({
463
- method: 'GET',
464
- url: getServerUrl(fastify) + '/test/notSupported'
465
- }, (err, response, body) => {
466
- t.error(err)
467
- t.equal(response.statusCode, 404)
468
- t.equal(body.toString(), 'this was not found 2')
469
- })
466
+ await t.test('unsupported route', (t, done) => {
467
+ t.plan(3)
468
+ sget({
469
+ method: 'GET',
470
+ url: getServerUrl(fastify) + '/test/notSupported'
471
+ }, (err, response, body) => {
472
+ t.assert.ifError(err)
473
+ t.assert.strictEqual(response.statusCode, 404)
474
+ t.assert.strictEqual(body.toString(), 'this was not found 2')
475
+ done()
470
476
  })
477
+ })
471
478
 
472
- test('unsupported method 2', t => {
473
- t.plan(3)
474
- sget({
475
- method: 'PUT',
476
- url: getServerUrl(fastify) + '/test2',
477
- body: JSON.stringify({ hello: 'world' }),
478
- headers: { 'Content-Type': 'application/json' }
479
- }, (err, response, body) => {
480
- t.error(err)
481
- t.equal(response.statusCode, 404)
482
- t.equal(body.toString(), 'this was not found 3')
483
- })
479
+ await t.test('unsupported method 2', (t, done) => {
480
+ t.plan(3)
481
+ sget({
482
+ method: 'PUT',
483
+ url: getServerUrl(fastify) + '/test2',
484
+ body: JSON.stringify({ hello: 'world' }),
485
+ headers: { 'Content-Type': 'application/json' }
486
+ }, (err, response, body) => {
487
+ t.assert.ifError(err)
488
+ t.assert.strictEqual(response.statusCode, 404)
489
+ t.assert.strictEqual(body.toString(), 'this was not found 3')
490
+ done()
484
491
  })
492
+ })
485
493
 
486
- test('framework-unsupported method 2', t => {
487
- t.plan(3)
488
- sget({
489
- method: 'PROPFIND',
490
- url: getServerUrl(fastify) + '/test2',
491
- body: JSON.stringify({ hello: 'world' }),
492
- headers: { 'Content-Type': 'application/json' }
493
- }, (err, response, body) => {
494
- t.error(err)
495
- t.equal(response.statusCode, 404)
496
- t.equal(body.toString(), 'this was not found 3')
497
- })
494
+ await t.test('framework-unsupported method 2', (t, done) => {
495
+ t.plan(3)
496
+ sget({
497
+ method: 'PROPFIND',
498
+ url: getServerUrl(fastify) + '/test2',
499
+ body: JSON.stringify({ hello: 'world' }),
500
+ headers: { 'Content-Type': 'application/json' }
501
+ }, (err, response, body) => {
502
+ t.assert.ifError(err)
503
+ t.assert.strictEqual(response.statusCode, 404)
504
+ t.assert.strictEqual(body.toString(), 'this was not found 3')
505
+ done()
498
506
  })
507
+ })
499
508
 
500
- test('unsupported route 2', t => {
501
- t.plan(3)
502
- sget({
503
- method: 'GET',
504
- url: getServerUrl(fastify) + '/test2/notSupported'
505
- }, (err, response, body) => {
506
- t.error(err)
507
- t.equal(response.statusCode, 404)
508
- t.equal(body.toString(), 'this was not found 3')
509
- })
509
+ await t.test('unsupported route 2', (t, done) => {
510
+ t.plan(3)
511
+ sget({
512
+ method: 'GET',
513
+ url: getServerUrl(fastify) + '/test2/notSupported'
514
+ }, (err, response, body) => {
515
+ t.assert.ifError(err)
516
+ t.assert.strictEqual(response.statusCode, 404)
517
+ t.assert.strictEqual(body.toString(), 'this was not found 3')
518
+ done()
510
519
  })
520
+ })
511
521
 
512
- test('unsupported method 3', t => {
513
- t.plan(3)
514
- sget({
515
- method: 'PUT',
516
- url: getServerUrl(fastify) + '/test3/',
517
- body: JSON.stringify({ hello: 'world' }),
518
- headers: { 'Content-Type': 'application/json' }
519
- }, (err, response, body) => {
520
- t.error(err)
521
- t.equal(response.statusCode, 404)
522
- t.equal(body.toString(), 'this was not found 4')
523
- })
522
+ await t.test('unsupported method 3', (t, done) => {
523
+ t.plan(3)
524
+ sget({
525
+ method: 'PUT',
526
+ url: getServerUrl(fastify) + '/test3/',
527
+ body: JSON.stringify({ hello: 'world' }),
528
+ headers: { 'Content-Type': 'application/json' }
529
+ }, (err, response, body) => {
530
+ t.assert.ifError(err)
531
+ t.assert.strictEqual(response.statusCode, 404)
532
+ t.assert.strictEqual(body.toString(), 'this was not found 4')
533
+ done()
524
534
  })
535
+ })
525
536
 
526
- test('framework-unsupported method 3', t => {
527
- t.plan(3)
528
- sget({
529
- method: 'PROPFIND',
530
- url: getServerUrl(fastify) + '/test3/',
531
- body: JSON.stringify({ hello: 'world' }),
532
- headers: { 'Content-Type': 'application/json' }
533
- }, (err, response, body) => {
534
- t.error(err)
535
- t.equal(response.statusCode, 404)
536
- t.equal(body.toString(), 'this was not found 4')
537
- })
537
+ await t.test('framework-unsupported method 3', (t, done) => {
538
+ t.plan(3)
539
+ sget({
540
+ method: 'PROPFIND',
541
+ url: getServerUrl(fastify) + '/test3/',
542
+ body: JSON.stringify({ hello: 'world' }),
543
+ headers: { 'Content-Type': 'application/json' }
544
+ }, (err, response, body) => {
545
+ t.assert.ifError(err)
546
+ t.assert.strictEqual(response.statusCode, 404)
547
+ t.assert.strictEqual(body.toString(), 'this was not found 4')
548
+ done()
538
549
  })
550
+ })
539
551
 
540
- test('unsupported route 3', t => {
541
- t.plan(3)
542
- sget({
543
- method: 'GET',
544
- url: getServerUrl(fastify) + '/test3/notSupported'
545
- }, (err, response, body) => {
546
- t.error(err)
547
- t.equal(response.statusCode, 404)
548
- t.equal(body.toString(), 'this was not found 4')
549
- })
552
+ await t.test('unsupported route 3', (t, done) => {
553
+ t.plan(3)
554
+ sget({
555
+ method: 'GET',
556
+ url: getServerUrl(fastify) + '/test3/notSupported'
557
+ }, (err, response, body) => {
558
+ t.assert.ifError(err)
559
+ t.assert.strictEqual(response.statusCode, 404)
560
+ t.assert.strictEqual(body.toString(), 'this was not found 4')
561
+ done()
550
562
  })
551
563
  })
552
564
  })
553
565
 
554
- test('custom 404 hook and handler context', t => {
555
- t.plan(21)
566
+ test('custom 404 hook and handler context', async t => {
567
+ t.plan(19)
556
568
 
557
569
  const fastify = Fastify()
558
570
 
559
571
  fastify.decorate('foo', 42)
560
572
 
561
573
  fastify.addHook('onRequest', function (req, res, done) {
562
- t.equal(this.foo, 42)
574
+ t.assert.strictEqual(this.foo, 42)
563
575
  done()
564
576
  })
565
577
  fastify.addHook('preHandler', function (request, reply, done) {
566
- t.equal(this.foo, 42)
578
+ t.assert.strictEqual(this.foo, 42)
567
579
  done()
568
580
  })
569
581
  fastify.addHook('onSend', function (request, reply, payload, done) {
570
- t.equal(this.foo, 42)
582
+ t.assert.strictEqual(this.foo, 42)
571
583
  done()
572
584
  })
573
585
  fastify.addHook('onResponse', function (request, reply, done) {
574
- t.equal(this.foo, 42)
586
+ t.assert.strictEqual(this.foo, 42)
575
587
  done()
576
588
  })
577
589
 
578
590
  fastify.setNotFoundHandler(function (req, reply) {
579
- t.equal(this.foo, 42)
591
+ t.assert.strictEqual(this.foo, 42)
580
592
  reply.code(404).send('this was not found')
581
593
  })
582
594
 
@@ -584,45 +596,45 @@ test('custom 404 hook and handler context', t => {
584
596
  instance.decorate('bar', 84)
585
597
 
586
598
  instance.addHook('onRequest', function (req, res, done) {
587
- t.equal(this.bar, 84)
599
+ t.assert.strictEqual(this.bar, 84)
588
600
  done()
589
601
  })
590
602
  instance.addHook('preHandler', function (request, reply, done) {
591
- t.equal(this.bar, 84)
603
+ t.assert.strictEqual(this.bar, 84)
592
604
  done()
593
605
  })
594
606
  instance.addHook('onSend', function (request, reply, payload, done) {
595
- t.equal(this.bar, 84)
607
+ t.assert.strictEqual(this.bar, 84)
596
608
  done()
597
609
  })
598
610
  instance.addHook('onResponse', function (request, reply, done) {
599
- t.equal(this.bar, 84)
611
+ t.assert.strictEqual(this.bar, 84)
600
612
  done()
601
613
  })
602
614
 
603
615
  instance.setNotFoundHandler(function (req, reply) {
604
- t.equal(this.foo, 42)
605
- t.equal(this.bar, 84)
616
+ t.assert.strictEqual(this.foo, 42)
617
+ t.assert.strictEqual(this.bar, 84)
606
618
  reply.code(404).send('encapsulated was not found')
607
619
  })
608
620
 
609
621
  done()
610
622
  }, { prefix: '/encapsulated' })
611
623
 
612
- fastify.inject('/not-found', (err, res) => {
613
- t.error(err)
614
- t.equal(res.statusCode, 404)
615
- t.equal(res.payload, 'this was not found')
616
- })
624
+ {
625
+ const res = await fastify.inject('/not-found')
626
+ t.assert.strictEqual(res.statusCode, 404)
627
+ t.assert.strictEqual(res.payload, 'this was not found')
628
+ }
617
629
 
618
- fastify.inject('/encapsulated/not-found', (err, res) => {
619
- t.error(err)
620
- t.equal(res.statusCode, 404)
621
- t.equal(res.payload, 'encapsulated was not found')
622
- })
630
+ {
631
+ const res = await fastify.inject('/encapsulated/not-found')
632
+ t.assert.strictEqual(res.statusCode, 404)
633
+ t.assert.strictEqual(res.payload, 'encapsulated was not found')
634
+ }
623
635
  })
624
636
 
625
- test('encapsulated custom 404 without - prefix hook and handler context', t => {
637
+ test('encapsulated custom 404 without - prefix hook and handler context', (t, done) => {
626
638
  t.plan(13)
627
639
 
628
640
  const fastify = Fastify()
@@ -633,29 +645,29 @@ test('encapsulated custom 404 without - prefix hook and handler context', t => {
633
645
  instance.decorate('bar', 84)
634
646
 
635
647
  instance.addHook('onRequest', function (req, res, done) {
636
- t.equal(this.foo, 42)
637
- t.equal(this.bar, 84)
648
+ t.assert.strictEqual(this.foo, 42)
649
+ t.assert.strictEqual(this.bar, 84)
638
650
  done()
639
651
  })
640
652
  instance.addHook('preHandler', function (request, reply, done) {
641
- t.equal(this.foo, 42)
642
- t.equal(this.bar, 84)
653
+ t.assert.strictEqual(this.foo, 42)
654
+ t.assert.strictEqual(this.bar, 84)
643
655
  done()
644
656
  })
645
657
  instance.addHook('onSend', function (request, reply, payload, done) {
646
- t.equal(this.foo, 42)
647
- t.equal(this.bar, 84)
658
+ t.assert.strictEqual(this.foo, 42)
659
+ t.assert.strictEqual(this.bar, 84)
648
660
  done()
649
661
  })
650
662
  instance.addHook('onResponse', function (request, reply, done) {
651
- t.equal(this.foo, 42)
652
- t.equal(this.bar, 84)
663
+ t.assert.strictEqual(this.foo, 42)
664
+ t.assert.strictEqual(this.bar, 84)
653
665
  done()
654
666
  })
655
667
 
656
668
  instance.setNotFoundHandler(function (request, reply) {
657
- t.equal(this.foo, 42)
658
- t.equal(this.bar, 84)
669
+ t.assert.strictEqual(this.foo, 42)
670
+ t.assert.strictEqual(this.bar, 84)
659
671
  reply.code(404).send('custom not found')
660
672
  })
661
673
 
@@ -663,34 +675,35 @@ test('encapsulated custom 404 without - prefix hook and handler context', t => {
663
675
  })
664
676
 
665
677
  fastify.inject('/not-found', (err, res) => {
666
- t.error(err)
667
- t.equal(res.statusCode, 404)
668
- t.equal(res.payload, 'custom not found')
678
+ t.assert.ifError(err)
679
+ t.assert.strictEqual(res.statusCode, 404)
680
+ t.assert.strictEqual(res.payload, 'custom not found')
681
+ done()
669
682
  })
670
683
  })
671
684
 
672
- test('run hooks on default 404', t => {
685
+ test('run hooks on default 404', (t, done) => {
673
686
  t.plan(7)
674
687
 
675
688
  const fastify = Fastify()
676
689
 
677
690
  fastify.addHook('onRequest', function (req, res, done) {
678
- t.pass('onRequest called')
691
+ t.assert.ok(true, 'onRequest called')
679
692
  done()
680
693
  })
681
694
 
682
695
  fastify.addHook('preHandler', function (request, reply, done) {
683
- t.pass('preHandler called')
696
+ t.assert.ok(true, 'preHandler called')
684
697
  done()
685
698
  })
686
699
 
687
700
  fastify.addHook('onSend', function (request, reply, payload, done) {
688
- t.pass('onSend called')
701
+ t.assert.ok(true, 'onSend called')
689
702
  done()
690
703
  })
691
704
 
692
705
  fastify.addHook('onResponse', function (request, reply, done) {
693
- t.pass('onResponse called')
706
+ t.assert.ok(true, 'onResponse called')
694
707
  done()
695
708
  })
696
709
 
@@ -698,10 +711,10 @@ test('run hooks on default 404', t => {
698
711
  reply.send({ hello: 'world' })
699
712
  })
700
713
 
701
- t.teardown(fastify.close.bind(fastify))
714
+ t.after(() => { fastify.close() })
702
715
 
703
716
  fastify.listen({ port: 0 }, err => {
704
- t.error(err)
717
+ t.assert.ifError(err)
705
718
 
706
719
  sget({
707
720
  method: 'PUT',
@@ -709,35 +722,36 @@ test('run hooks on default 404', t => {
709
722
  body: JSON.stringify({ hello: 'world' }),
710
723
  headers: { 'Content-Type': 'application/json' }
711
724
  }, (err, response, body) => {
712
- t.error(err)
713
- t.equal(response.statusCode, 404)
725
+ t.assert.ifError(err)
726
+ t.assert.strictEqual(response.statusCode, 404)
727
+ done()
714
728
  })
715
729
  })
716
730
  })
717
731
 
718
- test('run non-encapsulated plugin hooks on default 404', t => {
732
+ test('run non-encapsulated plugin hooks on default 404', (t, done) => {
719
733
  t.plan(6)
720
734
 
721
735
  const fastify = Fastify()
722
736
 
723
737
  fastify.register(fp(function (instance, options, done) {
724
738
  instance.addHook('onRequest', function (req, res, done) {
725
- t.pass('onRequest called')
739
+ t.assert.ok(true, 'onRequest called')
726
740
  done()
727
741
  })
728
742
 
729
743
  instance.addHook('preHandler', function (request, reply, done) {
730
- t.pass('preHandler called')
744
+ t.assert.ok(true, 'preHandler called')
731
745
  done()
732
746
  })
733
747
 
734
748
  instance.addHook('onSend', function (request, reply, payload, done) {
735
- t.pass('onSend called')
749
+ t.assert.ok(true, 'onSend called')
736
750
  done()
737
751
  })
738
752
 
739
753
  instance.addHook('onResponse', function (request, reply, done) {
740
- t.pass('onResponse called')
754
+ t.assert.ok(true, 'onResponse called')
741
755
  done()
742
756
  })
743
757
 
@@ -753,34 +767,35 @@ test('run non-encapsulated plugin hooks on default 404', t => {
753
767
  url: '/',
754
768
  payload: { hello: 'world' }
755
769
  }, (err, res) => {
756
- t.error(err)
757
- t.equal(res.statusCode, 404)
770
+ t.assert.ifError(err)
771
+ t.assert.strictEqual(res.statusCode, 404)
772
+ done()
758
773
  })
759
774
  })
760
775
 
761
- test('run non-encapsulated plugin hooks on custom 404', t => {
776
+ test('run non-encapsulated plugin hooks on custom 404', (t, done) => {
762
777
  t.plan(11)
763
778
 
764
779
  const fastify = Fastify()
765
780
 
766
781
  const plugin = fp((instance, opts, done) => {
767
782
  instance.addHook('onRequest', function (req, res, done) {
768
- t.pass('onRequest called')
783
+ t.assert.ok(true, 'onRequest called')
769
784
  done()
770
785
  })
771
786
 
772
787
  instance.addHook('preHandler', function (request, reply, done) {
773
- t.pass('preHandler called')
788
+ t.assert.ok(true, 'preHandler called')
774
789
  done()
775
790
  })
776
791
 
777
792
  instance.addHook('onSend', function (request, reply, payload, done) {
778
- t.pass('onSend called')
793
+ t.assert.ok(true, 'onSend called')
779
794
  done()
780
795
  })
781
796
 
782
797
  instance.addHook('onResponse', function (request, reply, done) {
783
- t.pass('onResponse called')
798
+ t.assert.ok(true, 'onResponse called')
784
799
  done()
785
800
  })
786
801
 
@@ -800,34 +815,35 @@ test('run non-encapsulated plugin hooks on custom 404', t => {
800
815
  fastify.register(plugin) // Registering plugin after handler also works
801
816
 
802
817
  fastify.inject({ url: '/not-found' }, (err, res) => {
803
- t.error(err)
804
- t.equal(res.statusCode, 404)
805
- t.equal(res.payload, 'this was not found')
818
+ t.assert.ifError(err)
819
+ t.assert.strictEqual(res.statusCode, 404)
820
+ t.assert.strictEqual(res.payload, 'this was not found')
821
+ done()
806
822
  })
807
823
  })
808
824
 
809
- test('run hook with encapsulated 404', t => {
825
+ test('run hook with encapsulated 404', (t, done) => {
810
826
  t.plan(11)
811
827
 
812
828
  const fastify = Fastify()
813
829
 
814
830
  fastify.addHook('onRequest', function (req, res, done) {
815
- t.pass('onRequest called')
831
+ t.assert.ok(true, 'onRequest called')
816
832
  done()
817
833
  })
818
834
 
819
835
  fastify.addHook('preHandler', function (request, reply, done) {
820
- t.pass('preHandler called')
836
+ t.assert.ok(true, 'preHandler called')
821
837
  done()
822
838
  })
823
839
 
824
840
  fastify.addHook('onSend', function (request, reply, payload, done) {
825
- t.pass('onSend called')
841
+ t.assert.ok(true, 'onSend called')
826
842
  done()
827
843
  })
828
844
 
829
845
  fastify.addHook('onResponse', function (request, reply, done) {
830
- t.pass('onResponse called')
846
+ t.assert.ok(true, 'onResponse called')
831
847
  done()
832
848
  })
833
849
 
@@ -837,32 +853,32 @@ test('run hook with encapsulated 404', t => {
837
853
  })
838
854
 
839
855
  f.addHook('onRequest', function (req, res, done) {
840
- t.pass('onRequest 2 called')
856
+ t.assert.ok(true, 'onRequest 2 called')
841
857
  done()
842
858
  })
843
859
 
844
860
  f.addHook('preHandler', function (request, reply, done) {
845
- t.pass('preHandler 2 called')
861
+ t.assert.ok(true, 'preHandler 2 called')
846
862
  done()
847
863
  })
848
864
 
849
865
  f.addHook('onSend', function (request, reply, payload, done) {
850
- t.pass('onSend 2 called')
866
+ t.assert.ok(true, 'onSend 2 called')
851
867
  done()
852
868
  })
853
869
 
854
870
  f.addHook('onResponse', function (request, reply, done) {
855
- t.pass('onResponse 2 called')
871
+ t.assert.ok(true, 'onResponse 2 called')
856
872
  done()
857
873
  })
858
874
 
859
875
  done()
860
876
  }, { prefix: '/test' })
861
877
 
862
- t.teardown(fastify.close.bind(fastify))
878
+ t.after(() => { fastify.close() })
863
879
 
864
880
  fastify.listen({ port: 0 }, err => {
865
- t.error(err)
881
+ t.assert.ifError(err)
866
882
 
867
883
  sget({
868
884
  method: 'PUT',
@@ -870,34 +886,35 @@ test('run hook with encapsulated 404', t => {
870
886
  body: JSON.stringify({ hello: 'world' }),
871
887
  headers: { 'Content-Type': 'application/json' }
872
888
  }, (err, response, body) => {
873
- t.error(err)
874
- t.equal(response.statusCode, 404)
889
+ t.assert.ifError(err)
890
+ t.assert.strictEqual(response.statusCode, 404)
891
+ done()
875
892
  })
876
893
  })
877
894
  })
878
895
 
879
- test('run hook with encapsulated 404 and framework-unsupported method', t => {
896
+ test('run hook with encapsulated 404 and framework-unsupported method', (t, done) => {
880
897
  t.plan(11)
881
898
 
882
899
  const fastify = Fastify()
883
900
 
884
901
  fastify.addHook('onRequest', function (req, res, done) {
885
- t.pass('onRequest called')
902
+ t.assert.ok(true, 'onRequest called')
886
903
  done()
887
904
  })
888
905
 
889
906
  fastify.addHook('preHandler', function (request, reply, done) {
890
- t.pass('preHandler called')
907
+ t.assert.ok(true, 'preHandler called')
891
908
  done()
892
909
  })
893
910
 
894
911
  fastify.addHook('onSend', function (request, reply, payload, done) {
895
- t.pass('onSend called')
912
+ t.assert.ok(true, 'onSend called')
896
913
  done()
897
914
  })
898
915
 
899
916
  fastify.addHook('onResponse', function (request, reply, done) {
900
- t.pass('onResponse called')
917
+ t.assert.ok(true, 'onResponse called')
901
918
  done()
902
919
  })
903
920
 
@@ -907,32 +924,32 @@ test('run hook with encapsulated 404 and framework-unsupported method', t => {
907
924
  })
908
925
 
909
926
  f.addHook('onRequest', function (req, res, done) {
910
- t.pass('onRequest 2 called')
927
+ t.assert.ok(true, 'onRequest 2 called')
911
928
  done()
912
929
  })
913
930
 
914
931
  f.addHook('preHandler', function (request, reply, done) {
915
- t.pass('preHandler 2 called')
932
+ t.assert.ok(true, 'preHandler 2 called')
916
933
  done()
917
934
  })
918
935
 
919
936
  f.addHook('onSend', function (request, reply, payload, done) {
920
- t.pass('onSend 2 called')
937
+ t.assert.ok(true, 'onSend 2 called')
921
938
  done()
922
939
  })
923
940
 
924
941
  f.addHook('onResponse', function (request, reply, done) {
925
- t.pass('onResponse 2 called')
942
+ t.assert.ok(true, 'onResponse 2 called')
926
943
  done()
927
944
  })
928
945
 
929
946
  done()
930
947
  }, { prefix: '/test' })
931
948
 
932
- t.teardown(fastify.close.bind(fastify))
949
+ t.after(() => { fastify.close() })
933
950
 
934
951
  fastify.listen({ port: 0 }, err => {
935
- t.error(err)
952
+ t.assert.ifError(err)
936
953
 
937
954
  sget({
938
955
  method: 'PROPFIND',
@@ -940,13 +957,14 @@ test('run hook with encapsulated 404 and framework-unsupported method', t => {
940
957
  body: JSON.stringify({ hello: 'world' }),
941
958
  headers: { 'Content-Type': 'application/json' }
942
959
  }, (err, response, body) => {
943
- t.error(err)
944
- t.equal(response.statusCode, 404)
960
+ t.assert.ifError(err)
961
+ t.assert.strictEqual(response.statusCode, 404)
962
+ done()
945
963
  })
946
964
  })
947
965
  })
948
966
 
949
- test('hooks check 404', t => {
967
+ test('hooks check 404', (t, done) => {
950
968
  t.plan(13)
951
969
 
952
970
  const fastify = Fastify()
@@ -956,23 +974,23 @@ test('hooks check 404', t => {
956
974
  })
957
975
 
958
976
  fastify.addHook('onSend', (req, reply, payload, done) => {
959
- t.same(req.query, { foo: 'asd' })
960
- t.ok('called', 'onSend')
977
+ t.assert.deepStrictEqual(req.query, { foo: 'asd' })
978
+ t.assert.ok(true, 'called onSend')
961
979
  done()
962
980
  })
963
981
  fastify.addHook('onRequest', (req, res, done) => {
964
- t.ok('called', 'onRequest')
982
+ t.assert.ok(true, 'called onRequest')
965
983
  done()
966
984
  })
967
985
  fastify.addHook('onResponse', (request, reply, done) => {
968
- t.ok('called', 'onResponse')
986
+ t.assert.ok(true, 'calledonResponse')
969
987
  done()
970
988
  })
971
989
 
972
- t.teardown(fastify.close.bind(fastify))
990
+ t.after(() => { fastify.close() })
973
991
 
974
992
  fastify.listen({ port: 0 }, err => {
975
- t.error(err)
993
+ t.assert.ifError(err)
976
994
 
977
995
  sget({
978
996
  method: 'PUT',
@@ -980,16 +998,17 @@ test('hooks check 404', t => {
980
998
  body: JSON.stringify({ hello: 'world' }),
981
999
  headers: { 'Content-Type': 'application/json' }
982
1000
  }, (err, response, body) => {
983
- t.error(err)
984
- t.equal(response.statusCode, 404)
1001
+ t.assert.ifError(err)
1002
+ t.assert.strictEqual(response.statusCode, 404)
985
1003
  })
986
1004
 
987
1005
  sget({
988
1006
  method: 'GET',
989
1007
  url: getServerUrl(fastify) + '/notSupported?foo=asd'
990
1008
  }, (err, response, body) => {
991
- t.error(err)
992
- t.equal(response.statusCode, 404)
1009
+ t.assert.ifError(err)
1010
+ t.assert.strictEqual(response.statusCode, 404)
1011
+ done()
993
1012
  })
994
1013
  })
995
1014
  })
@@ -1010,13 +1029,13 @@ test('setNotFoundHandler should not suppress duplicated routes checking', t => {
1010
1029
  reply.code(404).send('this was not found')
1011
1030
  })
1012
1031
 
1013
- t.fail('setNotFoundHandler should not interfere duplicated route error')
1032
+ t.assert.fail('setNotFoundHandler should not interfere duplicated route error')
1014
1033
  } catch (error) {
1015
- t.ok(error)
1034
+ t.assert.ok(error)
1016
1035
  }
1017
1036
  })
1018
1037
 
1019
- test('log debug for 404', t => {
1038
+ test('log debug for 404', async t => {
1020
1039
  t.plan(1)
1021
1040
 
1022
1041
  const Writable = require('node:stream').Writable
@@ -1039,28 +1058,29 @@ test('log debug for 404', t => {
1039
1058
  reply.send({ hello: 'world' })
1040
1059
  })
1041
1060
 
1042
- t.teardown(fastify.close.bind(fastify))
1061
+ t.after(() => { fastify.close() })
1043
1062
 
1044
- t.test('log debug', t => {
1063
+ await t.test('log debug', (t, done) => {
1045
1064
  t.plan(7)
1046
1065
  fastify.inject({
1047
1066
  method: 'GET',
1048
1067
  url: '/not-found'
1049
1068
  }, (err, response) => {
1050
- t.error(err)
1051
- t.equal(response.statusCode, 404)
1069
+ t.assert.ifError(err)
1070
+ t.assert.strictEqual(response.statusCode, 404)
1052
1071
 
1053
1072
  const INFO_LEVEL = 30
1054
- t.equal(JSON.parse(logStream.logs[0]).msg, 'incoming request')
1055
- t.equal(JSON.parse(logStream.logs[1]).msg, 'Route GET:/not-found not found')
1056
- t.equal(JSON.parse(logStream.logs[1]).level, INFO_LEVEL)
1057
- t.equal(JSON.parse(logStream.logs[2]).msg, 'request completed')
1058
- t.equal(logStream.logs.length, 3)
1073
+ t.assert.strictEqual(JSON.parse(logStream.logs[0]).msg, 'incoming request')
1074
+ t.assert.strictEqual(JSON.parse(logStream.logs[1]).msg, 'Route GET:/not-found not found')
1075
+ t.assert.strictEqual(JSON.parse(logStream.logs[1]).level, INFO_LEVEL)
1076
+ t.assert.strictEqual(JSON.parse(logStream.logs[2]).msg, 'request completed')
1077
+ t.assert.strictEqual(logStream.logs.length, 3)
1078
+ done()
1059
1079
  })
1060
1080
  })
1061
1081
  })
1062
1082
 
1063
- test('Unknown method', t => {
1083
+ test('Unknown method', (t, done) => {
1064
1084
  t.plan(5)
1065
1085
 
1066
1086
  const fastify = Fastify()
@@ -1069,14 +1089,14 @@ test('Unknown method', t => {
1069
1089
  reply.send({ hello: 'world' })
1070
1090
  })
1071
1091
 
1072
- t.teardown(fastify.close.bind(fastify))
1092
+ t.after(() => { fastify.close() })
1073
1093
 
1074
1094
  fastify.listen({ port: 0 }, err => {
1075
- t.error(err)
1095
+ t.assert.ifError(err)
1076
1096
 
1077
1097
  const handler = () => {}
1078
1098
  // See https://github.com/fastify/light-my-request/pull/20
1079
- t.throws(() => fastify.inject({
1099
+ t.assert.throws(() => fastify.inject({
1080
1100
  method: 'UNKNOWN_METHOD',
1081
1101
  url: '/'
1082
1102
  }, handler), Error)
@@ -1085,18 +1105,19 @@ test('Unknown method', t => {
1085
1105
  method: 'UNKNOWN_METHOD',
1086
1106
  url: getServerUrl(fastify)
1087
1107
  }, (err, response, body) => {
1088
- t.error(err)
1089
- t.equal(response.statusCode, 400)
1090
- t.strictSame(JSON.parse(body), {
1108
+ t.assert.ifError(err)
1109
+ t.assert.strictEqual(response.statusCode, 400)
1110
+ t.assert.deepStrictEqual(JSON.parse(body), {
1091
1111
  error: 'Bad Request',
1092
1112
  message: 'Client Error',
1093
1113
  statusCode: 400
1094
1114
  })
1115
+ done()
1095
1116
  })
1096
1117
  })
1097
1118
  })
1098
1119
 
1099
- test('recognizes errors from the http-errors module', t => {
1120
+ test('recognizes errors from the http-errors module', (t, done) => {
1100
1121
  t.plan(5)
1101
1122
 
1102
1123
  const fastify = Fastify()
@@ -1105,32 +1126,33 @@ test('recognizes errors from the http-errors module', t => {
1105
1126
  reply.send(new errors.NotFound())
1106
1127
  })
1107
1128
 
1108
- t.teardown(fastify.close.bind(fastify))
1129
+ t.after(() => { fastify.close() })
1109
1130
 
1110
1131
  fastify.listen({ port: 0 }, err => {
1111
- t.error(err)
1132
+ t.assert.ifError(err)
1112
1133
 
1113
1134
  fastify.inject({
1114
1135
  method: 'GET',
1115
1136
  url: '/'
1116
1137
  }, (err, res) => {
1117
- t.error(err)
1118
- t.equal(res.statusCode, 404)
1138
+ t.assert.ifError(err)
1139
+ t.assert.strictEqual(res.statusCode, 404)
1119
1140
 
1120
1141
  sget(getServerUrl(fastify), (err, response, body) => {
1121
- t.error(err)
1142
+ t.assert.ifError(err)
1122
1143
  const obj = JSON.parse(body.toString())
1123
- t.strictSame(obj, {
1144
+ t.assert.deepStrictEqual(obj, {
1124
1145
  error: 'Not Found',
1125
1146
  message: 'Not Found',
1126
1147
  statusCode: 404
1127
1148
  })
1149
+ done()
1128
1150
  })
1129
1151
  })
1130
1152
  })
1131
1153
  })
1132
1154
 
1133
- test('the default 404 handler can be invoked inside a prefixed plugin', t => {
1155
+ test('the default 404 handler can be invoked inside a prefixed plugin', (t, done) => {
1134
1156
  t.plan(3)
1135
1157
 
1136
1158
  const fastify = Fastify()
@@ -1144,17 +1166,18 @@ test('the default 404 handler can be invoked inside a prefixed plugin', t => {
1144
1166
  }, { prefix: '/v1' })
1145
1167
 
1146
1168
  fastify.inject('/v1/path', (err, res) => {
1147
- t.error(err)
1148
- t.equal(res.statusCode, 404)
1149
- t.strictSame(JSON.parse(res.payload), {
1169
+ t.assert.ifError(err)
1170
+ t.assert.strictEqual(res.statusCode, 404)
1171
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
1150
1172
  error: 'Not Found',
1151
1173
  message: 'Not Found',
1152
1174
  statusCode: 404
1153
1175
  })
1176
+ done()
1154
1177
  })
1155
1178
  })
1156
1179
 
1157
- test('an inherited custom 404 handler can be invoked inside a prefixed plugin', t => {
1180
+ test('an inherited custom 404 handler can be invoked inside a prefixed plugin', (t, done) => {
1158
1181
  t.plan(3)
1159
1182
 
1160
1183
  const fastify = Fastify()
@@ -1172,18 +1195,19 @@ test('an inherited custom 404 handler can be invoked inside a prefixed plugin',
1172
1195
  }, { prefix: '/v1' })
1173
1196
 
1174
1197
  fastify.inject('/v1/path', (err, res) => {
1175
- t.error(err)
1176
- t.equal(res.statusCode, 404)
1177
- t.same(JSON.parse(res.payload), {
1198
+ t.assert.ifError(err)
1199
+ t.assert.strictEqual(res.statusCode, 404)
1200
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
1178
1201
  error: 'Not Found',
1179
1202
  message: 'Not Found',
1180
1203
  statusCode: 404
1181
1204
  })
1205
+ done()
1182
1206
  })
1183
1207
  })
1184
1208
 
1185
- test('encapsulated custom 404 handler without a prefix is the handler for the entire 404 level', t => {
1186
- t.plan(6)
1209
+ test('encapsulated custom 404 handler without a prefix is the handler for the entire 404 level', async t => {
1210
+ t.plan(4)
1187
1211
 
1188
1212
  const fastify = Fastify()
1189
1213
 
@@ -1206,38 +1230,39 @@ test('encapsulated custom 404 handler without a prefix is the handler for the en
1206
1230
  done()
1207
1231
  }, { prefix: 'prefixed' })
1208
1232
 
1209
- fastify.inject('/not-found', (err, res) => {
1210
- t.error(err)
1211
- t.equal(res.statusCode, 404)
1212
- t.equal(res.payload, 'custom handler')
1213
- })
1233
+ {
1234
+ const res = await fastify.inject('/not-found')
1235
+ t.assert.strictEqual(res.statusCode, 404)
1236
+ t.assert.strictEqual(res.payload, 'custom handler')
1237
+ }
1214
1238
 
1215
- fastify.inject('/prefixed/not-found', (err, res) => {
1216
- t.error(err)
1217
- t.equal(res.statusCode, 404)
1218
- t.equal(res.payload, 'custom handler 2')
1219
- })
1239
+ {
1240
+ const res = await fastify.inject('/prefixed/not-found')
1241
+ t.assert.strictEqual(res.statusCode, 404)
1242
+ t.assert.strictEqual(res.payload, 'custom handler 2')
1243
+ }
1220
1244
  })
1221
1245
 
1222
- test('cannot set notFoundHandler after binding', t => {
1246
+ test('cannot set notFoundHandler after binding', (t, done) => {
1223
1247
  t.plan(2)
1224
1248
 
1225
1249
  const fastify = Fastify()
1226
- t.teardown(fastify.close.bind(fastify))
1250
+ t.after(() => { fastify.close() })
1227
1251
 
1228
1252
  fastify.listen({ port: 0 }, err => {
1229
- t.error(err)
1253
+ t.assert.ifError(err)
1230
1254
 
1231
1255
  try {
1232
1256
  fastify.setNotFoundHandler(() => { })
1233
- t.fail()
1257
+ t.assert.fail()
1234
1258
  } catch (e) {
1235
- t.pass()
1259
+ t.assert.ok(true)
1260
+ done()
1236
1261
  }
1237
1262
  })
1238
1263
  })
1239
1264
 
1240
- test('404 inside onSend', t => {
1265
+ test('404 inside onSend', (t, done) => {
1241
1266
  t.plan(3)
1242
1267
 
1243
1268
  const fastify = Fastify()
@@ -1257,29 +1282,30 @@ test('404 inside onSend', t => {
1257
1282
  }
1258
1283
  })
1259
1284
 
1260
- t.teardown(fastify.close.bind(fastify))
1285
+ t.after(() => { fastify.close() })
1261
1286
 
1262
1287
  fastify.listen({ port: 0 }, err => {
1263
- t.error(err)
1288
+ t.assert.ifError(err)
1264
1289
 
1265
1290
  sget({
1266
1291
  method: 'GET',
1267
1292
  url: getServerUrl(fastify)
1268
1293
  }, (err, response, body) => {
1269
- t.error(err)
1270
- t.equal(response.statusCode, 404)
1294
+ t.assert.ifError(err)
1295
+ t.assert.strictEqual(response.statusCode, 404)
1296
+ done()
1271
1297
  })
1272
1298
  })
1273
1299
  })
1274
1300
 
1275
1301
  // https://github.com/fastify/fastify/issues/868
1276
- test('onSend hooks run when an encapsulated route invokes the notFound handler', t => {
1302
+ test('onSend hooks run when an encapsulated route invokes the notFound handler', (t, done) => {
1277
1303
  t.plan(3)
1278
1304
  const fastify = Fastify()
1279
1305
 
1280
1306
  fastify.register((instance, options, done) => {
1281
1307
  instance.addHook('onSend', (request, reply, payload, done) => {
1282
- t.pass('onSend hook called')
1308
+ t.assert.ok(true, 'onSend hook called')
1283
1309
  done()
1284
1310
  })
1285
1311
 
@@ -1291,16 +1317,17 @@ test('onSend hooks run when an encapsulated route invokes the notFound handler',
1291
1317
  })
1292
1318
 
1293
1319
  fastify.inject('/', (err, res) => {
1294
- t.error(err)
1295
- t.equal(res.statusCode, 404)
1320
+ t.assert.ifError(err)
1321
+ t.assert.strictEqual(res.statusCode, 404)
1322
+ done()
1296
1323
  })
1297
1324
  })
1298
1325
 
1299
1326
  // https://github.com/fastify/fastify/issues/713
1300
- test('preHandler option for setNotFoundHandler', t => {
1327
+ test('preHandler option for setNotFoundHandler', async t => {
1301
1328
  t.plan(10)
1302
1329
 
1303
- t.test('preHandler option', t => {
1330
+ await t.test('preHandler option', (t, done) => {
1304
1331
  t.plan(2)
1305
1332
  const fastify = Fastify()
1306
1333
 
@@ -1318,14 +1345,15 @@ test('preHandler option for setNotFoundHandler', t => {
1318
1345
  url: '/not-found',
1319
1346
  payload: { hello: 'world' }
1320
1347
  }, (err, res) => {
1321
- t.error(err)
1348
+ t.assert.ifError(err)
1322
1349
  const payload = JSON.parse(res.payload)
1323
- t.same(payload, { preHandler: true, hello: 'world' })
1350
+ t.assert.deepStrictEqual(payload, { preHandler: true, hello: 'world' })
1351
+ done()
1324
1352
  })
1325
1353
  })
1326
1354
 
1327
1355
  // https://github.com/fastify/fastify/issues/2229
1328
- t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', { timeout: 40000 }, t => {
1356
+ await t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', { timeout: 40000 }, (t, done) => {
1329
1357
  t.plan(3)
1330
1358
  const fastify = Fastify()
1331
1359
 
@@ -1339,7 +1367,7 @@ test('preHandler option for setNotFoundHandler', t => {
1339
1367
  })
1340
1368
 
1341
1369
  fastify.post('/', function (req, reply) {
1342
- t.equal(reply.callNotFound(), reply)
1370
+ t.assert.strictEqual(reply.callNotFound(), reply)
1343
1371
  })
1344
1372
 
1345
1373
  fastify.inject({
@@ -1347,13 +1375,14 @@ test('preHandler option for setNotFoundHandler', t => {
1347
1375
  url: '/',
1348
1376
  payload: { hello: 'world' }
1349
1377
  }, (err, res) => {
1350
- t.error(err)
1378
+ t.assert.ifError(err)
1351
1379
  const payload = JSON.parse(res.payload)
1352
- t.same(payload, { preHandler: true, hello: 'world' })
1380
+ t.assert.deepStrictEqual(payload, { preHandler: true, hello: 'world' })
1381
+ done()
1353
1382
  })
1354
1383
  })
1355
1384
 
1356
- t.test('preHandler hook in setNotFoundHandler should accept an array of functions and be called when callNotFound', t => {
1385
+ await t.test('preHandler hook in setNotFoundHandler should accept an array of functions and be called when callNotFound', (t, done) => {
1357
1386
  t.plan(2)
1358
1387
  const fastify = Fastify()
1359
1388
 
@@ -1381,13 +1410,14 @@ test('preHandler option for setNotFoundHandler', t => {
1381
1410
  url: '/',
1382
1411
  payload: { hello: 'world' }
1383
1412
  }, (err, res) => {
1384
- t.error(err)
1413
+ t.assert.ifError(err)
1385
1414
  const payload = JSON.parse(res.payload)
1386
- t.same(payload, { preHandler1: true, preHandler2: true, hello: 'world' })
1415
+ t.assert.deepStrictEqual(payload, { preHandler1: true, preHandler2: true, hello: 'world' })
1416
+ done()
1387
1417
  })
1388
1418
  })
1389
1419
 
1390
- t.test('preHandler option should be called after preHandler hook', t => {
1420
+ await t.test('preHandler option should be called after preHandler hook', (t, done) => {
1391
1421
  t.plan(2)
1392
1422
  const fastify = Fastify()
1393
1423
 
@@ -1410,14 +1440,15 @@ test('preHandler option for setNotFoundHandler', t => {
1410
1440
  url: '/',
1411
1441
  payload: { hello: 'world' }
1412
1442
  }, (err, res) => {
1413
- t.error(err)
1443
+ t.assert.ifError(err)
1414
1444
  const payload = JSON.parse(res.payload)
1415
- t.same(payload, { check: 'ab', hello: 'world' })
1445
+ t.assert.deepStrictEqual(payload, { check: 'ab', hello: 'world' })
1446
+ done()
1416
1447
  })
1417
1448
  })
1418
1449
 
1419
- t.test('preHandler option should be unique per prefix', t => {
1420
- t.plan(4)
1450
+ await t.test('preHandler option should be unique per prefix', async t => {
1451
+ t.plan(2)
1421
1452
  const fastify = Fastify()
1422
1453
 
1423
1454
  fastify.setNotFoundHandler({
@@ -1437,28 +1468,30 @@ test('preHandler option for setNotFoundHandler', t => {
1437
1468
  n()
1438
1469
  }, { prefix: '/no' })
1439
1470
 
1440
- fastify.inject({
1441
- method: 'POST',
1442
- url: '/not-found',
1443
- payload: { hello: 'world' }
1444
- }, (err, res) => {
1445
- t.error(err)
1471
+ {
1472
+ const res = await fastify.inject({
1473
+ method: 'POST',
1474
+ url: '/not-found',
1475
+ payload: { hello: 'world' }
1476
+ })
1477
+
1446
1478
  const payload = JSON.parse(res.payload)
1447
- t.same(payload, { hello: 'earth' })
1448
- })
1479
+ t.assert.deepStrictEqual(payload, { hello: 'earth' })
1480
+ }
1481
+
1482
+ {
1483
+ const res = await fastify.inject({
1484
+ method: 'POST',
1485
+ url: '/no/not-found',
1486
+ payload: { hello: 'world' }
1487
+ })
1449
1488
 
1450
- fastify.inject({
1451
- method: 'POST',
1452
- url: '/no/not-found',
1453
- payload: { hello: 'world' }
1454
- }, (err, res) => {
1455
- t.error(err)
1456
1489
  const payload = JSON.parse(res.payload)
1457
- t.same(payload, { hello: 'world' })
1458
- })
1490
+ t.assert.deepStrictEqual(payload, { hello: 'world' })
1491
+ }
1459
1492
  })
1460
1493
 
1461
- t.test('preHandler option should handle errors', t => {
1494
+ await t.test('preHandler option should handle errors', (t, done) => {
1462
1495
  t.plan(3)
1463
1496
  const fastify = Fastify()
1464
1497
 
@@ -1475,18 +1508,19 @@ test('preHandler option for setNotFoundHandler', t => {
1475
1508
  url: '/not-found',
1476
1509
  payload: { hello: 'world' }
1477
1510
  }, (err, res) => {
1478
- t.error(err)
1511
+ t.assert.ifError(err)
1479
1512
  const payload = JSON.parse(res.payload)
1480
- t.equal(res.statusCode, 500)
1481
- t.same(payload, {
1513
+ t.assert.strictEqual(res.statusCode, 500)
1514
+ t.assert.deepStrictEqual(payload, {
1482
1515
  message: 'kaboom',
1483
1516
  error: 'Internal Server Error',
1484
1517
  statusCode: 500
1485
1518
  })
1519
+ done()
1486
1520
  })
1487
1521
  })
1488
1522
 
1489
- t.test('preHandler option should handle errors with custom status code', t => {
1523
+ await t.test('preHandler option should handle errors with custom status code', (t, done) => {
1490
1524
  t.plan(3)
1491
1525
  const fastify = Fastify()
1492
1526
 
@@ -1504,18 +1538,19 @@ test('preHandler option for setNotFoundHandler', t => {
1504
1538
  url: '/not-found',
1505
1539
  payload: { hello: 'world' }
1506
1540
  }, (err, res) => {
1507
- t.error(err)
1541
+ t.assert.ifError(err)
1508
1542
  const payload = JSON.parse(res.payload)
1509
- t.equal(res.statusCode, 401)
1510
- t.same(payload, {
1543
+ t.assert.strictEqual(res.statusCode, 401)
1544
+ t.assert.deepStrictEqual(payload, {
1511
1545
  message: 'go away',
1512
1546
  error: 'Unauthorized',
1513
1547
  statusCode: 401
1514
1548
  })
1549
+ done()
1515
1550
  })
1516
1551
  })
1517
1552
 
1518
- t.test('preHandler option could accept an array of functions', t => {
1553
+ await t.test('preHandler option could accept an array of functions', (t, done) => {
1519
1554
  t.plan(2)
1520
1555
  const fastify = Fastify()
1521
1556
 
@@ -1539,14 +1574,15 @@ test('preHandler option for setNotFoundHandler', t => {
1539
1574
  url: '/not-found',
1540
1575
  payload: { hello: 'world' }
1541
1576
  }, (err, res) => {
1542
- t.error(err)
1577
+ t.assert.ifError(err)
1543
1578
  const payload = JSON.parse(res.payload)
1544
- t.same(payload, { preHandler: 'ab', hello: 'world' })
1579
+ t.assert.deepStrictEqual(payload, { preHandler: 'ab', hello: 'world' })
1580
+ done()
1545
1581
  })
1546
1582
  })
1547
1583
 
1548
- t.test('preHandler option does not interfere with preHandler', t => {
1549
- t.plan(4)
1584
+ await t.test('preHandler option does not interfere with preHandler', async t => {
1585
+ t.plan(2)
1550
1586
  const fastify = Fastify()
1551
1587
 
1552
1588
  fastify.addHook('preHandler', (req, reply, done) => {
@@ -1571,28 +1607,30 @@ test('preHandler option for setNotFoundHandler', t => {
1571
1607
  n()
1572
1608
  }, { prefix: '/no' })
1573
1609
 
1574
- fastify.inject({
1575
- method: 'post',
1576
- url: '/not-found',
1577
- payload: { hello: 'world' }
1578
- }, (err, res) => {
1579
- t.error(err)
1610
+ {
1611
+ const res = await fastify.inject({
1612
+ method: 'post',
1613
+ url: '/not-found',
1614
+ payload: { hello: 'world' }
1615
+ })
1616
+
1580
1617
  const payload = JSON.parse(res.payload)
1581
- t.same(payload, { check: 'ab', hello: 'world' })
1582
- })
1618
+ t.assert.deepStrictEqual(payload, { check: 'ab', hello: 'world' })
1619
+ }
1620
+
1621
+ {
1622
+ const res = await fastify.inject({
1623
+ method: 'post',
1624
+ url: '/no/not-found',
1625
+ payload: { hello: 'world' }
1626
+ })
1583
1627
 
1584
- fastify.inject({
1585
- method: 'post',
1586
- url: '/no/not-found',
1587
- payload: { hello: 'world' }
1588
- }, (err, res) => {
1589
- t.error(err)
1590
1628
  const payload = JSON.parse(res.payload)
1591
- t.same(payload, { check: 'a', hello: 'world' })
1592
- })
1629
+ t.assert.deepStrictEqual(payload, { check: 'a', hello: 'world' })
1630
+ }
1593
1631
  })
1594
1632
 
1595
- t.test('preHandler option should keep the context', t => {
1633
+ await t.test('preHandler option should keep the context', (t, done) => {
1596
1634
  t.plan(3)
1597
1635
  const fastify = Fastify()
1598
1636
 
@@ -1600,7 +1638,7 @@ test('preHandler option for setNotFoundHandler', t => {
1600
1638
 
1601
1639
  fastify.setNotFoundHandler({
1602
1640
  preHandler: function (req, reply, done) {
1603
- t.equal(this.foo, 42)
1641
+ t.assert.strictEqual(this.foo, 42)
1604
1642
  this.foo += 1
1605
1643
  req.body.foo = this.foo
1606
1644
  done()
@@ -1614,14 +1652,15 @@ test('preHandler option for setNotFoundHandler', t => {
1614
1652
  url: '/not-found',
1615
1653
  payload: { hello: 'world' }
1616
1654
  }, (err, res) => {
1617
- t.error(err)
1655
+ t.assert.ifError(err)
1618
1656
  const payload = JSON.parse(res.payload)
1619
- t.same(payload, { foo: 43, hello: 'world' })
1657
+ t.assert.deepStrictEqual(payload, { foo: 43, hello: 'world' })
1658
+ done()
1620
1659
  })
1621
1660
  })
1622
1661
  })
1623
1662
 
1624
- test('reply.notFound invoked the notFound handler', t => {
1663
+ test('reply.notFound invoked the notFound handler', (t, done) => {
1625
1664
  t.plan(3)
1626
1665
 
1627
1666
  const fastify = Fastify()
@@ -1638,30 +1677,31 @@ test('reply.notFound invoked the notFound handler', t => {
1638
1677
  url: '/',
1639
1678
  method: 'GET'
1640
1679
  }, (err, res) => {
1641
- t.error(err)
1642
- t.equal(res.statusCode, 404)
1643
- t.same(JSON.parse(res.payload), {
1680
+ t.assert.ifError(err)
1681
+ t.assert.strictEqual(res.statusCode, 404)
1682
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
1644
1683
  error: 'Not Found',
1645
1684
  message: 'kaboom',
1646
1685
  statusCode: 404
1647
1686
  })
1687
+ done()
1648
1688
  })
1649
1689
  })
1650
1690
 
1651
- test('The custom error handler should be invoked after the custom not found handler', t => {
1691
+ test('The custom error handler should be invoked after the custom not found handler', (t, done) => {
1652
1692
  t.plan(6)
1653
1693
 
1654
1694
  const fastify = Fastify()
1655
1695
  const order = [1, 2]
1656
1696
 
1657
1697
  fastify.setErrorHandler((err, req, reply) => {
1658
- t.equal(order.shift(), 2)
1659
- t.type(err, Error)
1698
+ t.assert.strictEqual(order.shift(), 2)
1699
+ t.assert.ok(err instanceof Error)
1660
1700
  reply.send(err)
1661
1701
  })
1662
1702
 
1663
1703
  fastify.setNotFoundHandler((req, reply) => {
1664
- t.equal(order.shift(), 1)
1704
+ t.assert.strictEqual(order.shift(), 1)
1665
1705
  reply.code(404).send(new Error('kaboom'))
1666
1706
  })
1667
1707
 
@@ -1673,23 +1713,24 @@ test('The custom error handler should be invoked after the custom not found hand
1673
1713
  url: '/',
1674
1714
  method: 'GET'
1675
1715
  }, (err, res) => {
1676
- t.error(err)
1677
- t.equal(res.statusCode, 404)
1678
- t.same(JSON.parse(res.payload), {
1716
+ t.assert.ifError(err)
1717
+ t.assert.strictEqual(res.statusCode, 404)
1718
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
1679
1719
  error: 'Not Found',
1680
1720
  message: 'kaboom',
1681
1721
  statusCode: 404
1682
1722
  })
1723
+ done()
1683
1724
  })
1684
1725
  })
1685
1726
 
1686
- test('If the custom not found handler does not use an Error, the custom error handler should not be called', t => {
1727
+ test('If the custom not found handler does not use an Error, the custom error handler should not be called', (t, done) => {
1687
1728
  t.plan(3)
1688
1729
 
1689
1730
  const fastify = Fastify()
1690
1731
 
1691
1732
  fastify.setErrorHandler((_err, req, reply) => {
1692
- t.fail('Should not be called')
1733
+ t.assert.fail('Should not be called')
1693
1734
  })
1694
1735
 
1695
1736
  fastify.setNotFoundHandler((req, reply) => {
@@ -1704,13 +1745,14 @@ test('If the custom not found handler does not use an Error, the custom error ha
1704
1745
  url: '/',
1705
1746
  method: 'GET'
1706
1747
  }, (err, res) => {
1707
- t.error(err)
1708
- t.equal(res.statusCode, 404)
1709
- t.equal(res.payload, 'kaboom')
1748
+ t.assert.ifError(err)
1749
+ t.assert.strictEqual(res.statusCode, 404)
1750
+ t.assert.strictEqual(res.payload, 'kaboom')
1751
+ done()
1710
1752
  })
1711
1753
  })
1712
1754
 
1713
- test('preValidation option', t => {
1755
+ test('preValidation option', (t, done) => {
1714
1756
  t.plan(3)
1715
1757
  const fastify = Fastify()
1716
1758
 
@@ -1718,7 +1760,7 @@ test('preValidation option', t => {
1718
1760
 
1719
1761
  fastify.setNotFoundHandler({
1720
1762
  preValidation: function (req, reply, done) {
1721
- t.ok(this.foo)
1763
+ t.assert.ok(this.foo)
1722
1764
  done()
1723
1765
  }
1724
1766
  }, function (req, reply) {
@@ -1730,24 +1772,25 @@ test('preValidation option', t => {
1730
1772
  url: '/not-found',
1731
1773
  payload: { hello: 'world' }
1732
1774
  }, (err, res) => {
1733
- t.error(err)
1775
+ t.assert.ifError(err)
1734
1776
  const payload = JSON.parse(res.payload)
1735
- t.same(payload, { hello: 'world' })
1777
+ t.assert.deepStrictEqual(payload, { hello: 'world' })
1778
+ done()
1736
1779
  })
1737
1780
  })
1738
1781
 
1739
- t.test('preValidation option could accept an array of functions', t => {
1782
+ test('preValidation option could accept an array of functions', (t, done) => {
1740
1783
  t.plan(4)
1741
1784
  const fastify = Fastify()
1742
1785
 
1743
1786
  fastify.setNotFoundHandler({
1744
1787
  preValidation: [
1745
1788
  (req, reply, done) => {
1746
- t.ok('called')
1789
+ t.assert.ok('called')
1747
1790
  done()
1748
1791
  },
1749
1792
  (req, reply, done) => {
1750
- t.ok('called')
1793
+ t.assert.ok('called')
1751
1794
  done()
1752
1795
  }
1753
1796
  ]
@@ -1760,13 +1803,14 @@ t.test('preValidation option could accept an array of functions', t => {
1760
1803
  url: '/not-found',
1761
1804
  payload: { hello: 'world' }
1762
1805
  }, (err, res) => {
1763
- t.error(err)
1806
+ t.assert.ifError(err)
1764
1807
  const payload = JSON.parse(res.payload)
1765
- t.same(payload, { hello: 'world' })
1808
+ t.assert.deepStrictEqual(payload, { hello: 'world' })
1809
+ done()
1766
1810
  })
1767
1811
  })
1768
1812
 
1769
- test('Should fail to invoke callNotFound inside a 404 handler', t => {
1813
+ test('Should fail to invoke callNotFound inside a 404 handler', (t, done) => {
1770
1814
  t.plan(5)
1771
1815
 
1772
1816
  let fastify = null
@@ -1779,7 +1823,7 @@ test('Should fail to invoke callNotFound inside a 404 handler', t => {
1779
1823
  }
1780
1824
  })
1781
1825
  } catch (e) {
1782
- t.fail()
1826
+ t.assert.fail()
1783
1827
  }
1784
1828
 
1785
1829
  fastify.setNotFoundHandler((req, reply) => {
@@ -1791,95 +1835,100 @@ test('Should fail to invoke callNotFound inside a 404 handler', t => {
1791
1835
  })
1792
1836
 
1793
1837
  logStream.once('data', line => {
1794
- t.equal(line.msg, 'Trying to send a NotFound error inside a 404 handler. Sending basic 404 response.')
1795
- t.equal(line.level, 40)
1838
+ t.assert.strictEqual(line.msg, 'Trying to send a NotFound error inside a 404 handler. Sending basic 404 response.')
1839
+ t.assert.strictEqual(line.level, 40)
1796
1840
  })
1797
1841
 
1798
1842
  fastify.inject({
1799
1843
  url: '/',
1800
1844
  method: 'GET'
1801
1845
  }, (err, res) => {
1802
- t.error(err)
1803
- t.equal(res.statusCode, 404)
1804
- t.equal(res.payload, '404 Not Found')
1846
+ t.assert.ifError(err)
1847
+ t.assert.strictEqual(res.statusCode, 404)
1848
+ t.assert.strictEqual(res.payload, '404 Not Found')
1849
+ done()
1805
1850
  })
1806
1851
  })
1807
1852
 
1808
- test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
1809
- t.test('Dynamic route', t => {
1853
+ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', async t => {
1854
+ await t.test('Dynamic route', (t, done) => {
1810
1855
  t.plan(3)
1811
1856
  const fastify = Fastify()
1812
- fastify.get('/hello/:id', () => t.fail('we should not be here'))
1857
+ fastify.get('/hello/:id', () => t.assert.fail('we should not be here'))
1813
1858
  fastify.inject({
1814
1859
  url: '/hello/%world',
1815
1860
  method: 'GET'
1816
1861
  }, (err, response) => {
1817
- t.error(err)
1818
- t.equal(response.statusCode, 400)
1819
- t.same(JSON.parse(response.payload), {
1862
+ t.assert.ifError(err)
1863
+ t.assert.strictEqual(response.statusCode, 400)
1864
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
1820
1865
  error: 'Bad Request',
1821
1866
  message: "'/hello/%world' is not a valid url component",
1822
1867
  statusCode: 400,
1823
1868
  code: 'FST_ERR_BAD_URL'
1824
1869
  })
1870
+ done()
1825
1871
  })
1826
1872
  })
1827
1873
 
1828
- t.test('Wildcard', t => {
1874
+ await t.test('Wildcard', (t, done) => {
1829
1875
  t.plan(3)
1830
1876
  const fastify = Fastify()
1831
- fastify.get('*', () => t.fail('we should not be here'))
1877
+ fastify.get('*', () => t.assert.fail('we should not be here'))
1832
1878
  fastify.inject({
1833
1879
  url: '/hello/%world',
1834
1880
  method: 'GET'
1835
1881
  }, (err, response) => {
1836
- t.error(err)
1837
- t.equal(response.statusCode, 400)
1838
- t.same(JSON.parse(response.payload), {
1882
+ t.assert.ifError(err)
1883
+ t.assert.strictEqual(response.statusCode, 400)
1884
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
1839
1885
  error: 'Bad Request',
1840
1886
  message: "'/hello/%world' is not a valid url component",
1841
1887
  statusCode: 400,
1842
1888
  code: 'FST_ERR_BAD_URL'
1843
1889
  })
1890
+ done()
1844
1891
  })
1845
1892
  })
1846
1893
 
1847
- t.test('No route registered', t => {
1894
+ await t.test('No route registered', (t, done) => {
1848
1895
  t.plan(3)
1849
1896
  const fastify = Fastify()
1850
1897
  fastify.inject({
1851
1898
  url: '/%c0',
1852
1899
  method: 'GET'
1853
1900
  }, (err, response) => {
1854
- t.error(err)
1855
- t.equal(response.statusCode, 404)
1856
- t.same(JSON.parse(response.payload), {
1901
+ t.assert.ifError(err)
1902
+ t.assert.strictEqual(response.statusCode, 404)
1903
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
1857
1904
  error: 'Not Found',
1858
1905
  message: 'Route GET:/%c0 not found',
1859
1906
  statusCode: 404
1860
1907
  })
1908
+ done()
1861
1909
  })
1862
1910
  })
1863
1911
 
1864
- t.test('Only / is registered', t => {
1912
+ await t.test('Only / is registered', (t, done) => {
1865
1913
  t.plan(3)
1866
1914
  const fastify = Fastify()
1867
- fastify.get('/', () => t.fail('we should not be here'))
1915
+ fastify.get('/', () => t.assert.fail('we should not be here'))
1868
1916
  fastify.inject({
1869
1917
  url: '/non-existing',
1870
1918
  method: 'GET'
1871
1919
  }, (err, response) => {
1872
- t.error(err)
1873
- t.equal(response.statusCode, 404)
1874
- t.same(JSON.parse(response.payload), {
1920
+ t.assert.ifError(err)
1921
+ t.assert.strictEqual(response.statusCode, 404)
1922
+ t.assert.deepStrictEqual(JSON.parse(response.payload), {
1875
1923
  error: 'Not Found',
1876
1924
  message: 'Route GET:/non-existing not found',
1877
1925
  statusCode: 404
1878
1926
  })
1927
+ done()
1879
1928
  })
1880
1929
  })
1881
1930
 
1882
- t.test('customized 404', t => {
1931
+ await t.test('customized 404', (t, done) => {
1883
1932
  t.plan(3)
1884
1933
  const fastify = Fastify({ logger: true })
1885
1934
  fastify.setNotFoundHandler(function (req, reply) {
@@ -1889,18 +1938,17 @@ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
1889
1938
  url: '/%c0',
1890
1939
  method: 'GET'
1891
1940
  }, (err, response) => {
1892
- t.error(err)
1893
- t.equal(response.statusCode, 404)
1894
- t.same(response.payload, 'this was not found')
1941
+ t.assert.ifError(err)
1942
+ t.assert.strictEqual(response.statusCode, 404)
1943
+ t.assert.deepStrictEqual(response.payload, 'this was not found')
1944
+ done()
1895
1945
  })
1896
1946
  })
1897
-
1898
- t.end()
1899
1947
  })
1900
1948
 
1901
- test('setNotFoundHandler should be chaining fastify instance', t => {
1902
- t.test('Register route after setNotFoundHandler', t => {
1903
- t.plan(6)
1949
+ test('setNotFoundHandler should be chaining fastify instance', async t => {
1950
+ await t.test('Register route after setNotFoundHandler', async t => {
1951
+ t.plan(4)
1904
1952
  const fastify = Fastify()
1905
1953
  fastify.setNotFoundHandler(function (_req, reply) {
1906
1954
  reply.code(404).send('this was not found')
@@ -1908,52 +1956,50 @@ test('setNotFoundHandler should be chaining fastify instance', t => {
1908
1956
  reply.send('valid route')
1909
1957
  })
1910
1958
 
1911
- fastify.inject({
1912
- url: '/invalid-route',
1913
- method: 'GET'
1914
- }, (err, response) => {
1915
- t.error(err)
1916
- t.equal(response.statusCode, 404)
1917
- t.equal(response.payload, 'this was not found')
1918
- })
1959
+ {
1960
+ const response = await fastify.inject({
1961
+ url: '/invalid-route',
1962
+ method: 'GET'
1963
+ })
1964
+ t.assert.strictEqual(response.statusCode, 404)
1965
+ t.assert.strictEqual(response.payload, 'this was not found')
1966
+ }
1919
1967
 
1920
- fastify.inject({
1921
- url: '/valid-route',
1922
- method: 'GET'
1923
- }, (err, response) => {
1924
- t.error(err)
1925
- t.equal(response.statusCode, 200)
1926
- t.equal(response.payload, 'valid route')
1927
- })
1928
- })
1968
+ {
1969
+ const response = await fastify.inject({
1970
+ url: '/valid-route',
1971
+ method: 'GET'
1972
+ })
1929
1973
 
1930
- t.end()
1974
+ t.assert.strictEqual(response.statusCode, 200)
1975
+ t.assert.strictEqual(response.payload, 'valid route')
1976
+ }
1977
+ })
1931
1978
  })
1932
1979
 
1933
- test('Send 404 when frameworkError calls reply.callNotFound', t => {
1934
- t.test('Dynamic route', t => {
1980
+ test('Send 404 when frameworkError calls reply.callNotFound', async t => {
1981
+ await t.test('Dynamic route', (t, done) => {
1935
1982
  t.plan(4)
1936
1983
  const fastify = Fastify({
1937
1984
  frameworkErrors: (error, req, reply) => {
1938
- t.equal(error.message, "'/hello/%world' is not a valid url component")
1985
+ t.assert.strictEqual(error.message, "'/hello/%world' is not a valid url component")
1939
1986
  return reply.callNotFound()
1940
1987
  }
1941
1988
  })
1942
- fastify.get('/hello/:id', () => t.fail('we should not be here'))
1989
+ fastify.get('/hello/:id', () => t.assert.fail('we should not be here'))
1943
1990
  fastify.inject({
1944
1991
  url: '/hello/%world',
1945
1992
  method: 'GET'
1946
1993
  }, (err, response) => {
1947
- t.error(err)
1948
- t.equal(response.statusCode, 404)
1949
- t.equal(response.payload, '404 Not Found')
1994
+ t.assert.ifError(err)
1995
+ t.assert.strictEqual(response.statusCode, 404)
1996
+ t.assert.strictEqual(response.payload, '404 Not Found')
1997
+ done()
1950
1998
  })
1951
1999
  })
1952
-
1953
- t.end()
1954
2000
  })
1955
2001
 
1956
- test('hooks are applied to not found handlers /1', async ({ equal }) => {
2002
+ test('hooks are applied to not found handlers /1', async t => {
1957
2003
  const fastify = Fastify()
1958
2004
 
1959
2005
  // adding await here is fundamental for this test
@@ -1969,10 +2015,10 @@ test('hooks are applied to not found handlers /1', async ({ equal }) => {
1969
2015
  })
1970
2016
 
1971
2017
  const { statusCode } = await fastify.inject('/')
1972
- equal(statusCode, 401)
2018
+ t.assert.strictEqual(statusCode, 401)
1973
2019
  })
1974
2020
 
1975
- test('hooks are applied to not found handlers /2', async ({ equal }) => {
2021
+ test('hooks are applied to not found handlers /2', async t => {
1976
2022
  const fastify = Fastify()
1977
2023
 
1978
2024
  async function plugin (fastify) {
@@ -1990,15 +2036,15 @@ test('hooks are applied to not found handlers /2', async ({ equal }) => {
1990
2036
  })
1991
2037
 
1992
2038
  const { statusCode } = await fastify.inject('/')
1993
- equal(statusCode, 401)
2039
+ t.assert.strictEqual(statusCode, 401)
1994
2040
  })
1995
2041
 
1996
- test('hooks are applied to not found handlers /3', async ({ equal, fail }) => {
2042
+ test('hooks are applied to not found handlers /3', async t => {
1997
2043
  const fastify = Fastify()
1998
2044
 
1999
2045
  async function plugin (fastify) {
2000
2046
  fastify.setNotFoundHandler({ errorHandler }, async () => {
2001
- fail('this should never be called')
2047
+ t.assert.fail('this should never be called')
2002
2048
  })
2003
2049
 
2004
2050
  function errorHandler (_, request, reply) {
@@ -2015,5 +2061,5 @@ test('hooks are applied to not found handlers /3', async ({ equal, fail }) => {
2015
2061
  })
2016
2062
 
2017
2063
  const { statusCode } = await fastify.inject('/')
2018
- equal(statusCode, 401)
2064
+ t.assert.strictEqual(statusCode, 401)
2019
2065
  })