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.
- package/.borp.yaml +3 -0
- package/.vscode/settings.json +22 -0
- package/README.md +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +14 -15
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +133 -9
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +58 -57
- package/docs/Guides/Write-Plugin.md +2 -2
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +3 -3
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +55 -58
- package/docs/Reference/Request.md +49 -42
- package/docs/Reference/Routes.md +16 -13
- package/docs/Reference/Server.md +32 -28
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +14 -5
- package/fastify.js +8 -6
- package/lib/contentTypeParser.js +9 -7
- package/lib/context.js +1 -2
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/{logger.js → logger-factory.js} +70 -122
- package/lib/logger-pino.js +68 -0
- package/lib/pluginOverride.js +1 -1
- package/lib/pluginUtils.js +2 -2
- package/lib/reply.js +4 -5
- package/lib/request.js +16 -9
- package/lib/route.js +23 -22
- package/lib/validation.js +2 -2
- package/package.json +13 -15
- package/test/404s.test.js +675 -629
- package/test/500s.test.js +72 -63
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
- package/test/als.test.js +48 -45
- package/test/async-await.test.js +148 -134
- package/test/async-dispose.test.js +4 -5
- package/test/async_hooks.test.js +30 -28
- package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
- package/test/buffer.test.js +9 -10
- package/test/build/error-serializer.test.js +3 -4
- package/test/build/version.test.js +2 -3
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/bundler/esbuild/bundler-test.js +10 -9
- package/test/bundler/webpack/bundler-test.js +10 -9
- package/test/case-insensitive.test.js +31 -28
- package/test/chainable.test.js +4 -5
- package/test/check.test.js +8 -10
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
- package/test/client-timeout.test.js +5 -5
- package/test/close-pipelining.test.js +6 -8
- package/test/conditional-pino.test.js +47 -0
- package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
- package/test/constrained-routes.test.js +243 -236
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +186 -158
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-http-server.test.js +16 -20
- package/test/custom-parser.5.test.js +32 -32
- package/test/diagnostics-channel/404.test.js +15 -15
- package/test/diagnostics-channel/async-delay-request.test.js +25 -25
- package/test/diagnostics-channel/async-request.test.js +24 -24
- package/test/diagnostics-channel/error-before-handler.test.js +4 -5
- package/test/diagnostics-channel/error-request.test.js +19 -19
- package/test/diagnostics-channel/error-status.test.js +8 -8
- package/test/diagnostics-channel/init.test.js +6 -7
- package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
- package/test/diagnostics-channel/sync-request.test.js +19 -19
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/esm/errorCodes.test.mjs +5 -5
- package/test/esm/esm.test.mjs +3 -3
- package/test/esm/named-exports.mjs +3 -3
- package/test/esm/other.mjs +2 -2
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +25 -25
- package/test/http-methods/copy.test.js +22 -24
- package/test/http-methods/custom-http-methods.test.js +24 -21
- package/test/http-methods/get.test.js +97 -84
- package/test/http-methods/head.test.js +63 -57
- package/test/http-methods/lock.test.js +21 -20
- package/test/http-methods/mkcalendar.test.js +31 -27
- package/test/http-methods/mkcol.test.js +10 -10
- package/test/http-methods/move.test.js +11 -11
- package/test/http-methods/propfind.test.js +32 -27
- package/test/http-methods/proppatch.test.js +21 -19
- package/test/http-methods/report.test.js +32 -27
- package/test/http-methods/search.test.js +52 -47
- package/test/http-methods/trace.test.js +3 -4
- package/test/http-methods/unlock.test.js +10 -10
- package/test/http2/closing.test.js +50 -58
- package/test/http2/constraint.test.js +47 -50
- package/test/http2/head.test.js +18 -19
- package/test/http2/missing-http2-module.test.js +4 -5
- package/test/http2/plain.test.js +31 -31
- package/test/http2/secure-with-fallback.test.js +61 -61
- package/test/http2/secure.test.js +28 -31
- package/test/http2/unknown-http-method.test.js +13 -14
- package/test/https/custom-https-server.test.js +6 -7
- package/test/https/https.test.js +78 -78
- package/test/imports.test.js +5 -6
- package/test/internals/all.test.js +8 -11
- package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
- package/test/internals/context.test.js +9 -11
- package/test/internals/decorator.test.js +20 -21
- package/test/internals/errors.test.js +427 -427
- package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
- package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
- package/test/internals/hooks.test.js +31 -35
- package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
- package/test/internals/logger.test.js +28 -28
- package/test/internals/plugin.test.js +17 -18
- package/test/internals/reply-serialize.test.js +106 -106
- package/test/internals/reply.test.js +620 -585
- package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
- package/test/internals/request-validate.test.js +218 -221
- package/test/internals/request.test.js +225 -107
- package/test/internals/server.test.js +15 -12
- package/test/internals/validation.test.js +35 -36
- package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +5 -5
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-error.test.js +122 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.1.test.js +79 -72
- package/test/route.2.test.js +17 -16
- package/test/route.3.test.js +32 -27
- package/test/route.4.test.js +21 -25
- package/test/route.5.test.js +45 -64
- package/test/route.6.test.js +70 -89
- package/test/route.7.test.js +61 -65
- package/test/route.8.test.js +80 -18
- package/test/router-options.test.js +80 -77
- package/test/same-shape.test.js +5 -5
- package/test/schema-examples.test.js +72 -38
- package/test/serialize-response.test.js +9 -10
- package/test/server.test.js +75 -78
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/test-reporter.mjs +68 -0
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +16 -4
- package/test/types/hooks.test-d.ts +2 -1
- package/test/types/instance.test-d.ts +13 -13
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +17 -9
- package/test/types/register.test-d.ts +22 -6
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +34 -4
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/test/versioned-routes.test.js +126 -113
- package/test/web-api.test.js +48 -37
- package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
- package/types/hooks.d.ts +2 -1
- package/types/instance.d.ts +9 -2
- package/types/register.d.ts +12 -3
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +2 -6
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +13 -5
- package/test/types/import.js +0 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
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
|
-
|
|
12
|
+
test.before(buildCertificate)
|
|
13
13
|
|
|
14
|
-
|
|
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.
|
|
20
|
+
t.assert.ok('http2 successfully loaded')
|
|
21
21
|
} catch (e) {
|
|
22
|
-
t.fail('http2 loading failed'
|
|
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.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
49
|
+
done()
|
|
53
50
|
})
|
|
51
|
+
fastify.close()
|
|
54
52
|
})
|
|
55
|
-
|
|
56
|
-
t.end()
|
|
57
53
|
})
|
|
58
54
|
})
|
|
59
55
|
|
|
60
|
-
|
|
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.
|
|
63
|
+
t.assert.ok('http2 successfully loaded')
|
|
68
64
|
} catch (e) {
|
|
69
|
-
t.fail('http2 loading failed'
|
|
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.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
|
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
|
-
|
|
11
|
+
test.before(buildCertificate)
|
|
13
12
|
|
|
14
|
-
test('A route supports host constraints under http2 protocol and secure connection', (t) => {
|
|
15
|
-
t.plan(
|
|
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.
|
|
25
|
+
t.assert.ok(true, 'Key/cert successfully loaded')
|
|
27
26
|
} catch (e) {
|
|
28
|
-
t.fail('Key/cert loading failed'
|
|
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 }
|
|
58
|
-
t.error(err)
|
|
59
|
-
t.teardown(() => { fastify.close() })
|
|
57
|
+
await fastify.listen({ port: 0 })
|
|
60
58
|
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
await t.test('https get request - no constrain', async (t) => {
|
|
60
|
+
t.plan(3)
|
|
63
61
|
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
const url = `https://localhost:${fastify.server.address().port}`
|
|
63
|
+
const res = await h2url.concat({ url })
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
76
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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.
|
|
89
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
url
|
|
94
|
-
})
|
|
86
|
+
await t.test('https get request - constrain - not found', async (t) => {
|
|
87
|
+
t.plan(1)
|
|
95
88
|
|
|
96
|
-
|
|
89
|
+
const url = `https://localhost:${fastify.server.address().port}/beta`
|
|
90
|
+
const res = await h2url.concat({
|
|
91
|
+
url
|
|
97
92
|
})
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
})
|
package/test/http2/head.test.js
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
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 }
|
|
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.
|
|
32
|
+
t.assert.strictEqual(res.headers[':status'], 200)
|
|
34
33
|
})
|
|
35
34
|
})
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
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.
|
|
16
|
-
t.
|
|
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
|
})
|
package/test/http2/plain.test.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
21
|
-
})
|
|
23
|
+
fastify.get('/host', function (req, reply) {
|
|
24
|
+
reply.code(200).send(req.host)
|
|
25
|
+
})
|
|
22
26
|
|
|
23
|
-
fastify.get('/
|
|
24
|
-
|
|
25
|
-
})
|
|
27
|
+
fastify.get('/hostname_port', function (req, reply) {
|
|
28
|
+
reply.code(200).send({ hostname: req.hostname, port: req.port })
|
|
29
|
+
})
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
reply.code(200).send({ hostname: req.hostname, port: req.port })
|
|
29
|
-
})
|
|
31
|
+
t.after(() => { fastify.close() })
|
|
30
32
|
|
|
31
|
-
fastify.listen({ port: 0 }
|
|
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.
|
|
42
|
-
t.
|
|
41
|
+
t.assert.strictEqual(res.headers[':status'], 200)
|
|
42
|
+
t.assert.strictEqual(res.headers['content-length'], '' + JSON.stringify(msg).length)
|
|
43
43
|
|
|
44
|
-
t.
|
|
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.
|
|
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.
|
|
66
|
-
t.
|
|
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
|
|
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
|
-
|
|
10
|
+
test.before(buildCertificate)
|
|
12
11
|
|
|
13
|
-
test('secure with fallback', (t) => {
|
|
14
|
-
t.plan(
|
|
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.
|
|
25
|
+
t.assert.ok(true, 'Key/cert successfully loaded')
|
|
27
26
|
} catch (e) {
|
|
28
|
-
t.fail('Key/cert loading failed'
|
|
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
|
-
|
|
44
|
-
t.error(err)
|
|
45
|
-
t.teardown(() => { fastify.close() })
|
|
42
|
+
t.after(() => { fastify.close() })
|
|
46
43
|
|
|
47
|
-
|
|
48
|
-
t.plan(1)
|
|
44
|
+
await fastify.listen({ port: 0 })
|
|
49
45
|
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
await t.test('https get error', async (t) => {
|
|
47
|
+
t.plan(1)
|
|
52
48
|
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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.
|
|
74
|
-
|
|
68
|
+
t.assert.strictEqual(res.headers[':status'], 200)
|
|
69
|
+
t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'http2' })
|
|
70
|
+
})
|
|
75
71
|
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
await t.test('https get request', async (t) => {
|
|
73
|
+
t.plan(3)
|
|
78
74
|
|
|
79
|
-
|
|
80
|
-
|
|
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.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
})
|