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,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
|
|
|
7
6
|
test('should remove content-type for setErrorHandler', async t => {
|
|
@@ -10,22 +9,22 @@ test('should remove content-type for setErrorHandler', async t => {
|
|
|
10
9
|
|
|
11
10
|
const fastify = Fastify()
|
|
12
11
|
fastify.setErrorHandler(function (error, request, reply) {
|
|
13
|
-
t.
|
|
14
|
-
t.
|
|
12
|
+
t.assert.strictEqual(error.message, 'kaboom')
|
|
13
|
+
t.assert.strictEqual(reply.hasHeader('content-type'), false)
|
|
15
14
|
reply.code(400).send({ foo: 'bar' })
|
|
16
15
|
})
|
|
17
16
|
fastify.addHook('onSend', async function (request, reply, payload) {
|
|
18
17
|
count++
|
|
19
|
-
t.
|
|
18
|
+
t.assert.strictEqual(typeof payload, 'string')
|
|
20
19
|
switch (count) {
|
|
21
20
|
case 1: {
|
|
22
21
|
// should guess the correct content-type based on payload
|
|
23
|
-
t.
|
|
22
|
+
t.assert.strictEqual(reply.getHeader('content-type'), 'text/plain; charset=utf-8')
|
|
24
23
|
throw Error('kaboom')
|
|
25
24
|
}
|
|
26
25
|
case 2: {
|
|
27
26
|
// should guess the correct content-type based on payload
|
|
28
|
-
t.
|
|
27
|
+
t.assert.strictEqual(reply.getHeader('content-type'), 'application/json; charset=utf-8')
|
|
29
28
|
return payload
|
|
30
29
|
}
|
|
31
30
|
default: {
|
|
@@ -38,6 +37,6 @@ test('should remove content-type for setErrorHandler', async t => {
|
|
|
38
37
|
})
|
|
39
38
|
|
|
40
39
|
const { statusCode, body } = await fastify.inject({ method: 'GET', path: '/' })
|
|
41
|
-
t.
|
|
42
|
-
t.
|
|
40
|
+
t.assert.strictEqual(statusCode, 400)
|
|
41
|
+
t.assert.strictEqual(body, JSON.stringify({ foo: 'bar' }))
|
|
43
42
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
|
|
5
5
|
const { kRouteContext } = require('../lib/symbols')
|
|
6
6
|
const Fastify = require('..')
|
|
7
7
|
|
|
@@ -17,8 +17,8 @@ function handler (req, reply) {
|
|
|
17
17
|
reply.send(reply[kRouteContext].config)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
test('config', t => {
|
|
21
|
-
t.plan(
|
|
20
|
+
test('config', async t => {
|
|
21
|
+
t.plan(6)
|
|
22
22
|
const fastify = Fastify()
|
|
23
23
|
|
|
24
24
|
fastify.get('/get', {
|
|
@@ -41,36 +41,33 @@ test('config', t => {
|
|
|
41
41
|
handler
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
fastify.inject({
|
|
44
|
+
let response = await fastify.inject({
|
|
45
45
|
method: 'GET',
|
|
46
|
-
url: '/
|
|
47
|
-
}, (err, response) => {
|
|
48
|
-
t.error(err)
|
|
49
|
-
t.equal(response.statusCode, 200)
|
|
50
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/get', method: 'GET' }, schema.config))
|
|
46
|
+
url: '/route'
|
|
51
47
|
})
|
|
52
48
|
|
|
53
|
-
|
|
49
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
50
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
51
|
+
|
|
52
|
+
response = await fastify.inject({
|
|
54
53
|
method: 'GET',
|
|
55
54
|
url: '/route'
|
|
56
|
-
}, (err, response) => {
|
|
57
|
-
t.error(err)
|
|
58
|
-
t.equal(response.statusCode, 200)
|
|
59
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
60
55
|
})
|
|
61
56
|
|
|
62
|
-
|
|
57
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
58
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
59
|
+
|
|
60
|
+
response = await fastify.inject({
|
|
63
61
|
method: 'GET',
|
|
64
62
|
url: '/no-config'
|
|
65
|
-
}, (err, response) => {
|
|
66
|
-
t.error(err)
|
|
67
|
-
t.equal(response.statusCode, 200)
|
|
68
|
-
t.same(JSON.parse(response.payload), { url: '/no-config', method: 'GET' })
|
|
69
63
|
})
|
|
64
|
+
|
|
65
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
66
|
+
t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
|
|
70
67
|
})
|
|
71
68
|
|
|
72
|
-
test('config with exposeHeadRoutes', t => {
|
|
73
|
-
t.plan(
|
|
69
|
+
test('config with exposeHeadRoutes', async t => {
|
|
70
|
+
t.plan(6)
|
|
74
71
|
const fastify = Fastify({ exposeHeadRoutes: true })
|
|
75
72
|
|
|
76
73
|
fastify.get('/get', {
|
|
@@ -93,36 +90,33 @@ test('config with exposeHeadRoutes', t => {
|
|
|
93
90
|
handler
|
|
94
91
|
})
|
|
95
92
|
|
|
96
|
-
fastify.inject({
|
|
93
|
+
let response = await fastify.inject({
|
|
97
94
|
method: 'GET',
|
|
98
95
|
url: '/get'
|
|
99
|
-
}, (err, response) => {
|
|
100
|
-
t.error(err)
|
|
101
|
-
t.equal(response.statusCode, 200)
|
|
102
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/get', method: 'GET' }, schema.config))
|
|
103
96
|
})
|
|
104
97
|
|
|
105
|
-
|
|
98
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
99
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))
|
|
100
|
+
|
|
101
|
+
response = await fastify.inject({
|
|
106
102
|
method: 'GET',
|
|
107
103
|
url: '/route'
|
|
108
|
-
}, (err, response) => {
|
|
109
|
-
t.error(err)
|
|
110
|
-
t.equal(response.statusCode, 200)
|
|
111
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
112
104
|
})
|
|
113
105
|
|
|
114
|
-
|
|
106
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
107
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
108
|
+
|
|
109
|
+
response = await fastify.inject({
|
|
115
110
|
method: 'GET',
|
|
116
111
|
url: '/no-config'
|
|
117
|
-
}, (err, response) => {
|
|
118
|
-
t.error(err)
|
|
119
|
-
t.equal(response.statusCode, 200)
|
|
120
|
-
t.same(JSON.parse(response.payload), { url: '/no-config', method: 'GET' })
|
|
121
112
|
})
|
|
113
|
+
|
|
114
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
115
|
+
t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
|
|
122
116
|
})
|
|
123
117
|
|
|
124
|
-
test('config without exposeHeadRoutes', t => {
|
|
125
|
-
t.plan(
|
|
118
|
+
test('config without exposeHeadRoutes', async t => {
|
|
119
|
+
t.plan(6)
|
|
126
120
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
127
121
|
|
|
128
122
|
fastify.get('/get', {
|
|
@@ -145,30 +139,26 @@ test('config without exposeHeadRoutes', t => {
|
|
|
145
139
|
handler
|
|
146
140
|
})
|
|
147
141
|
|
|
148
|
-
fastify.inject({
|
|
142
|
+
let response = await fastify.inject({
|
|
149
143
|
method: 'GET',
|
|
150
144
|
url: '/get'
|
|
151
|
-
}, (err, response) => {
|
|
152
|
-
t.error(err)
|
|
153
|
-
t.equal(response.statusCode, 200)
|
|
154
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/get', method: 'GET' }, schema.config))
|
|
155
145
|
})
|
|
156
146
|
|
|
157
|
-
|
|
147
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
148
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))
|
|
149
|
+
|
|
150
|
+
response = await fastify.inject({
|
|
158
151
|
method: 'GET',
|
|
159
152
|
url: '/route'
|
|
160
|
-
}, (err, response) => {
|
|
161
|
-
t.error(err)
|
|
162
|
-
t.equal(response.statusCode, 200)
|
|
163
|
-
t.same(JSON.parse(response.payload), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
164
153
|
})
|
|
154
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
155
|
+
t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))
|
|
165
156
|
|
|
166
|
-
fastify.inject({
|
|
157
|
+
response = await fastify.inject({
|
|
167
158
|
method: 'GET',
|
|
168
159
|
url: '/no-config'
|
|
169
|
-
}, (err, response) => {
|
|
170
|
-
t.error(err)
|
|
171
|
-
t.equal(response.statusCode, 200)
|
|
172
|
-
t.same(JSON.parse(response.payload), { url: '/no-config', method: 'GET' })
|
|
173
160
|
})
|
|
161
|
+
|
|
162
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
163
|
+
t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
|
|
174
164
|
})
|
|
@@ -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 http = require('node:http')
|
|
6
5
|
const dns = require('node:dns').promises
|
|
7
6
|
const sget = require('simple-get').concat
|
|
@@ -16,7 +15,7 @@ async function setup () {
|
|
|
16
15
|
|
|
17
16
|
const fastify = Fastify({
|
|
18
17
|
serverFactory: (handler, opts) => {
|
|
19
|
-
t.ok(opts.serverFactory, 'it is called once for localhost')
|
|
18
|
+
t.assert.ok(opts.serverFactory, 'it is called once for localhost')
|
|
20
19
|
|
|
21
20
|
const server = http.createServer((req, res) => {
|
|
22
21
|
req.custom = true
|
|
@@ -27,10 +26,9 @@ async function setup () {
|
|
|
27
26
|
}
|
|
28
27
|
})
|
|
29
28
|
|
|
30
|
-
t.
|
|
31
|
-
|
|
29
|
+
t.after(() => fastify.close())
|
|
32
30
|
fastify.get('/', (req, reply) => {
|
|
33
|
-
t.ok(req.raw.custom)
|
|
31
|
+
t.assert.ok(req.raw.custom)
|
|
34
32
|
reply.send({ hello: 'world' })
|
|
35
33
|
})
|
|
36
34
|
|
|
@@ -45,8 +43,8 @@ async function setup () {
|
|
|
45
43
|
if (err) {
|
|
46
44
|
return reject(err)
|
|
47
45
|
}
|
|
48
|
-
t.
|
|
49
|
-
t.
|
|
46
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
47
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
50
48
|
resolve()
|
|
51
49
|
})
|
|
52
50
|
})
|
|
@@ -55,7 +53,7 @@ async function setup () {
|
|
|
55
53
|
test('Should not allow forceCloseConnection=idle if the server does not support closeIdleConnections', t => {
|
|
56
54
|
t.plan(1)
|
|
57
55
|
|
|
58
|
-
t.throws(
|
|
56
|
+
t.assert.throws(
|
|
59
57
|
() => {
|
|
60
58
|
Fastify({
|
|
61
59
|
forceCloseConnections: 'idle',
|
|
@@ -75,13 +73,11 @@ async function setup () {
|
|
|
75
73
|
|
|
76
74
|
test('Should accept user defined serverFactory and ignore secondary server creation', async t => {
|
|
77
75
|
const server = http.createServer(() => { })
|
|
78
|
-
t.
|
|
79
|
-
const app =
|
|
76
|
+
t.after(() => new Promise(resolve => server.close(resolve)))
|
|
77
|
+
const app = Fastify({
|
|
80
78
|
serverFactory: () => server
|
|
81
79
|
})
|
|
82
|
-
t.
|
|
83
|
-
await app.listen({ port: 0 })
|
|
84
|
-
})
|
|
80
|
+
await t.assert.doesNotReject(async () => { await app.listen({ port: 0 }) })
|
|
85
81
|
})
|
|
86
82
|
|
|
87
83
|
test('Should not call close on the server if it has not created it', async t => {
|
|
@@ -107,12 +103,12 @@ async function setup () {
|
|
|
107
103
|
})
|
|
108
104
|
|
|
109
105
|
const address = server.address()
|
|
110
|
-
t.
|
|
106
|
+
t.assert.strictEqual(server.listening, true)
|
|
111
107
|
await fastify.close()
|
|
112
108
|
|
|
113
|
-
t.
|
|
114
|
-
t.
|
|
115
|
-
t.
|
|
109
|
+
t.assert.strictEqual(server.listening, true)
|
|
110
|
+
t.assert.deepStrictEqual(server.address(), address)
|
|
111
|
+
t.assert.deepStrictEqual(fastify.addresses(), [address])
|
|
116
112
|
|
|
117
113
|
await new Promise((resolve, reject) => {
|
|
118
114
|
server.close((err) => {
|
|
@@ -122,8 +118,8 @@ async function setup () {
|
|
|
122
118
|
resolve()
|
|
123
119
|
})
|
|
124
120
|
})
|
|
125
|
-
t.
|
|
126
|
-
t.
|
|
121
|
+
t.assert.strictEqual(server.listening, false)
|
|
122
|
+
t.assert.deepStrictEqual(server.address(), null)
|
|
127
123
|
})
|
|
128
124
|
}
|
|
129
125
|
|
|
@@ -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 sget = require('simple-get').concat
|
|
6
5
|
const Fastify = require('../fastify')
|
|
7
6
|
const jsonParser = require('fast-json-body')
|
|
@@ -9,35 +8,34 @@ const { getServerUrl, plainTextParser } = require('./helper')
|
|
|
9
8
|
|
|
10
9
|
process.removeAllListeners('warning')
|
|
11
10
|
|
|
12
|
-
test('cannot remove all content type parsers after binding', t => {
|
|
11
|
+
test('cannot remove all content type parsers after binding', (t, done) => {
|
|
13
12
|
t.plan(2)
|
|
14
13
|
|
|
15
14
|
const fastify = Fastify()
|
|
16
15
|
|
|
17
|
-
t.teardown(fastify.close.bind(fastify))
|
|
18
|
-
|
|
19
16
|
fastify.listen({ port: 0 }, function (err) {
|
|
20
|
-
t.
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
t.assert.ifError(err)
|
|
18
|
+
t.assert.throws(() => fastify.removeAllContentTypeParsers())
|
|
19
|
+
fastify.close()
|
|
20
|
+
done()
|
|
23
21
|
})
|
|
24
22
|
})
|
|
25
23
|
|
|
26
|
-
test('cannot remove content type parsers after binding', t => {
|
|
24
|
+
test('cannot remove content type parsers after binding', (t, done) => {
|
|
27
25
|
t.plan(2)
|
|
28
26
|
|
|
29
27
|
const fastify = Fastify()
|
|
30
28
|
|
|
31
|
-
t.
|
|
29
|
+
t.after(() => fastify.close())
|
|
32
30
|
|
|
33
31
|
fastify.listen({ port: 0 }, function (err) {
|
|
34
|
-
t.
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
t.assert.ifError(err)
|
|
33
|
+
t.assert.throws(() => fastify.removeContentTypeParser('application/json'))
|
|
34
|
+
done()
|
|
37
35
|
})
|
|
38
36
|
})
|
|
39
37
|
|
|
40
|
-
test('should be able to override the default json parser after removeAllContentTypeParsers', t => {
|
|
38
|
+
test('should be able to override the default json parser after removeAllContentTypeParsers', (t, done) => {
|
|
41
39
|
t.plan(5)
|
|
42
40
|
|
|
43
41
|
const fastify = Fastify()
|
|
@@ -49,14 +47,14 @@ test('should be able to override the default json parser after removeAllContentT
|
|
|
49
47
|
fastify.removeAllContentTypeParsers()
|
|
50
48
|
|
|
51
49
|
fastify.addContentTypeParser('application/json', function (req, payload, done) {
|
|
52
|
-
t.ok('called')
|
|
50
|
+
t.assert.ok('called')
|
|
53
51
|
jsonParser(payload, function (err, body) {
|
|
54
52
|
done(err, body)
|
|
55
53
|
})
|
|
56
54
|
})
|
|
57
55
|
|
|
58
56
|
fastify.listen({ port: 0 }, err => {
|
|
59
|
-
t.
|
|
57
|
+
t.assert.ifError(err)
|
|
60
58
|
|
|
61
59
|
sget({
|
|
62
60
|
method: 'POST',
|
|
@@ -66,15 +64,16 @@ test('should be able to override the default json parser after removeAllContentT
|
|
|
66
64
|
'Content-Type': 'application/json'
|
|
67
65
|
}
|
|
68
66
|
}, (err, response, body) => {
|
|
69
|
-
t.
|
|
70
|
-
t.
|
|
71
|
-
t.
|
|
67
|
+
t.assert.ifError(err)
|
|
68
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
69
|
+
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
72
70
|
fastify.close()
|
|
71
|
+
done()
|
|
73
72
|
})
|
|
74
73
|
})
|
|
75
74
|
})
|
|
76
75
|
|
|
77
|
-
test('should be able to override the default plain text parser after removeAllContentTypeParsers', t => {
|
|
76
|
+
test('should be able to override the default plain text parser after removeAllContentTypeParsers', (t, done) => {
|
|
78
77
|
t.plan(5)
|
|
79
78
|
|
|
80
79
|
const fastify = Fastify()
|
|
@@ -86,14 +85,14 @@ test('should be able to override the default plain text parser after removeAllCo
|
|
|
86
85
|
fastify.removeAllContentTypeParsers()
|
|
87
86
|
|
|
88
87
|
fastify.addContentTypeParser('text/plain', function (req, payload, done) {
|
|
89
|
-
t.ok('called')
|
|
88
|
+
t.assert.ok('called')
|
|
90
89
|
plainTextParser(payload, function (err, body) {
|
|
91
90
|
done(err, body)
|
|
92
91
|
})
|
|
93
92
|
})
|
|
94
93
|
|
|
95
94
|
fastify.listen({ port: 0 }, err => {
|
|
96
|
-
t.
|
|
95
|
+
t.assert.ifError(err)
|
|
97
96
|
|
|
98
97
|
sget({
|
|
99
98
|
method: 'POST',
|
|
@@ -103,15 +102,16 @@ test('should be able to override the default plain text parser after removeAllCo
|
|
|
103
102
|
'Content-Type': 'text/plain'
|
|
104
103
|
}
|
|
105
104
|
}, (err, response, body) => {
|
|
106
|
-
t.
|
|
107
|
-
t.
|
|
108
|
-
t.
|
|
105
|
+
t.assert.ifError(err)
|
|
106
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
107
|
+
t.assert.strictEqual(body.toString(), 'hello world')
|
|
109
108
|
fastify.close()
|
|
109
|
+
done()
|
|
110
110
|
})
|
|
111
111
|
})
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
test('should be able to add a custom content type parser after removeAllContentTypeParsers', t => {
|
|
114
|
+
test('should be able to add a custom content type parser after removeAllContentTypeParsers', (t, done) => {
|
|
115
115
|
t.plan(5)
|
|
116
116
|
|
|
117
117
|
const fastify = Fastify()
|
|
@@ -121,16 +121,15 @@ test('should be able to add a custom content type parser after removeAllContentT
|
|
|
121
121
|
})
|
|
122
122
|
|
|
123
123
|
fastify.removeAllContentTypeParsers()
|
|
124
|
-
|
|
125
124
|
fastify.addContentTypeParser('application/jsoff', function (req, payload, done) {
|
|
126
|
-
t.ok('called')
|
|
125
|
+
t.assert.ok('called')
|
|
127
126
|
jsonParser(payload, function (err, body) {
|
|
128
127
|
done(err, body)
|
|
129
128
|
})
|
|
130
129
|
})
|
|
131
130
|
|
|
132
131
|
fastify.listen({ port: 0 }, err => {
|
|
133
|
-
t.
|
|
132
|
+
t.assert.ifError(err)
|
|
134
133
|
|
|
135
134
|
sget({
|
|
136
135
|
method: 'POST',
|
|
@@ -140,10 +139,11 @@ test('should be able to add a custom content type parser after removeAllContentT
|
|
|
140
139
|
'Content-Type': 'application/jsoff'
|
|
141
140
|
}
|
|
142
141
|
}, (err, response, body) => {
|
|
143
|
-
t.
|
|
144
|
-
t.
|
|
145
|
-
t.
|
|
142
|
+
t.assert.ifError(err)
|
|
143
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
144
|
+
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
146
145
|
fastify.close()
|
|
146
|
+
done()
|
|
147
147
|
})
|
|
148
148
|
})
|
|
149
149
|
})
|
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const diagnostics = require('node:diagnostics_channel')
|
|
5
|
-
const test = t.test
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
const Fastify = require('../..')
|
|
8
7
|
const { getServerUrl } = require('../helper')
|
|
9
8
|
const Request = require('../../lib/request')
|
|
10
9
|
const Reply = require('../../lib/reply')
|
|
11
10
|
|
|
12
|
-
test('diagnostics channel sync events fire in expected order', t => {
|
|
11
|
+
test('diagnostics channel sync events fire in expected order', (t, done) => {
|
|
13
12
|
t.plan(9)
|
|
14
13
|
let callOrder = 0
|
|
15
14
|
let firstEncounteredMessage
|
|
16
15
|
|
|
17
16
|
diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
|
|
18
|
-
t.
|
|
17
|
+
t.assert.strictEqual(callOrder++, 0)
|
|
19
18
|
firstEncounteredMessage = msg
|
|
20
|
-
t.ok(msg.request instanceof Request)
|
|
21
|
-
t.ok(msg.reply instanceof Reply)
|
|
19
|
+
t.assert.ok(msg.request instanceof Request)
|
|
20
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
22
21
|
})
|
|
23
22
|
|
|
24
23
|
diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
|
|
25
|
-
t.ok(msg.request instanceof Request)
|
|
26
|
-
t.ok(msg.reply instanceof Reply)
|
|
27
|
-
t.
|
|
28
|
-
t.
|
|
24
|
+
t.assert.ok(msg.request instanceof Request)
|
|
25
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
26
|
+
t.assert.strictEqual(callOrder++, 1)
|
|
27
|
+
t.assert.strictEqual(msg, firstEncounteredMessage)
|
|
29
28
|
})
|
|
30
29
|
|
|
31
30
|
diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
|
|
32
|
-
t.fail('should not trigger error channel')
|
|
31
|
+
t.assert.fail('should not trigger error channel')
|
|
33
32
|
})
|
|
34
33
|
|
|
35
34
|
const fastify = Fastify()
|
|
@@ -42,16 +41,17 @@ test('diagnostics channel sync events fire in expected order', t => {
|
|
|
42
41
|
})
|
|
43
42
|
|
|
44
43
|
fastify.listen({ port: 0 }, function (err) {
|
|
45
|
-
if (err) t.
|
|
44
|
+
if (err) t.assert.ifError(err)
|
|
46
45
|
|
|
47
|
-
t.
|
|
46
|
+
t.after(() => { fastify.close() })
|
|
48
47
|
|
|
49
48
|
sget({
|
|
50
49
|
method: 'GET',
|
|
51
50
|
url: getServerUrl(fastify) + '/'
|
|
52
51
|
}, (err, response, body) => {
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
52
|
+
t.assert.ifError(err)
|
|
53
|
+
t.assert.strictEqual(response.statusCode, 404)
|
|
54
|
+
done()
|
|
55
55
|
})
|
|
56
56
|
})
|
|
57
57
|
})
|
|
@@ -1,50 +1,49 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const t = require('tap')
|
|
4
3
|
const diagnostics = require('node:diagnostics_channel')
|
|
5
|
-
const test =
|
|
4
|
+
const { test } = require('node:test')
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
const Fastify = require('../..')
|
|
8
7
|
const { getServerUrl } = require('../helper')
|
|
9
8
|
const Request = require('../../lib/request')
|
|
10
9
|
const Reply = require('../../lib/reply')
|
|
11
10
|
|
|
12
|
-
test('diagnostics channel async events fire in expected order', t => {
|
|
11
|
+
test('diagnostics channel async events fire in expected order', (t, done) => {
|
|
13
12
|
t.plan(19)
|
|
14
13
|
let callOrder = 0
|
|
15
14
|
let firstEncounteredMessage
|
|
16
15
|
|
|
17
16
|
diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
|
|
18
|
-
t.
|
|
17
|
+
t.assert.strictEqual(callOrder++, 0)
|
|
19
18
|
firstEncounteredMessage = msg
|
|
20
|
-
t.ok(msg.request instanceof Request)
|
|
21
|
-
t.ok(msg.reply instanceof Reply)
|
|
19
|
+
t.assert.ok(msg.request instanceof Request)
|
|
20
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
22
21
|
})
|
|
23
22
|
|
|
24
23
|
diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
|
|
25
|
-
t.
|
|
26
|
-
t.ok(msg.request instanceof Request)
|
|
27
|
-
t.ok(msg.reply instanceof Reply)
|
|
28
|
-
t.
|
|
29
|
-
t.
|
|
24
|
+
t.assert.strictEqual(callOrder++, 1)
|
|
25
|
+
t.assert.ok(msg.request instanceof Request)
|
|
26
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
27
|
+
t.assert.strictEqual(msg, firstEncounteredMessage)
|
|
28
|
+
t.assert.strictEqual(msg.async, true)
|
|
30
29
|
})
|
|
31
30
|
|
|
32
31
|
diagnostics.subscribe('tracing:fastify.request.handler:asyncStart', (msg) => {
|
|
33
|
-
t.
|
|
34
|
-
t.ok(msg.request instanceof Request)
|
|
35
|
-
t.ok(msg.reply instanceof Reply)
|
|
36
|
-
t.
|
|
32
|
+
t.assert.strictEqual(callOrder++, 2)
|
|
33
|
+
t.assert.ok(msg.request instanceof Request)
|
|
34
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
35
|
+
t.assert.strictEqual(msg, firstEncounteredMessage)
|
|
37
36
|
})
|
|
38
37
|
|
|
39
38
|
diagnostics.subscribe('tracing:fastify.request.handler:asyncEnd', (msg) => {
|
|
40
|
-
t.
|
|
41
|
-
t.ok(msg.request instanceof Request)
|
|
42
|
-
t.ok(msg.reply instanceof Reply)
|
|
43
|
-
t.
|
|
39
|
+
t.assert.strictEqual(callOrder++, 3)
|
|
40
|
+
t.assert.ok(msg.request instanceof Request)
|
|
41
|
+
t.assert.ok(msg.reply instanceof Reply)
|
|
42
|
+
t.assert.strictEqual(msg, firstEncounteredMessage)
|
|
44
43
|
})
|
|
45
44
|
|
|
46
45
|
diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
|
|
47
|
-
t.fail('should not trigger error channel')
|
|
46
|
+
t.assert.fail('should not trigger error channel')
|
|
48
47
|
})
|
|
49
48
|
|
|
50
49
|
const fastify = Fastify()
|
|
@@ -58,17 +57,18 @@ test('diagnostics channel async events fire in expected order', t => {
|
|
|
58
57
|
})
|
|
59
58
|
|
|
60
59
|
fastify.listen({ port: 0 }, function (err) {
|
|
61
|
-
if (err) t.
|
|
60
|
+
if (err) t.assert.ifError(err)
|
|
62
61
|
|
|
63
|
-
t.
|
|
62
|
+
t.after(() => { fastify.close() })
|
|
64
63
|
|
|
65
64
|
sget({
|
|
66
65
|
method: 'GET',
|
|
67
66
|
url: getServerUrl(fastify) + '/'
|
|
68
67
|
}, (err, response, body) => {
|
|
69
|
-
t.
|
|
70
|
-
t.
|
|
71
|
-
t.
|
|
68
|
+
t.assert.ifError(err)
|
|
69
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
70
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
71
|
+
done()
|
|
72
72
|
})
|
|
73
73
|
})
|
|
74
74
|
})
|