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.
- package/.vscode/settings.json +15 -15
- package/LICENSE +1 -1
- package/README.md +2 -0
- package/SECURITY.md +158 -2
- package/build/build-validation.js +20 -1
- package/docs/Guides/Delay-Accepting-Requests.md +8 -5
- package/docs/Guides/Ecosystem.md +20 -5
- package/docs/Guides/Migration-Guide-V5.md +6 -10
- package/docs/Guides/Recommendations.md +1 -1
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +5 -3
- package/docs/Reference/Hooks.md +16 -20
- package/docs/Reference/Lifecycle.md +2 -2
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +2 -2
- package/docs/Reference/Routes.md +7 -6
- package/docs/Reference/Server.md +341 -200
- package/docs/Reference/TypeScript.md +1 -3
- package/docs/Reference/Validation-and-Serialization.md +56 -4
- package/docs/Reference/Warnings.md +2 -1
- package/fastify.d.ts +4 -3
- package/fastify.js +47 -34
- package/lib/configValidator.js +196 -28
- package/lib/contentTypeParser.js +41 -48
- package/lib/error-handler.js +3 -3
- package/lib/errors.js +11 -0
- package/lib/handleRequest.js +13 -17
- package/lib/pluginOverride.js +3 -1
- package/lib/promise.js +23 -0
- package/lib/reply.js +24 -30
- package/lib/request.js +3 -10
- package/lib/route.js +37 -3
- package/lib/server.js +36 -35
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +19 -1
- package/package.json +14 -10
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +84 -128
- package/test/async_hooks.test.js +18 -37
- package/test/body-limit.test.js +90 -63
- package/test/buffer.test.js +22 -0
- package/test/build-certificate.js +1 -1
- package/test/case-insensitive.test.js +44 -65
- package/test/check.test.js +17 -21
- package/test/close-pipelining.test.js +24 -15
- package/test/constrained-routes.test.js +231 -0
- package/test/custom-http-server.test.js +7 -15
- package/test/custom-parser-async.test.js +17 -22
- package/test/custom-parser.0.test.js +267 -348
- package/test/custom-parser.1.test.js +141 -191
- package/test/custom-parser.2.test.js +34 -44
- package/test/custom-parser.3.test.js +56 -104
- package/test/custom-parser.4.test.js +106 -144
- package/test/custom-parser.5.test.js +56 -75
- package/test/custom-querystring-parser.test.js +51 -77
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +76 -259
- package/test/delete.test.js +101 -110
- package/test/diagnostics-channel/404.test.js +7 -15
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/async-request.test.js +8 -16
- package/test/diagnostics-channel/error-request.test.js +7 -15
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
- package/test/diagnostics-channel/sync-request.test.js +9 -16
- package/test/fastify-instance.test.js +1 -1
- package/test/header-overflow.test.js +18 -29
- package/test/helper.js +139 -135
- package/test/hooks-async.test.js +259 -235
- package/test/hooks.test.js +951 -996
- package/test/http-methods/copy.test.js +14 -19
- package/test/http-methods/get.test.js +131 -143
- package/test/http-methods/head.test.js +53 -84
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcalendar.test.js +45 -72
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/move.test.js +6 -10
- package/test/http-methods/propfind.test.js +34 -44
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http-methods/unlock.test.js +5 -9
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +31 -28
- package/test/https/custom-https-server.test.js +9 -13
- package/test/https/https.test.js +56 -53
- package/test/input-validation.js +139 -150
- package/test/internals/errors.test.js +50 -1
- package/test/internals/handle-request.test.js +72 -65
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply.test.js +277 -496
- package/test/issue-4959.test.js +12 -3
- package/test/listen.4.test.js +31 -43
- package/test/nullable-validation.test.js +33 -46
- package/test/output-validation.test.js +24 -26
- package/test/plugin.1.test.js +40 -68
- package/test/plugin.2.test.js +108 -120
- package/test/plugin.3.test.js +50 -72
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +42 -63
- package/test/proto-poisoning.test.js +78 -97
- package/test/register.test.js +8 -18
- package/test/request-error.test.js +57 -146
- package/test/request-id.test.js +30 -49
- package/test/route-hooks.test.js +117 -101
- package/test/route-prefix.test.js +194 -133
- package/test/route-shorthand.test.js +9 -27
- package/test/route.1.test.js +74 -131
- package/test/route.8.test.js +9 -17
- package/test/router-options.test.js +450 -0
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +254 -218
- package/test/server.test.js +143 -5
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +33 -50
- package/test/stream.4.test.js +18 -28
- package/test/stream.5.test.js +11 -19
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +13 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/request.test-d.ts +1 -0
- package/test/types/type-provider.test-d.ts +55 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +117 -59
- package/test/versioned-routes.test.js +39 -56
- package/types/errors.d.ts +11 -1
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +1 -1
- package/types/reply.d.ts +2 -2
- package/types/request.d.ts +1 -0
- 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',
|
|
132
|
+
await t.test('request - search', async t => {
|
|
134
133
|
t.plan(4)
|
|
135
|
-
|
|
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',
|
|
144
|
+
await t.test('request search params schema', async t => {
|
|
148
145
|
t.plan(4)
|
|
149
|
-
|
|
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',
|
|
156
|
+
await t.test('request search params schema error', async t => {
|
|
162
157
|
t.plan(3)
|
|
163
|
-
|
|
164
|
-
method: 'SEARCH'
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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',
|
|
172
|
+
await t.test('request search querystring schema', async t => {
|
|
180
173
|
t.plan(4)
|
|
181
|
-
|
|
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',
|
|
184
|
+
await t.test('request search querystring schema error', async t => {
|
|
194
185
|
t.plan(3)
|
|
195
|
-
|
|
196
|
-
method: 'SEARCH'
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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',
|
|
200
|
+
await t.test('request search body schema', async t => {
|
|
212
201
|
t.plan(4)
|
|
213
202
|
const replyBody = { foo: 'bar', test: 5 }
|
|
214
|
-
|
|
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',
|
|
215
|
+
await t.test('request search body schema error', async t => {
|
|
229
216
|
t.plan(4)
|
|
230
|
-
|
|
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
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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',
|
|
27
|
+
await t.test('request - unlock', async t => {
|
|
29
28
|
t.plan(2)
|
|
30
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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(() =>
|
|
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 =
|
|
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
|
|
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',
|
|
81
|
+
await t.test('http1 get request', async t => {
|
|
84
82
|
t.plan(4)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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',
|
|
99
|
+
await t.test('http1 get error', async t => {
|
|
99
100
|
t.plan(2)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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(
|
|
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
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
package/test/https/https.test.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const
|
|
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',
|
|
39
|
+
await t.test('https get request', async t => {
|
|
39
40
|
t.plan(4)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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',
|
|
54
|
-
t.plan(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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',
|
|
102
|
-
t.plan(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
})
|