fastify 5.0.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/README.md +12 -7
  4. package/docs/Guides/Database.md +15 -15
  5. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  6. package/docs/Guides/Ecosystem.md +14 -15
  7. package/docs/Guides/Index.md +1 -1
  8. package/docs/Guides/Migration-Guide-V4.md +11 -11
  9. package/docs/Guides/Migration-Guide-V5.md +133 -9
  10. package/docs/Guides/Plugins-Guide.md +1 -1
  11. package/docs/Guides/Prototype-Poisoning.md +3 -3
  12. package/docs/Guides/Recommendations.md +9 -9
  13. package/docs/Guides/Serverless.md +5 -5
  14. package/docs/Guides/Testing.md +58 -57
  15. package/docs/Guides/Write-Plugin.md +2 -2
  16. package/docs/Guides/Write-Type-Provider.md +3 -3
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Decorators.md +2 -2
  19. package/docs/Reference/Errors.md +3 -3
  20. package/docs/Reference/Hooks.md +7 -7
  21. package/docs/Reference/LTS.md +8 -0
  22. package/docs/Reference/Logging.md +5 -4
  23. package/docs/Reference/Reply.md +55 -58
  24. package/docs/Reference/Request.md +49 -42
  25. package/docs/Reference/Routes.md +16 -13
  26. package/docs/Reference/Server.md +32 -28
  27. package/docs/Reference/TypeScript.md +9 -9
  28. package/docs/Reference/Validation-and-Serialization.md +5 -5
  29. package/examples/typescript-server.ts +1 -1
  30. package/fastify.d.ts +14 -5
  31. package/fastify.js +8 -6
  32. package/lib/contentTypeParser.js +9 -7
  33. package/lib/context.js +1 -2
  34. package/lib/error-handler.js +9 -9
  35. package/lib/errors.js +1 -1
  36. package/lib/fourOhFour.js +1 -1
  37. package/lib/hooks.js +4 -1
  38. package/lib/{logger.js → logger-factory.js} +70 -122
  39. package/lib/logger-pino.js +68 -0
  40. package/lib/pluginOverride.js +1 -1
  41. package/lib/pluginUtils.js +2 -2
  42. package/lib/reply.js +4 -5
  43. package/lib/request.js +16 -9
  44. package/lib/route.js +23 -22
  45. package/lib/validation.js +2 -2
  46. package/package.json +13 -15
  47. package/test/404s.test.js +675 -629
  48. package/test/500s.test.js +72 -63
  49. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
  50. package/test/als.test.js +48 -45
  51. package/test/async-await.test.js +148 -134
  52. package/test/async-dispose.test.js +4 -5
  53. package/test/async_hooks.test.js +30 -28
  54. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  55. package/test/buffer.test.js +9 -10
  56. package/test/build/error-serializer.test.js +3 -4
  57. package/test/build/version.test.js +2 -3
  58. package/test/build-certificate.js +1 -1
  59. package/test/bundler/README.md +5 -5
  60. package/test/bundler/esbuild/bundler-test.js +10 -9
  61. package/test/bundler/webpack/bundler-test.js +10 -9
  62. package/test/case-insensitive.test.js +31 -28
  63. package/test/chainable.test.js +4 -5
  64. package/test/check.test.js +8 -10
  65. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
  66. package/test/client-timeout.test.js +5 -5
  67. package/test/close-pipelining.test.js +6 -8
  68. package/test/conditional-pino.test.js +47 -0
  69. package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
  70. package/test/constrained-routes.test.js +243 -236
  71. package/test/content-length.test.js +53 -68
  72. package/test/content-parser.test.js +186 -158
  73. package/test/content-type.test.js +8 -9
  74. package/test/context-config.test.js +44 -54
  75. package/test/custom-http-server.test.js +16 -20
  76. package/test/custom-parser.5.test.js +32 -32
  77. package/test/diagnostics-channel/404.test.js +15 -15
  78. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  79. package/test/diagnostics-channel/async-request.test.js +24 -24
  80. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  81. package/test/diagnostics-channel/error-request.test.js +19 -19
  82. package/test/diagnostics-channel/error-status.test.js +8 -8
  83. package/test/diagnostics-channel/init.test.js +6 -7
  84. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  85. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  86. package/test/diagnostics-channel/sync-request.test.js +19 -19
  87. package/test/encapsulated-child-logger-factory.test.js +8 -8
  88. package/test/encapsulated-error-handler.test.js +20 -20
  89. package/test/esm/errorCodes.test.mjs +5 -5
  90. package/test/esm/esm.test.mjs +3 -3
  91. package/test/esm/named-exports.mjs +3 -3
  92. package/test/esm/other.mjs +2 -2
  93. package/test/fastify-instance.test.js +33 -34
  94. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  95. package/test/fluent-schema.test.js +33 -36
  96. package/test/handler-context.test.js +11 -11
  97. package/test/has-route.test.js +12 -15
  98. package/test/header-overflow.test.js +13 -12
  99. package/test/hooks.on-ready.test.js +2 -2
  100. package/test/hooks.test.js +25 -25
  101. package/test/http-methods/copy.test.js +22 -24
  102. package/test/http-methods/custom-http-methods.test.js +24 -21
  103. package/test/http-methods/get.test.js +97 -84
  104. package/test/http-methods/head.test.js +63 -57
  105. package/test/http-methods/lock.test.js +21 -20
  106. package/test/http-methods/mkcalendar.test.js +31 -27
  107. package/test/http-methods/mkcol.test.js +10 -10
  108. package/test/http-methods/move.test.js +11 -11
  109. package/test/http-methods/propfind.test.js +32 -27
  110. package/test/http-methods/proppatch.test.js +21 -19
  111. package/test/http-methods/report.test.js +32 -27
  112. package/test/http-methods/search.test.js +52 -47
  113. package/test/http-methods/trace.test.js +3 -4
  114. package/test/http-methods/unlock.test.js +10 -10
  115. package/test/http2/closing.test.js +50 -58
  116. package/test/http2/constraint.test.js +47 -50
  117. package/test/http2/head.test.js +18 -19
  118. package/test/http2/missing-http2-module.test.js +4 -5
  119. package/test/http2/plain.test.js +31 -31
  120. package/test/http2/secure-with-fallback.test.js +61 -61
  121. package/test/http2/secure.test.js +28 -31
  122. package/test/http2/unknown-http-method.test.js +13 -14
  123. package/test/https/custom-https-server.test.js +6 -7
  124. package/test/https/https.test.js +78 -78
  125. package/test/imports.test.js +5 -6
  126. package/test/internals/all.test.js +8 -11
  127. package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
  128. package/test/internals/context.test.js +9 -11
  129. package/test/internals/decorator.test.js +20 -21
  130. package/test/internals/errors.test.js +427 -427
  131. package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
  132. package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
  133. package/test/internals/hooks.test.js +31 -35
  134. package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
  135. package/test/internals/logger.test.js +28 -28
  136. package/test/internals/plugin.test.js +17 -18
  137. package/test/internals/reply-serialize.test.js +106 -106
  138. package/test/internals/reply.test.js +620 -585
  139. package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
  140. package/test/internals/request-validate.test.js +218 -221
  141. package/test/internals/request.test.js +225 -107
  142. package/test/internals/server.test.js +15 -12
  143. package/test/internals/validation.test.js +35 -36
  144. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  145. package/test/listen.5.test.js +9 -9
  146. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  147. package/test/middleware.test.js +4 -5
  148. package/test/noop-set.test.js +5 -5
  149. package/test/post-empty-body.test.js +18 -11
  150. package/test/pretty-print.test.js +59 -49
  151. package/test/proto-poisoning.test.js +42 -37
  152. package/test/reply-code.test.js +34 -32
  153. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  154. package/test/request-error.test.js +122 -0
  155. package/test/request-header-host.test.js +339 -0
  156. package/test/request-id.test.js +31 -25
  157. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  158. package/test/route.1.test.js +79 -72
  159. package/test/route.2.test.js +17 -16
  160. package/test/route.3.test.js +32 -27
  161. package/test/route.4.test.js +21 -25
  162. package/test/route.5.test.js +45 -64
  163. package/test/route.6.test.js +70 -89
  164. package/test/route.7.test.js +61 -65
  165. package/test/route.8.test.js +80 -18
  166. package/test/router-options.test.js +80 -77
  167. package/test/same-shape.test.js +5 -5
  168. package/test/schema-examples.test.js +72 -38
  169. package/test/serialize-response.test.js +9 -10
  170. package/test/server.test.js +75 -78
  171. package/test/set-error-handler.test.js +2 -3
  172. package/test/stream-serializers.test.js +10 -7
  173. package/test/sync-routes.test.js +18 -18
  174. package/test/test-reporter.mjs +68 -0
  175. package/test/trust-proxy.test.js +51 -45
  176. package/test/type-provider.test.js +8 -6
  177. package/test/types/content-type-parser.test-d.ts +1 -1
  178. package/test/types/fastify.test-d.ts +16 -4
  179. package/test/types/hooks.test-d.ts +2 -1
  180. package/test/types/instance.test-d.ts +13 -13
  181. package/test/types/logger.test-d.ts +2 -2
  182. package/test/types/plugin.test-d.ts +17 -9
  183. package/test/types/register.test-d.ts +22 -6
  184. package/test/types/reply.test-d.ts +1 -1
  185. package/test/types/route.test-d.ts +34 -4
  186. package/test/types/serverFactory.test-d.ts +1 -1
  187. package/test/types/type-provider.test-d.ts +1 -1
  188. package/test/url-rewriting.test.js +35 -38
  189. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  190. package/test/validation-error-handling.test.js +259 -285
  191. package/test/versioned-routes.test.js +126 -113
  192. package/test/web-api.test.js +48 -37
  193. package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
  194. package/types/hooks.d.ts +2 -1
  195. package/types/instance.d.ts +9 -2
  196. package/types/register.d.ts +12 -3
  197. package/types/reply.d.ts +1 -1
  198. package/types/request.d.ts +2 -6
  199. package/types/serverFactory.d.ts +3 -3
  200. package/types/utils.d.ts +13 -5
  201. package/test/types/import.js +0 -2
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('../..')
5
5
  const http2 = require('node:http2')
