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 sget = require('simple-get').concat
6
5
  const http = require('node:http')
7
6
  const NotFound = require('http-errors').NotFound
@@ -40,22 +39,22 @@ test('Once called, Reply should return an object with methods', t => {
40
39
  const context = { config: { onSend: [] }, schema: {}, _parserOptions: {}, server: { hasConstraintStrategy: () => false, initialConfig: {} } }
41
40
  const request = new Request(null, null, null, null, null, context)
42
41
  const reply = new Reply(response, request)
43
- t.equal(typeof reply, 'object')
44
- t.equal(typeof reply[kReplyIsError], 'boolean')
45
- t.equal(typeof reply[kReplyErrorHandlerCalled], 'boolean')
46
- t.equal(typeof reply.send, 'function')
47
- t.equal(typeof reply.code, 'function')
48
- t.equal(typeof reply.status, 'function')
49
- t.equal(typeof reply.header, 'function')
50
- t.equal(typeof reply.serialize, 'function')
51
- t.equal(typeof reply[kReplyHeaders], 'object')
52
- t.same(reply.raw, response)
53
- t.equal(reply[kRouteContext], context)
54
- t.equal(reply.routeOptions.config, context.config)
55
- t.equal(reply.routeOptions.schema, context.schema)
56
- t.equal(reply.request, request)
42
+ t.assert.strictEqual(typeof reply, 'object')
43
+ t.assert.strictEqual(typeof reply[kReplyIsError], 'boolean')
44
+ t.assert.strictEqual(typeof reply[kReplyErrorHandlerCalled], 'boolean')
45
+ t.assert.strictEqual(typeof reply.send, 'function')
46
+ t.assert.strictEqual(typeof reply.code, 'function')
47
+ t.assert.strictEqual(typeof reply.status, 'function')
48
+ t.assert.strictEqual(typeof reply.header, 'function')
49
+ t.assert.strictEqual(typeof reply.serialize, 'function')
50
+ t.assert.strictEqual(typeof reply[kReplyHeaders], 'object')
51
+ t.assert.deepStrictEqual(reply.raw, response)
52
+ t.assert.strictEqual(reply[kRouteContext], context)
53
+ t.assert.strictEqual(reply.routeOptions.config, context.config)
54
+ t.assert.strictEqual(reply.routeOptions.schema, context.schema)
55
+ t.assert.strictEqual(reply.request, request)
57
56
  // Aim to not bad property keys (including Symbols)
58
- t.notOk('undefined' in reply)
57
+ t.assert.ok(!('undefined' in reply))
59
58
  })
60
59
 
