fastify 5.3.3 → 5.5.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 (137) hide show
  1. package/.vscode/settings.json +15 -15
  2. package/LICENSE +1 -1
  3. package/README.md +2 -0
  4. package/SECURITY.md +158 -2
  5. package/build/build-validation.js +20 -1
  6. package/docs/Guides/Delay-Accepting-Requests.md +8 -5
  7. package/docs/Guides/Ecosystem.md +20 -5
  8. package/docs/Guides/Migration-Guide-V5.md +6 -10
  9. package/docs/Guides/Recommendations.md +1 -1
  10. package/docs/Reference/ContentTypeParser.md +1 -1
  11. package/docs/Reference/Errors.md +5 -3
  12. package/docs/Reference/Hooks.md +16 -20
  13. package/docs/Reference/Lifecycle.md +2 -2
  14. package/docs/Reference/Logging.md +3 -3
  15. package/docs/Reference/Middleware.md +1 -1
  16. package/docs/Reference/Reply.md +8 -8
  17. package/docs/Reference/Request.md +2 -2
  18. package/docs/Reference/Routes.md +7 -6
  19. package/docs/Reference/Server.md +341 -200
  20. package/docs/Reference/TypeScript.md +1 -3
  21. package/docs/Reference/Validation-and-Serialization.md +56 -4
  22. package/docs/Reference/Warnings.md +2 -1
  23. package/fastify.d.ts +4 -3
  24. package/fastify.js +47 -34
  25. package/lib/configValidator.js +196 -28
  26. package/lib/contentTypeParser.js +41 -48
  27. package/lib/error-handler.js +3 -3
  28. package/lib/errors.js +11 -0
  29. package/lib/handleRequest.js +13 -17
  30. package/lib/pluginOverride.js +3 -1
  31. package/lib/promise.js +23 -0
  32. package/lib/reply.js +24 -30
  33. package/lib/request.js +3 -10
  34. package/lib/route.js +37 -3
  35. package/lib/server.js +36 -35
  36. package/lib/symbols.js +1 -0
  37. package/lib/warnings.js +19 -1
  38. package/package.json +14 -10
  39. package/test/404s.test.js +226 -325
  40. package/test/allow-unsafe-regex.test.js +19 -48
  41. package/test/als.test.js +28 -40
  42. package/test/async-await.test.js +84 -128
  43. package/test/async_hooks.test.js +18 -37
  44. package/test/body-limit.test.js +90 -63
  45. package/test/buffer.test.js +22 -0
  46. package/test/build-certificate.js +1 -1
  47. package/test/case-insensitive.test.js +44 -65
  48. package/test/check.test.js +17 -21
  49. package/test/close-pipelining.test.js +24 -15
  50. package/test/constrained-routes.test.js +231 -0
  51. package/test/custom-http-server.test.js +7 -15
  52. package/test/custom-parser-async.test.js +17 -22
  53. package/test/custom-parser.0.test.js +267 -348
  54. package/test/custom-parser.1.test.js +141 -191
  55. package/test/custom-parser.2.test.js +34 -44
  56. package/test/custom-parser.3.test.js +56 -104
  57. package/test/custom-parser.4.test.js +106 -144
  58. package/test/custom-parser.5.test.js +56 -75
  59. package/test/custom-querystring-parser.test.js +51 -77
  60. package/test/decorator-namespace.test._js_ +3 -4
  61. package/test/decorator.test.js +76 -259
  62. package/test/delete.test.js +101 -110
  63. package/test/diagnostics-channel/404.test.js +7 -15
  64. package/test/diagnostics-channel/async-delay-request.test.js +7 -16
  65. package/test/diagnostics-channel/async-request.test.js +8 -16
  66. package/test/diagnostics-channel/error-request.test.js +7 -15
  67. package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
  68. package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
  69. package/test/diagnostics-channel/sync-request.test.js +9 -16
  70. package/test/fastify-instance.test.js +1 -1
  71. package/test/header-overflow.test.js +18 -29
  72. package/test/helper.js +139 -135
  73. package/test/hooks-async.test.js +259 -235
  74. package/test/hooks.test.js +951 -996
  75. package/test/http-methods/copy.test.js +14 -19
  76. package/test/http-methods/get.test.js +131 -143
  77. package/test/http-methods/head.test.js +53 -84
  78. package/test/http-methods/lock.test.js +31 -31
  79. package/test/http-methods/mkcalendar.test.js +45 -72
  80. package/test/http-methods/mkcol.test.js +5 -9
  81. package/test/http-methods/move.test.js +6 -10
  82. package/test/http-methods/propfind.test.js +34 -44
  83. package/test/http-methods/proppatch.test.js +23 -29
  84. package/test/http-methods/report.test.js +44 -69
  85. package/test/http-methods/search.test.js +67 -82
  86. package/test/http-methods/unlock.test.js +5 -9
  87. package/test/http2/closing.test.js +38 -20
  88. package/test/http2/secure-with-fallback.test.js +31 -28
  89. package/test/https/custom-https-server.test.js +9 -13
  90. package/test/https/https.test.js +56 -53
  91. package/test/input-validation.js +139 -150
  92. package/test/internals/errors.test.js +50 -1
  93. package/test/internals/handle-request.test.js +72 -65
  94. package/test/internals/promise.test.js +63 -0
  95. package/test/internals/reply.test.js +277 -496
  96. package/test/issue-4959.test.js +12 -3
  97. package/test/listen.4.test.js +31 -43
  98. package/test/nullable-validation.test.js +33 -46
  99. package/test/output-validation.test.js +24 -26
  100. package/test/plugin.1.test.js +40 -68
  101. package/test/plugin.2.test.js +108 -120
  102. package/test/plugin.3.test.js +50 -72
  103. package/test/plugin.4.test.js +124 -119
  104. package/test/promises.test.js +42 -63
  105. package/test/proto-poisoning.test.js +78 -97
  106. package/test/register.test.js +8 -18
  107. package/test/request-error.test.js +57 -146
  108. package/test/request-id.test.js +30 -49
  109. package/test/route-hooks.test.js +117 -101
  110. package/test/route-prefix.test.js +194 -133
  111. package/test/route-shorthand.test.js +9 -27
  112. package/test/route.1.test.js +74 -131
  113. package/test/route.8.test.js +9 -17
  114. package/test/router-options.test.js +450 -0
  115. package/test/schema-serialization.test.js +177 -154
  116. package/test/schema-special-usage.test.js +165 -132
  117. package/test/schema-validation.test.js +254 -218
  118. package/test/server.test.js +143 -5
  119. package/test/set-error-handler.test.js +58 -1
  120. package/test/skip-reply-send.test.js +64 -69
  121. package/test/stream.1.test.js +33 -50
  122. package/test/stream.4.test.js +18 -28
  123. package/test/stream.5.test.js +11 -19
  124. package/test/trust-proxy.test.js +32 -58
  125. package/test/types/errors.test-d.ts +13 -1
  126. package/test/types/fastify.test-d.ts +3 -0
  127. package/test/types/request.test-d.ts +1 -0
  128. package/test/types/type-provider.test-d.ts +55 -0
  129. package/test/url-rewriting.test.js +45 -62
  130. package/test/use-semicolon-delimiter.test.js +117 -59
  131. package/test/versioned-routes.test.js +39 -56
  132. package/types/errors.d.ts +11 -1
  133. package/types/hooks.d.ts +1 -1
  134. package/types/instance.d.ts +1 -1
  135. package/types/reply.d.ts +2 -2
  136. package/types/request.d.ts +1 -0
  137. package/.taprc +0 -7
