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
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('SEARCH', { hasBody: true })
7
6
 
@@ -130,119 +129,105 @@ test('search test', async t => {
130
129
  t.after(() => { fastify.close() })
131
130
  const url = `http://localhost:${fastify.server.address().port}`
132
131
 
133
- await t.test('request - search', (t, done) => {
132
+ await t.test('request - search', async t => {
134
133
  t.plan(4)
135
- sget({
136
- method: 'SEARCH',
137
- url
138
- }, (err, response, body) => {
139
- t.assert.ifError(err)
140
- t.assert.strictEqual(response.statusCode, 200)
141
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
142
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
143
- done()
134
+ const result = await fetch(url, {
135
+ method: 'SEARCH'
144
136
  })
137
+ const body = await result.text()
138
+ t.assert.ok(result.ok)
139
+ t.assert.strictEqual(result.status, 200)
140
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
141
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
145
142
  })
146
143
 
147
- await t.test('request search params schema', (t, done) => {
144
+ await t.test('request search params schema', async t => {
148
145
  t.plan(4)
149
- sget({
150
- method: 'SEARCH',
151
- url: `${url}/params/world/123`
152
- }, (err, response, body) => {
153
- t.assert.ifError(err)
154
- t.assert.strictEqual(response.statusCode, 200)
155
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
156
- t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
157
- done()
146
+ const result = await fetch(`${url}/params/world/123`, {
147
+ method: 'SEARCH'
158
148
  })
149
+ const body = await result.text()
150
+ t.assert.ok(result.ok)
151
+ t.assert.strictEqual(result.status, 200)
152
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
153
+ t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })
159
154
  })
160
155
 
161
- await t.test('request search params schema error', (t, done) => {
156
+ await t.test('request search params schema error', async t => {
162
157
  t.plan(3)
163
- sget({
164
- method: 'SEARCH',
165
- url: `${url}/params/world/string`
166
- }, (err, response, body) => {
167
- t.assert.ifError(err)
168
- t.assert.strictEqual(response.statusCode, 400)
169
- t.assert.deepStrictEqual(JSON.parse(body), {
170
- error: 'Bad Request',
171
- code: 'FST_ERR_VALIDATION',
172
- message: 'params/test must be integer',
173
- statusCode: 400
174
- })
175
- done()
158
+ const result = await fetch(`${url}/params/world/string`, {
159
+ method: 'SEARCH'
160
+ })
161
+ const body = await result.text()
162
+ t.assert.strictEqual(result.status, 400)
163
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
164
+ t.assert.deepStrictEqual(JSON.parse(body), {
165
+ error: 'Bad Request',
166
+ code: 'FST_ERR_VALIDATION',
167
+ message: 'params/test must be integer',
168
+ statusCode: 400
176
169
  })
177
170
  })
178
171
 
179
- await t.test('request search querystring schema', (t, done) => {
172
+ await t.test('request search querystring schema', async t => {
180
173
  t.plan(4)
181
- sget({
182
- method: 'SEARCH',
183
- url: `${url}/query?hello=123`
184
- }, (err, response, body) => {
185
- t.assert.ifError(err)
186
- t.assert.strictEqual(response.statusCode, 200)
187
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
188
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
189
- done()
174
+ const result = await fetch(`${url}/query?hello=123`, {
175
+ method: 'SEARCH'
190
176
  })
177
+ const body = await result.text()
178
+ t.assert.ok(result.ok)
179
+ t.assert.strictEqual(result.status, 200)
180
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
181
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })
191
182
  })
192
183
 
193
- await t.test('request search querystring schema error', (t, done) => {
184
+ await t.test('request search querystring schema error', async t => {
194
185
  t.plan(3)
195
- sget({
196
- method: 'SEARCH',
197
- url: `${url}/query?hello=world`
198
- }, (err, response, body) => {
199
- t.assert.ifError(err)
200
- t.assert.strictEqual(response.statusCode, 400)
201
- t.assert.deepStrictEqual(JSON.parse(body), {
202
- error: 'Bad Request',
203
- code: 'FST_ERR_VALIDATION',
204
- message: 'querystring/hello must be integer',
205
- statusCode: 400
206
- })
207
- done()
186
+ const result = await fetch(`${url}/query?hello=world`, {
187
+ method: 'SEARCH'
188
+ })
189
+ const body = await result.text()
190
+ t.assert.strictEqual(result.status, 400)
191
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
192
+ t.assert.deepStrictEqual(JSON.parse(body), {
193
+ error: 'Bad Request',
194
+ code: 'FST_ERR_VALIDATION',
195
+ message: 'querystring/hello must be integer',
196
+ statusCode: 400
208
197
  })
209
198
  })
210
199
 
211
- await t.test('request search body schema', (t, done) => {
200
+ await t.test('request search body schema', async t => {
212
201
  t.plan(4)
213
202
  const replyBody = { foo: 'bar', test: 5 }
214
- sget({
203
+ const result = await fetch(`${url}/body`, {
215
204
  method: 'SEARCH',
216
- url: `${url}/body`,
217
205
  body: JSON.stringify(replyBody),
218
206
  headers: { 'content-type': 'application/json' }
219
- }, (err, response, body) => {
220
- t.assert.ifError(err)
221
- t.assert.strictEqual(response.statusCode, 200)
222
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
223
- t.assert.deepStrictEqual(JSON.parse(body), replyBody)
224
- done()
225
207
  })
208
+ const body = await result.text()
209
+ t.assert.ok(result.ok)
210
+ t.assert.strictEqual(result.status, 200)
211
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
212
+ t.assert.deepStrictEqual(JSON.parse(body), replyBody)
226
213
  })
227
214
 
228
- await t.test('request search body schema error', (t, done) => {
215
+ await t.test('request search body schema error', async t => {
229
216
  t.plan(4)
230
- sget({
217
+ const result = await fetch(`${url}/body`, {
231
218
  method: 'SEARCH',
232
- url: `${url}/body`,
233
219
  body: JSON.stringify({ foo: 'bar', test: 'test' }),
234
220
  headers: { 'content-type': 'application/json' }
235
- }, (err, response, body) => {
236
- t.assert.ifError(err)
237
- t.assert.strictEqual(response.statusCode, 400)
238
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
239
- t.assert.deepStrictEqual(JSON.parse(body), {
240
- error: 'Bad Request',
241
- code: 'FST_ERR_VALIDATION',
242
- message: 'body/test must be integer',
243
- statusCode: 400
244
- })
245
- done()
221
+ })
222
+ const body = await result.text()
223
+ t.assert.ok(!result.ok)
224
+ t.assert.strictEqual(result.status, 400)
225
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
226
+ t.assert.deepStrictEqual(JSON.parse(body), {
227
+ error: 'Bad Request',
228
+ code: 'FST_ERR_VALIDATION',
229
+ message: 'body/test must be integer',
230
+ statusCode: 400
246
231
  })
247
232
  })
248
233
  })
@@ -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('../../fastify')()
6
5
  fastify.addHttpMethod('UNLOCK')
7
6
 
@@ -22,21 +21,18 @@ test('can be created - unlock', t => {
22
21
  })
23
22
 
24
23
  test('unlock test', async t => {
25
- await fastify.listen({ port: 0 })
24
+ const fastifyServer = await fastify.listen({ port: 0 })
26
25
 
27
26
  t.after(() => { fastify.close() })
28
- await t.test('request - unlock', (t, done) => {
27
+ await t.test('request - unlock', async t => {
29
28
  t.plan(2)
30
- sget({
31
- url: `http://localhost:${fastify.server.address().port}/test/a.txt`,
29
+ const result = await fetch(`${fastifyServer}/test/a.txt`, {
32
30
  method: 'UNLOCK',
33
31
  headers: {
34
32
  'Lock-Token': 'urn:uuid:a515cfa4-5da4-22e1-f5b5-00a0451e6bf7'
35
33
  }
36
- }, (err, response, body) => {
37
- t.assert.ifError(err)
38
- t.assert.strictEqual(response.statusCode, 204)
39
- done()
40
34
  })
35
+ t.assert.ok(result.ok)
36
+ t.assert.strictEqual(result.status, 204)
41
37
  })
42
38
  })
@@ -11,16 +11,13 @@ const { getServerUrl } = require('../helper')
11
11
 
12
12
  test.before(buildCertificate)
13
13
 
14
- test('http/2 request while fastify closing', (t, done) => {
15
- let fastify
16
- try {
17
- fastify = Fastify({
18
- http2: true
19
- })
20
- t.assert.ok('http2 successfully loaded')
21
- } catch (e) {
22
- t.assert.fail('http2 loading failed')
23
- }
14
+ const isNode24OrGreater = Number(process.versions.node.split('.')[0]) >= 24
15
+
16
+ test('http/2 request while fastify closing Node <24', { skip: isNode24OrGreater }, (t, done) => {
17
+ const fastify = Fastify({
18
+ http2: true
19
+ })
20
+ t.assert.ok('http2 successfully loaded')
24
21
 
25
22
  fastify.get('/', () => Promise.resolve({}))
26
23
 
@@ -53,17 +50,38 @@ test('http/2 request while fastify closing', (t, done) => {
53
50
  })
54
51
  })
55
52
 
56
- test('http/2 request while fastify closing - return503OnClosing: false', (t, done) => {
57
- let fastify
58
- try {
59
- fastify = Fastify({
60
- http2: true,
61
- return503OnClosing: false
53
+ test('http/2 request while fastify closing Node >=24', { skip: !isNode24OrGreater }, (t, done) => {
54
+ const fastify = Fastify({
55
+ http2: true
56
+ })
57
+ t.assert.ok('http2 successfully loaded')
58
+
59
+ fastify.get('/', () => Promise.resolve({}))
60
+
61
+ t.after(() => { fastify.close() })
62
+ fastify.listen({ port: 0 }, err => {
63
+ t.assert.ifError(err)
64
+
65
+ const url = getServerUrl(fastify)
66
+ const session = http2.connect(url, function () {
67
+ session.on('error', () => {
68
+ // Nothing to do here,
69
+ // we are not interested in this error that might
70
+ // happen or not
71
+ })
72
+ session.on('close', () => {
73
+ done()
74
+ })
75
+ fastify.close()
62
76
  })
63
- t.assert.ok('http2 successfully loaded')
64
- } catch (e) {
65
- t.assert.fail('http2 loading failed')
66
- }
77
+ })
78
+ })
79
+
80
+ test('http/2 request while fastify closing - return503OnClosing: false', { skip: isNode24OrGreater }, (t, done) => {
81
+ const fastify = Fastify({
82
+ http2: true,
83
+ return503OnClosing: false
84
+ })
67
85
 
68
86
  t.after(() => { fastify.close() })
69
87
 
@@ -3,10 +3,10 @@
3
3
  const { test } = require('node:test')
4
4
  const Fastify = require('../..')
5
5
  const h2url = require('h2url')
6
- const sget = require('simple-get').concat
7
6
  const msg = { hello: 'world' }
8
7
 
9
8
  const { buildCertificate } = require('../build-certificate')
9
+ const { Agent } = require('undici')
10
10
  test.before(buildCertificate)
11
11
 
12
12
  test('secure with fallback', async (t) => {
@@ -39,14 +39,14 @@ test('secure with fallback', async (t) => {
39
39
  throw new Error('kaboom')
40
40
  })
41
41
 
42
- t.after(() => { fastify.close() })
42
+ t.after(() => fastify.close())
43
43
 
44
- await fastify.listen({ port: 0 })
44
+ const fastifyServer = await fastify.listen({ port: 0 })
45
45
 
46
46
  await t.test('https get error', async (t) => {
47
47
  t.plan(1)
48
48
 
49
- const url = `https://localhost:${fastify.server.address().port}/error`
49
+ const url = `${fastifyServer}/error`
50
50
  const res = await h2url.concat({ url })
51
51
 
52
52
  t.assert.strictEqual(res.headers[':status'], 500)
@@ -55,9 +55,8 @@ test('secure with fallback', async (t) => {
55
55
  await t.test('https post', async (t) => {
56
56
  t.plan(2)
57
57
 
58
- const url = `https://localhost:${fastify.server.address().port}`
59
58
  const res = await h2url.concat({
60
- url,
59
+ url: fastifyServer,
61
60
  method: 'POST',
62
61
  body: JSON.stringify({ hello: 'http2' }),
63
62
  headers: {
@@ -72,39 +71,43 @@ test('secure with fallback', async (t) => {
72
71
  await t.test('https get request', async (t) => {
73
72
  t.plan(3)
74
73
 
75
- const url = `https://localhost:${fastify.server.address().port}`
76
- const res = await h2url.concat({ url })
74
+ const res = await h2url.concat({ url: fastifyServer })
77
75
 
78
76
  t.assert.strictEqual(res.headers[':status'], 200)
79
77
  t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
80
78
  t.assert.deepStrictEqual(JSON.parse(res.body), msg)
81
79
  })
82
80
 
83
- await t.test('http1 get request', (t, done) => {
81
+ await t.test('http1 get request', async t => {
84
82
  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()
83
+
84
+ const result = await fetch(fastifyServer, {
85
+ dispatcher: new Agent({
86
+ connect: {
87
+ rejectUnauthorized: false
88
+ }
89
+ })
95
90
  })
91
+
92
+ const body = await result.text()
93
+ t.assert.ok(result.ok)
94
+ t.assert.strictEqual(result.status, 200)
95
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
96
+ t.assert.deepStrictEqual(JSON.parse(body), msg)
96
97
  })
97
98
 
98
- await t.test('http1 get error', (t, done) => {
99
+ await t.test('http1 get error', async t => {
99
100
  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()
101
+
102
+ const result = await fetch(`${fastifyServer}/error`, {
103
+ dispatcher: new Agent({
104
+ connect: {
105
+ rejectUnauthorized: false
106
+ }
107
+ })
108
108
  })
109
+
110
+ t.assert.ok(!result.ok)
111
+ t.assert.strictEqual(result.status, 500)
109
112
  })
110
113
  })
@@ -4,8 +4,8 @@ const { test } = require('node:test')
4
4
  const Fastify = require('../..')
5
5
  const https = require('node:https')
6
6
  const dns = require('node:dns').promises
7
- const sget = require('simple-get').concat
8
7
  const { buildCertificate } = require('../build-certificate')
8
+ const { Agent } = require('undici')
9
9
 
10
10
  async function setup () {
11
11
  await buildCertificate()
@@ -13,7 +13,7 @@ async function setup () {
13
13
  const localAddresses = await dns.lookup('localhost', { all: true })
14
14
 
15
15
  test('Should support a custom https server', { skip: localAddresses.length < 1 }, async t => {
16
- t.plan(4)
16
+ t.plan(5)
17
17
 
18
18
  const fastify = Fastify({
19
19
  serverFactory: (handler, opts) => {
@@ -42,20 +42,16 @@ async function setup () {
42
42
 
43
43
  await fastify.listen({ port: 0 })
44
44
 
45
- await new Promise((resolve, reject) => {
46
- sget({
47
- method: 'GET',
48
- url: 'https://localhost:' + fastify.server.address().port,
49
- rejectUnauthorized: false
50
- }, (err, response, body) => {
51
- if (err) {
52
- return reject(err)
45
+ const result = await fetch('https://localhost:' + fastify.server.address().port, {
46
+ dispatcher: new Agent({
47
+ connect: {
48
+ rejectUnauthorized: false
53
49
  }
54
- t.assert.strictEqual(response.statusCode, 200)
55
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
56
- resolve()
57
50
  })
58
51
  })
52
+ t.assert.ok(result.ok)
53
+ t.assert.strictEqual(result.status, 200)
54
+ t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
59
55
  })
60
56
  }
61
57
 
@@ -1,10 +1,11 @@
1
1
  'use strict'
2
2
 
3
3
  const { test } = require('node:test')
4
- const sget = require('simple-get').concat
4
+ const { request } = require('undici')
5
5
  const Fastify = require('../..')
6
6
 
7
7
  const { buildCertificate } = require('../build-certificate')
8
+ const { Agent } = require('undici')
8
9
  test.before(buildCertificate)
9
10
 
10
11
  test('https', async (t) => {
@@ -35,43 +36,45 @@ test('https', async (t) => {
35
36
 
36
37
  t.after(() => { fastify.close() })
37
38
 
38
- await t.test('https get request', (t, done) => {
39
+ await t.test('https get request', async t => {
39
40
  t.plan(4)
40
- sget({
41
- method: 'GET',
42
- url: 'https://localhost:' + fastify.server.address().port,
43
- rejectUnauthorized: false
44
- }, (err, response, body) => {
45
- t.assert.ifError(err)
46
- t.assert.strictEqual(response.statusCode, 200)
47
- t.assert.strictEqual(response.headers['content-length'], '' + body.length)
48
- t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
49
- done()
41
+ const result = await fetch('https://localhost:' + fastify.server.address().port, {
42
+ dispatcher: new Agent({
43
+ connect: {
44
+ rejectUnauthorized: false
45
+ }
46
+ })
50
47
  })
48
+ t.assert.ok(result.ok)
49
+ t.assert.strictEqual(result.status, 200)
50
+ const body = await result.text()
51
+ t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
52
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
51
53
  })
52
54
 
53
- await t.test('https get request without trust proxy - protocol', (t, done) => {
54
- t.plan(4)
55
- sget({
56
- method: 'GET',
57
- url: 'https://localhost:' + fastify.server.address().port + '/proto',
58
- rejectUnauthorized: false
59
- }, (err, response, body) => {
60
- t.assert.ifError(err)
61
- t.assert.deepStrictEqual(JSON.parse(body), { proto: 'https' })
55
+ await t.test('https get request without trust proxy - protocol', async t => {
56
+ t.plan(3)
57
+ const result1 = await fetch(`${'https://localhost:' + fastify.server.address().port}/proto`, {
58
+ dispatcher: new Agent({
59
+ connect: {
60
+ rejectUnauthorized: false
61
+ }
62
+ })
62
63
  })
63
- sget({
64
- method: 'GET',
65
- url: 'https://localhost:' + fastify.server.address().port + '/proto',
66
- rejectUnauthorized: false,
64
+ t.assert.ok(result1.ok)
65
+ t.assert.deepStrictEqual(await result1.json(), { proto: 'https' })
66
+
67
+ const result2 = await fetch(`${'https://localhost:' + fastify.server.address().port}/proto`, {
68
+ dispatcher: new Agent({
69
+ connect: {
70
+ rejectUnauthorized: false
71
+ }
72
+ }),
67
73
  headers: {
68
74
  'x-forwarded-proto': 'lorem'
69
75
  }
70
- }, (err, response, body) => {
71
- t.assert.ifError(err)
72
- t.assert.deepStrictEqual(JSON.parse(body), { proto: 'https' })
73
- done()
74
76
  })
77
+ t.assert.deepStrictEqual(await result2.json(), { proto: 'https' })
75
78
  })
76
79
  })
77
80
 
@@ -98,36 +101,36 @@ test('https - headers', async (t) => {
98
101
 
99
102
  await fastify.listen({ port: 0 })
100
103
 
101
- await t.test('https get request', (t, done) => {
102
- t.plan(4)
103
- sget({
104
- method: 'GET',
105
- url: 'https://localhost:' + fastify.server.address().port,
106
- rejectUnauthorized: false
107
- }, (err, response, body) => {
108
- t.assert.ifError(err)
109
- t.assert.strictEqual(response.statusCode, 200)
110
- const parsedBody = JSON.parse(body)
111
- t.assert.strictEqual(parsedBody.hostname, 'localhost')
112
- t.assert.strictEqual(parsedBody.port, fastify.server.address().port)
113
- done()
104
+ await t.test('https get request', async t => {
105
+ t.plan(3)
106
+ const result = await fetch('https://localhost:' + fastify.server.address().port, {
107
+ dispatcher: new Agent({
108
+ connect: {
109
+ rejectUnauthorized: false
110
+ }
111
+ })
114
112
  })
113
+ t.assert.ok(result.ok)
114
+ t.assert.strictEqual(result.status, 200)
115
+ t.assert.deepStrictEqual(await result.json(), { hostname: 'localhost', port: fastify.server.address().port, hello: 'world' })
115
116
  })
116
- await t.test('https get request - test port fall back', (t, done) => {
117
- t.plan(3)
118
- sget({
117
+
118
+ await t.test('https get request - test port fall back', async t => {
119
+ t.plan(2)
120
+
121
+ const result = await request('https://localhost:' + fastify.server.address().port, {
119
122
  method: 'GET',
120
123
  headers: {
121
124
  host: 'example.com'
122
125
  },
123
- url: 'https://localhost:' + fastify.server.address().port,
124
- rejectUnauthorized: false
125
- }, (err, response, body) => {
126
- t.assert.ifError(err)
127
- t.assert.strictEqual(response.statusCode, 200)
128
- const parsedBody = JSON.parse(body)
129
- t.assert.strictEqual(parsedBody.port, null)
130
- done()
126
+ dispatcher: new Agent({
127
+ connect: {
128
+ rejectUnauthorized: false
129
+ }
130
+ })
131
131
  })
132
+
133
+ t.assert.strictEqual(result.statusCode, 200)
134
+ t.assert.deepStrictEqual(await result.body.json(), { hello: 'world', hostname: 'example.com', port: null })
132
135
  })
133
136
  })