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,21 +1,20 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('../fastify')
|
|
5
5
|
|
|
6
6
|
// asyncDispose doesn't exist in node <= 16
|
|
7
|
-
|
|
7
|
+
test('async dispose should close fastify', { skip: !('asyncDispose' in Symbol) }, async t => {
|
|
8
8
|
t.plan(2)
|
|
9
9
|
|
|
10
10
|
const fastify = Fastify()
|
|
11
11
|
|
|
12
12
|
await fastify.listen({ port: 0 })
|
|
13
13
|
|
|
14
|
-
t.
|
|
14
|
+
t.assert.strictEqual(fastify.server.listening, true)
|
|
15
15
|
|
|
16
16
|
// the same as syntax sugar for
|
|
17
17
|
// await using app = fastify()
|
|
18
18
|
await fastify[Symbol.asyncDispose]()
|
|
19
|
-
|
|
20
|
-
t.equal(fastify.server.listening, false)
|
|
19
|
+
t.assert.strictEqual(fastify.server.listening, false)
|
|
21
20
|
})
|
package/test/async_hooks.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { createHook } = require('node:async_hooks')
|
|
4
|
-
const
|
|
4
|
+
const { test } = require('node:test')
|
|
5
5
|
const Fastify = require('..')
|
|
6
6
|
const sget = require('simple-get').concat
|
|
7
7
|
|
|
@@ -20,27 +20,17 @@ createHook({
|
|
|
20
20
|
|
|
21
21
|
const app = Fastify({ logger: false })
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
app.post('/', function (request, reply) {
|
|
28
|
-
reply.send({ id: 42 })
|
|
29
|
-
})
|
|
23
|
+
test('test async hooks', (t, done) => {
|
|
24
|
+
app.get('/', function (request, reply) {
|
|
25
|
+
reply.send({ id: 42 })
|
|
26
|
+
})
|
|
30
27
|
|
|
31
|
-
app.
|
|
32
|
-
|
|
28
|
+
app.post('/', function (request, reply) {
|
|
29
|
+
reply.send({ id: 42 })
|
|
30
|
+
})
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
url: 'http://localhost:' + app.server.address().port,
|
|
37
|
-
body: {
|
|
38
|
-
hello: 'world'
|
|
39
|
-
},
|
|
40
|
-
json: true
|
|
41
|
-
}, (err, response, body) => {
|
|
42
|
-
t.error(err)
|
|
43
|
-
t.equal(response.statusCode, 200)
|
|
32
|
+
app.listen({ port: 0 }, function (err, address) {
|
|
33
|
+
t.assert.ifError(err)
|
|
44
34
|
|
|
45
35
|
sget({
|
|
46
36
|
method: 'POST',
|
|
@@ -50,19 +40,31 @@ app.listen({ port: 0 }, function (err, address) {
|
|
|
50
40
|
},
|
|
51
41
|
json: true
|
|
52
42
|
}, (err, response, body) => {
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
43
|
+
t.assert.ifError(err)
|
|
44
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
55
45
|
|
|
56
46
|
sget({
|
|
57
|
-
method: '
|
|
47
|
+
method: 'POST',
|
|
58
48
|
url: 'http://localhost:' + app.server.address().port,
|
|
49
|
+
body: {
|
|
50
|
+
hello: 'world'
|
|
51
|
+
},
|
|
59
52
|
json: true
|
|
60
53
|
}, (err, response, body) => {
|
|
61
|
-
t.
|
|
62
|
-
t.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
54
|
+
t.assert.ifError(err)
|
|
55
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
56
|
+
|
|
57
|
+
sget({
|
|
58
|
+
method: 'GET',
|
|
59
|
+
url: 'http://localhost:' + app.server.address().port,
|
|
60
|
+
json: true
|
|
61
|
+
}, (err, response, body) => {
|
|
62
|
+
t.assert.ifError(err)
|
|
63
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
64
|
+
app.close()
|
|
65
|
+
t.assert.strictEqual(remainingIds.size, 0)
|
|
66
|
+
done()
|
|
67
|
+
})
|
|
66
68
|
})
|
|
67
69
|
})
|
|
68
70
|
})
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const Fastify = require('
|
|
3
|
+
const Fastify = require('../fastify')
|
|
4
4
|
const sget = require('simple-get').concat
|
|
5
5
|
const zlib = require('node:zlib')
|
|
6
|
-
const
|
|
7
|
-
const test = t.test
|
|
6
|
+
const { test } = require('node:test')
|
|
8
7
|
|
|
9
|
-
test('bodyLimit', t => {
|
|
8
|
+
test('bodyLimit', (t, done) => {
|
|
10
9
|
t.plan(5)
|
|
11
10
|
|
|
12
11
|
try {
|
|
13
12
|
Fastify({ bodyLimit: 1.3 })
|
|
14
|
-
t.fail('option must be an integer')
|
|
13
|
+
t.assert.fail('option must be an integer')
|
|
15
14
|
} catch (err) {
|
|
16
|
-
t.ok(err)
|
|
15
|
+
t.assert.ok(err)
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
try {
|
|
20
19
|
Fastify({ bodyLimit: [] })
|
|
21
|
-
t.fail('option must be an integer')
|
|
20
|
+
t.assert.fail('option must be an integer')
|
|
22
21
|
} catch (err) {
|
|
23
|
-
t.ok(err)
|
|
22
|
+
t.assert.ok(err)
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
const fastify = Fastify({ bodyLimit: 1 })
|
|
@@ -30,8 +29,8 @@ test('bodyLimit', t => {
|
|
|
30
29
|
})
|
|
31
30
|
|
|
32
31
|
fastify.listen({ port: 0 }, function (err) {
|
|
33
|
-
t.
|
|
34
|
-
t.
|
|
32
|
+
t.assert.ifError(err)
|
|
33
|
+
t.after(() => { fastify.close() })
|
|
35
34
|
|
|
36
35
|
sget({
|
|
37
36
|
method: 'POST',
|
|
@@ -40,14 +39,15 @@ test('bodyLimit', t => {
|
|
|
40
39
|
body: [],
|
|
41
40
|
json: true
|
|
42
41
|
}, (err, response, body) => {
|
|
43
|
-
t.
|
|
44
|
-
t.
|
|
42
|
+
t.assert.ifError(err)
|
|
43
|
+
t.assert.strictEqual(response.statusCode, 413)
|
|
44
|
+
done()
|
|
45
45
|
})
|
|
46
46
|
})
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
-
test('bodyLimit is applied to decoded content', t => {
|
|
50
|
-
t.plan(
|
|
49
|
+
test('bodyLimit is applied to decoded content', async (t) => {
|
|
50
|
+
t.plan(6)
|
|
51
51
|
|
|
52
52
|
const body = { x: 'x'.repeat(30000) }
|
|
53
53
|
const json = JSON.stringify(body)
|
|
@@ -56,7 +56,7 @@ test('bodyLimit is applied to decoded content', t => {
|
|
|
56
56
|
const fastify = Fastify()
|
|
57
57
|
|
|
58
58
|
fastify.addHook('preParsing', async (req, reply, payload) => {
|
|
59
|
-
t.
|
|
59
|
+
t.assert.strictEqual(req.headers['content-length'], `${encoded.length}`)
|
|
60
60
|
const unzip = zlib.createGunzip()
|
|
61
61
|
Object.defineProperty(unzip, 'receivedEncodedLength', {
|
|
62
62
|
get () {
|
|
@@ -79,53 +79,53 @@ test('bodyLimit is applied to decoded content', t => {
|
|
|
79
79
|
fastify.post('/body-limit-20k', {
|
|
80
80
|
bodyLimit: 20000,
|
|
81
81
|
onError: async (req, res, err) => {
|
|
82
|
-
t.
|
|
83
|
-
t.
|
|
82
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_BODY_TOO_LARGE')
|
|
83
|
+
t.assert.strictEqual(err.statusCode, 413)
|
|
84
84
|
}
|
|
85
85
|
}, (request, reply) => {
|
|
86
86
|
reply.send({ x: 'handler should not be called' })
|
|
87
87
|
})
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
t.
|
|
100
|
-
t.
|
|
89
|
+
await t.test('bodyLimit 40k', async (t) => {
|
|
90
|
+
const result = await fastify.inject({
|
|
91
|
+
method: 'POST',
|
|
92
|
+
url: '/body-limit-40k',
|
|
93
|
+
headers: {
|
|
94
|
+
'content-encoding': 'gzip',
|
|
95
|
+
'content-type': 'application/json'
|
|
96
|
+
},
|
|
97
|
+
payload: encoded
|
|
98
|
+
})
|
|
99
|
+
t.assert.strictEqual(result.statusCode, 200)
|
|
100
|
+
t.assert.deepStrictEqual(result.json(), body)
|
|
101
101
|
})
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
t.
|
|
103
|
+
await t.test('bodyLimit 20k', async (t) => {
|
|
104
|
+
const result = await fastify.inject({
|
|
105
|
+
method: 'POST',
|
|
106
|
+
url: '/body-limit-20k',
|
|
107
|
+
headers: {
|
|
108
|
+
'content-encoding': 'gzip',
|
|
109
|
+
'content-type': 'application/json'
|
|
110
|
+
},
|
|
111
|
+
payload: encoded
|
|
112
|
+
})
|
|
113
|
+
t.assert.strictEqual(result.statusCode, 413)
|
|
114
114
|
})
|
|
115
115
|
})
|
|
116
116
|
|
|
117
|
-
test('default request.routeOptions.bodyLimit should be 1048576', t => {
|
|
117
|
+
test('default request.routeOptions.bodyLimit should be 1048576', (t, done) => {
|
|
118
118
|
t.plan(4)
|
|
119
119
|
const fastify = Fastify()
|
|
120
120
|
fastify.post('/default-bodylimit', {
|
|
121
121
|
handler (request, reply) {
|
|
122
|
-
t.
|
|
122
|
+
t.assert.strictEqual(1048576, request.routeOptions.bodyLimit)
|
|
123
123
|
reply.send({ })
|
|
124
124
|
}
|
|
125
125
|
})
|
|
126
126
|
fastify.listen({ port: 0 }, function (err) {
|
|
127
|
-
t.
|
|
128
|
-
t.
|
|
127
|
+
t.assert.ifError(err)
|
|
128
|
+
t.after(() => { fastify.close() })
|
|
129
129
|
|
|
130
130
|
sget({
|
|
131
131
|
method: 'POST',
|
|
@@ -134,25 +134,26 @@ test('default request.routeOptions.bodyLimit should be 1048576', t => {
|
|
|
134
134
|
body: [],
|
|
135
135
|
json: true
|
|
136
136
|
}, (err, response, body) => {
|
|
137
|
-
t.
|
|
138
|
-
t.
|
|
137
|
+
t.assert.ifError(err)
|
|
138
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
139
|
+
done()
|
|
139
140
|
})
|
|
140
141
|
})
|
|
141
142
|
})
|
|
142
143
|
|
|
143
|
-
test('request.routeOptions.bodyLimit should be equal to route limit', t => {
|
|
144
|
+
test('request.routeOptions.bodyLimit should be equal to route limit', (t, done) => {
|
|
144
145
|
t.plan(4)
|
|
145
146
|
const fastify = Fastify({ bodyLimit: 1 })
|
|
146
147
|
fastify.post('/route-limit', {
|
|
147
148
|
bodyLimit: 1000,
|
|
148
149
|
handler (request, reply) {
|
|
149
|
-
t.
|
|
150
|
+
t.assert.strictEqual(1000, request.routeOptions.bodyLimit)
|
|
150
151
|
reply.send({})
|
|
151
152
|
}
|
|
152
153
|
})
|
|
153
154
|
fastify.listen({ port: 0 }, function (err) {
|
|
154
|
-
t.
|
|
155
|
-
t.
|
|
155
|
+
t.assert.ifError(err)
|
|
156
|
+
t.after(() => { fastify.close() })
|
|
156
157
|
|
|
157
158
|
sget({
|
|
158
159
|
method: 'POST',
|
|
@@ -161,24 +162,25 @@ test('request.routeOptions.bodyLimit should be equal to route limit', t => {
|
|
|
161
162
|
body: [],
|
|
162
163
|
json: true
|
|
163
164
|
}, (err, response, body) => {
|
|
164
|
-
t.
|
|
165
|
-
t.
|
|
165
|
+
t.assert.ifError(err)
|
|
166
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
167
|
+
done()
|
|
166
168
|
})
|
|
167
169
|
})
|
|
168
170
|
})
|
|
169
171
|
|
|
170
|
-
test('request.routeOptions.bodyLimit should be equal to server limit', t => {
|
|
172
|
+
test('request.routeOptions.bodyLimit should be equal to server limit', (t, done) => {
|
|
171
173
|
t.plan(4)
|
|
172
174
|
const fastify = Fastify({ bodyLimit: 100 })
|
|
173
175
|
fastify.post('/server-limit', {
|
|
174
176
|
handler (request, reply) {
|
|
175
|
-
t.
|
|
177
|
+
t.assert.strictEqual(100, request.routeOptions.bodyLimit)
|
|
176
178
|
reply.send({})
|
|
177
179
|
}
|
|
178
180
|
})
|
|
179
181
|
fastify.listen({ port: 0 }, function (err) {
|
|
180
|
-
t.
|
|
181
|
-
t.
|
|
182
|
+
t.assert.ifError(err)
|
|
183
|
+
t.after(() => { fastify.close() })
|
|
182
184
|
|
|
183
185
|
sget({
|
|
184
186
|
method: 'POST',
|
|
@@ -187,8 +189,9 @@ test('request.routeOptions.bodyLimit should be equal to server limit', t => {
|
|
|
187
189
|
body: [],
|
|
188
190
|
json: true
|
|
189
191
|
}, (err, response, body) => {
|
|
190
|
-
t.
|
|
191
|
-
t.
|
|
192
|
+
t.assert.ifError(err)
|
|
193
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
194
|
+
done()
|
|
192
195
|
})
|
|
193
196
|
})
|
|
194
197
|
})
|
package/test/buffer.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('..')
|
|
6
5
|
|
|
7
6
|
test('Buffer test', async t => {
|
|
@@ -12,7 +11,7 @@ test('Buffer test', async t => {
|
|
|
12
11
|
return request.body
|
|
13
12
|
})
|
|
14
13
|
|
|
15
|
-
test('should return 200 if the body is not empty', async t => {
|
|
14
|
+
await test('should return 200 if the body is not empty', async t => {
|
|
16
15
|
t.plan(3)
|
|
17
16
|
|
|
18
17
|
const response = await fastify.inject({
|
|
@@ -24,12 +23,12 @@ test('Buffer test', async t => {
|
|
|
24
23
|
}
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
t.
|
|
28
|
-
t.
|
|
29
|
-
t.
|
|
26
|
+
t.assert.ifError(response.error)
|
|
27
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
28
|
+
t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
|
|
30
29
|
})
|
|
31
30
|
|
|
32
|
-
test('should return 400 if the body is empty', async t => {
|
|
31
|
+
await test('should return 400 if the body is empty', async t => {
|
|
33
32
|
t.plan(3)
|
|
34
33
|
|
|
35
34
|
const response = await fastify.inject({
|
|
@@ -41,9 +40,9 @@ test('Buffer test', async t => {
|
|
|
41
40
|
}
|
|
42
41
|
})
|
|
43
42
|
|
|
44
|
-
t.
|
|
45
|
-
t.
|
|
46
|
-
t.
|
|
43
|
+
t.assert.ifError(response.error)
|
|
44
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
45
|
+
t.assert.deepStrictEqual(JSON.parse(response.payload.toString()), {
|
|
47
46
|
error: 'Bad Request',
|
|
48
47
|
code: 'FST_ERR_CTP_EMPTY_JSON_BODY',
|
|
49
48
|
message: 'Body cannot be empty when content-type is set to \'application/json\'',
|
|
@@ -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 fs = require('node:fs')
|
|
6
5
|
const path = require('node:path')
|
|
7
6
|
const { loadESLint } = require('eslint')
|
|
@@ -22,7 +21,7 @@ test('check generated code syntax', async (t) => {
|
|
|
22
21
|
|
|
23
22
|
// if there are any invalid syntax
|
|
24
23
|
// fatal count will be greater than 0
|
|
25
|
-
t.
|
|
24
|
+
t.assert.strictEqual(result[0].fatalErrorCount, 0)
|
|
26
25
|
})
|
|
27
26
|
|
|
28
27
|
const isPrepublish = !!process.env.PREPUBLISH
|
|
@@ -33,5 +32,5 @@ test('ensure the current error serializer is latest', { skip: !isPrepublish }, a
|
|
|
33
32
|
const current = await fs.promises.readFile(path.resolve('lib/error-serializer.js'))
|
|
34
33
|
|
|
35
34
|
// line break should not be a problem depends on system
|
|
36
|
-
t.
|
|
35
|
+
t.assert.strictEqual(unifyLineBreak(current), unifyLineBreak(code))
|
|
37
36
|
})
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs')
|
|
4
4
|
const path = require('node:path')
|
|
5
|
-
const
|
|
6
|
-
const test = t.test
|
|
5
|
+
const { test } = require('node:test')
|
|
7
6
|
const fastify = require('../../fastify')()
|
|
8
7
|
|
|
9
8
|
test('should be the same as package.json', t => {
|
|
@@ -11,5 +10,5 @@ test('should be the same as package.json', t => {
|
|
|
11
10
|
|
|
12
11
|
const json = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'package.json')).toString('utf8'))
|
|
13
12
|
|
|
14
|
-
t.
|
|
13
|
+
t.assert.strictEqual(fastify.version, json.version)
|
|
15
14
|
})
|
package/test/bundler/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Bundlers test stack
|
|
2
2
|
|
|
3
|
-
In some cases, developers bundle their apps for several targets such as serverless applications.
|
|
4
|
-
Even if it's not recommended by Fastify team; we need to ensure we do not break the build process.
|
|
3
|
+
In some cases, developers bundle their apps for several targets such as serverless applications.
|
|
4
|
+
Even if it's not recommended by Fastify team; we need to ensure we do not break the build process.
|
|
5
5
|
Please note this might result in features behaving differently, like the version handling check for plugins.
|
|
6
6
|
|
|
7
7
|
## Test bundlers
|
|
8
8
|
|
|
9
|
-
The bundler test stack has been defined separately from the rest of the Unit testing stack because it's not a
|
|
9
|
+
The bundler test stack has been defined separately from the rest of the Unit testing stack because it's not a
|
|
10
10
|
part of the fastify lib itself. Note that the tests run in CI only on NodeJs LTS version.
|
|
11
11
|
Developers do not need to install every bundler to run unit tests.
|
|
12
12
|
|
|
@@ -23,7 +23,7 @@ stack dependencies. See:
|
|
|
23
23
|
|
|
24
24
|
## Bundler test development
|
|
25
25
|
|
|
26
|
-
To not break the fastify unit testing stack please name test files like this `*-test.js` and not `*.test.js`,
|
|
26
|
+
To not break the fastify unit testing stack please name test files like this `*-test.js` and not `*.test.js`,
|
|
27
27
|
otherwise it will be targeted by the regular expression used for unit tests for fastify.
|
|
28
|
-
Tests need to ensure the build process works and the fastify application can be run,
|
|
28
|
+
Tests need to ensure the build process works and the fastify application can be run,
|
|
29
29
|
no need to go in deep testing unless an issue is raised.
|
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const fastifySuccess = require('./dist/success')
|
|
6
5
|
const fastifyFailPlugin = require('./dist/failPlugin')
|
|
7
6
|
|
|
8
|
-
test('Bundled package should work', (t) => {
|
|
7
|
+
test('Bundled package should work', (t, done) => {
|
|
9
8
|
t.plan(4)
|
|
10
9
|
fastifySuccess.ready((err) => {
|
|
11
|
-
t.
|
|
10
|
+
t.assert.ifError(err)
|
|
12
11
|
fastifySuccess.inject(
|
|
13
12
|
{
|
|
14
13
|
method: 'GET',
|
|
15
14
|
url: '/'
|
|
16
15
|
},
|
|
17
16
|
(error, res) => {
|
|
18
|
-
t.
|
|
19
|
-
t.
|
|
20
|
-
t.
|
|
17
|
+
t.assert.ifError(error)
|
|
18
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
19
|
+
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
|
20
|
+
done()
|
|
21
21
|
}
|
|
22
22
|
)
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
-
test('Bundled package should not work with bad plugin version', (t) => {
|
|
26
|
+
test('Bundled package should not work with bad plugin version', (t, done) => {
|
|
27
27
|
t.plan(1)
|
|
28
28
|
fastifyFailPlugin.ready((err) => {
|
|
29
|
-
t.match(err.message, /expected '9.x' fastify version/i)
|
|
29
|
+
t.assert.match(err.message, /expected '9.x' fastify version/i)
|
|
30
|
+
done()
|
|
30
31
|
})
|
|
31
32
|
})
|
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const fastifySuccess = require('./dist/success')
|
|
6
5
|
const fastifyFailPlugin = require('./dist/failPlugin')
|
|
7
6
|
|
|
8
|
-
test('Bundled package should work', (t) => {
|
|
7
|
+
test('Bundled package should work', (t, done) => {
|
|
9
8
|
t.plan(4)
|
|
10
9
|
fastifySuccess.ready((err) => {
|
|
11
|
-
t.
|
|
10
|
+
t.assert.ifError(err)
|
|
12
11
|
fastifySuccess.inject(
|
|
13
12
|
{
|
|
14
13
|
method: 'GET',
|
|
15
14
|
url: '/'
|
|
16
15
|
},
|
|
17
16
|
(error, res) => {
|
|
18
|
-
t.
|
|
19
|
-
t.
|
|
20
|
-
t.
|
|
17
|
+
t.assert.ifError(error)
|
|
18
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
19
|
+
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
|
20
|
+
done()
|
|
21
21
|
}
|
|
22
22
|
)
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
-
test('Bundled package should not work with bad plugin version', (t) => {
|
|
26
|
+
test('Bundled package should not work with bad plugin version', (t, done) => {
|
|
27
27
|
t.plan(1)
|
|
28
28
|
fastifyFailPlugin.ready((err) => {
|
|
29
|
-
t.match(err.message, /expected '9.x' fastify version/i)
|
|
29
|
+
t.assert.match(err.message, /expected '9.x' fastify version/i)
|
|
30
|
+
done()
|
|
30
31
|
})
|
|
31
32
|
})
|