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
package/test/route.1.test.js
CHANGED
|
@@ -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('..')
|
|
7
6
|
const {
|
|
@@ -10,15 +9,14 @@ const {
|
|
|
10
9
|
} = require('../lib/errors')
|
|
11
10
|
const { getServerUrl } = require('./helper')
|
|
12
11
|
|
|
13
|
-
test('route', t => {
|
|
12
|
+
test('route', async t => {
|
|
14
13
|
t.plan(10)
|
|
15
|
-
const test = t.test
|
|
16
14
|
|
|
17
|
-
test('route - get', t => {
|
|
15
|
+
await t.test('route - get', (t, done) => {
|
|
18
16
|
t.plan(4)
|
|
19
17
|
|
|
20
18
|
const fastify = Fastify()
|
|
21
|
-
t.doesNotThrow(() =>
|
|
19
|
+
t.assert.doesNotThrow(() =>
|
|
22
20
|
fastify.route({
|
|
23
21
|
method: 'GET',
|
|
24
22
|
url: '/',
|
|
@@ -41,24 +39,25 @@ test('route', t => {
|
|
|
41
39
|
)
|
|
42
40
|
|
|
43
41
|
fastify.listen({ port: 0 }, function (err) {
|
|
44
|
-
if (err) t.
|
|
45
|
-
t.
|
|
42
|
+
if (err) t.assert.ifError(err)
|
|
43
|
+
t.after(() => { fastify.close() })
|
|
46
44
|
sget({
|
|
47
45
|
method: 'GET',
|
|
48
46
|
url: getServerUrl(fastify) + '/'
|
|
49
47
|
}, (err, response, body) => {
|
|
50
|
-
t.
|
|
51
|
-
t.
|
|
52
|
-
t.
|
|
48
|
+
t.assert.ifError(err)
|
|
49
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
50
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
51
|
+
done()
|
|
53
52
|
})
|
|
54
53
|
})
|
|
55
54
|
})
|
|
56
55
|
|
|
57
|
-
test('missing schema - route', t => {
|
|
56
|
+
await t.test('missing schema - route', (t, done) => {
|
|
58
57
|
t.plan(4)
|
|
59
58
|
|
|
60
59
|
const fastify = Fastify()
|
|
61
|
-
t.doesNotThrow(() =>
|
|
60
|
+
t.assert.doesNotThrow(() =>
|
|
62
61
|
fastify.route({
|
|
63
62
|
method: 'GET',
|
|
64
63
|
url: '/missing',
|
|
@@ -69,31 +68,32 @@ test('route', t => {
|
|
|
69
68
|
)
|
|
70
69
|
|
|
71
70
|
fastify.listen({ port: 0 }, function (err) {
|
|
72
|
-
if (err) t.
|
|
73
|
-
t.
|
|
71
|
+
if (err) t.assert.ifError(err)
|
|
72
|
+
t.after(() => { fastify.close() })
|
|
74
73
|
sget({
|
|
75
74
|
method: 'GET',
|
|
76
75
|
url: getServerUrl(fastify) + '/missing'
|
|
77
76
|
}, (err, response, body) => {
|
|
78
|
-
t.
|
|
79
|
-
t.
|
|
80
|
-
t.
|
|
77
|
+
t.assert.ifError(err)
|
|
78
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
79
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
80
|
+
done()
|
|
81
81
|
})
|
|
82
82
|
})
|
|
83
83
|
})
|
|
84
84
|
|
|
85
|
-
test('invalid handler attribute - route', t => {
|
|
85
|
+
await t.test('invalid handler attribute - route', t => {
|
|
86
86
|
t.plan(1)
|
|
87
87
|
|
|
88
88
|
const fastify = Fastify()
|
|
89
|
-
t.throws(() => fastify.get('/', { handler: 'not a function' }, () => { }))
|
|
89
|
+
t.assert.throws(() => fastify.get('/', { handler: 'not a function' }, () => { }))
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
-
test('Add Multiple methods per route all uppercase', t => {
|
|
92
|
+
await t.test('Add Multiple methods per route all uppercase', (t, done) => {
|
|
93
93
|
t.plan(7)
|
|
94
94
|
|
|
95
95
|
const fastify = Fastify()
|
|
96
|
-
t.doesNotThrow(() =>
|
|
96
|
+
t.assert.doesNotThrow(() =>
|
|
97
97
|
fastify.route({
|
|
98
98
|
method: ['GET', 'DELETE'],
|
|
99
99
|
url: '/multiple',
|
|
@@ -103,33 +103,34 @@ test('route', t => {
|
|
|
103
103
|
}))
|
|
104
104
|
|
|
105
105
|
fastify.listen({ port: 0 }, function (err) {
|
|
106
|
-
if (err) t.
|
|
107
|
-
t.
|
|
106
|
+
if (err) t.assert.ifError(err)
|
|
107
|
+
t.after(() => { fastify.close() })
|
|
108
108
|
sget({
|
|
109
109
|
method: 'GET',
|
|
110
110
|
url: getServerUrl(fastify) + '/multiple'
|
|
111
111
|
}, (err, response, body) => {
|
|
112
|
-
t.
|
|
113
|
-
t.
|
|
114
|
-
t.
|
|
112
|
+
t.assert.ifError(err)
|
|
113
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
114
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
115
115
|
})
|
|
116
116
|
|
|
117
117
|
sget({
|
|
118
118
|
method: 'DELETE',
|
|
119
119
|
url: getServerUrl(fastify) + '/multiple'
|
|
120
120
|
}, (err, response, body) => {
|
|
121
|
-
t.
|
|
122
|
-
t.
|
|
123
|
-
t.
|
|
121
|
+
t.assert.ifError(err)
|
|
122
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
123
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
124
|
+
done()
|
|
124
125
|
})
|
|
125
126
|
})
|
|
126
127
|
})
|
|
127
128
|
|
|
128
|
-
test('Add Multiple methods per route all lowercase', t => {
|
|
129
|
+
await t.test('Add Multiple methods per route all lowercase', (t, done) => {
|
|
129
130
|
t.plan(7)
|
|
130
131
|
|
|
131
132
|
const fastify = Fastify()
|
|
132
|
-
t.doesNotThrow(() =>
|
|
133
|
+
t.assert.doesNotThrow(() =>
|
|
133
134
|
fastify.route({
|
|
134
135
|
method: ['get', 'delete'],
|
|
135
136
|
url: '/multiple',
|
|
@@ -139,33 +140,34 @@ test('route', t => {
|
|
|
139
140
|
}))
|
|
140
141
|
|
|
141
142
|
fastify.listen({ port: 0 }, function (err) {
|
|
142
|
-
if (err) t.
|
|
143
|
-
t.
|
|
143
|
+
if (err) t.assert.ifError(err)
|
|
144
|
+
t.after(() => { fastify.close() })
|
|
144
145
|
sget({
|
|
145
146
|
method: 'GET',
|
|
146
147
|
url: getServerUrl(fastify) + '/multiple'
|
|
147
148
|
}, (err, response, body) => {
|
|
148
|
-
t.
|
|
149
|
-
t.
|
|
150
|
-
t.
|
|
149
|
+
t.assert.ifError(err)
|
|
150
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
151
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
151
152
|
})
|
|
152
153
|
|
|
153
154
|
sget({
|
|
154
155
|
method: 'DELETE',
|
|
155
156
|
url: getServerUrl(fastify) + '/multiple'
|
|
156
157
|
}, (err, response, body) => {
|
|
157
|
-
t.
|
|
158
|
-
t.
|
|
159
|
-
t.
|
|
158
|
+
t.assert.ifError(err)
|
|
159
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
160
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
161
|
+
done()
|
|
160
162
|
})
|
|
161
163
|
})
|
|
162
164
|
})
|
|
163
165
|
|
|
164
|
-
test('Add Multiple methods per route mixed uppercase and lowercase', t => {
|
|
166
|
+
await t.test('Add Multiple methods per route mixed uppercase and lowercase', (t, done) => {
|
|
165
167
|
t.plan(7)
|
|
166
168
|
|
|
167
169
|
const fastify = Fastify()
|
|
168
|
-
t.doesNotThrow(() =>
|
|
170
|
+
t.assert.doesNotThrow(() =>
|
|
169
171
|
fastify.route({
|
|
170
172
|
method: ['GET', 'delete'],
|
|
171
173
|
url: '/multiple',
|
|
@@ -175,33 +177,34 @@ test('route', t => {
|
|
|
175
177
|
}))
|
|
176
178
|
|
|
177
179
|
fastify.listen({ port: 0 }, function (err) {
|
|
178
|
-
if (err) t.
|
|
179
|
-
t.
|
|
180
|
+
if (err) t.assert.ifError(err)
|
|
181
|
+
t.after(() => { fastify.close() })
|
|
180
182
|
sget({
|
|
181
183
|
method: 'GET',
|
|
182
184
|
url: getServerUrl(fastify) + '/multiple'
|
|
183
185
|
}, (err, response, body) => {
|
|
184
|
-
t.
|
|
185
|
-
t.
|
|
186
|
-
t.
|
|
186
|
+
t.assert.ifError(err)
|
|
187
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
188
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
187
189
|
})
|
|
188
190
|
|
|
189
191
|
sget({
|
|
190
192
|
method: 'DELETE',
|
|
191
193
|
url: getServerUrl(fastify) + '/multiple'
|
|
192
194
|
}, (err, response, body) => {
|
|
193
|
-
t.
|
|
194
|
-
t.
|
|
195
|
-
t.
|
|
195
|
+
t.assert.ifError(err)
|
|
196
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
197
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
198
|
+
done()
|
|
196
199
|
})
|
|
197
200
|
})
|
|
198
201
|
})
|
|
199
202
|
|
|
200
|
-
test('Add invalid Multiple methods per route', t => {
|
|
203
|
+
t.test('Add invalid Multiple methods per route', t => {
|
|
201
204
|
t.plan(1)
|
|
202
205
|
|
|
203
206
|
const fastify = Fastify()
|
|
204
|
-
t.throws(() =>
|
|
207
|
+
t.assert.throws(() =>
|
|
205
208
|
fastify.route({
|
|
206
209
|
method: ['GET', 1],
|
|
207
210
|
url: '/invalid-method',
|
|
@@ -211,11 +214,11 @@ test('route', t => {
|
|
|
211
214
|
}), new FST_ERR_ROUTE_METHOD_INVALID())
|
|
212
215
|
})
|
|
213
216
|
|
|
214
|
-
test('Add method', t => {
|
|
217
|
+
await t.test('Add method', t => {
|
|
215
218
|
t.plan(1)
|
|
216
219
|
|
|
217
220
|
const fastify = Fastify()
|
|
218
|
-
t.throws(() =>
|
|
221
|
+
t.assert.throws(() =>
|
|
219
222
|
fastify.route({
|
|
220
223
|
method: 1,
|
|
221
224
|
url: '/invalid-method',
|
|
@@ -225,11 +228,11 @@ test('route', t => {
|
|
|
225
228
|
}), new FST_ERR_ROUTE_METHOD_INVALID())
|
|
226
229
|
})
|
|
227
230
|
|
|
228
|
-
test('Add additional multiple methods to existing route', t => {
|
|
231
|
+
await t.test('Add additional multiple methods to existing route', (t, done) => {
|
|
229
232
|
t.plan(7)
|
|
230
233
|
|
|
231
234
|
const fastify = Fastify()
|
|
232
|
-
t.doesNotThrow(() => {
|
|
235
|
+
t.assert.doesNotThrow(() => {
|
|
233
236
|
fastify.get('/add-multiple', function (req, reply) {
|
|
234
237
|
reply.send({ hello: 'Bob!' })
|
|
235
238
|
})
|
|
@@ -243,49 +246,52 @@ test('route', t => {
|
|
|
243
246
|
})
|
|
244
247
|
|
|
245
248
|
fastify.listen({ port: 0 }, function (err) {
|
|
246
|
-
if (err) t.
|
|
247
|
-
t.
|
|
249
|
+
if (err) t.assert.ifError(err)
|
|
250
|
+
t.after(() => { fastify.close() })
|
|
248
251
|
sget({
|
|
249
252
|
method: 'PUT',
|
|
250
253
|
url: getServerUrl(fastify) + '/add-multiple'
|
|
251
254
|
}, (err, response, body) => {
|
|
252
|
-
t.
|
|
253
|
-
t.
|
|
254
|
-
t.
|
|
255
|
+
t.assert.ifError(err)
|
|
256
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
257
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
255
258
|
})
|
|
256
259
|
|
|
257
260
|
sget({
|
|
258
261
|
method: 'DELETE',
|
|
259
262
|
url: getServerUrl(fastify) + '/add-multiple'
|
|
260
263
|
}, (err, response, body) => {
|
|
261
|
-
t.
|
|
262
|
-
t.
|
|
263
|
-
t.
|
|
264
|
+
t.assert.ifError(err)
|
|
265
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
266
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
267
|
+
done()
|
|
264
268
|
})
|
|
265
269
|
})
|
|
266
270
|
})
|
|
267
271
|
|
|
268
|
-
test('cannot add another route after binding', t => {
|
|
272
|
+
await t.test('cannot add another route after binding', (t, done) => {
|
|
269
273
|
t.plan(1)
|
|
270
274
|
|
|
271
275
|
const fastify = Fastify()
|
|
272
276
|
|
|
273
277
|
fastify.listen({ port: 0 }, function (err) {
|
|
274
|
-
if (err) t.
|
|
275
|
-
t.
|
|
278
|
+
if (err) t.assert.ifError(err)
|
|
279
|
+
t.after(() => { fastify.close() })
|
|
276
280
|
|
|
277
|
-
t.throws(() => fastify.route({
|
|
281
|
+
t.assert.throws(() => fastify.route({
|
|
278
282
|
method: 'GET',
|
|
279
283
|
url: '/another-get-route',
|
|
280
284
|
handler: function (req, reply) {
|
|
281
285
|
reply.send({ hello: 'world' })
|
|
282
286
|
}
|
|
283
287
|
}), new FST_ERR_INSTANCE_ALREADY_LISTENING('Cannot add route!'))
|
|
288
|
+
|
|
289
|
+
done()
|
|
284
290
|
})
|
|
285
291
|
})
|
|
286
292
|
})
|
|
287
293
|
|
|
288
|
-
test('invalid schema - route', t => {
|
|
294
|
+
test('invalid schema - route', (t, done) => {
|
|
289
295
|
t.plan(3)
|
|
290
296
|
|
|
291
297
|
const fastify = Fastify()
|
|
@@ -300,10 +306,11 @@ test('invalid schema - route', t => {
|
|
|
300
306
|
}
|
|
301
307
|
})
|
|
302
308
|
fastify.after(err => {
|
|
303
|
-
t.
|
|
309
|
+
t.assert.ok(!err, 'the error is throw on preReady')
|
|
304
310
|
})
|
|
305
311
|
fastify.ready(err => {
|
|
306
|
-
t.
|
|
307
|
-
t.match(err.message, /Failed building the validation schema for GET: \/invalid/)
|
|
312
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
|
|
313
|
+
t.assert.match(err.message, /Failed building the validation schema for GET: \/invalid/)
|
|
314
|
+
done()
|
|
308
315
|
})
|
|
309
316
|
})
|
package/test/route.2.test.js
CHANGED
|
@@ -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('../fastify')
|
|
6
5
|
|
|
7
6
|
test('same route definition object on multiple prefixes', async t => {
|
|
@@ -17,13 +16,13 @@ test('same route definition object on multiple prefixes', async t => {
|
|
|
17
16
|
|
|
18
17
|
fastify.register(async function (f) {
|
|
19
18
|
f.addHook('onRoute', (routeOptions) => {
|
|
20
|
-
t.
|
|
19
|
+
t.assert.strictEqual(routeOptions.url, '/v1/simple')
|
|
21
20
|
})
|
|
22
21
|
f.route(routeObject)
|
|
23
22
|
}, { prefix: '/v1' })
|
|
24
23
|
fastify.register(async function (f) {
|
|
25
24
|
f.addHook('onRoute', (routeOptions) => {
|
|
26
|
-
t.
|
|
25
|
+
t.assert.strictEqual(routeOptions.url, '/v2/simple')
|
|
27
26
|
})
|
|
28
27
|
f.route(routeObject)
|
|
29
28
|
}, { prefix: '/v2' })
|
|
@@ -31,7 +30,7 @@ test('same route definition object on multiple prefixes', async t => {
|
|
|
31
30
|
await fastify.ready()
|
|
32
31
|
})
|
|
33
32
|
|
|
34
|
-
test('path can be specified in place of uri', t => {
|
|
33
|
+
test('path can be specified in place of uri', (t, done) => {
|
|
35
34
|
t.plan(3)
|
|
36
35
|
const fastify = Fastify()
|
|
37
36
|
|
|
@@ -49,9 +48,10 @@ test('path can be specified in place of uri', t => {
|
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
fastify.inject(reqOpts, (err, res) => {
|
|
52
|
-
t.
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
51
|
+
t.assert.ifError(err)
|
|
52
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
53
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
54
|
+
done()
|
|
55
55
|
})
|
|
56
56
|
})
|
|
57
57
|
|
|
@@ -65,20 +65,20 @@ test('invalid bodyLimit option - route', t => {
|
|
|
65
65
|
method: 'PUT',
|
|
66
66
|
handler: () => null
|
|
67
67
|
})
|
|
68
|
-
t.fail('bodyLimit must be an integer')
|
|
68
|
+
t.assert.fail('bodyLimit must be an integer')
|
|
69
69
|
} catch (err) {
|
|
70
|
-
t.
|
|
70
|
+
t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
try {
|
|
74
74
|
fastify.post('/url', { bodyLimit: 10000.1 }, () => null)
|
|
75
|
-
t.fail('bodyLimit must be an integer')
|
|
75
|
+
t.assert.fail('bodyLimit must be an integer')
|
|
76
76
|
} catch (err) {
|
|
77
|
-
t.
|
|
77
|
+
t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")
|
|
78
78
|
}
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
-
test('handler function in options of shorthand route should works correctly', t => {
|
|
81
|
+
test('handler function in options of shorthand route should works correctly', (t, done) => {
|
|
82
82
|
t.plan(3)
|
|
83
83
|
|
|
84
84
|
const fastify = Fastify()
|
|
@@ -92,8 +92,9 @@ test('handler function in options of shorthand route should works correctly', t
|
|
|
92
92
|
method: 'GET',
|
|
93
93
|
url: '/foo'
|
|
94
94
|
}, (err, res) => {
|
|
95
|
-
t.
|
|
96
|
-
t.
|
|
97
|
-
t.
|
|
95
|
+
t.assert.ifError(err)
|
|
96
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
97
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
98
|
+
done()
|
|
98
99
|
})
|
|
99
100
|
})
|
package/test/route.3.test.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const joi = require('joi')
|
|
6
5
|
const Fastify = require('..')
|
|
7
6
|
|
|
8
|
-
test('does not mutate joi schemas', t => {
|
|
9
|
-
t.plan(
|
|
7
|
+
test('does not mutate joi schemas', (t, done) => {
|
|
8
|
+
t.plan(5)
|
|
10
9
|
|
|
11
10
|
const fastify = Fastify()
|
|
12
11
|
function validatorCompiler ({ schema, method, url, httpPart }) {
|
|
@@ -32,7 +31,8 @@ test('does not mutate joi schemas', t => {
|
|
|
32
31
|
params: { an_id: joi.number() }
|
|
33
32
|
},
|
|
34
33
|
handler (req, res) {
|
|
35
|
-
t.
|
|
34
|
+
t.assert.strictEqual(Object.keys(req.params).length, 1)
|
|
35
|
+
t.assert.strictEqual(req.params.an_id, '42')
|
|
36
36
|
res.send({ hello: 'world' })
|
|
37
37
|
}
|
|
38
38
|
})
|
|
@@ -40,14 +40,15 @@ test('does not mutate joi schemas', t => {
|
|
|
40
40
|
fastify.inject({
|
|
41
41
|
method: 'GET',
|
|
42
42
|
url: '/foo/42'
|
|
43
|
-
}, (err,
|
|
44
|
-
t.
|
|
45
|
-
t.
|
|
46
|
-
t.
|
|
43
|
+
}, (err, res) => {
|
|
44
|
+
t.assert.ifError(err)
|
|
45
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
46
|
+
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
|
47
|
+
done()
|
|
47
48
|
})
|
|
48
49
|
})
|
|
49
50
|
|
|
50
|
-
test('multiple routes with one schema', t => {
|
|
51
|
+
test('multiple routes with one schema', (t, done) => {
|
|
51
52
|
t.plan(2)
|
|
52
53
|
|
|
53
54
|
const fastify = Fastify()
|
|
@@ -80,18 +81,19 @@ test('multiple routes with one schema', t => {
|
|
|
80
81
|
})
|
|
81
82
|
|
|
82
83
|
fastify.ready(error => {
|
|
83
|
-
t.
|
|
84
|
-
t.
|
|
84
|
+
t.assert.ifError(error)
|
|
85
|
+
t.assert.deepStrictEqual(schema, schema)
|
|
86
|
+
done()
|
|
85
87
|
})
|
|
86
88
|
})
|
|
87
89
|
|
|
88
|
-
test('route error handler overrides default error handler', t => {
|
|
90
|
+
test('route error handler overrides default error handler', (t, done) => {
|
|
89
91
|
t.plan(4)
|
|
90
92
|
|
|
91
93
|
const fastify = Fastify()
|
|
92
94
|
|
|
93
95
|
const customRouteErrorHandler = (error, request, reply) => {
|
|
94
|
-
t.
|
|
96
|
+
t.assert.strictEqual(error.message, 'Wrong Pot Error')
|
|
95
97
|
|
|
96
98
|
reply.code(418).send({
|
|
97
99
|
message: 'Make a brew',
|
|
@@ -113,23 +115,24 @@ test('route error handler overrides default error handler', t => {
|
|
|
113
115
|
method: 'GET',
|
|
114
116
|
url: '/coffee'
|
|
115
117
|
}, (error, res) => {
|
|
116
|
-
t.
|
|
117
|
-
t.
|
|
118
|
-
t.
|
|
118
|
+
t.assert.ifError(error)
|
|
119
|
+
t.assert.strictEqual(res.statusCode, 418)
|
|
120
|
+
t.assert.deepStrictEqual(res.json(), {
|
|
119
121
|
message: 'Make a brew',
|
|
120
122
|
statusCode: 418,
|
|
121
123
|
error: 'Wrong Pot Error'
|
|
122
124
|
})
|
|
125
|
+
done()
|
|
123
126
|
})
|
|
124
127
|
})
|
|
125
128
|
|
|
126
|
-
test('route error handler does not affect other routes', t => {
|
|
129
|
+
test('route error handler does not affect other routes', (t, done) => {
|
|
127
130
|
t.plan(3)
|
|
128
131
|
|
|
129
132
|
const fastify = Fastify()
|
|
130
133
|
|
|
131
134
|
const customRouteErrorHandler = (error, request, reply) => {
|
|
132
|
-
t.
|
|
135
|
+
t.assert.strictEqual(error.message, 'Wrong Pot Error')
|
|
133
136
|
|
|
134
137
|
reply.code(418).send({
|
|
135
138
|
message: 'Make a brew',
|
|
@@ -159,23 +162,24 @@ test('route error handler does not affect other routes', t => {
|
|
|
159
162
|
method: 'GET',
|
|
160
163
|
url: '/tea'
|
|
161
164
|
}, (error, res) => {
|
|
162
|
-
t.
|
|
163
|
-
t.
|
|
164
|
-
t.
|
|
165
|
+
t.assert.ifError(error)
|
|
166
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
167
|
+
t.assert.deepStrictEqual(res.json(), {
|
|
165
168
|
message: 'No tea today',
|
|
166
169
|
statusCode: 500,
|
|
167
170
|
error: 'Internal Server Error'
|
|
168
171
|
})
|
|
172
|
+
done()
|
|
169
173
|
})
|
|
170
174
|
})
|
|
171
175
|
|
|
172
|
-
test('async error handler for a route', t => {
|
|
176
|
+
test('async error handler for a route', (t, done) => {
|
|
173
177
|
t.plan(4)
|
|
174
178
|
|
|
175
179
|
const fastify = Fastify()
|
|
176
180
|
|
|
177
181
|
const customRouteErrorHandler = async (error, request, reply) => {
|
|
178
|
-
t.
|
|
182
|
+
t.assert.strictEqual(error.message, 'Delayed Pot Error')
|
|
179
183
|
reply.code(418)
|
|
180
184
|
return {
|
|
181
185
|
message: 'Make a brew sometime later',
|
|
@@ -197,12 +201,13 @@ test('async error handler for a route', t => {
|
|
|
197
201
|
method: 'GET',
|
|
198
202
|
url: '/late-coffee'
|
|
199
203
|
}, (error, res) => {
|
|
200
|
-
t.
|
|
201
|
-
t.
|
|
202
|
-
t.
|
|
204
|
+
t.assert.ifError(error)
|
|
205
|
+
t.assert.strictEqual(res.statusCode, 418)
|
|
206
|
+
t.assert.deepStrictEqual(res.json(), {
|
|
203
207
|
message: 'Make a brew sometime later',
|
|
204
208
|
statusCode: 418,
|
|
205
209
|
error: 'Delayed Pot Error'
|
|
206
210
|
})
|
|
211
|
+
done()
|
|
207
212
|
})
|
|
208
213
|
})
|
package/test/route.4.test.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
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
|
-
test('route error handler overrides global custom error handler', t => {
|
|
8
|
-
t.plan(
|
|
6
|
+
test('route error handler overrides global custom error handler', async t => {
|
|
7
|
+
t.plan(3)
|
|
9
8
|
|
|
10
9
|
const fastify = Fastify()
|
|
11
10
|
|
|
12
11
|
const customGlobalErrorHandler = (error, request, reply) => {
|
|
13
|
-
t.
|
|
12
|
+
t.assert.ifError(error)
|
|
14
13
|
reply.code(429).send({ message: 'Too much coffee' })
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
const customRouteErrorHandler = (error, request, reply) => {
|
|
18
|
-
t.
|
|
17
|
+
t.assert.strictEqual(error.message, 'Wrong Pot Error')
|
|
19
18
|
reply.code(418).send({
|
|
20
19
|
message: 'Make a brew',
|
|
21
20
|
statusCode: 418,
|
|
@@ -34,17 +33,15 @@ test('route error handler overrides global custom error handler', t => {
|
|
|
34
33
|
errorHandler: customRouteErrorHandler
|
|
35
34
|
})
|
|
36
35
|
|
|
37
|
-
fastify.inject({
|
|
36
|
+
const res = await fastify.inject({
|
|
38
37
|
method: 'GET',
|
|
39
38
|
url: '/more-coffee'
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
error: 'Wrong Pot Error'
|
|
47
|
-
})
|
|
39
|
+
})
|
|
40
|
+
t.assert.strictEqual(res.statusCode, 418)
|
|
41
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
42
|
+
message: 'Make a brew',
|
|
43
|
+
statusCode: 418,
|
|
44
|
+
error: 'Wrong Pot Error'
|
|
48
45
|
})
|
|
49
46
|
})
|
|
50
47
|
|
|
@@ -61,7 +58,7 @@ test('throws when route with empty url', async t => {
|
|
|
61
58
|
}
|
|
62
59
|
})
|
|
63
60
|
} catch (err) {
|
|
64
|
-
t.
|
|
61
|
+
t.assert.strictEqual(err.message, 'The path could not be empty')
|
|
65
62
|
}
|
|
66
63
|
})
|
|
67
64
|
|
|
@@ -75,7 +72,7 @@ test('throws when route with empty url in shorthand declaration', async t => {
|
|
|
75
72
|
async function handler () { return {} }
|
|
76
73
|
)
|
|
77
74
|
} catch (err) {
|
|
78
|
-
t.
|
|
75
|
+
t.assert.strictEqual(err.message, 'The path could not be empty')
|
|
79
76
|
}
|
|
80
77
|
})
|
|
81
78
|
|
|
@@ -94,19 +91,19 @@ test('throws when route-level error handler is not a function', t => {
|
|
|
94
91
|
errorHandler: 'teapot'
|
|
95
92
|
})
|
|
96
93
|
} catch (err) {
|
|
97
|
-
t.
|
|
94
|
+
t.assert.strictEqual(err.message, 'Error Handler for GET:/tea route, if defined, must be a function')
|
|
98
95
|
}
|
|
99
96
|
})
|
|
100
97
|
|
|
101
|
-
test('route child logger factory overrides default child logger factory', t => {
|
|
102
|
-
t.plan(
|
|
98
|
+
test('route child logger factory overrides default child logger factory', async t => {
|
|
99
|
+
t.plan(2)
|
|
103
100
|
|
|
104
101
|
const fastify = Fastify()
|
|
105
102
|
|
|
106
103
|
const customRouteChildLogger = (logger, bindings, opts, req) => {
|
|
107
104
|
const child = logger.child(bindings, opts)
|
|
108
105
|
child.customLog = function (message) {
|
|
109
|
-
t.
|
|
106
|
+
t.assert.strictEqual(message, 'custom')
|
|
110
107
|
}
|
|
111
108
|
return child
|
|
112
109
|
}
|
|
@@ -121,11 +118,10 @@ test('route child logger factory overrides default child logger factory', t => {
|
|
|
121
118
|
childLoggerFactory: customRouteChildLogger
|
|
122
119
|
})
|
|
123
120
|
|
|
124
|
-
fastify.inject({
|
|
121
|
+
const res = await fastify.inject({
|
|
125
122
|
method: 'GET',
|
|
126
123
|
url: '/coffee'
|
|
127
|
-
}, (error, res) => {
|
|
128
|
-
t.error(error)
|
|
129
|
-
t.equal(res.statusCode, 200)
|
|
130
124
|
})
|
|
125
|
+
|
|
126
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
131
127
|
})
|