@@ -2,10 +2,9 @@
2
2
 
3
3
  const { test } = require('node:test')
4
4
  const Fastify = require('..')
5
- const sget = require('simple-get').concat
6
5
 
7
- test('allow unsafe regex', (t, done) => {
8
- t.plan(4)
6
+ test('allow unsafe regex', async t => {
7
+ t.plan(2)
9
8
 
10
9
  const fastify = Fastify({
11
10
  allowUnsafeRegex: false
@@ -16,25 +15,15 @@ test('allow unsafe regex', (t, done) => {
16
15
  reply.send({ foo: req.params.foo })
17
16
  })
18
17
 
19
- fastify.listen({ port: 0 }, err => {
20
- t.assert.ifError(err)
21
-
22
- sget({
23
- method: 'GET',
24
- url: 'http://localhost:' + fastify.server.address().port + '/1234'
25
- }, (err, response, body) => {
26
- t.assert.ifError(err)
27
- t.assert.strictEqual(response.statusCode, 200)
28
- t.assert.deepStrictEqual(JSON.parse(body), {
29
- foo: '1234'
30
- })
31
- done()
32
- })
33
- })
18
+ await fastify.listen({ port: 0 })
19
+
20
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
21
+ t.assert.strictEqual(result.status, 200)
22
+ t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
34
23
  })
35
24
 
36
- test('allow unsafe regex not match', (t, done) => {
37
- t.plan(3)
25
+ test('allow unsafe regex not match', async t => {
26
+ t.plan(1)
38
27
 
39
28
  const fastify = Fastify({
40
29
  allowUnsafeRegex: false
@@ -45,18 +34,10 @@ test('allow unsafe regex not match', (t, done) => {
45
34
  reply.send({ foo: req.params.foo })
46
35
  })
47
36
 
48
- fastify.listen({ port: 0 }, err => {
49
- t.assert.ifError(err)
37
+ await fastify.listen({ port: 0 })
50
38
 
51
- sget({
52
- method: 'GET',
53
- url: 'http://localhost:' + fastify.server.address().port + '/a1234'
54
- }, (err, response, body) => {
55
- t.assert.ifError(err)
56
- t.assert.strictEqual(response.statusCode, 404)
57
- done()
58
- })
59
- })
39
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/a1234`)
40
+ t.assert.strictEqual(result.status, 404)
60
41
  })
61
42
 
62
43
  test('allow unsafe regex not safe', (t, done) => {
@@ -89,8 +70,8 @@ test('allow unsafe regex not safe by default', (t, done) => {
89
70
  done()
90
71
  })
91
72
 
92
- test('allow unsafe regex allow unsafe', (t, done) => {
93
- t.plan(5)
73
+ test('allow unsafe regex allow unsafe', async t => {
74
+ t.plan(3)
94
75
 
95
76
  const fastify = Fastify({
96
77
  allowUnsafeRegex: true
@@ -103,19 +84,9 @@ test('allow unsafe regex allow unsafe', (t, done) => {
103
84
  })
104
85
  })
105
86
 
106
- fastify.listen({ port: 0 }, err => {
107
- t.assert.ifError(err)
108
-
109
- sget({
110
- method: 'GET',
111
- url: 'http://localhost:' + fastify.server.address().port + '/1234'
112
- }, (err, response, body) => {
113
- t.assert.ifError(err)
114
- t.assert.strictEqual(response.statusCode, 200)
115
- t.assert.deepStrictEqual(JSON.parse(body), {
116
- foo: '1234'
117
- })
118
- done()
119
- })
120
- })
87
+ await fastify.listen({ port: 0 })
88
+
89
+ const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
90
+ t.assert.strictEqual(result.status, 200)
91
+ t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
121
92
  })
package/test/als.test.js CHANGED
@@ -3,10 +3,9 @@
3
3
  const { AsyncLocalStorage } = require('node:async_hooks')
4
4
  const { test } = require('node:test')
5
5
  const Fastify = require('..')
6
- const sget = require('simple-get').concat
7
6
 
8
- test('Async Local Storage test', (t, done) => {
9
- t.plan(13)
7
+ test('Async Local Storage test', async (t) => {
8
+ t.plan(12)
10
9
  if (!AsyncLocalStorage) {
11
10
  t.skip('AsyncLocalStorage not available, skipping test')
12
11
  process.exit(0)
@@ -15,6 +14,8 @@ test('Async Local Storage test', (t, done) => {
15
14
  const storage = new AsyncLocalStorage()
16
15
  const app = Fastify({ logger: false })
17
16
 
17
+ t.after(() => app.close())
18
+
18
19
  let counter = 0
19
20
  app.addHook('onRequest', (req, reply, next) => {
20
21
  const id = counter++
@@ -33,45 +34,32 @@ test('Async Local Storage test', (t, done) => {
33
34
  reply.send({ id })
34
35
  })
35
36
 
36
- app.listen({ port: 0 }, function (err, address) {
37
- t.assert.ifError(err)
37
+ const fastifyServer = await app.listen({ port: 0 })
38
38
 
39
- sget({
40
- method: 'POST',
41
- url: 'http://localhost:' + app.server.address().port,
42
- body: {
43
- hello: 'world'
44
- },
45
- json: true
46
- }, (err, response, body) => {
47
- t.assert.ifError(err)
48
- t.assert.strictEqual(response.statusCode, 200)
49
- t.assert.deepStrictEqual(body, { id: 0 })
39
+ // First POST request
40
+ const result1 = await fetch(fastifyServer, {
41
+ method: 'POST',
42
+ headers: { 'Content-Type': 'application/json' },
43
+ body: JSON.stringify({ hello: 'world' })
44
+ })
45
+ t.assert.ok(result1.ok)
46
+ t.assert.strictEqual(result1.status, 200)
47
+ t.assert.deepStrictEqual(await result1.json(), { id: 0 })
50
48
 
51
- sget({
52
- method: 'POST',
53
- url: 'http://localhost:' + app.server.address().port,
54
- body: {
55
- hello: 'world'
56
- },
57
- json: true
58
- }, (err, response, body) => {
59
- t.assert.ifError(err)
60
- t.assert.strictEqual(response.statusCode, 200)
61
- t.assert.deepStrictEqual(body, { id: 1 })
49
+ const result2 = await fetch(fastifyServer, {
50
+ method: 'POST',
51
+ headers: { 'Content-Type': 'application/json' },
52
+ body: JSON.stringify({ hello: 'world' })
53
+ })
54
+ t.assert.ok(result2.ok)
55
+ t.assert.strictEqual(result2.status, 200)
56
+ t.assert.deepStrictEqual(await result2.json(), { id: 1 })
62
57
 
63
- sget({
64
- method: 'GET',
65
- url: 'http://localhost:' + app.server.address().port,
66
- json: true
67
- }, (err, response, body) => {
68
- t.assert.ifError(err)
69
- t.assert.strictEqual(response.statusCode, 200)
70
- t.assert.deepStrictEqual(body, { id: 2 })
71
- app.close()
72
- done()
73
- })
74
- })
75
- })
58
+ // GET request
59
+ const result3 = await fetch(fastifyServer, {
60
+ method: 'GET'
76
61
  })
62
+ t.assert.ok(result3.ok)
63
+ t.assert.strictEqual(result3.status, 200)
64
+ t.assert.deepStrictEqual(await result3.json(), { id: 2 })
77
65
  })
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('node:test')
4
- const sget = require('simple-get').concat
5
4
  const Fastify = require('..')
6
5
  const split = require('split2')
7
6
  const pino = require('pino')
@@ -43,8 +42,8 @@ const optsWithHostnameAndPort = {
43
42
  }
44
43
  }
45
44
  }
46
- test('async await', (t, done) => {
47
- t.plan(16)
45
+ test('async await', async t => {
46
+ t.plan(15)
48
47
  const fastify = Fastify()
49
48
  try {
50
49
  fastify.get('/', opts, async function awaitMyFunc (req, reply) {
@@ -75,46 +74,37 @@ test('async await', (t, done) => {
75
74
  t.assert.fail()
76
75
  }
77
76
 
78
- fastify.listen({ port: 0 }, async err => {
79
- t.assert.ifError(err)
80
- t.after(() => { fastify.close() })
81
-
82
- sget({
83
- method: 'GET',
84
- url: 'http://localhost:' + fastify.server.address().port
85
- }, (err, response, body) => {
86
- t.assert.ifError(err)
87
- t.assert.strictEqual(response.statusCode, 200)
88
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
89
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
90
- })
77
+ const fastifyServer = await fastify.listen({ port: 0 })
91
78
 
92
- sget({
93
- method: 'GET',
94
- url: 'http://localhost:' + fastify.server.address().port + '/no-await'
95
- }, (err, response, body) => {
96
- t.assert.ifError(err)
97
- t.assert.strictEqual(response.statusCode, 200)
98
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
99
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
100
- })
79
+ t.after(() => { fastify.close() })
101
80
 
102
- sget({
103
- method: 'GET',
104
- url: 'http://localhost:' + fastify.server.address().port + '/await/hostname_port'
105
- }, (err, response, body) => {
106
- t.assert.ifError(err)
107
- t.assert.strictEqual(response.statusCode, 200)
108
- const parsedBody = JSON.parse(body)
109
- t.assert.strictEqual(parsedBody.hostname, 'localhost')
110
- t.assert.strictEqual(parseInt(parsedBody.port), fastify.server.address().port)
111
- done()
112
- })
113
- })
81
+ const result = await fetch(fastifyServer)
82
+
83
+ const body = await result.text()
84
+ t.assert.ok(result.ok)
85
+ t.assert.strictEqual(result.status, 200)
86
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
87
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
88
+
89
+ const result1 = await fetch(`${fastifyServer}/no-await`)
90
+
91
+ const body1 = await result1.text()
92
+ t.assert.ok(result1.ok)
93
+ t.assert.strictEqual(result1.status, 200)
94
+ t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
95
+ t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' })
96
+
97
+ const result2 = await fetch(`http://localhost:${fastify.server.address().port}/await/hostname_port`)
98
+
99
+ const parsedBody = await result2.json()
100
+ t.assert.ok(result2.ok)
101
+ t.assert.strictEqual(result2.status, 200)
102
+ t.assert.strictEqual(parsedBody.hostname, 'localhost')
103
+ t.assert.strictEqual(parseInt(parsedBody.port), fastify.server.address().port)
114
104
  })
115
105
 
116
- test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t, done) => {
117
- t.plan(4)
106
+ test('ignore the result of the promise if reply.send is called beforehand (undefined)', async (t) => {
107
+ t.plan(3)
118
108
 
119
109
  const server = Fastify()
120
110
  const payload = { hello: 'world' }
@@ -125,22 +115,17 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
125
115
 
126
116
  t.after(() => { server.close() })
127
117
 
128
- server.listen({ port: 0 }, (err) => {
129
- t.assert.ifError(err)
130
- sget({
131
- method: 'GET',
132
- url: 'http://localhost:' + server.server.address().port + '/'
133
- }, (err, res, body) => {
134
- t.assert.ifError(err)
135
- t.assert.deepStrictEqual(payload, JSON.parse(body))
136
- t.assert.strictEqual(res.statusCode, 200)
137
- done()
138
- })
139
- })
118
+ const fastifyServer = await server.listen({ port: 0 })
119
+
120
+ const result = await fetch(fastifyServer)
121
+
122
+ t.assert.ok(result.ok)
123
+ t.assert.deepStrictEqual(payload, await result.json())
124
+ t.assert.strictEqual(result.status, 200)
140
125
  })
141
126
 
142
- test('ignore the result of the promise if reply.send is called beforehand (object)', (t, done) => {
143
- t.plan(4)
127
+ test('ignore the result of the promise if reply.send is called beforehand (object)', async (t) => {
128
+ t.plan(3)
144
129
 
145
130
  const server = Fastify()
146
131
  const payload = { hello: 'world2' }
@@ -152,18 +137,13 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
152
137
 
153
138
  t.after(() => { server.close() })
154
139
 
155
- server.listen({ port: 0 }, (err) => {
156
- t.assert.ifError(err)
157
- sget({
158
- method: 'GET',
159
- url: 'http://localhost:' + server.server.address().port + '/'
160
- }, (err, res, body) => {
161
- t.assert.ifError(err)
162
- t.assert.deepStrictEqual(payload, JSON.parse(body))
163
- t.assert.strictEqual(res.statusCode, 200)
164
- done()
165
- })
166
- })
140
+ const fastifyServer = await server.listen({ port: 0 })
141
+
142
+ const result = await fetch(fastifyServer)
143
+
144
+ t.assert.ok(result.ok)
145
+ t.assert.deepStrictEqual(payload, await result.json())
146
+ t.assert.strictEqual(result.status, 200)
167
147
  })
168
148
 
169
149
  test('server logs an error if reply.send is called and a value is returned via async/await', (t, done) => {
@@ -197,8 +177,8 @@ test('server logs an error if reply.send is called and a value is returned via a
197
177
  })
198
178
  })
199
179
 
200
- test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t, done) => {
201
- t.plan(4)
180
+ test('ignore the result of the promise if reply.send is called beforehand (undefined)', async (t) => {
181
+ t.plan(3)
202
182
 
203
183
  const server = Fastify()
204
184
  const payload = { hello: 'world' }
@@ -209,22 +189,17 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
209
189
 
210
190
  t.after(() => { server.close() })
211
191
 
212
- server.listen({ port: 0 }, (err) => {
213
- t.assert.ifError(err)
214
- sget({
215
- method: 'GET',
216
- url: 'http://localhost:' + server.server.address().port + '/'
217
- }, (err, res, body) => {
218
- t.assert.ifError(err)
219
- t.assert.deepStrictEqual(payload, JSON.parse(body))
220
- t.assert.strictEqual(res.statusCode, 200)
221
- done()
222
- })
223
- })
192
+ const fastifyServer = await server.listen({ port: 0 })
193
+
194
+ const result = await fetch(fastifyServer)
195
+
196
+ t.assert.ok(result.ok)
197
+ t.assert.deepStrictEqual(payload, await result.json())
198
+ t.assert.strictEqual(result.status, 200)
224
199
  })
225
200
 
226
- test('ignore the result of the promise if reply.send is called beforehand (object)', (t, done) => {
227
- t.plan(4)
201
+ test('ignore the result of the promise if reply.send is called beforehand (object)', async (t) => {
202
+ t.plan(3)
228
203
 
229
204
  const server = Fastify()
230
205
  const payload = { hello: 'world2' }
@@ -236,18 +211,13 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
236
211
 
237
212
  t.after(() => { server.close() })
238
213
 
239
- server.listen({ port: 0 }, (err) => {
240
- t.assert.ifError(err)
241
- sget({
242
- method: 'GET',
243
- url: 'http://localhost:' + server.server.address().port + '/'
244
- }, (err, res, body) => {
245
- t.assert.ifError(err)
246
- t.assert.deepStrictEqual(payload, JSON.parse(body))
247
- t.assert.strictEqual(res.statusCode, 200)
248
- done()
249
- })
250
- })
214
+ const fastifyServer = await server.listen({ port: 0 })
215
+
216
+ const result = await fetch(fastifyServer)
217
+
218
+ t.assert.ok(result.ok)
219
+ t.assert.deepStrictEqual(payload, await result.json())
220
+ t.assert.strictEqual(result.status, 200)
251
221
  })
252
222
 
253
223
  test('await reply if we will be calling reply.send in the future', (t, done) => {
@@ -433,8 +403,8 @@ test('does not call reply.send() twice if 204 response equal already sent', (t,
433
403
  })
434
404
  })
435
405
 
436
- test('promise was fulfilled with undefined', (t, done) => {
437
- t.plan(4)
406
+ test('promise was fulfilled with undefined', async (t) => {
407
+ t.plan(3)
438
408
 
439
409
  let fastify = null
440
410
  const stream = split(JSON.parse)
@@ -458,20 +428,13 @@ test('promise was fulfilled with undefined', (t, done) => {
458
428
  t.assert.fail('should not log an error')
459
429
  })
460
430
 
461
- fastify.listen({ port: 0 }, (err) => {
462
- t.assert.ifError(err)
463
- t.after(() => { fastify.close() })
464
-
465
- sget({
466
- method: 'GET',
467
- url: 'http://localhost:' + fastify.server.address().port + '/'
468
- }, (err, res, body) => {
469
- t.assert.ifError(err)
470
- t.assert.strictEqual(res.body, undefined)
471
- t.assert.strictEqual(res.statusCode, 200)
472
- done()
473
- })
474
- })
431
+ const fastifyServer = await fastify.listen({ port: 0 })
432
+
433
+ const result = await fetch(fastifyServer)
434
+
435
+ t.assert.ok(result.ok)
436
+ t.assert.strictEqual(await result.text(), '')
437
+ t.assert.strictEqual(result.status, 200)
475
438
  })
476
439
 
477
440
  test('promise was fulfilled with undefined using inject', async (t) => {
@@ -496,8 +459,8 @@ test('promise was fulfilled with undefined using inject', async (t) => {
496
459
  t.assert.strictEqual(res.statusCode, 200)
497
460
  })
498
461
 
499
- test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', (t, done) => {
500
- t.plan(4)
462
+ test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', async (t) => {
463
+ t.plan(3)
501
464
 
502
465
  let fastify = null
503
466
  const stream = split(JSON.parse)
@@ -523,23 +486,16 @@ test('error is not logged because promise was fulfilled with undefined but respo
523
486
  t.assert.fail('should not log an error')
524
487
  })
525
488
 
526
- fastify.listen({ port: 0 }, (err) => {
527
- t.assert.ifError(err)
528
- t.after(() => { fastify.close() })
529
-
530
- sget({
531
- method: 'GET',
532
- url: 'http://localhost:' + fastify.server.address().port + '/'
533
- }, (err, res, body) => {
534
- t.assert.ifError(err)
535
- t.assert.strictEqual(res.statusCode, 200)
536
- t.assert.deepStrictEqual(
537
- payload,
538
- JSON.parse(body)
539
- )
540
- done()
541
- })
542
- })
489
+ const fastifyServer = await fastify.listen({ port: 0 })
490
+
491
+ const result = await fetch(fastifyServer)
492
+
493
+ t.assert.ok(result.ok)
494
+ t.assert.strictEqual(result.status, 200)
495
+ t.assert.deepStrictEqual(
496
+ payload,
497
+ await result.json()
498
+ )
543
499
  })
544
500
 
545
501
  test('Thrown Error instance sets HTTP status code', (t, done) => {
@@ -3,7 +3,6 @@
3
3
  const { createHook } = require('node:async_hooks')
4
4
  const { test } = require('node:test')
5
5
  const Fastify = require('..')
6
- const sget = require('simple-get').concat
7
6
 
8
7
  const remainingIds = new Set()
9
8
 
@@ -20,7 +19,7 @@ createHook({
20
19
 
21
20
  const app = Fastify({ logger: false })
22
21
 
23
- test('test async hooks', (t, done) => {
22
+ test('test async hooks', async (t) => {
24
23
  app.get('/', function (request, reply) {
25
24
  reply.send({ id: 42 })
26
25
  })
@@ -29,43 +28,25 @@ test('test async hooks', (t, done) => {
29
28
  reply.send({ id: 42 })
30
29
  })
31
30
 
32
- app.listen({ port: 0 }, function (err, address) {
33
- t.assert.ifError(err)
31
+ t.after(() => app.close())
34
32
 
35
- sget({
36
- method: 'POST',
37
- url: 'http://localhost:' + app.server.address().port,
38
- body: {
39
- hello: 'world'
40
- },
41
- json: true
42
- }, (err, response, body) => {
43
- t.assert.ifError(err)
44
- t.assert.strictEqual(response.statusCode, 200)
33
+ const fastifyServer = await app.listen({ port: 0 })
45
34
 
46
- sget({
47
- method: 'POST',
48
- url: 'http://localhost:' + app.server.address().port,
49
- body: {
50
- hello: 'world'
51
- },
52
- json: true
53
- }, (err, response, body) => {
54
- t.assert.ifError(err)
55
- t.assert.strictEqual(response.statusCode, 200)
35
+ const result1 = await fetch(fastifyServer, {
36
+ method: 'POST',
37
+ headers: { 'Content-Type': 'application/json' },
38
+ body: JSON.stringify({ hello: 'world' })
39
+ })
40
+ t.assert.strictEqual(result1.status, 200)
56
41
 
57
- sget({
58
- method: 'GET',
59
- url: 'http://localhost:' + app.server.address().port,
60
- json: true
61
- }, (err, response, body) => {
62
- t.assert.ifError(err)
63
- t.assert.strictEqual(response.statusCode, 200)
64
- app.close()
65
- t.assert.strictEqual(remainingIds.size, 0)
66
- done()
67
- })
68
- })
69
- })
42
+ const result2 = await fetch(fastifyServer, {
43
+ method: 'POST',
44
+ headers: { 'Content-Type': 'application/json' },
45
+ body: JSON.stringify({ hello: 'world' })
70
46
  })
47
+ t.assert.strictEqual(result2.status, 200)
48
+
49
+ const result3 = await fetch(fastifyServer)
50
+ t.assert.strictEqual(result3.status, 200)
51
+ t.assert.strictEqual(remainingIds.size, 0)
71
52
  })