61
60
  test('reply.send will logStream error and destroy the stream', t => {
@@ -87,7 +86,7 @@ test('reply.send will logStream error and destroy the stream', t => {
87
86
  reply.send(payload)
88
87
  payload.destroy(new Error('stream error'))
89
88
 
90
- t.equal(destroyCalled, true, 'Error not logged and not streamed')
89
+ t.assert.strictEqual(destroyCalled, true, 'Error not logged and not streamed')
91
90
  })
92
91
 
93
92
  test('reply.send throw with circular JSON', t => {
@@ -101,7 +100,7 @@ test('reply.send throw with circular JSON', t => {
101
100
  end: () => { }
102
101
  }
103
102
  const reply = new Reply(response, { [kRouteContext]: { onSend: [] } })
104
- t.throws(() => {
103
+ t.assert.throws(() => {
105
104
  const obj = {}
106
105
  obj.obj = obj
107
106
  reply.send(JSON.stringify(obj))
@@ -119,23 +118,23 @@ test('reply.send returns itself', t => {
119
118
  end: () => { }
120
119
  }
121
120
  const reply = new Reply(response, { [kRouteContext]: { onSend: [] } })
122
- t.equal(reply.send('hello'), reply)
121
+ t.assert.strictEqual(reply.send('hello'), reply)
123
122
  })
124
123
 
125
124
  test('reply.serializer should set a custom serializer', t => {
126
125
  t.plan(2)
127
126
  const reply = new Reply(null, null, null)
128
- t.equal(reply[kReplySerializer], null)
127
+ t.assert.strictEqual(reply[kReplySerializer], null)
129
128
  reply.serializer('serializer')
130
- t.equal(reply[kReplySerializer], 'serializer')
129
+ t.assert.strictEqual(reply[kReplySerializer], 'serializer')
131
130
  })
132
131
 
133
- test('reply.serializer should support running preSerialization hooks', t => {
132
+ test('reply.serializer should support running preSerialization hooks', (t, done) => {
134
133
  t.plan(3)
135
134
  const fastify = Fastify()
136
- t.teardown(fastify.close.bind(fastify))
135
+ t.after(() => fastify.close())
137
136
 
138
- fastify.addHook('preSerialization', async (request, reply, payload) => { t.ok('called', 'preSerialization') })
137
+ fastify.addHook('preSerialization', async (request, reply, payload) => { t.assert.ok('called', 'preSerialization') })
139
138
  fastify.route({
140
139
  method: 'GET',
141
140
  url: '/',
@@ -151,8 +150,9 @@ test('reply.serializer should support running preSerialization hooks', t => {
151
150
  method: 'GET',
152
151
  url: '/'
153
152
  }, (err, res) => {
154
- t.error(err)
155
- t.equal(res.payload, '{"foo":"bar"}')
153
+ t.assert.ifError(err)
154
+ t.assert.strictEqual(res.payload, '{"foo":"bar"}')
155
+ done()
156
156
  })
157
157
  })
158
158
 
@@ -161,7 +161,7 @@ test('reply.serialize should serialize payload', t => {
161
161
  const response = { statusCode: 200 }
162
162
  const context = {}
163
163
  const reply = new Reply(response, { [kRouteContext]: context })
164
- t.equal(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
164
+ t.assert.strictEqual(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
165
165
  })
166
166
 
167
167
  test('reply.serialize should serialize payload with a custom serializer', t => {
@@ -171,8 +171,8 @@ test('reply.serialize should serialize payload with a custom serializer', t => {
171
171
  const context = {}
172
172
  const reply = new Reply(response, { [kRouteContext]: context })
173
173
  reply.serializer((x) => (customSerializerCalled = true) && JSON.stringify(x))
174
- t.equal(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
175
- t.equal(customSerializerCalled, true, 'custom serializer not called')
174
+ t.assert.strictEqual(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
175
+ t.assert.strictEqual(customSerializerCalled, true, 'custom serializer not called')
176
176
  })
177
177
 
178
178
  test('reply.serialize should serialize payload with a context default serializer', t => {
@@ -181,14 +181,14 @@ test('reply.serialize should serialize payload with a context default serializer
181
181
  const response = { statusCode: 200 }
182
182
  const context = { [kReplySerializerDefault]: (x) => (customSerializerCalled = true) && JSON.stringify(x) }
183
183
  const reply = new Reply(response, { [kRouteContext]: context })
184
- t.equal(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
185
- t.equal(customSerializerCalled, true, 'custom serializer not called')
184
+ t.assert.strictEqual(reply.serialize({ foo: 'bar' }), '{"foo":"bar"}')
185
+ t.assert.strictEqual(customSerializerCalled, true, 'custom serializer not called')
186
186
  })
187
187
 
188
- test('reply.serialize should serialize payload with Fastify instance', t => {
188
+ test('reply.serialize should serialize payload with Fastify instance', (t, done) => {
189
189
  t.plan(2)
190
190
  const fastify = Fastify()
191
- t.teardown(fastify.close.bind(fastify))
191
+ t.after(() => fastify.close())
192
192
  fastify.route({
193
193
  method: 'GET',
194
194
  url: '/',
@@ -213,15 +213,15 @@ test('reply.serialize should serialize payload with Fastify instance', t => {
213
213
  method: 'GET',
214
214
  url: '/'
215
215
  }, (err, res) => {
216
- t.error(err)
217
- t.equal(res.payload, '{"foo":"bar"}')
216
+ t.assert.ifError(err)
217
+ t.assert.strictEqual(res.payload, '{"foo":"bar"}')
218
+ done()
218
219
  })
219
220
  })
220
221
 
221
- test('within an instance', t => {
222
+ test('within an instance', async t => {
222
223
  const fastify = Fastify()
223
- t.teardown(fastify.close.bind(fastify))
224
- const test = t.test
224
+ t.after(() => fastify.close())
225
225
 
226
226
  fastify.get('/', function (req, reply) {
227
227
  reply.code(200)
@@ -279,166 +279,175 @@ test('within an instance', t => {
279
279
  done()
280
280
  })
281
281
 
282
- fastify.listen({ port: 0 }, err => {
283
- t.error(err)
284
- t.teardown(fastify.close.bind(fastify))
282
+ await fastify.listen({ port: 0 })
285
283
 
286
- test('custom serializer should be used', t => {
287
- t.plan(3)
288
- sget({
289
- method: 'GET',
290
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/custom-serializer'
291
- }, (err, response, body) => {
292
- t.error(err)
293
- t.equal(response.headers['content-type'], 'text/plain')
294
- t.same(body.toString(), 'hello=world!')
295
- })
284
+ await t.test('custom serializer should be used', (t, done) => {
285
+ t.plan(3)
286
+ sget({
287
+ method: 'GET',
288
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/custom-serializer'
289
+ }, (err, response, body) => {
290
+ t.assert.ifError(err)
291
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
292
+ t.assert.deepStrictEqual(body.toString(), 'hello=world!')
293
+ done()
296
294
  })
295
+ })
297
296
 
298
- test('status code and content-type should be correct', t => {
299
- t.plan(4)
300
- sget({
301
- method: 'GET',
302
- url: 'http://127.0.0.1:' + fastify.server.address().port
303
- }, (err, response, body) => {
304
- t.error(err)
305
- t.equal(response.statusCode, 200)
306
- t.equal(response.headers['content-type'], 'text/plain')
307
- t.same(body.toString(), 'hello world!')
308
- })
297
+ await t.test('status code and content-type should be correct', (t, done) => {
298
+ t.plan(4)
299
+ sget({
300
+ method: 'GET',
301
+ url: 'http://127.0.0.1:' + fastify.server.address().port
302
+ }, (err, response, body) => {
303
+ t.assert.ifError(err)
304
+ t.assert.strictEqual(response.statusCode, 200)
305
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
306
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
307
+ done()
309
308
  })
309
+ })
310
310
 
311
- test('auto status code should be 200', t => {
312
- t.plan(3)
313
- sget({
314
- method: 'GET',
315
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-status-code'
316
- }, (err, response, body) => {
317
- t.error(err)
318
- t.equal(response.statusCode, 200)
319
- t.same(body.toString(), 'hello world!')
320
- })
311
+ await t.test('auto status code should be 200', (t, done) => {
312
+ t.plan(3)
313
+ sget({
314
+ method: 'GET',
315
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-status-code'
316
+ }, (err, response, body) => {
317
+ t.assert.ifError(err)
318
+ t.assert.strictEqual(response.statusCode, 200)
319
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
320
+ done()
321
321
  })
322
+ })
322
323
 
323
- test('auto type should be text/plain', t => {
324
- t.plan(3)
325
- sget({
326
- method: 'GET',
327
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-type'
328
- }, (err, response, body) => {
329
- t.error(err)
330
- t.equal(response.headers['content-type'], 'text/plain')
331
- t.same(body.toString(), 'hello world!')
332
- })
324
+ await t.test('auto type should be text/plain', (t, done) => {
325
+ t.plan(3)
326
+ sget({
327
+ method: 'GET',
328
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/auto-type'
329
+ }, (err, response, body) => {
330
+ t.assert.ifError(err)
331
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
332
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
333
+ done()
333
334
  })
335
+ })
334
336
 
335
- test('redirect to `/` - 1', t => {
336
- t.plan(1)
337
+ await t.test('redirect to `/` - 1', (t, done) => {
338
+ t.plan(1)
337
339
 
338
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect', function (response) {
339
- t.equal(response.statusCode, 302)
340
- })
340
+ http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect', function (response) {
341
+ t.assert.strictEqual(response.statusCode, 302)
342
+ done()
341
343
  })
344
+ })
342
345
 
343
- test('redirect to `/` - 2', t => {
344
- t.plan(1)
346
+ await t.test('redirect to `/` - 2', (t, done) => {
347
+ t.plan(1)
345
348
 
346
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code', function (response) {
347
- t.equal(response.statusCode, 301)
348
- })
349
+ http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code', function (response) {
350
+ t.assert.strictEqual(response.statusCode, 301)
351
+ done()
349
352
  })
353
+ })
350
354
 
351
- test('redirect to `/` - 3', t => {
352
- t.plan(4)
353
- sget({
354
- method: 'GET',
355
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect'
356
- }, (err, response, body) => {
357
- t.error(err)
358
- t.equal(response.statusCode, 200)
359
- t.equal(response.headers['content-type'], 'text/plain')
360
- t.same(body.toString(), 'hello world!')
361
- })
355
+ await t.test('redirect to `/` - 3', (t, done) => {
356
+ t.plan(4)
357
+ sget({
358
+ method: 'GET',
359
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect'
360
+ }, (err, response, body) => {
361
+ t.assert.ifError(err)
362
+ t.assert.strictEqual(response.statusCode, 200)
363
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
364
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
365
+ done()
362
366
  })
367
+ })
363
368
 
364
- test('redirect to `/` - 4', t => {
365
- t.plan(4)
366
- sget({
367
- method: 'GET',
368
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code'
369
- }, (err, response, body) => {
370
- t.error(err)
371
- t.equal(response.statusCode, 200)
372
- t.equal(response.headers['content-type'], 'text/plain')
373
- t.same(body.toString(), 'hello world!')
374
- })
369
+ await t.test('redirect to `/` - 4', (t, done) => {
370
+ t.plan(4)
371
+ sget({
372
+ method: 'GET',
373
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code'
374
+ }, (err, response, body) => {
375
+ t.assert.ifError(err)
376
+ t.assert.strictEqual(response.statusCode, 200)
377
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
378
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
379
+ done()
375
380
  })
381
+ })
376
382
 
377
- test('redirect to `/` - 5', t => {
378
- t.plan(3)
379
- const url = 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-onsend'
380
- http.get(url, (response) => {
381
- t.equal(response.headers['x-onsend'], 'yes')
382
- t.equal(response.headers['content-length'], '0')
383
- t.equal(response.headers.location, '/')
384
- })
383
+ await t.test('redirect to `/` - 5', (t, done) => {
384
+ t.plan(3)
385
+ const url = 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-onsend'
386
+ http.get(url, (response) => {
387
+ t.assert.strictEqual(response.headers['x-onsend'], 'yes')
388
+ t.assert.strictEqual(response.headers['content-length'], '0')
389
+ t.assert.strictEqual(response.headers.location, '/')
390
+ done()
385
391
  })
392
+ })
386
393
 
387
- test('redirect to `/` - 6', t => {
388
- t.plan(4)
389
- sget({
390
- method: 'GET',
391
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call'
392
- }, (err, response, body) => {
393
- t.error(err)
394
- t.equal(response.statusCode, 200)
395
- t.equal(response.headers['content-type'], 'text/plain')
396
- t.same(body.toString(), 'hello world!')
397
- })
394
+ await t.test('redirect to `/` - 6', (t, done) => {
395
+ t.plan(4)
396
+ sget({
397
+ method: 'GET',
398
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call'
399
+ }, (err, response, body) => {
400
+ t.assert.ifError(err)
401
+ t.assert.strictEqual(response.statusCode, 200)
402
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
403
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
404
+ done()
398
405
  })
406
+ })
399
407
 
400
- test('redirect to `/` - 7', t => {
401
- t.plan(4)
402
- sget({
403
- method: 'GET',
404
- url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite'
405
- }, (err, response, body) => {
406
- t.error(err)
407
- t.equal(response.statusCode, 200)
408
- t.equal(response.headers['content-type'], 'text/plain')
409
- t.same(body.toString(), 'hello world!')
410
- })
408
+ await t.test('redirect to `/` - 7', (t, done) => {
409
+ t.plan(4)
410
+ sget({
411
+ method: 'GET',
412
+ url: 'http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite'
413
+ }, (err, response, body) => {
414
+ t.assert.ifError(err)
415
+ t.assert.strictEqual(response.statusCode, 200)
416
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
417
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
418
+ done()
411
419
  })
420
+ })
412
421
 
413
- test('redirect to `/` - 8', t => {
414
- t.plan(1)
422
+ await t.test('redirect to `/` - 8', (t, done) => {
423
+ t.plan(1)
415
424
 
416
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call', function (response) {
417
- t.equal(response.statusCode, 307)
418
- })
425
+ http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call', function (response) {
426
+ t.assert.strictEqual(response.statusCode, 307)
427
+ done()
419
428
  })
429
+ })
420
430
 
421
- test('redirect to `/` - 9', t => {
422
- t.plan(1)
431
+ await t.test('redirect to `/` - 9', (t, done) => {
432
+ t.plan(1)
423
433
 
424
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite', function (response) {
425
- t.equal(response.statusCode, 302)
426
- })
434
+ http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-code-before-call-overwrite', function (response) {
435
+ t.assert.strictEqual(response.statusCode, 302)
436
+ done()
427
437
  })
438
+ })
428
439
 
429
- test('redirect with async function to `/` - 10', t => {
430
- t.plan(1)
440
+ await t.test('redirect with async function to `/` - 10', (t, done) => {
441
+ t.plan(1)
431
442
 
432
- http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-async', function (response) {
433
- t.equal(response.statusCode, 302)
434
- })
443
+ http.get('http://127.0.0.1:' + fastify.server.address().port + '/redirect-async', function (response) {
444
+ t.assert.strictEqual(response.statusCode, 302)
445
+ done()
435
446
  })
436
-
437
- t.end()
438
447
  })
439
448
  })
440
449
 
441
- test('buffer without content type should send a application/octet-stream and raw buffer', t => {
450
+ test('buffer without content type should send a application/octet-stream and raw buffer', (t, done) => {
442
451
  t.plan(4)
443
452
 
444
453
  const fastify = Fastify()
@@ -448,20 +457,22 @@ test('buffer without content type should send a application/octet-stream and raw
448
457
  })
449
458
 
450
459
  fastify.listen({ port: 0 }, err => {
451
- t.error(err)
452
- t.teardown(fastify.close.bind(fastify))
460
+ t.assert.ifError(err)
461
+ t.after(() => fastify.close())
453
462
 
454
463
  sget({
455
464
  method: 'GET',
456
465
  url: 'http://127.0.0.1:' + fastify.server.address().port
457
466
  }, (err, response, body) => {
458
- t.error(err)
459
- t.equal(response.headers['content-type'], 'application/octet-stream')
460
- t.same(body, Buffer.alloc(1024))
467
+ t.assert.ifError(err)
468
+ t.assert.strictEqual(response.headers['content-type'], 'application/octet-stream')
469
+ t.assert.deepStrictEqual(body, Buffer.alloc(1024))
470
+ done()
461
471
  })
462
472
  })
463
473
  })
464
- test('Uint8Array without content type should send a application/octet-stream and raw buffer', t => {
474
+
475
+ test('Uint8Array without content type should send a application/octet-stream and raw buffer', (t, done) => {
465
476
  t.plan(4)
466
477
 
467
478
  const fastify = Fastify()
@@ -471,20 +482,21 @@ test('Uint8Array without content type should send a application/octet-stream and
471
482
  })
472
483
 
473
484
  fastify.listen({ port: 0 }, err => {
474
- t.error(err)
475
- t.teardown(fastify.close.bind(fastify))
485
+ t.assert.ifError(err)
486
+ t.after(() => fastify.close())
476
487
 
477
488
  fastify.inject({
478
489
  method: 'GET',
479
490
  url: '/'
480
491
  }, (err, response) => {
481
- t.error(err)
482
- t.equal(response.headers['content-type'], 'application/octet-stream')
483
- t.same(new Uint8Array(response.rawPayload), new Uint8Array(1024).fill(0xff))
492
+ t.assert.ifError(err)
493
+ t.assert.strictEqual(response.headers['content-type'], 'application/octet-stream')
494
+ t.assert.deepStrictEqual(new Uint8Array(response.rawPayload), new Uint8Array(1024).fill(0xff))
495
+ done()
484
496
  })
485
497
  })
486
498
  })
487
- test('Uint16Array without content type should send a application/octet-stream and raw buffer', t => {
499
+ test('Uint16Array without content type should send a application/octet-stream and raw buffer', (t, done) => {
488
500
  t.plan(4)
489
501
 
490
502
  const fastify = Fastify()
@@ -494,20 +506,21 @@ test('Uint16Array without content type should send a application/octet-stream an
494
506
  })
495
507
 
496
508
  fastify.listen({ port: 0 }, err => {
497
- t.error(err)
498
- t.teardown(fastify.close.bind(fastify))
509
+ t.assert.ifError(err)
510
+ t.after(() => fastify.close())
499
511
 
500
512
  fastify.inject({
501
513
  method: 'GET',
502
514
  url: '/'
503
515
  }, (err, res) => {
504
- t.error(err)
505
- t.equal(res.headers['content-type'], 'application/octet-stream')
506
- t.same(new Uint16Array(res.rawPayload.buffer, res.rawPayload.byteOffset, res.rawPayload.byteLength / Uint16Array.BYTES_PER_ELEMENT), new Uint16Array(50).fill(0xffffffff))
516
+ t.assert.ifError(err)
517
+ t.assert.strictEqual(res.headers['content-type'], 'application/octet-stream')
518
+ t.assert.deepStrictEqual(new Uint16Array(res.rawPayload.buffer, res.rawPayload.byteOffset, res.rawPayload.byteLength / Uint16Array.BYTES_PER_ELEMENT), new Uint16Array(50).fill(0xffffffff))
519
+ done()
507
520
  })
508
521
  })
509
522
  })
510
- test('TypedArray with content type should not send application/octet-stream', t => {
523
+ test('TypedArray with content type should not send application/octet-stream', (t, done) => {
511
524
  t.plan(4)
512
525
 
513
526
  const fastify = Fastify()
@@ -518,20 +531,21 @@ test('TypedArray with content type should not send application/octet-stream', t
518
531
  })
519
532
 
520
533
  fastify.listen({ port: 0 }, err => {
521
- t.error(err)
522
- t.teardown(fastify.close.bind(fastify))
534
+ t.assert.ifError(err)
535
+ t.after(() => fastify.close())
523
536
 
524
537
  fastify.inject({
525
538
  method: 'GET',
526
539
  url: '/'
527
540
  }, (err, res) => {
528
- t.error(err)
529
- t.equal(res.headers['content-type'], 'text/plain')
530
- t.same(new Uint16Array(res.rawPayload.buffer, res.rawPayload.byteOffset, res.rawPayload.byteLength / Uint16Array.BYTES_PER_ELEMENT), new Uint16Array(1024).fill(0xffffffff))
541
+ t.assert.ifError(err)
542
+ t.assert.strictEqual(res.headers['content-type'], 'text/plain')
543
+ t.assert.deepStrictEqual(new Uint16Array(res.rawPayload.buffer, res.rawPayload.byteOffset, res.rawPayload.byteLength / Uint16Array.BYTES_PER_ELEMENT), new Uint16Array(1024).fill(0xffffffff))
544
+ done()
531
545
  })
532
546
  })
533
547
  })
534
- test('buffer with content type should not send application/octet-stream', t => {
548
+ test('buffer with content type should not send application/octet-stream', (t, done) => {
535
549
  t.plan(4)
536
550
 
537
551
  const fastify = Fastify()
@@ -542,21 +556,22 @@ test('buffer with content type should not send application/octet-stream', t => {
542
556
  })
543
557
 
544
558
  fastify.listen({ port: 0 }, err => {
545
- t.error(err)
546
- t.teardown(fastify.close.bind(fastify))
559
+ t.assert.ifError(err)
560
+ t.after(() => fastify.close())
547
561
 
548
562
  sget({
549
563
  method: 'GET',
550
564
  url: 'http://127.0.0.1:' + fastify.server.address().port
551
565
  }, (err, response, body) => {
552
- t.error(err)
553
- t.equal(response.headers['content-type'], 'text/plain')
554
- t.same(body, Buffer.alloc(1024))
566
+ t.assert.ifError(err)
567
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
568
+ t.assert.deepStrictEqual(body, Buffer.alloc(1024))
569
+ done()
555
570
  })
556
571
  })
557
572
  })
558
573
 
559
- test('stream with content type should not send application/octet-stream', t => {
574
+ test('stream with content type should not send application/octet-stream', (t, done) => {
560
575
  t.plan(4)
561
576
 
562
577
  const fastify = Fastify()
@@ -570,20 +585,21 @@ test('stream with content type should not send application/octet-stream', t => {
570
585
  })
571
586
 
572
587
  fastify.listen({ port: 0 }, err => {
573
- t.error(err)
574
- t.teardown(fastify.close.bind(fastify))
588
+ t.assert.ifError(err)
589
+ t.after(() => fastify.close())
575
590
  sget({
576
591
  method: 'GET',
577
592
  url: 'http://127.0.0.1:' + fastify.server.address().port
578
593
  }, (err, response, body) => {
579
- t.error(err)
580
- t.equal(response.headers['content-type'], 'text/plain')
581
- t.same(body, buf)
594
+ t.assert.ifError(err)
595
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain')
596
+ t.assert.deepStrictEqual(body, buf)
597
+ done()
582
598
  })
583
599
  })
584
600
  })
585
601
 
586
- test('stream without content type should not send application/octet-stream', t => {
602
+ test('stream without content type should not send application/octet-stream', (t, done) => {
587
603
  t.plan(4)
588
604
 
589
605
  const fastify = Fastify()
@@ -596,20 +612,21 @@ test('stream without content type should not send application/octet-stream', t =
596
612
  })
597
613
 
598
614
  fastify.listen({ port: 0 }, err => {
599
- t.error(err)
600
- t.teardown(fastify.close.bind(fastify))
615
+ t.assert.ifError(err)
616
+ t.after(() => fastify.close())
601
617
  sget({
602
618
  method: 'GET',
603
619
  url: 'http://127.0.0.1:' + fastify.server.address().port
604
620
  }, (err, response, body) => {
605
- t.error(err)
606
- t.equal(response.headers['content-type'], undefined)
607
- t.same(body, buf)
621
+ t.assert.ifError(err)
622
+ t.assert.strictEqual(response.headers['content-type'], undefined)
623
+ t.assert.deepStrictEqual(body, buf)
624
+ done()
608
625
  })
609
626
  })
610
627
  })
611
628
 
612
- test('stream using reply.raw.writeHead should return customize headers', t => {
629
+ test('stream using reply.raw.writeHead should return customize headers', (t, done) => {
613
630
  t.plan(6)
614
631
 
615
632
  const fastify = Fastify()
@@ -622,7 +639,7 @@ test('stream using reply.raw.writeHead should return customize headers', t => {
622
639
 
623
640
  fastify.get('/', function (req, reply) {
624
641
  reply.log.warn = function mockWarn (message) {
625
- t.equal(message, 'response will send, but you shouldn\'t use res.writeHead in stream mode')
642
+ t.assert.strictEqual(message, 'response will send, but you shouldn\'t use res.writeHead in stream mode')
626
643
  }
627
644
  reply.raw.writeHead(200, {
628
645
  location: '/'
@@ -631,21 +648,22 @@ test('stream using reply.raw.writeHead should return customize headers', t => {
631
648
  })
632
649
 
633
650
  fastify.listen({ port: 0 }, err => {
634
- t.error(err)
635
- t.teardown(fastify.close.bind(fastify))
651
+ t.assert.ifError(err)
652
+ t.after(() => fastify.close())
636
653
  sget({
637
654
  method: 'GET',
638
655
  url: 'http://127.0.0.1:' + fastify.server.address().port
639
656
  }, (err, response, body) => {
640
- t.error(err)
641
- t.equal(response.headers.location, '/')
642
- t.equal(response.headers['Content-Type'], undefined)
643
- t.same(body, buf)
657
+ t.assert.ifError(err)
658
+ t.assert.strictEqual(response.headers.location, '/')
659
+ t.assert.strictEqual(response.headers['Content-Type'], undefined)
660
+ t.assert.deepStrictEqual(body, buf)
661
+ done()
644
662
  })
645
663
  })
646
664
  })
647
665
 
648
- test('plain string without content type should send a text/plain', t => {
666
+ test('plain string without content type should send a text/plain', (t, done) => {
649
667
  t.plan(4)
650
668
 
651
669
  const fastify = Fastify()
@@ -655,21 +673,22 @@ test('plain string without content type should send a text/plain', t => {
655
673
  })
656
674
 
657
675
  fastify.listen({ port: 0 }, err => {
658
- t.error(err)
659
- t.teardown(fastify.close.bind(fastify))
676
+ t.assert.ifError(err)
677
+ t.after(() => fastify.close())
660
678
 
661
679
  sget({
662
680
  method: 'GET',
663
681
  url: 'http://127.0.0.1:' + fastify.server.address().port
664
682
  }, (err, response, body) => {
665
- t.error(err)
666
- t.equal(response.headers['content-type'], 'text/plain; charset=utf-8')
667
- t.same(body.toString(), 'hello world!')
683
+ t.assert.ifError(err)
684
+ t.assert.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
685
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
686
+ done()
668
687
  })
669
688
  })
670
689
  })
671
690
 
672
- test('plain string with content type should be sent unmodified', t => {
691
+ test('plain string with content type should be sent unmodified', (t, done) => {
673
692
  t.plan(4)
674
693
 
675
694
  const fastify = Fastify()
@@ -679,21 +698,22 @@ test('plain string with content type should be sent unmodified', t => {
679
698
  })
680
699
 
681
700
  fastify.listen({ port: 0 }, err => {
682
- t.error(err)
683
- t.teardown(fastify.close.bind(fastify))
701
+ t.assert.ifError(err)
702
+ t.after(() => fastify.close())
684
703
 
685
704
  sget({
686
705
  method: 'GET',
687
706
  url: 'http://127.0.0.1:' + fastify.server.address().port
688
707
  }, (err, response, body) => {
689
- t.error(err)
690
- t.equal(response.headers['content-type'], 'text/css')
691
- t.same(body.toString(), 'hello world!')
708
+ t.assert.ifError(err)
709
+ t.assert.strictEqual(response.headers['content-type'], 'text/css')
710
+ t.assert.deepStrictEqual(body.toString(), 'hello world!')
711
+ done()
692
712
  })
693
713
  })
694
714
  })
695
715
 
696
- test('plain string with content type and custom serializer should be serialized', t => {
716
+ test('plain string with content type and custom serializer should be serialized', (t, done) => {
697
717
  t.plan(4)
698
718
 
699
719
  const fastify = Fastify()
@@ -706,21 +726,22 @@ test('plain string with content type and custom serializer should be serialized'
706
726
  })
707
727
 
708
728
  fastify.listen({ port: 0 }, err => {
709
- t.error(err)
710
- t.teardown(fastify.close.bind(fastify))
729
+ t.assert.ifError(err)
730
+ t.after(() => fastify.close())
711
731
 
712
732
  sget({
713
733
  method: 'GET',
714
734
  url: 'http://127.0.0.1:' + fastify.server.address().port
715
735
  }, (err, response, body) => {
716
- t.error(err)
717
- t.equal(response.headers['content-type'], 'text/css')
718
- t.same(body.toString(), 'serialized')
736
+ t.assert.ifError(err)
737
+ t.assert.strictEqual(response.headers['content-type'], 'text/css')
738
+ t.assert.deepStrictEqual(body.toString(), 'serialized')
739
+ done()
719
740
  })
720
741
  })
721
742
  })
722
743
 
723
- test('plain string with content type application/json should NOT be serialized as json', t => {
744
+ test('plain string with content type application/json should NOT be serialized as json', (t, done) => {
724
745
  t.plan(4)
725
746
 
726
747
  const fastify = Fastify()
@@ -730,25 +751,28 @@ test('plain string with content type application/json should NOT be serialized a
730
751
  })
731
752
 
732
753
  fastify.listen({ port: 0 }, err => {
733
- t.error(err)
734
- t.teardown(fastify.close.bind(fastify))
754
+ t.assert.ifError(err)
755
+ t.after(() => fastify.close())
735
756
 
736
757
  sget({
737
758
  method: 'GET',
738
759
  url: 'http://127.0.0.1:' + fastify.server.address().port
739
760
  }, (err, response, body) => {
740
- t.error(err)
741
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
742
- t.same(body.toString(), '{"key": "hello world!"}')
761
+ t.assert.ifError(err)
762
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
763
+ t.assert.deepStrictEqual(body.toString(), '{"key": "hello world!"}')
764
+ done()
743
765
  })
744
766
  })
745
767
  })
746
768
 
747
- test('plain string with custom json content type should NOT be serialized as json', t => {
748
- t.plan(19)
769
+ test('plain string with custom json content type should NOT be serialized as json', async t => {
770
+ t.plan(12)
749
771
 
750
772
  const fastify = Fastify()
751
773
 
774
+ t.after(() => fastify.close())
775
+
752
776
  const customSamples = {
753
777
  collectionjson: {
754
778
  mimeType: 'application/vnd.collection+json',
@@ -782,24 +806,26 @@ test('plain string with custom json content type should NOT be serialized as jso
782
806
  })
783
807
  })
784
808
 
785
- fastify.listen({ port: 0 }, err => {
786
- t.error(err)
787
- t.teardown(fastify.close.bind(fastify))
809
+ await fastify.listen({ port: 0 })
788
810
 
789
- Object.keys(customSamples).forEach((path) => {
811
+ await Promise.all(Object.keys(customSamples).map(path => {
812
+ return new Promise((resolve, reject) => {
790
813
  sget({
791
814
  method: 'GET',
792
815
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/' + path
793
816
  }, (err, response, body) => {
794
- t.error(err)
795
- t.equal(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
796
- t.same(body.toString(), customSamples[path].sample)
817
+ if (err) {
818
+ reject(err)
819
+ }
820
+ t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
821
+ t.assert.deepStrictEqual(body.toString(), customSamples[path].sample)
822
+ resolve()
797
823
  })
798
824
  })
799
- })
825
+ }))
800
826
  })
801
827
 
802
- test('non-string with content type application/json SHOULD be serialized as json', t => {
828
+ test('non-string with content type application/json SHOULD be serialized as json', (t, done) => {
803
829
  t.plan(4)
804
830
 
805
831
  const fastify = Fastify()
@@ -809,21 +835,22 @@ test('non-string with content type application/json SHOULD be serialized as json
809
835
  })
810
836
 
811
837
  fastify.listen({ port: 0 }, err => {
812
- t.error(err)
813
- t.teardown(fastify.close.bind(fastify))
838
+ t.assert.ifError(err)
839
+ t.after(() => fastify.close())
814
840
 
815
841
  sget({
816
842
  method: 'GET',
817
843
  url: 'http://127.0.0.1:' + fastify.server.address().port
818
844
  }, (err, response, body) => {
819
- t.error(err)
820
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
821
- t.same(body.toString(), JSON.stringify({ key: 'hello world!' }))
845
+ t.assert.ifError(err)
846
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
847
+ t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
848
+ done()
822
849
  })
823
850
  })
824
851
  })
825
852
 
826
- test('non-string with custom json\'s content-type SHOULD be serialized as json', t => {
853
+ test('non-string with custom json\'s content-type SHOULD be serialized as json', (t, done) => {
827
854
  t.plan(4)
828
855
 
829
856
  const fastify = Fastify()
@@ -833,24 +860,26 @@ test('non-string with custom json\'s content-type SHOULD be serialized as json',
833
860
  })
834
861
 
835
862
  fastify.listen({ port: 0 }, err => {
836
- t.error(err)
837
- t.teardown(fastify.close.bind(fastify))
863
+ t.assert.ifError(err)
864
+ t.after(() => fastify.close())
838
865
 
839
866
  sget({
840
867
  method: 'GET',
841
868
  url: 'http://127.0.0.1:' + fastify.server.address().port
842
869
  }, (err, response, body) => {
843
- t.error(err)
844
- t.equal(response.headers['content-type'], 'application/json; version=2; charset=utf-8')
845
- t.same(body.toString(), JSON.stringify({ key: 'hello world!' }))
870
+ t.assert.ifError(err)
871
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; version=2; charset=utf-8')
872
+ t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
873
+ done()
846
874
  })
847
875
  })
848
876
  })
849
877
 
850
- test('non-string with custom json content type SHOULD be serialized as json', t => {
851
- t.plan(16)
878
+ test('non-string with custom json content type SHOULD be serialized as json', async t => {
879
+ t.plan(10)
852
880
 
853
881
  const fastify = Fastify()
882
+ t.after(() => fastify.close())
854
883
 
855
884
  const customSamples = {
856
885
  collectionjson: {
@@ -881,25 +910,27 @@ test('non-string with custom json content type SHOULD be serialized as json', t
881
910
  })
882
911
  })
883
912
 
884
- fastify.listen({ port: 0 }, err => {
885
- t.error(err)
886
- t.teardown(fastify.close.bind(fastify))
913
+ await fastify.listen({ port: 0 })
887
914
 
888
- Object.keys(customSamples).forEach((path) => {
915
+ await Promise.all(Object.keys(customSamples).map(path => {
916
+ return new Promise((resolve, reject) => {
889
917
  sget({
890
918
  method: 'GET',
891
919
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/' + path
892
920
  }, (err, response, body) => {
893
- t.error(err)
894
- t.equal(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
895
- t.same(body.toString(), JSON.stringify(customSamples[path].sample))
921
+ if (err) {
922
+ reject(err)
923
+ }
924
+ t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
925
+ t.assert.deepStrictEqual(body.toString(), JSON.stringify(customSamples[path].sample))
926
+ resolve()
896
927
  })
897
928
  })
898
- })
929
+ }))
899
930
  })
900
931
 
901
- test('error object with a content type that is not application/json should work', t => {
902
- t.plan(6)
932
+ test('error object with a content type that is not application/json should work', async t => {
933
+ t.plan(4)
903
934
 
904
935
  const fastify = Fastify()
905
936
 
@@ -913,32 +944,32 @@ test('error object with a content type that is not application/json should work'
913
944
  reply.send(new Error('some application error'))
914
945
  })
915
946
 
916
- fastify.inject({
917
- method: 'GET',
918
- url: '/text'
919
- }, (err, res) => {
920
- t.error(err)
921
- t.equal(res.statusCode, 500)
922
- t.equal(JSON.parse(res.payload).message, 'some application error')
923
- })
947
+ {
948
+ const res = await fastify.inject({
949
+ method: 'GET',
950
+ url: '/text'
951
+ })
952
+ t.assert.strictEqual(res.statusCode, 500)
953
+ t.assert.strictEqual(JSON.parse(res.payload).message, 'some application error')
954
+ }
924
955
 
925
- fastify.inject({
926
- method: 'GET',
927
- url: '/html'
928
- }, (err, res) => {
929
- t.error(err)
930
- t.equal(res.statusCode, 500)
931
- t.equal(JSON.parse(res.payload).message, 'some application error')
932
- })
956
+ {
957
+ const res = await fastify.inject({
958
+ method: 'GET',
959
+ url: '/html'
960
+ })
961
+ t.assert.strictEqual(res.statusCode, 500)
962
+ t.assert.strictEqual(JSON.parse(res.payload).message, 'some application error')
963
+ }
933
964
  })
934
965
 
935
- test('undefined payload should be sent as-is', t => {
966
+ test('undefined payload should be sent as-is', (t, done) => {
936
967
  t.plan(6)
937
968
 
938
969
  const fastify = Fastify()
939
970
 
940
971
  fastify.addHook('onSend', function (request, reply, payload, done) {
941
- t.equal(payload, undefined)
972
+ t.assert.strictEqual(payload, undefined)
942
973
  done()
943
974
  })
944
975
 
@@ -947,31 +978,33 @@ test('undefined payload should be sent as-is', t => {
947
978
  })
948
979
 
949
980
  fastify.listen({ port: 0 }, err => {
950
- t.error(err)
951
- t.teardown(fastify.close.bind(fastify))
981
+ t.assert.ifError(err)
982
+ t.after(() => fastify.close())
952
983
 
953
984
  sget({
954
985
  method: 'GET',
955
986
  url: `http://127.0.0.1:${fastify.server.address().port}`
956
987
  }, (err, response, body) => {
957
- t.error(err)
958
- t.equal(response.headers['content-type'], undefined)
959
- t.equal(response.headers['content-length'], undefined)
960
- t.equal(body.length, 0)
988
+ t.assert.ifError(err)
989
+ t.assert.strictEqual(response.headers['content-type'], undefined)
990
+ t.assert.strictEqual(response.headers['content-length'], undefined)
991
+ t.assert.strictEqual(body.length, 0)
992
+ done()
961
993
  })
962
994
  })
963
995
  })
964
996
 
965
- test('for HEAD method, no body should be sent but content-length should be', t => {
966
- t.plan(11)
997
+ test('for HEAD method, no body should be sent but content-length should be', async t => {
998
+ t.plan(8)
967
999
 
968
1000
  const fastify = Fastify()
1001
+ t.after(() => fastify.close())
969
1002
  const contentType = 'application/json; charset=utf-8'
970
1003
  const bodySize = JSON.stringify({ foo: 'bar' }).length
971
1004
 
972
1005
  fastify.head('/', {
973
1006
  onSend: function (request, reply, payload, done) {
974
- t.equal(payload, undefined)
1007
+ t.assert.strictEqual(payload, undefined)
975
1008
  done()
976
1009
  }
977
1010
  }, function (req, reply) {
@@ -982,7 +1015,7 @@ test('for HEAD method, no body should be sent but content-length should be', t =
982
1015
 
983
1016
  fastify.head('/with/null', {
984
1017
  onSend: function (request, reply, payload, done) {
985
- t.equal(payload, 'null')
1018
+ t.assert.strictEqual(payload, 'null')
986
1019
  done()
987
1020
  }
988
1021
  }, function (req, reply) {
@@ -991,36 +1024,46 @@ test('for HEAD method, no body should be sent but content-length should be', t =
991
1024
  reply.code(200).send(null)
992
1025
  })
993
1026
 
994
- fastify.listen({ port: 0 }, err => {
995
- t.error(err)
996
- t.teardown(fastify.close.bind(fastify))
1027
+ await fastify.listen({ port: 0 })
997
1028
 
1029
+ const promise1 = new Promise((resolve, reject) => {
998
1030
  sget({
999
1031
  method: 'HEAD',
1000
1032
  url: `http://127.0.0.1:${fastify.server.address().port}`
1001
1033
  }, (err, response, body) => {
1002
- t.error(err)
1003
- t.equal(response.headers['content-type'], contentType)
1004
- t.equal(response.headers['content-length'], bodySize.toString())
1005
- t.equal(body.length, 0)
1034
+ if (err) {
1035
+ reject(err)
1036
+ }
1037
+ t.assert.strictEqual(response.headers['content-type'], contentType)
1038
+ t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
1039
+ t.assert.strictEqual(body.length, 0)
1040
+ resolve()
1006
1041
  })
1042
+ })
1007
1043
 
1044
+ const promise2 = new Promise((resolve, reject) => {
1008
1045
  sget({
1009
1046
  method: 'HEAD',
1010
1047
  url: `http://127.0.0.1:${fastify.server.address().port}/with/null`
1011
1048
  }, (err, response, body) => {
1012
- t.error(err)
1013
- t.equal(response.headers['content-type'], contentType)
1014
- t.equal(response.headers['content-length'], bodySize.toString())
1015
- t.equal(body.length, 0)
1049
+ if (err) {
1050
+ reject(err)
1051
+ }
1052
+ t.assert.strictEqual(response.headers['content-type'], contentType)
1053
+ t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
1054
+ t.assert.strictEqual(body.length, 0)
1055
+ resolve()
1016
1056
  })
1017
1057
  })
1058
+
1059
+ await Promise.all([promise1, promise2])
1018
1060
  })
1019
1061
 
1020
- test('reply.send(new NotFound()) should not invoke the 404 handler', t => {
1021
- t.plan(9)
1062
+ test('reply.send(new NotFound()) should not invoke the 404 handler', async t => {
1063
+ t.plan(6)
1022
1064
 
1023
1065
  const fastify = Fastify()
1066
+ t.after(() => fastify.close())
1024
1067
 
1025
1068
  fastify.setNotFoundHandler((req, reply) => {
1026
1069
  t.fail('Should not be called')
@@ -1038,42 +1081,50 @@ test('reply.send(new NotFound()) should not invoke the 404 handler', t => {
1038
1081
  done()
1039
1082
  }, { prefix: '/prefixed' })
1040
1083
 
1041
- fastify.listen({ port: 0 }, err => {
1042
- t.error(err)
1043
-
1044
- t.teardown(fastify.close.bind(fastify))
1084
+ await fastify.listen({ port: 0 })
1045
1085
 
1086
+ const promise1 = new Promise((resolve, reject) => {
1046
1087
  sget({
1047
1088
  method: 'GET',
1048
1089
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/not-found'
1049
1090
  }, (err, response, body) => {
1050
- t.error(err)
1051
- t.equal(response.statusCode, 404)
1052
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
1053
- t.same(JSON.parse(body.toString()), {
1091
+ if (err) {
1092
+ reject(err)
1093
+ }
1094
+ t.assert.strictEqual(response.statusCode, 404)
1095
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
1096
+ t.assert.deepStrictEqual(JSON.parse(body.toString()), {
1054
1097
  statusCode: 404,
1055
1098
  error: 'Not Found',
1056
1099
  message: 'Not Found'
1057
1100
  })
1101
+ resolve()
1058
1102
  })
1103
+ })
1059
1104
 
1105
+ const promise2 = new Promise((resolve, reject) => {
1060
1106
  sget({
1061
1107
  method: 'GET',
1062
1108
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/prefixed/not-found'
1063
1109
  }, (err, response, body) => {
1064
- t.error(err)
1065
- t.equal(response.statusCode, 404)
1066
- t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
1067
- t.same(JSON.parse(body), {
1110
+ if (err) {
1111
+ reject(err)
1112
+ }
1113
+ t.assert.strictEqual(response.statusCode, 404)
1114
+ t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
1115
+ t.assert.deepStrictEqual(JSON.parse(body.toString()), {
1116
+ statusCode: 404,
1068
1117
  error: 'Not Found',
1069
- message: 'Not Found',
1070
- statusCode: 404
1118
+ message: 'Not Found'
1071
1119
  })
1120
+ resolve()
1072
1121
  })
1073
1122
  })
1123
+
1124
+ await Promise.all([promise1, promise2])
1074
1125
  })
1075
1126
 
1076
- test('reply can set multiple instances of same header', t => {
1127
+ test('reply can set multiple instances of same header', (t, done) => {
1077
1128
  t.plan(4)
1078
1129
 
1079
1130
  const fastify = require('../../')()
@@ -1086,75 +1137,80 @@ test('reply can set multiple instances of same header', t => {
1086
1137
  })
1087
1138
 
1088
1139
  fastify.listen({ port: 0 }, err => {
1089
- t.error(err)
1090
- t.teardown(fastify.close.bind(fastify))
1140
+ t.assert.ifError(err)
1141
+ t.after(() => fastify.close())
1091
1142
 
1092
1143
  sget({
1093
1144
  method: 'GET',
1094
1145
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1095
1146
  }, (err, response, body) => {
1096
- t.error(err)
1097
- t.ok(response.headers['set-cookie'])
1098
- t.strictSame(response.headers['set-cookie'], ['one', 'two'])
1147
+ t.assert.ifError(err)
1148
+ t.assert.ok(response.headers['set-cookie'])
1149
+ t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two'])
1150
+ done()
1099
1151
  })
1100
1152
  })
1101
1153
  })
1102
1154
 
1103
- test('reply.hasHeader returns correct values', t => {
1155
+ test('reply.hasHeader returns correct values', (t, done) => {
1104
1156
  t.plan(3)
1105
1157
 
1106
1158
  const fastify = require('../../')()
1107
1159
 
1108
1160
  fastify.get('/headers', function (req, reply) {
1109
1161
  reply.header('x-foo', 'foo')
1110
- t.equal(reply.hasHeader('x-foo'), true)
1111
- t.equal(reply.hasHeader('x-bar'), false)
1162
+ t.assert.strictEqual(reply.hasHeader('x-foo'), true)
1163
+ t.assert.strictEqual(reply.hasHeader('x-bar'), false)
1112
1164
  reply.send()
1113
1165
  })
1114
1166
 
1115
1167
  fastify.listen({ port: 0 }, err => {
1116
- t.error(err)
1117
- t.teardown(fastify.close.bind(fastify))
1168
+ t.assert.ifError(err)
1169
+ t.after(() => fastify.close())
1118
1170
  sget({
1119
1171
  method: 'GET',
1120
1172
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1121
- }, () => { })
1173
+ }, () => {
1174
+ done()
1175
+ })
1122
1176
  })
1123
1177
  })
1124
1178
 
1125
- test('reply.getHeader returns correct values', t => {
1179
+ test('reply.getHeader returns correct values', (t, done) => {
1126
1180
  t.plan(5)
1127
1181
 
1128
1182
  const fastify = require('../../')()
1129
1183
 
1130
1184
  fastify.get('/headers', function (req, reply) {
1131
1185
  reply.header('x-foo', 'foo')
1132
- t.equal(reply.getHeader('x-foo'), 'foo')
1186
+ t.assert.strictEqual(reply.getHeader('x-foo'), 'foo')
1133
1187
 
1134
1188
  reply.header('x-foo', 'bar')
1135
- t.strictSame(reply.getHeader('x-foo'), 'bar')
1189
+ t.assert.deepStrictEqual(reply.getHeader('x-foo'), 'bar')
1136
1190
 
1137
1191
  reply.header('x-foo', 42)
1138
- t.strictSame(reply.getHeader('x-foo'), 42)
1192
+ t.assert.deepStrictEqual(reply.getHeader('x-foo'), 42)
1139
1193
 
1140
1194
  reply.header('set-cookie', 'one')
1141
1195
  reply.header('set-cookie', 'two')
1142
- t.strictSame(reply.getHeader('set-cookie'), ['one', 'two'])
1196
+ t.assert.deepStrictEqual(reply.getHeader('set-cookie'), ['one', 'two'])
1143
1197
 
1144
1198
  reply.send()
1145
1199
  })
1146
1200
 
1147
1201
  fastify.listen({ port: 0 }, err => {
1148
- t.error(err)
1149
- t.teardown(fastify.close.bind(fastify))
1202
+ t.assert.ifError(err)
1203
+ t.after(() => fastify.close())
1150
1204
  sget({
1151
1205
  method: 'GET',
1152
1206
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1153
- }, () => { })
1207
+ }, () => {
1208
+ done()
1209
+ })
1154
1210
  })
1155
1211
  })
1156
1212
 
1157
- test('reply.getHeader returns raw header if there is not in the reply headers', t => {
1213
+ test('reply.getHeader returns raw header if there is not in the reply headers', (t) => {
1158
1214
  t.plan(1)
1159
1215
  const response = {
1160
1216
  setHeader: () => { },
@@ -1164,10 +1220,10 @@ test('reply.getHeader returns raw header if there is not in the reply headers',
1164
1220
  end: () => { }
1165
1221
  }
1166
1222
  const reply = new Reply(response, { onSend: [] }, null)
1167
- t.equal(reply.getHeader('foo'), 'bar')
1223
+ t.assert.strictEqual(reply.getHeader('foo'), 'bar')
1168
1224
  })
1169
1225
 
1170
- test('reply.getHeaders returns correct values', t => {
1226
+ test('reply.getHeaders returns correct values', (t, done) => {
1171
1227
  t.plan(3)
1172
1228
 
1173
1229
  const fastify = require('../../')()
@@ -1175,7 +1231,7 @@ test('reply.getHeaders returns correct values', t => {
1175
1231
  fastify.get('/headers', function (req, reply) {
1176
1232
  reply.header('x-foo', 'foo')
1177
1233
 
1178
- t.strictSame(reply.getHeaders(), {
1234
+ t.assert.deepStrictEqual(reply.getHeaders(), {
1179
1235
  'x-foo': 'foo'
1180
1236
  })
1181
1237
 
@@ -1183,7 +1239,7 @@ test('reply.getHeaders returns correct values', t => {
1183
1239
  reply.raw.setHeader('x-foo', 'foo2')
1184
1240
  reply.raw.setHeader('x-baz', 'baz')
1185
1241
 
1186
- t.strictSame(reply.getHeaders(), {
1242
+ t.assert.deepStrictEqual(reply.getHeaders(), {
1187
1243
  'x-foo': 'foo',
1188
1244
  'x-bar': 'bar',
1189
1245
  'x-baz': 'baz'
@@ -1193,97 +1249,98 @@ test('reply.getHeaders returns correct values', t => {
1193
1249
  })
1194
1250
 
1195
1251
  fastify.inject('/headers', (err) => {
1196
- t.error(err)
1252
+ t.assert.ifError(err)
1253
+ done()
1197
1254
  })
1198
1255
  })
1199
1256
 
1200
- test('reply.removeHeader can remove the value', t => {
1201
- t.plan(5)
1257
+ test('reply.removeHeader can remove the value', (t, done) => {
1258
+ t.plan(4)
1202
1259
 
1203
1260
  const fastify = require('../../')()
1204
1261
 
1205
- t.teardown(fastify.close.bind(fastify))
1262
+ t.after(() => fastify.close())
1206
1263
 
1207
1264
  fastify.get('/headers', function (req, reply) {
1208
1265
  reply.header('x-foo', 'foo')
1209
- t.equal(reply.getHeader('x-foo'), 'foo')
1266
+ t.assert.strictEqual(reply.getHeader('x-foo'), 'foo')
1210
1267
 
1211
- t.equal(reply.removeHeader('x-foo'), reply)
1212
- t.strictSame(reply.getHeader('x-foo'), undefined)
1268
+ t.assert.strictEqual(reply.removeHeader('x-foo'), reply)
1269
+ t.assert.deepStrictEqual(reply.getHeader('x-foo'), undefined)
1213
1270
 
1214
1271
  reply.send()
1215
1272
  })
1216
1273
 
1217
1274
  fastify.listen({ port: 0 }, err => {
1218
- t.error(err)
1219
- t.teardown(fastify.close.bind(fastify))
1275
+ t.assert.ifError(err)
1276
+ t.after(() => fastify.close())
1220
1277
  sget({
1221
1278
  method: 'GET',
1222
1279
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1223
1280
  }, () => {
1224
- t.pass()
1281
+ done()
1225
1282
  })
1226
1283
  })
1227
1284
  })
1228
1285
 
1229
- test('reply.header can reset the value', t => {
1230
- t.plan(3)
1286
+ test('reply.header can reset the value', (t, done) => {
1287
+ t.plan(2)
1231
1288
 
1232
1289
  const fastify = require('../../')()
1233
1290
 
1234
- t.teardown(fastify.close.bind(fastify))
1291
+ t.after(() => fastify.close())
1235
1292
 
1236
1293
  fastify.get('/headers', function (req, reply) {
1237
1294
  reply.header('x-foo', 'foo')
1238
1295
  reply.header('x-foo', undefined)
1239
- t.strictSame(reply.getHeader('x-foo'), '')
1296
+ t.assert.deepStrictEqual(reply.getHeader('x-foo'), '')
1240
1297
 
1241
1298
  reply.send()
1242
1299
  })
1243
1300
 
1244
1301
  fastify.listen({ port: 0 }, err => {
1245
- t.error(err)
1246
- t.teardown(fastify.close.bind(fastify))
1302
+ t.assert.ifError(err)
1303
+ t.after(() => fastify.close())
1247
1304
  sget({
1248
1305
  method: 'GET',
1249
1306
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1250
1307
  }, () => {
1251
- t.pass()
1308
+ done()
1252
1309
  })
1253
1310
  })
1254
1311
  })
1255
1312
 
1256
1313
  // https://github.com/fastify/fastify/issues/3030
1257
- test('reply.hasHeader computes raw and fastify headers', t => {
1258
- t.plan(4)
1314
+ test('reply.hasHeader computes raw and fastify headers', (t, done) => {
1315
+ t.plan(3)
1259
1316
 
1260
1317
  const fastify = require('../../')()
1261
1318
 
1262
- t.teardown(fastify.close.bind(fastify))
1319
+ t.after(() => fastify.close())
1263
1320
 
1264
1321
  fastify.get('/headers', function (req, reply) {
1265
1322
  reply.header('x-foo', 'foo')
1266
1323
  reply.raw.setHeader('x-bar', 'bar')
1267
- t.ok(reply.hasHeader('x-foo'))
1268
- t.ok(reply.hasHeader('x-bar'))
1324
+ t.assert.ok(reply.hasHeader('x-foo'))
1325
+ t.assert.ok(reply.hasHeader('x-bar'))
1269
1326
 
1270
1327
  reply.send()
1271
1328
  })
1272
1329
 
1273
1330
  fastify.listen({ port: 0 }, err => {
1274
- t.error(err)
1275
- t.teardown(fastify.close.bind(fastify))
1331
+ t.assert.ifError(err)
1332
+ t.after(() => fastify.close())
1276
1333
  sget({
1277
1334
  method: 'GET',
1278
1335
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1279
1336
  }, () => {
1280
- t.pass()
1337
+ done()
1281
1338
  })
1282
1339
  })
1283
1340
  })
1284
1341
 
1285
- test('Reply should handle JSON content type with a charset', t => {
1286
- t.plan(16)
1342
+ test('Reply should handle JSON content type with a charset', async t => {
1343
+ t.plan(8)
1287
1344
 
1288
1345
  const fastify = require('../../')()
1289
1346
 
@@ -1333,48 +1390,47 @@ test('Reply should handle JSON content type with a charset', t => {
1333
1390
  .send({ hello: 'world' })
1334
1391
  })
1335
1392
 
1336
- fastify.inject('/default', (err, res) => {
1337
- t.error(err)
1338
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
1339
- })
1340
-
1341
- fastify.inject('/utf8', (err, res) => {
1342
- t.error(err)
1343
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
1344
- })
1393
+ {
1394
+ const res = await fastify.inject('/default')
1395
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
1396
+ }
1345
1397
 
1346
- fastify.inject('/utf16', (err, res) => {
1347
- t.error(err)
1348
- t.equal(res.headers['content-type'], 'application/json; charset=utf-16')
1349
- })
1398
+ {
1399
+ const res = await fastify.inject('/utf8')
1400
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
1401
+ }
1350
1402
 
1351
- fastify.inject('/utf32', (err, res) => {
1352
- t.error(err)
1353
- t.equal(res.headers['content-type'], 'application/json; charset=utf-32')
1354
- })
1403
+ {
1404
+ const res = await fastify.inject('/utf16')
1405
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-16')
1406
+ }
1355
1407
 
1356
- fastify.inject('/type-utf8', (err, res) => {
1357
- t.error(err)
1358
- t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
1359
- })
1408
+ {
1409
+ const res = await fastify.inject('/utf32')
1410
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-32')
1411
+ }
1360
1412
 
1361
- fastify.inject('/type-utf16', (err, res) => {
1362
- t.error(err)
1363
- t.equal(res.headers['content-type'], 'application/json; charset=utf-16')
1364
- })
1413
+ {
1414
+ const res = await fastify.inject('/type-utf8')
1415
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
1416
+ }
1365
1417
 
1366
- fastify.inject('/type-utf32', (err, res) => {
1367
- t.error(err)
1368
- t.equal(res.headers['content-type'], 'application/json; charset=utf-32')
1369
- })
1418
+ {
1419
+ const res = await fastify.inject('/type-utf16')
1420
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-16')
1421
+ }
1422
+ {
1423
+ const res = await fastify.inject('/type-utf32')
1424
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-32')
1425
+ }
1370
1426
 
1371
- fastify.inject('/no-space-type-utf32', (err, res) => {
1372
- t.error(err)
1373
- t.equal(res.headers['content-type'], 'application/json;charset=utf-32')
1374
- })
1427
+ {
1428
+ const res = await fastify.inject('/no-space-type-utf32')
1429
+ t.assert.strictEqual(res.headers['content-type'], 'application/json;charset=utf-32')
1430
+ }
1375
1431
  })
1376
1432
 
1377
- test('Content type and charset set previously', t => {
1433
+ test('Content type and charset set previously', (t, done) => {
1378
1434
  t.plan(2)
1379
1435
 
1380
1436
  const fastify = require('../../')()
@@ -1389,12 +1445,13 @@ test('Content type and charset set previously', t => {
1389
1445
  })
1390
1446
 
1391
1447
  fastify.inject('/', (err, res) => {
1392
- t.error(err)
1393
- t.equal(res.headers['content-type'], 'application/json; charset=utf-16')
1448
+ t.assert.ifError(err)
1449
+ t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-16')
1450
+ done()
1394
1451
  })
1395
1452
  })
1396
1453
 
1397
- test('.status() is an alias for .code()', t => {
1454
+ test('.status() is an alias for .code()', (t, done) => {
1398
1455
  t.plan(2)
1399
1456
  const fastify = Fastify()
1400
1457
 
@@ -1403,32 +1460,35 @@ test('.status() is an alias for .code()', t => {
1403
1460
  })
1404
1461
 
1405
1462
  fastify.inject('/', (err, res) => {
1406
- t.error(err)
1407
- t.equal(res.statusCode, 418)
1463
+ t.assert.ifError(err)
1464
+ t.assert.strictEqual(res.statusCode, 418)
1465
+ done()
1408
1466
  })
1409
1467
  })
1410
1468
 
1411
- test('.statusCode is getter and setter', t => {
1469
+ test('.statusCode is getter and setter', (t, done) => {
1412
1470
  t.plan(4)
1413
1471
  const fastify = Fastify()
1414
1472
 
1415
1473
  fastify.get('/', function (req, reply) {
1416
- t.equal(reply.statusCode, 200, 'default status value')
1474
+ t.assert.strictEqual(reply.statusCode, 200, 'default status value')
1417
1475
  reply.statusCode = 418
1418
- t.equal(reply.statusCode, 418)
1476
+ t.assert.strictEqual(reply.statusCode, 418)
1419
1477
  reply.send()
1420
1478
  })
1421
1479
 
1422
1480
  fastify.inject('/', (err, res) => {
1423
- t.error(err)
1424
- t.equal(res.statusCode, 418)
1481
+ t.assert.ifError(err)
1482
+ t.assert.strictEqual(res.statusCode, 418)
1483
+ done()
1425
1484
  })
1426
1485
  })
1427
1486
 
1428
- test('reply.header setting multiple cookies as multiple Set-Cookie headers', t => {
1429
- t.plan(7)
1487
+ test('reply.header setting multiple cookies as multiple Set-Cookie headers', async t => {
1488
+ t.plan(4)
1430
1489
 
1431
1490
  const fastify = require('../../')()
1491
+ t.after(() => fastify.close())
1432
1492
 
1433
1493
  fastify.get('/headers', function (req, reply) {
1434
1494
  reply
@@ -1439,28 +1499,28 @@ test('reply.header setting multiple cookies as multiple Set-Cookie headers', t =
1439
1499
  .send({})
1440
1500
  })
1441
1501
 
1442
- fastify.listen({ port: 0 }, err => {
1443
- t.error(err)
1444
- t.teardown(fastify.close.bind(fastify))
1502
+ await fastify.listen({ port: 0 })
1445
1503
 
1504
+ await new Promise((resolve, reject) => {
1446
1505
  sget({
1447
1506
  method: 'GET',
1448
1507
  url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
1449
1508
  }, (err, response, body) => {
1450
- t.error(err)
1451
- t.ok(response.headers['set-cookie'])
1452
- t.strictSame(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
1509
+ if (err) {
1510
+ reject(err)
1511
+ }
1512
+ t.assert.ok(response.headers['set-cookie'])
1513
+ t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
1514
+ resolve()
1453
1515
  })
1454
1516
  })
1455
1517
 
1456
- fastify.inject('/headers', (error, response) => {
1457
- t.error(error)
1458
- t.ok(response.headers['set-cookie'])
1459
- t.strictSame(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
1460
- })
1518
+ const response = await fastify.inject('/headers')
1519
+ t.assert.ok(response.headers['set-cookie'])
1520
+ t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
1461
1521
  })
1462
1522
 
1463
- test('should throw when trying to modify the reply.sent property', t => {
1523
+ test('should throw when trying to modify the reply.sent property', (t, done) => {
1464
1524
  t.plan(3)
1465
1525
  const fastify = Fastify()
1466
1526
 
@@ -1468,14 +1528,15 @@ test('should throw when trying to modify the reply.sent property', t => {
1468
1528
  try {
1469
1529
  reply.sent = true
1470
1530
  } catch (err) {
1471
- t.ok(err)
1531
+ t.assert.ok(err)
1472
1532
  reply.send()
1473
1533
  }
1474
1534
  })
1475
1535
 
1476
1536
  fastify.inject('/', (err, res) => {
1477
- t.error(err)
1478
- t.pass()
1537
+ t.assert.ifError(err)
1538
+ t.assert.ok(true)
1539
+ done()
1479
1540
  })
1480
1541
  })
1481
1542
 
@@ -1483,10 +1544,10 @@ test('reply.elapsedTime should return 0 before the timer is initialised on the r
1483
1544
  t.plan(1)
1484
1545
  const response = { statusCode: 200 }
1485
1546
  const reply = new Reply(response, null)
1486
- t.equal(reply.elapsedTime, 0)
1547
+ t.assert.strictEqual(reply.elapsedTime, 0)
1487
1548
  })
1488
1549
 
1489
- test('reply.elapsedTime should return a number greater than 0 after the timer is initialised on the reply by setting up response listeners', t => {
1550
+ test('reply.elapsedTime should return a number greater than 0 after the timer is initialised on the reply by setting up response listeners', async t => {
1490
1551
  t.plan(1)
1491
1552
  const fastify = Fastify()
1492
1553
  fastify.route({
@@ -1498,14 +1559,13 @@ test('reply.elapsedTime should return a number greater than 0 after the timer is
1498
1559
  })
1499
1560
 
1500
1561
  fastify.addHook('onResponse', (req, reply) => {
1501
- t.ok(reply.elapsedTime > 0)
1502
- t.end()
1562
+ t.assert.ok(reply.elapsedTime > 0)
1503
1563
  })
1504
1564
 
1505
- fastify.inject({ method: 'GET', url: '/' })
1565
+ await fastify.inject({ method: 'GET', url: '/' })
1506
1566
  })
1507
1567
 
1508
- test('reply.elapsedTime should return the time since a request started while inflight', t => {
1568
+ test('reply.elapsedTime should return the time since a request started while inflight', async t => {
1509
1569
  t.plan(1)
1510
1570
  const fastify = Fastify()
1511
1571
  fastify.route({
@@ -1525,14 +1585,13 @@ test('reply.elapsedTime should return the time since a request started while inf
1525
1585
  })
1526
1586
 
1527
1587
  fastify.addHook('onResponse', (req, reply) => {
1528
- t.ok(reply.elapsedTime > preValidationElapsedTime)
1529
- t.end()
1588
+ t.assert.ok(reply.elapsedTime > preValidationElapsedTime)
1530
1589
  })
1531
1590
 
1532
- fastify.inject({ method: 'GET', url: '/' })
1591
+ await fastify.inject({ method: 'GET', url: '/' })
1533
1592
  })
1534
1593
 
1535
- test('reply.elapsedTime should return the same value after a request is finished', t => {
1594
+ test('reply.elapsedTime should return the same value after a request is finished', async t => {
1536
1595
  t.plan(1)
1537
1596
  const fastify = Fastify()
1538
1597
  fastify.route({
@@ -1544,19 +1603,18 @@ test('reply.elapsedTime should return the same value after a request is finished
1544
1603
  })
1545
1604
 
1546
1605
  fastify.addHook('onResponse', (req, reply) => {
1547
- t.equal(reply.elapsedTime, reply.elapsedTime)
1548
- t.end()
1606
+ t.assert.strictEqual(reply.elapsedTime, reply.elapsedTime)
1549
1607
  })
1550
1608
 
1551
- fastify.inject({ method: 'GET', url: '/' })
1609
+ await fastify.inject({ method: 'GET', url: '/' })
1552
1610
  })
1553
1611
 
1554
- test('reply should use the custom serializer', t => {
1612
+ test('reply should use the custom serializer', (t, done) => {
1555
1613
  t.plan(4)
1556
1614
  const fastify = Fastify()
1557
1615
  fastify.setReplySerializer((payload, statusCode) => {
1558
- t.same(payload, { foo: 'bar' })
1559
- t.equal(statusCode, 200)
1616
+ t.assert.deepStrictEqual(payload, { foo: 'bar' })
1617
+ t.assert.strictEqual(statusCode, 200)
1560
1618
  payload.foo = 'bar bar'
1561
1619
  return JSON.stringify(payload)
1562
1620
  })
@@ -1573,17 +1631,18 @@ test('reply should use the custom serializer', t => {
1573
1631
  method: 'GET',
1574
1632
  url: '/'
1575
1633
  }, (err, res) => {
1576
- t.error(err)
1577
- t.equal(res.payload, '{"foo":"bar bar"}')
1634
+ t.assert.ifError(err)
1635
+ t.assert.strictEqual(res.payload, '{"foo":"bar bar"}')
1636
+ done()
1578
1637
  })
1579
1638
  })
1580
1639
 
1581
- test('reply should use the right serializer in encapsulated context', t => {
1582
- t.plan(9)
1640
+ test('reply should use the right serializer in encapsulated context', async t => {
1641
+ t.plan(6)
1583
1642
 
1584
1643
  const fastify = Fastify()
1585
1644
  fastify.setReplySerializer((payload) => {
1586
- t.same(payload, { foo: 'bar' })
1645
+ t.assert.deepStrictEqual(payload, { foo: 'bar' })
1587
1646
  payload.foo = 'bar bar'
1588
1647
  return JSON.stringify(payload)
1589
1648
  })
@@ -1601,7 +1660,7 @@ test('reply should use the right serializer in encapsulated context', t => {
1601
1660
  handler: (req, reply) => { reply.send({ john: 'doo' }) }
1602
1661
  })
1603
1662
  instance.setReplySerializer((payload) => {
1604
- t.same(payload, { john: 'doo' })
1663
+ t.assert.deepStrictEqual(payload, { john: 'doo' })
1605
1664
  payload.john = 'too too'
1606
1665
  return JSON.stringify(payload)
1607
1666
  })
@@ -1615,40 +1674,31 @@ test('reply should use the right serializer in encapsulated context', t => {
1615
1674
  handler: (req, reply) => { reply.send({ sweet: 'potato' }) }
1616
1675
  })
1617
1676
  instance.setReplySerializer((payload) => {
1618
- t.same(payload, { sweet: 'potato' })
1677
+ t.assert.deepStrictEqual(payload, { sweet: 'potato' })
1619
1678
  payload.sweet = 'potato potato'
1620
1679
  return JSON.stringify(payload)
1621
1680
  })
1622
1681
  done()
1623
1682
  }, { prefix: 'sub' })
1624
1683
 
1625
- fastify.inject({
1626
- method: 'GET',
1627
- url: '/'
1628
- }, (err, res) => {
1629
- t.error(err)
1630
- t.equal(res.payload, '{"foo":"bar bar"}')
1631
- })
1684
+ {
1685
+ const res = await fastify.inject('/')
1686
+ t.assert.strictEqual(res.payload, '{"foo":"bar bar"}')
1687
+ }
1632
1688
 
1633
- fastify.inject({
1634
- method: 'GET',
1635
- url: '/sub'
1636
- }, (err, res) => {
1637
- t.error(err)
1638
- t.equal(res.payload, '{"john":"too too"}')
1639
- })
1689
+ {
1690
+ const res = await fastify.inject('/sub')
1691
+ t.assert.strictEqual(res.payload, '{"john":"too too"}')
1692
+ }
1640
1693
 
1641
- fastify.inject({
1642
- method: 'GET',
1643
- url: '/sub/sub'
1644
- }, (err, res) => {
1645
- t.error(err)
1646
- t.equal(res.payload, '{"sweet":"potato potato"}')
1647
- })
1694
+ {
1695
+ const res = await fastify.inject('/sub/sub')
1696
+ t.assert.strictEqual(res.payload, '{"sweet":"potato potato"}')
1697
+ }
1648
1698
  })
1649
1699
 
1650
- test('reply should use the right serializer in deep encapsulated context', t => {
1651
- t.plan(8)
1700
+ test('reply should use the right serializer in deep encapsulated context', async t => {
1701
+ t.plan(5)
1652
1702
 
1653
1703
  const fastify = Fastify()
1654
1704
 
@@ -1665,7 +1715,7 @@ test('reply should use the right serializer in deep encapsulated context', t =>
1665
1715
  handler: (req, reply) => { reply.send({ john: 'doo' }) }
1666
1716
  })
1667
1717
  instance.setReplySerializer((payload) => {
1668
- t.same(payload, { john: 'doo' })
1718
+ t.assert.deepStrictEqual(payload, { john: 'doo' })
1669
1719
  payload.john = 'too too'
1670
1720
  return JSON.stringify(payload)
1671
1721
  })
@@ -1677,7 +1727,7 @@ test('reply should use the right serializer in deep encapsulated context', t =>
1677
1727
  handler: (req, reply) => { reply.send({ john: 'deep' }) }
1678
1728
  })
1679
1729
  subInstance.setReplySerializer((payload) => {
1680
- t.same(payload, { john: 'deep' })
1730
+ t.assert.deepStrictEqual(payload, { john: 'deep' })
1681
1731
  payload.john = 'deep deep'
1682
1732
  return JSON.stringify(payload)
1683
1733
  })
@@ -1686,32 +1736,21 @@ test('reply should use the right serializer in deep encapsulated context', t =>
1686
1736
  done()
1687
1737
  })
1688
1738
 
1689
- fastify.inject({
1690
- method: 'GET',
1691
- url: '/'
1692
- }, (err, res) => {
1693
- t.error(err)
1694
- t.equal(res.payload, '{"foo":"bar"}')
1695
- })
1696
-
1697
- fastify.inject({
1698
- method: 'GET',
1699
- url: '/sub'
1700
- }, (err, res) => {
1701
- t.error(err)
1702
- t.equal(res.payload, '{"john":"too too"}')
1703
- })
1704
-
1705
- fastify.inject({
1706
- method: 'GET',
1707
- url: '/deep'
1708
- }, (err, res) => {
1709
- t.error(err)
1710
- t.equal(res.payload, '{"john":"deep deep"}')
1711
- })
1739
+ {
1740
+ const res = await fastify.inject('/')
1741
+ t.assert.strictEqual(res.payload, '{"foo":"bar"}')
1742
+ }
1743
+ {
1744
+ const res = await fastify.inject('/sub')
1745
+ t.assert.strictEqual(res.payload, '{"john":"too too"}')
1746
+ }
1747
+ {
1748
+ const res = await fastify.inject('/deep')
1749
+ t.assert.strictEqual(res.payload, '{"john":"deep deep"}')
1750
+ }
1712
1751
  })
1713
1752
 
1714
- test('reply should use the route serializer', t => {
1753
+ test('reply should use the route serializer', (t, done) => {
1715
1754
  t.plan(3)
1716
1755
 
1717
1756
  const fastify = Fastify()
@@ -1725,7 +1764,7 @@ test('reply should use the route serializer', t => {
1725
1764
  handler: (req, reply) => {
1726
1765
  reply
1727
1766
  .serializer((payload) => {
1728
- t.same(payload, { john: 'doo' })
1767
+ t.assert.deepStrictEqual(payload, { john: 'doo' })
1729
1768
  payload.john = 'too too'
1730
1769
  return JSON.stringify(payload)
1731
1770
  })
@@ -1737,86 +1776,79 @@ test('reply should use the route serializer', t => {
1737
1776
  method: 'GET',
1738
1777
  url: '/'
1739
1778
  }, (err, res) => {
1740
- t.error(err)
1741
- t.equal(res.payload, '{"john":"too too"}')
1779
+ t.assert.ifError(err)
1780
+ t.assert.strictEqual(res.payload, '{"john":"too too"}')
1781
+ done()
1742
1782
  })
1743
1783
  })
1744
1784
 
1745
- test('cannot set the replySerializer when the server is running', t => {
1785
+ test('cannot set the replySerializer when the server is running', (t, done) => {
1746
1786
  t.plan(2)
1747
1787
 
1748
1788
  const fastify = Fastify()
1749
- t.teardown(fastify.close.bind(fastify))
1789
+ t.after(() => fastify.close())
1750
1790
 
1751
1791
  fastify.listen({ port: 0 }, err => {
1752
- t.error(err)
1792
+ t.assert.ifError(err)
1753
1793
  try {
1754
1794
  fastify.setReplySerializer(() => { })
1755
- t.fail('this serializer should not be setup')
1795
+ t.assert.fail('this serializer should not be setup')
1756
1796
  } catch (e) {
1757
- t.equal(e.code, 'FST_ERR_INSTANCE_ALREADY_LISTENING')
1797
+ t.assert.strictEqual(e.code, 'FST_ERR_INSTANCE_ALREADY_LISTENING')
1798
+ } finally {
1799
+ done()
1758
1800
  }
1759
1801
  })
1760
1802
  })
1761
1803
 
1762
- test('reply should not call the custom serializer for errors and not found', t => {
1763
- t.plan(9)
1804
+ test('reply should not call the custom serializer for errors and not found', async t => {
1805
+ t.plan(6)
1764
1806
 
1765
1807
  const fastify = Fastify()
1766
1808
  fastify.setReplySerializer((payload, statusCode) => {
1767
- t.same(payload, { foo: 'bar' })
1768
- t.equal(statusCode, 200)
1809
+ t.assert.deepStrictEqual(payload, { foo: 'bar' })
1810
+ t.assert.strictEqual(statusCode, 200)
1769
1811
  return JSON.stringify(payload)
1770
1812
  })
1771
1813
 
1772
1814
  fastify.get('/', (req, reply) => { reply.send({ foo: 'bar' }) })
1773
1815
  fastify.get('/err', (req, reply) => { reply.send(new Error('an error')) })
1774
1816
 
1775
- fastify.inject({
1776
- method: 'GET',
1777
- url: '/'
1778
- }, (err, res) => {
1779
- t.error(err)
1780
- t.equal(res.statusCode, 200)
1781
- t.equal(res.payload, '{"foo":"bar"}')
1782
- })
1783
-
1784
- fastify.inject({
1785
- method: 'GET',
1786
- url: '/err'
1787
- }, (err, res) => {
1788
- t.error(err)
1789
- t.equal(res.statusCode, 500)
1790
- })
1791
-
1792
- fastify.inject({
1793
- method: 'GET',
1794
- url: '/not-existing'
1795
- }, (err, res) => {
1796
- t.error(err)
1797
- t.equal(res.statusCode, 404)
1798
- })
1817
+ {
1818
+ const res = await fastify.inject('/')
1819
+ t.assert.strictEqual(res.statusCode, 200)
1820
+ t.assert.strictEqual(res.payload, '{"foo":"bar"}')
1821
+ }
1822
+ {
1823
+ const res = await fastify.inject('/err')
1824
+ t.assert.strictEqual(res.statusCode, 500)
1825
+ }
1826
+ {
1827
+ const res = await fastify.inject('/not-existing')
1828
+ t.assert.strictEqual(res.statusCode, 404)
1829
+ }
1799
1830
  })
1800
1831
 
1801
- test('reply.then', t => {
1832
+ test('reply.then', async t => {
1802
1833
  t.plan(4)
1803
1834
 
1804
1835
  function request () { }
1805
1836
 
1806
- t.test('without an error', t => {
1837
+ await t.test('without an error', (t, done) => {
1807
1838
  t.plan(1)
1808
1839
 
1809
1840
  const response = new Writable()
1810
1841
  const reply = new Reply(response, request)
1811
1842
 
1812
1843
  reply.then(function () {
1813
- t.pass('fulfilled called')
1844
+ t.assert.ok(true)
1845
+ done()
1814
1846
  })
1815
1847
 
1816
1848
  response.destroy()
1817
1849
  })
1818
1850
 
1819
- t.test('with an error', t => {
1851
+ await t.test('with an error', (t, done) => {
1820
1852
  t.plan(1)
1821
1853
 
1822
1854
  const response = new Writable()
@@ -1824,15 +1856,16 @@ test('reply.then', t => {
1824
1856
  const _err = new Error('kaboom')
1825
1857
 
1826
1858
  reply.then(function () {
1827
- t.fail('fulfilled called')
1859
+ t.assert.fail('fulfilled called')
1828
1860
  }, function (err) {
1829
- t.equal(err, _err)
1861
+ t.assert.strictEqual(err, _err)
1862
+ done()
1830
1863
  })
1831
1864
 
1832
1865
  response.destroy(_err)
1833
1866
  })
1834
1867
 
1835
- t.test('with error but without reject callback', t => {
1868
+ await t.test('with error but without reject callback', t => {
1836
1869
  t.plan(1)
1837
1870
 
1838
1871
  const response = new Writable()
@@ -1840,15 +1873,15 @@ test('reply.then', t => {
1840
1873
  const _err = new Error('kaboom')
1841
1874
 
1842
1875
  reply.then(function () {
1843
- t.fail('fulfilled called')
1876
+ t.assert.fail('fulfilled called')
1844
1877
  })
1845
1878
 
1846
- t.pass()
1879
+ t.assert.ok(true)
1847
1880
 
1848
1881
  response.destroy(_err)
1849
1882
  })
1850
1883
 
1851
- t.test('with error, without reject callback, with logger', t => {
1884
+ await t.test('with error, without reject callback, with logger', (t, done) => {
1852
1885
  t.plan(1)
1853
1886
 
1854
1887
  const response = new Writable()
@@ -1856,13 +1889,14 @@ test('reply.then', t => {
1856
1889
  // spy logger
1857
1890
  reply.log = {
1858
1891
  warn: (message) => {
1859
- t.equal(message, 'unhandled rejection on reply.then')
1892
+ t.assert.strictEqual(message, 'unhandled rejection on reply.then')
1893
+ done()
1860
1894
  }
1861
1895
  }
1862
1896
  const _err = new Error('kaboom')
1863
1897
 
1864
1898
  reply.then(function () {
1865
- t.fail('fulfilled called')
1899
+ t.assert.fail('fulfilled called')
1866
1900
  })
1867
1901
 
1868
1902
  response.destroy(_err)
@@ -1874,7 +1908,7 @@ test('reply.sent should read from response.writableEnded if it is defined', t =>
1874
1908
 
1875
1909
  const reply = new Reply({ writableEnded: true }, {}, {})
1876
1910
 
1877
- t.equal(reply.sent, true)
1911
+ t.assert.strictEqual(reply.sent, true)
1878
1912
  })
1879
1913
 
1880
1914
  test('redirect to an invalid URL should not crash the server', async t => {
@@ -1884,7 +1918,7 @@ test('redirect to an invalid URL should not crash the server', async t => {
1884
1918
  url: '/redirect',
1885
1919
  handler: (req, reply) => {
1886
1920
  reply.log.warn = function mockWarn (obj, message) {
1887
- t.equal(message, 'Invalid character in header content ["location"]')
1921
+ t.assert.strictEqual(message, 'Invalid character in header content ["location"]')
1888
1922
  }
1889
1923
 
1890
1924
  switch (req.query.useCase) {
@@ -1907,8 +1941,8 @@ test('redirect to an invalid URL should not crash the server', async t => {
1907
1941
 
1908
1942
  {
1909
1943
  const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=1`)
1910
- t.equal(response.statusCode, 500)
1911
- t.same(JSON.parse(body), {
1944
+ t.assert.strictEqual(response.statusCode, 500)
1945
+ t.assert.deepStrictEqual(JSON.parse(body), {
1912
1946
  statusCode: 500,
1913
1947
  code: 'ERR_INVALID_CHAR',
1914
1948
  error: 'Internal Server Error',
@@ -1917,14 +1951,14 @@ test('redirect to an invalid URL should not crash the server', async t => {
1917
1951
  }
1918
1952
  {
1919
1953
  const { response } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=2`)
1920
- t.equal(response.statusCode, 302)
1921
- t.equal(response.headers.location, '/?key=a%E2%80%99b')
1954
+ t.assert.strictEqual(response.statusCode, 302)
1955
+ t.assert.strictEqual(response.headers.location, '/?key=a%E2%80%99b')
1922
1956
  }
1923
1957
 
1924
1958
  {
1925
1959
  const { response } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/redirect?useCase=3`)
1926
- t.equal(response.statusCode, 302)
1927
- t.equal(response.headers.location, '/?key=ab')
1960
+ t.assert.strictEqual(response.statusCode, 302)
1961
+ t.assert.strictEqual(response.headers.location, '/?key=ab')
1928
1962
  }
1929
1963
 
1930
1964
  await fastify.close()
@@ -1937,7 +1971,7 @@ test('invalid response headers should not crash the server', async t => {
1937
1971
  url: '/bad-headers',
1938
1972
  handler: (req, reply) => {
1939
1973
  reply.log.warn = function mockWarn (obj, message) {
1940
- t.equal(message, 'Invalid character in header content ["smile-encoded"]', 'only the first invalid header is logged')
1974
+ t.assert.strictEqual(message, 'Invalid character in header content ["smile-encoded"]', 'only the first invalid header is logged')
1941
1975
  }
1942
1976
 
1943
1977
  reply.header('foo', '$')
@@ -1952,8 +1986,8 @@ test('invalid response headers should not crash the server', async t => {
1952
1986
  await fastify.listen({ port: 0 })
1953
1987
 
1954
1988
  const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
1955
- t.equal(response.statusCode, 500)
1956
- t.same(JSON.parse(body), {
1989
+ t.assert.strictEqual(response.statusCode, 500)
1990
+ t.assert.deepStrictEqual(JSON.parse(body), {
1957
1991
  statusCode: 500,
1958
1992
  code: 'ERR_INVALID_CHAR',
1959
1993
  error: 'Internal Server Error',
@@ -1970,7 +2004,7 @@ test('invalid response headers when sending back an error', async t => {
1970
2004
  url: '/bad-headers',
1971
2005
  handler: (req, reply) => {
1972
2006
  reply.log.warn = function mockWarn (obj, message) {
1973
- t.equal(message, 'Invalid character in header content ["smile"]', 'only the first invalid header is logged')
2007
+ t.assert.strictEqual(message, 'Invalid character in header content ["smile"]', 'only the first invalid header is logged')
1974
2008
  }
1975
2009
 
1976
2010
  reply.header('smile', '😄')
@@ -1981,8 +2015,8 @@ test('invalid response headers when sending back an error', async t => {
1981
2015
  await fastify.listen({ port: 0 })
1982
2016
 
1983
2017
  const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
1984
- t.equal(response.statusCode, 500)
1985
- t.same(JSON.parse(body), {
2018
+ t.assert.strictEqual(response.statusCode, 500)
2019
+ t.assert.deepStrictEqual(JSON.parse(body), {
1986
2020
  statusCode: 500,
1987
2021
  code: 'ERR_INVALID_CHAR',
1988
2022
  error: 'Internal Server Error',
@@ -1999,7 +2033,7 @@ test('invalid response headers and custom error handler', async t => {
1999
2033
  url: '/bad-headers',
2000
2034
  handler: (req, reply) => {
2001
2035
  reply.log.warn = function mockWarn (obj, message) {
2002
- t.equal(message, 'Invalid character in header content ["smile"]', 'only the first invalid header is logged')
2036
+ t.assert.strictEqual(message, 'Invalid character in header content ["smile"]', 'only the first invalid header is logged')
2003
2037
  }
2004
2038
 
2005
2039
  reply.header('smile', '😄')
@@ -2008,15 +2042,15 @@ test('invalid response headers and custom error handler', async t => {
2008
2042
  })
2009
2043
 
2010
2044
  fastify.setErrorHandler(function (error, request, reply) {
2011
- t.equal(error.message, 'user land error', 'custom error handler receives the error')
2045
+ t.assert.strictEqual(error.message, 'user land error', 'custom error handler receives the error')
2012
2046
  reply.status(500).send({ ops: true })
2013
2047
  })
2014
2048
 
2015
2049
  await fastify.listen({ port: 0 })
2016
2050
 
2017
2051
  const { response, body } = await doGet(`http://127.0.0.1:${fastify.server.address().port}/bad-headers`)
2018
- t.equal(response.statusCode, 500)
2019
- t.same(JSON.parse(body), {
2052
+ t.assert.strictEqual(response.statusCode, 500)
2053
+ t.assert.deepStrictEqual(JSON.parse(body), {
2020
2054
  statusCode: 500,
2021
2055
  code: 'ERR_INVALID_CHAR',
2022
2056
  error: 'Internal Server Error',
@@ -2045,7 +2079,7 @@ test('reply.send will intercept ERR_HTTP_HEADERS_SENT and log an error message',
2045
2079
 
2046
2080
  const log = {
2047
2081
  warn: (msg) => {
2048
- t.equal(msg, 'Reply was already sent, did you forget to "return reply" in the "/hello" (GET) route?')
2082
+ t.assert.strictEqual(msg, 'Reply was already sent, did you forget to "return reply" in the "/hello" (GET) route?')
2049
2083
  }
2050
2084
  }
2051
2085
 
@@ -2054,11 +2088,11 @@ test('reply.send will intercept ERR_HTTP_HEADERS_SENT and log an error message',
2054
2088
  try {
2055
2089
  reply.send('')
2056
2090
  } catch (err) {
2057
- t.equal(err.code, 'ERR_HTTP_HEADERS_SENT')
2091
+ t.assert.strictEqual(err.code, 'ERR_HTTP_HEADERS_SENT')
2058
2092
  }
2059
2093
  })
2060
2094
 
2061
- test('Uint8Array view of ArrayBuffer returns correct byteLength', t => {
2095
+ test('Uint8Array view of ArrayBuffer returns correct byteLength', (t, done) => {
2062
2096
  t.plan(5)
2063
2097
  const fastify = Fastify()
2064
2098
 
@@ -2069,17 +2103,18 @@ test('Uint8Array view of ArrayBuffer returns correct byteLength', t => {
2069
2103
  })
2070
2104
 
2071
2105
  fastify.listen({ port: 0 }, err => {
2072
- t.error(err)
2073
- t.teardown(fastify.close.bind(fastify))
2106
+ t.assert.ifError(err)
2107
+ t.after(() => fastify.close())
2074
2108
 
2075
2109
  fastify.inject({
2076
2110
  method: 'GET',
2077
2111
  url: '/'
2078
2112
  }, (err, response) => {
2079
- t.error(err)
2080
- t.equal(response.headers['content-type'], 'application/octet-stream')
2081
- t.equal(response.headers['content-length'], '10')
2082
- t.same(response.rawPayload.byteLength, arrView.byteLength)
2113
+ t.assert.ifError(err)
2114
+ t.assert.strictEqual(response.headers['content-type'], 'application/octet-stream')
2115
+ t.assert.strictEqual(response.headers['content-length'], '10')
2116
+ t.assert.deepStrictEqual(response.rawPayload.byteLength, arrView.byteLength)
2117
+ done()
2083
2118
  })
2084
2119
  })
2085
2120
  })