6
6
  const { promisify } = require('node:util')
@@ -9,102 +9,94 @@ const { once } = require('node:events')
9
9
  const { buildCertificate } = require('../build-certificate')
10
10
  const { getServerUrl } = require('../helper')
11
11
 
12
- t.before(buildCertificate)
12
+ test.before(buildCertificate)
13
13
 
14
- t.test('http/2 request while fastify closing', t => {
14
+ test('http/2 request while fastify closing', (t, done) => {
15
15
  let fastify
16
16
  try {
17
17
  fastify = Fastify({
18
18
  http2: true
19
19
  })
20
- t.pass('http2 successfully loaded')
20
+ t.assert.ok('http2 successfully loaded')
21
21
  } catch (e) {
22
- t.fail('http2 loading failed', e)
22
+ t.assert.fail('http2 loading failed')
23
23
  }
24
24
 
25
25
  fastify.get('/', () => Promise.resolve({}))
26
26
 
27
+ t.after(() => { fastify.close() })
27
28
  fastify.listen({ port: 0 }, err => {
28
- t.error(err)
29
- t.teardown(() => { fastify.close() })
30
-
31
- t.test('return 200', t => {
32
- const url = getServerUrl(fastify)
33
- const session = http2.connect(url, function () {
34
- this.request({
35
- ':method': 'GET',
36
- ':path': '/'
37
- }).on('response', headers => {
38
- t.equal(headers[':status'], 503)
39
- t.end()
40
- this.destroy()
41
- }).on('error', () => {
42
- // Nothing to do here,
43
- // we are not interested in this error that might
44
- // happen or not
45
- })
46
- fastify.close()
29
+ t.assert.ifError(err)
30
+
31
+ const url = getServerUrl(fastify)
32
+ const session = http2.connect(url, function () {
33
+ this.request({
34
+ ':method': 'GET',
35
+ ':path': '/'
36
+ }).on('response', headers => {
37
+ t.assert.strictEqual(headers[':status'], 503)
38
+ done()
39
+ this.destroy()
40
+ }).on('error', () => {
41
+ // Nothing to do here,
42
+ // we are not interested in this error that might
43
+ // happen or not
47
44
  })
48
45
  session.on('error', () => {
49
46
  // Nothing to do here,
50
47
  // we are not interested in this error that might
51
48
  // happen or not
52
- t.end()
49
+ done()
53
50
  })
51
+ fastify.close()
54
52
  })
55
-
56
- t.end()
57
53
  })
58
54
  })
59
55
 
60
- t.test('http/2 request while fastify closing - return503OnClosing: false', t => {
56
+ test('http/2 request while fastify closing - return503OnClosing: false', (t, done) => {
61
57
  let fastify
62
58
  try {
63
59
  fastify = Fastify({
64
60
  http2: true,
65
61
  return503OnClosing: false
66
62
  })
67
- t.pass('http2 successfully loaded')
63
+ t.assert.ok('http2 successfully loaded')
68
64
  } catch (e) {
69
- t.fail('http2 loading failed', e)
65
+ t.assert.fail('http2 loading failed')
70
66
  }
71
67
 
68
+ t.after(() => { fastify.close() })
69
+
72
70
  fastify.get('/', () => Promise.resolve({}))
73
71
 
74
72
  fastify.listen({ port: 0 }, err => {
75
- t.error(err)
76
- t.teardown(() => { fastify.close() })
77
-
78
- t.test('return 200', t => {
79
- const url = getServerUrl(fastify)
80
- const session = http2.connect(url, function () {
81
- this.request({
82
- ':method': 'GET',
83
- ':path': '/'
84
- }).on('response', headers => {
85
- t.equal(headers[':status'], 200)
86
- t.end()
87
- this.destroy()
88
- }).on('error', () => {
89
- // Nothing to do here,
90
- // we are not interested in this error that might
91
- // happen or not
92
- })
93
- fastify.close()
94
- })
95
- session.on('error', () => {
73
+ t.assert.ifError(err)
74
+ const url = getServerUrl(fastify)
75
+ const session = http2.connect(url, function () {
76
+ this.request({
77
+ ':method': 'GET',
78
+ ':path': '/'
79
+ }).on('response', headers => {
80
+ t.assert.strictEqual(headers[':status'], 200)
81
+ done()
82
+ this.destroy()
83
+ }).on('error', () => {
96
84
  // Nothing to do here,
97
85
  // we are not interested in this error that might
98
86
  // happen or not
99
- t.end()
100
87
  })
88
+ fastify.close()
89
+ })
90
+ session.on('error', () => {
91
+ // Nothing to do here,
92
+ // we are not interested in this error that might
93
+ // happen or not
94
+ done()
101
95
  })
102
-
103
- t.end()
104
96
  })
105
97
  })
106
98
 
107
- t.test('http/2 closes successfully with async await', async t => {
99
+ test('http/2 closes successfully with async await', async t => {
108
100
  const fastify = Fastify({
109
101
  http2SessionTimeout: 100,
110
102
  http2: true
@@ -119,7 +111,7 @@ t.test('http/2 closes successfully with async await', async t => {
119
111
  await fastify.close()
120
112
  })
121
113
 
122
- t.test('https/2 closes successfully with async await', async t => {
114
+ test('https/2 closes successfully with async await', async t => {
123
115
  const fastify = Fastify({
124
116
  http2SessionTimeout: 100,
125
117
  http2: true,
@@ -138,7 +130,7 @@ t.test('https/2 closes successfully with async await', async t => {
138
130
  await fastify.close()
139
131
  })
140
132
 
141
- t.test('http/2 server side session emits a timeout event', async t => {
133
+ test('http/2 server side session emits a timeout event', async t => {
142
134
  let _resolve
143
135
  const p = new Promise((resolve) => { _resolve = resolve })
144
136
 
@@ -162,7 +154,7 @@ t.test('http/2 server side session emits a timeout event', async t => {
162
154
  }).end()
163
155
 
164
156
  const [headers] = await once(req, 'response')
165
- t.equal(headers[':status'], 200)
157
+ t.assert.strictEqual(headers[':status'], 200)
166
158
  req.resume()
167
159
 
168
160
  // An error might or might not happen, as it's OS dependent.
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../..')
6
5
  const h2url = require('h2url')
7
6
 
@@ -9,10 +8,10 @@ const alpha = { res: 'alpha' }
9
8
  const beta = { res: 'beta' }
10
9
 
11
10
  const { buildCertificate } = require('../build-certificate')
12
- t.before(buildCertificate)
11
+ test.before(buildCertificate)
13
12
 
14
- test('A route supports host constraints under http2 protocol and secure connection', (t) => {
15
- t.plan(6)
13
+ test('A route supports host constraints under http2 protocol and secure connection', async (t) => {
14
+ t.plan(5)
16
15
 
17
16
  let fastify
18
17
  try {
@@ -23,9 +22,9 @@ test('A route supports host constraints under http2 protocol and secure connecti
23
22
  cert: global.context.cert
24
23
  }
25
24
  })
26
- t.pass('Key/cert successfully loaded')
25
+ t.assert.ok(true, 'Key/cert successfully loaded')
27
26
  } catch (e) {
28
- t.fail('Key/cert loading failed', e)
27
+ t.assert.fail('Key/cert loading failed')
29
28
  }
30
29
 
31
30
  const constrain = 'fastify.dev'
@@ -53,60 +52,58 @@ test('A route supports host constraints under http2 protocol and secure connecti
53
52
  reply.code(200).send({ ...beta, hostname: req.hostname })
54
53
  }
55
54
  })
55
+ t.after(() => { fastify.close() })
56
56
 
57
- fastify.listen({ port: 0 }, err => {
58
- t.error(err)
59
- t.teardown(() => { fastify.close() })
57
+ await fastify.listen({ port: 0 })
60
58
 
61
- t.test('https get request - no constrain', async (t) => {
62
- t.plan(3)
59
+ await t.test('https get request - no constrain', async (t) => {
60
+ t.plan(3)
63
61
 
64
- const url = `https://localhost:${fastify.server.address().port}`
65
- const res = await h2url.concat({ url })
62
+ const url = `https://localhost:${fastify.server.address().port}`
63
+ const res = await h2url.concat({ url })
66
64
 
67
- t.equal(res.headers[':status'], 200)
68
- t.equal(res.headers['content-length'], '' + JSON.stringify(alpha).length)
69
- t.same(JSON.parse(res.body), alpha)
70
- })
71
-
72
- t.test('https get request - constrain', async (t) => {
73
- t.plan(3)
65
+ t.assert.strictEqual(res.headers[':status'], 200)
66
+ t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(alpha).length)
67
+ t.assert.deepStrictEqual(JSON.parse(res.body), alpha)
68
+ })
74
69
 
75
- const url = `https://localhost:${fastify.server.address().port}/beta`
76
- const res = await h2url.concat({
77
- url,
78
- headers: {
79
- ':authority': constrain
80
- }
81
- })
70
+ await t.test('https get request - constrain', async (t) => {
71
+ t.plan(3)
82
72
 
83
- t.equal(res.headers[':status'], 200)
84
- t.equal(res.headers['content-length'], '' + JSON.stringify(beta).length)
85
- t.same(JSON.parse(res.body), beta)
73
+ const url = `https://localhost:${fastify.server.address().port}/beta`
74
+ const res = await h2url.concat({
75
+ url,
76
+ headers: {
77
+ ':authority': constrain
78
+ }
86
79
  })
87
80
 
88
- t.test('https get request - constrain - not found', async (t) => {
89
- t.plan(1)
81
+ t.assert.strictEqual(res.headers[':status'], 200)
82
+ t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(beta).length)
83
+ t.assert.deepStrictEqual(JSON.parse(res.body), beta)
84
+ })
90
85
 
91
- const url = `https://localhost:${fastify.server.address().port}/beta`
92
- const res = await h2url.concat({
93
- url
94
- })
86
+ await t.test('https get request - constrain - not found', async (t) => {
87
+ t.plan(1)
95
88
 
96
- t.equal(res.headers[':status'], 404)
89
+ const url = `https://localhost:${fastify.server.address().port}/beta`
90
+ const res = await h2url.concat({
91
+ url
97
92
  })
98
- t.test('https get request - constrain - verify hostname and port from request', async (t) => {
99
- t.plan(1)
100
-
101
- const url = `https://localhost:${fastify.server.address().port}/hostname_port`
102
- const res = await h2url.concat({
103
- url,
104
- headers: {
105
- ':authority': constrain
106
- }
107
- })
108
- const body = JSON.parse(res.body)
109
- t.equal(body.hostname, constrain)
93
+
94
+ t.assert.strictEqual(res.headers[':status'], 404)
95
+ })
96
+ await t.test('https get request - constrain - verify hostname and port from request', async (t) => {
97
+ t.plan(1)
98
+
99
+ const url = `https://localhost:${fastify.server.address().port}/hostname_port`
100
+ const res = await h2url.concat({
101
+ url,
102
+ headers: {
103
+ ':authority': constrain
104
+ }
110
105
  })
106
+ const body = JSON.parse(res.body)
107
+ t.assert.strictEqual(body.hostname, constrain)
111
108
  })
112
109
  })
@@ -1,35 +1,34 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../..')
6
5
  const h2url = require('h2url')
7
6
  const msg = { hello: 'world' }
8
7
 
9
- let fastify
10
- try {
11
- fastify = Fastify({
12
- http2: true
13
- })
14
- t.pass('http2 successfully loaded')
15
- } catch (e) {
16
- t.fail('http2 loading failed', e)
17
- }
8
+ test('http2 HEAD test', async (t) => {
9
+ let fastify
10
+ try {
11
+ fastify = Fastify({
12
+ http2: true
13
+ })
14
+ t.assert.ok(true, 'http2 successfully loaded')
15
+ } catch (e) {
16
+ t.assert.fail('http2 loading failed')
17
+ }
18
18
 
19
- fastify.all('/', function (req, reply) {
20
- reply.code(200).send(msg)
21
- })
19
+ fastify.all('/', function (req, reply) {
20
+ reply.code(200).send(msg)
21
+ })
22
+ t.after(() => { fastify.close() })
22
23
 
23
- fastify.listen({ port: 0 }, err => {
24
- t.error(err)
25
- t.teardown(() => { fastify.close() })
24
+ await fastify.listen({ port: 0 })
26
25
 
27
- test('http HEAD request', async (t) => {
26
+ await t.test('http HEAD request', async (t) => {
28
27
  t.plan(1)
29
28
 
30
29
  const url = `http://localhost:${fastify.server.address().port}`
31
30
  const res = await h2url.concat({ url, method: 'HEAD' })
32
31
 
33
- t.equal(res.headers[':status'], 200)
32
+ t.assert.strictEqual(res.headers[':status'], 200)
34
33
  })
35
34
  })
@@ -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 proxyquire = require('proxyquire')
6
5
  const server = proxyquire('../../lib/server', { 'node:http2': null })
7
6
  const Fastify = proxyquire('../..', { './lib/server.js': server })
@@ -10,9 +9,9 @@ test('should throw when http2 module cannot be found', t => {
10
9
  t.plan(2)
11
10
  try {
12
11
  Fastify({ http2: true })
13
- t.fail('fastify did not throw expected error')
12
+ t.assert.fail('fastify did not throw expected error')
14
13
  } catch (err) {
15
- t.equal(err.code, 'FST_ERR_HTTP2_INVALID_VERSION')
16
- t.equal(err.message, 'HTTP2 is available only from node >= 8.8.1')
14
+ t.assert.strictEqual(err.code, 'FST_ERR_HTTP2_INVALID_VERSION')
15
+ t.assert.strictEqual(err.message, 'HTTP2 is available only from node >= 8.8.1')
17
16
  }
18
17
  })
@@ -1,50 +1,50 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../..')
6
5
  const h2url = require('h2url')
7
6
  const msg = { hello: 'world' }
8
7
 
9
- let fastify
10
- try {
11
- fastify = Fastify({
12
- http2: true
8
+ test('http2 plain test', async t => {
9
+ let fastify
10
+ try {
11
+ fastify = Fastify({
12
+ http2: true
13
+ })
14
+ t.assert.ok(true, 'http2 successfully loaded')
15
+ } catch (e) {
16
+ t.assert.fail('http2 loading failed')
17
+ }
18
+
19
+ fastify.get('/', function (req, reply) {
20
+ reply.code(200).send(msg)
13
21
  })
14
- t.pass('http2 successfully loaded')
15
- } catch (e) {
16
- t.fail('http2 loading failed', e)
17
- }
18
22
 
19
- fastify.get('/', function (req, reply) {
20
- reply.code(200).send(msg)
21
- })
23
+ fastify.get('/host', function (req, reply) {
24
+ reply.code(200).send(req.host)
25
+ })
22
26
 
23
- fastify.get('/host', function (req, reply) {
24
- reply.code(200).send(req.host)
25
- })
27
+ fastify.get('/hostname_port', function (req, reply) {
28
+ reply.code(200).send({ hostname: req.hostname, port: req.port })
29
+ })
26
30
 
27
- fastify.get('/hostname_port', function (req, reply) {
28
- reply.code(200).send({ hostname: req.hostname, port: req.port })
29
- })
31
+ t.after(() => { fastify.close() })
30
32
 
31
- fastify.listen({ port: 0 }, err => {
32
- t.error(err)
33
- t.teardown(() => { fastify.close() })
33
+ await fastify.listen({ port: 0 })
34
34
 
35
- test('http get request', async (t) => {
35
+ await t.test('http get request', async (t) => {
36
36
  t.plan(3)
37
37
 
38
38
  const url = `http://localhost:${fastify.server.address().port}`
39
39
  const res = await h2url.concat({ url })
40
40
 
41
- t.equal(res.headers[':status'], 200)
42
- t.equal(res.headers['content-length'], '' + JSON.stringify(msg).length)
41
+ t.assert.strictEqual(res.headers[':status'], 200)
42
+ t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
43
43
 
44
- t.same(JSON.parse(res.body), msg)
44
+ t.assert.deepStrictEqual(JSON.parse(res.body), msg)
45
45
  })
46
46
 
47
- test('http host', async (t) => {
47
+ await t.test('http host', async (t) => {
48
48
  t.plan(1)
49
49
 
50
50
  const host = `localhost:${fastify.server.address().port}`
@@ -52,9 +52,9 @@ fastify.listen({ port: 0 }, err => {
52
52
  const url = `http://${host}/host`
53
53
  const res = await h2url.concat({ url })
54
54
 
55
- t.equal(res.body, host)
55
+ t.assert.strictEqual(res.body, host)
56
56
  })
57
- test('http hostname and port', async (t) => {
57
+ await t.test('http hostname and port', async (t) => {
58
58
  t.plan(2)
59
59
 
60
60
  const host = `localhost:${fastify.server.address().port}`
@@ -62,7 +62,7 @@ fastify.listen({ port: 0 }, err => {
62
62
  const url = `http://${host}/hostname_port`
63
63
  const res = await h2url.concat({ url })
64
64
 
65
- t.equal(JSON.parse(res.body).hostname, host.split(':')[0])
66
- t.equal(JSON.parse(res.body).port, parseInt(host.split(':')[1]))
65
+ t.assert.strictEqual(JSON.parse(res.body).hostname, host.split(':')[0])
66
+ t.assert.strictEqual(JSON.parse(res.body).port, parseInt(host.split(':')[1]))
67
67
  })
68
68
  })
@@ -1,17 +1,16 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const Fastify = require('../..')
6
5
  const h2url = require('h2url')
7
6
  const sget = require('simple-get').concat
8
7
  const msg = { hello: 'world' }
9
8
 
10
9
  const { buildCertificate } = require('../build-certificate')
11
- t.before(buildCertificate)
10
+ test.before(buildCertificate)
12
11
 
13
- test('secure with fallback', (t) => {
14
- t.plan(7)
12
+ test('secure with fallback', async (t) => {
13
+ t.plan(6)
15
14
 
16
15
  let fastify
17
16
  try {
@@ -23,9 +22,9 @@ test('secure with fallback', (t) => {
23
22
  cert: global.context.cert
24
23
  }
25
24
  })
26
- t.pass('Key/cert successfully loaded')
25
+ t.assert.ok(true, 'Key/cert successfully loaded')
27
26
  } catch (e) {
28
- t.fail('Key/cert loading failed', e)
27
+ t.assert.fail('Key/cert loading failed')
29
28
  }
30
29
 
31
30
  fastify.get('/', function (req, reply) {
@@ -40,71 +39,72 @@ test('secure with fallback', (t) => {
40
39
  throw new Error('kaboom')
41
40
  })
42
41
 
43
- fastify.listen({ port: 0 }, err => {
44
- t.error(err)
45
- t.teardown(() => { fastify.close() })
42
+ t.after(() => { fastify.close() })
46
43
 
47
- t.test('https get error', async (t) => {
48
- t.plan(1)
44
+ await fastify.listen({ port: 0 })
49
45
 
50
- const url = `https://localhost:${fastify.server.address().port}/error`
51
- const res = await h2url.concat({ url })
46
+ await t.test('https get error', async (t) => {
47
+ t.plan(1)
52
48
 
53
- t.equal(res.headers[':status'], 500)
54
- })
49
+ const url = `https://localhost:${fastify.server.address().port}/error`
50
+ const res = await h2url.concat({ url })
51
+
52
+ t.assert.strictEqual(res.headers[':status'], 500)
53
+ })
55
54
 
56
- t.test('https post', async (t) => {
57
- t.plan(2)
58
-
59
- const url = `https://localhost:${fastify.server.address().port}`
60
- const res = await h2url.concat({
61
- url,
62
- method: 'POST',
63
- body: JSON.stringify({ hello: 'http2' }),
64
- headers: {
65
- 'content-type': 'application/json'
66
- }
67
- })
68
-
69
- t.equal(res.headers[':status'], 200)
70
- t.same(JSON.parse(res.body), { hello: 'http2' })
55
+ await t.test('https post', async (t) => {
56
+ t.plan(2)
57
+
58
+ const url = `https://localhost:${fastify.server.address().port}`
59
+ const res = await h2url.concat({
60
+ url,
61
+ method: 'POST',
62
+ body: JSON.stringify({ hello: 'http2' }),
63
+ headers: {
64
+ 'content-type': 'application/json'
65
+ }
71
66
  })
72
67
 
73
- t.test('https get request', async (t) => {
74
- t.plan(3)
68
+ t.assert.strictEqual(res.headers[':status'], 200)
69
+ t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'http2' })
70
+ })
75
71
 
76
- const url = `https://localhost:${fastify.server.address().port}`
77
- const res = await h2url.concat({ url })
72
+ await t.test('https get request', async (t) => {
73
+ t.plan(3)
78
74
 
79
- t.equal(res.headers[':status'], 200)
80
- t.equal(res.headers['content-length'], '' + JSON.stringify(msg).length)
81
- t.same(JSON.parse(res.body), msg)
82
- })
75
+ const url = `https://localhost:${fastify.server.address().port}`
76
+ const res = await h2url.concat({ url })
83
77
 
84
- t.test('http1 get request', t => {
85
- t.plan(4)
86
- sget({
87
- method: 'GET',
88
- url: 'https://localhost:' + fastify.server.address().port,
89
- rejectUnauthorized: false
90
- }, (err, response, body) => {
91
- t.error(err)
92
- t.equal(response.statusCode, 200)
93
- t.equal(response.headers['content-length'], '' + body.length)
94
- t.same(JSON.parse(body), { hello: 'world' })
95
- })
78
+ t.assert.strictEqual(res.headers[':status'], 200)
79
+ t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
80
+ t.assert.deepStrictEqual(JSON.parse(res.body), msg)
81
+ })
82
+
83
+ await t.test('http1 get request', (t, done) => {
84
+ t.plan(4)
85
+ sget({
86
+ method: 'GET',
87
+ url: 'https://localhost:' + fastify.server.address().port,
88
+ rejectUnauthorized: false
89
+ }, (err, response, body) => {
90
+ t.assert.ifError(err)
91
+ t.assert.strictEqual(response.statusCode, 200)
92
+ t.assert.strictEqual(response.headers['content-length'], '' + body.length)
93
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
94
+ done()
96
95
  })
96
+ })
97
97
 
98
- t.test('http1 get error', (t) => {
99
- t.plan(2)
100
- sget({
101
- method: 'GET',
102
- url: 'https://localhost:' + fastify.server.address().port + '/error',
103
- rejectUnauthorized: false
104
- }, (err, response, body) => {
105
- t.error(err)
106
- t.equal(response.statusCode, 500)
107
- })
98
+ await t.test('http1 get error', (t, done) => {
99
+ t.plan(2)
100
+ sget({
101
+ method: 'GET',
102
+ url: 'https://localhost:' + fastify.server.address().port + '/error',
103
+ rejectUnauthorized: false
104
+ }, (err, response, body) => {
105
+ t.assert.ifError(err)
106
+ t.assert.strictEqual(response.statusCode, 500)
107
+ done()
108
108
  })
109
109
  })
110
110
  })