fastify 5.3.3 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +15 -15
- package/LICENSE +1 -1
- package/README.md +2 -0
- package/SECURITY.md +158 -2
- package/build/build-validation.js +20 -1
- package/docs/Guides/Delay-Accepting-Requests.md +8 -5
- package/docs/Guides/Ecosystem.md +20 -5
- package/docs/Guides/Migration-Guide-V5.md +6 -10
- package/docs/Guides/Recommendations.md +1 -1
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +5 -3
- package/docs/Reference/Hooks.md +16 -20
- package/docs/Reference/Lifecycle.md +2 -2
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +2 -2
- package/docs/Reference/Routes.md +7 -6
- package/docs/Reference/Server.md +341 -200
- package/docs/Reference/TypeScript.md +1 -3
- package/docs/Reference/Validation-and-Serialization.md +56 -4
- package/docs/Reference/Warnings.md +2 -1
- package/fastify.d.ts +4 -3
- package/fastify.js +47 -34
- package/lib/configValidator.js +196 -28
- package/lib/contentTypeParser.js +41 -48
- package/lib/error-handler.js +3 -3
- package/lib/errors.js +11 -0
- package/lib/handleRequest.js +13 -17
- package/lib/pluginOverride.js +3 -1
- package/lib/promise.js +23 -0
- package/lib/reply.js +24 -30
- package/lib/request.js +3 -10
- package/lib/route.js +37 -3
- package/lib/server.js +36 -35
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +19 -1
- package/package.json +14 -10
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +84 -128
- package/test/async_hooks.test.js +18 -37
- package/test/body-limit.test.js +90 -63
- package/test/buffer.test.js +22 -0
- package/test/build-certificate.js +1 -1
- package/test/case-insensitive.test.js +44 -65
- package/test/check.test.js +17 -21
- package/test/close-pipelining.test.js +24 -15
- package/test/constrained-routes.test.js +231 -0
- package/test/custom-http-server.test.js +7 -15
- package/test/custom-parser-async.test.js +17 -22
- package/test/custom-parser.0.test.js +267 -348
- package/test/custom-parser.1.test.js +141 -191
- package/test/custom-parser.2.test.js +34 -44
- package/test/custom-parser.3.test.js +56 -104
- package/test/custom-parser.4.test.js +106 -144
- package/test/custom-parser.5.test.js +56 -75
- package/test/custom-querystring-parser.test.js +51 -77
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +76 -259
- package/test/delete.test.js +101 -110
- package/test/diagnostics-channel/404.test.js +7 -15
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/async-request.test.js +8 -16
- package/test/diagnostics-channel/error-request.test.js +7 -15
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
- package/test/diagnostics-channel/sync-request.test.js +9 -16
- package/test/fastify-instance.test.js +1 -1
- package/test/header-overflow.test.js +18 -29
- package/test/helper.js +139 -135
- package/test/hooks-async.test.js +259 -235
- package/test/hooks.test.js +951 -996
- package/test/http-methods/copy.test.js +14 -19
- package/test/http-methods/get.test.js +131 -143
- package/test/http-methods/head.test.js +53 -84
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcalendar.test.js +45 -72
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/move.test.js +6 -10
- package/test/http-methods/propfind.test.js +34 -44
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http-methods/unlock.test.js +5 -9
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +31 -28
- package/test/https/custom-https-server.test.js +9 -13
- package/test/https/https.test.js +56 -53
- package/test/input-validation.js +139 -150
- package/test/internals/errors.test.js +50 -1
- package/test/internals/handle-request.test.js +72 -65
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply.test.js +277 -496
- package/test/issue-4959.test.js +12 -3
- package/test/listen.4.test.js +31 -43
- package/test/nullable-validation.test.js +33 -46
- package/test/output-validation.test.js +24 -26
- package/test/plugin.1.test.js +40 -68
- package/test/plugin.2.test.js +108 -120
- package/test/plugin.3.test.js +50 -72
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +42 -63
- package/test/proto-poisoning.test.js +78 -97
- package/test/register.test.js +8 -18
- package/test/request-error.test.js +57 -146
- package/test/request-id.test.js +30 -49
- package/test/route-hooks.test.js +117 -101
- package/test/route-prefix.test.js +194 -133
- package/test/route-shorthand.test.js +9 -27
- package/test/route.1.test.js +74 -131
- package/test/route.8.test.js +9 -17
- package/test/router-options.test.js +450 -0
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +254 -218
- package/test/server.test.js +143 -5
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +33 -50
- package/test/stream.4.test.js +18 -28
- package/test/stream.5.test.js +11 -19
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +13 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/request.test-d.ts +1 -0
- package/test/types/type-provider.test-d.ts +55 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +117 -59
- package/test/versioned-routes.test.js +39 -56
- package/types/errors.d.ts +11 -1
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +1 -1
- package/types/reply.d.ts +2 -2
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
|
-
const sget = require('simple-get').concat
|
|
6
5
|
|
|
7
|
-
test('allow unsafe regex',
|
|
8
|
-
t.plan(
|
|
6
|
+
test('allow unsafe regex', async t => {
|
|
7
|
+
t.plan(2)
|
|
9
8
|
|
|
10
9
|
const fastify = Fastify({
|
|
11
10
|
allowUnsafeRegex: false
|
|
@@ -16,25 +15,15 @@ test('allow unsafe regex', (t, done) => {
|
|
|
16
15
|
reply.send({ foo: req.params.foo })
|
|
17
16
|
})
|
|
18
17
|
|
|
19
|
-
fastify.listen({ port: 0 }
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
url: 'http://localhost:' + fastify.server.address().port + '/1234'
|
|
25
|
-
}, (err, response, body) => {
|
|
26
|
-
t.assert.ifError(err)
|
|
27
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
28
|
-
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
29
|
-
foo: '1234'
|
|
30
|
-
})
|
|
31
|
-
done()
|
|
32
|
-
})
|
|
33
|
-
})
|
|
18
|
+
await fastify.listen({ port: 0 })
|
|
19
|
+
|
|
20
|
+
const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
|
|
21
|
+
t.assert.strictEqual(result.status, 200)
|
|
22
|
+
t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
|
|
34
23
|
})
|
|
35
24
|
|
|
36
|
-
test('allow unsafe regex not match',
|
|
37
|
-
t.plan(
|
|
25
|
+
test('allow unsafe regex not match', async t => {
|
|
26
|
+
t.plan(1)
|
|
38
27
|
|
|
39
28
|
const fastify = Fastify({
|
|
40
29
|
allowUnsafeRegex: false
|
|
@@ -45,18 +34,10 @@ test('allow unsafe regex not match', (t, done) => {
|
|
|
45
34
|
reply.send({ foo: req.params.foo })
|
|
46
35
|
})
|
|
47
36
|
|
|
48
|
-
fastify.listen({ port: 0 }
|
|
49
|
-
t.assert.ifError(err)
|
|
37
|
+
await fastify.listen({ port: 0 })
|
|
50
38
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
url: 'http://localhost:' + fastify.server.address().port + '/a1234'
|
|
54
|
-
}, (err, response, body) => {
|
|
55
|
-
t.assert.ifError(err)
|
|
56
|
-
t.assert.strictEqual(response.statusCode, 404)
|
|
57
|
-
done()
|
|
58
|
-
})
|
|
59
|
-
})
|
|
39
|
+
const result = await fetch(`http://localhost:${fastify.server.address().port}/a1234`)
|
|
40
|
+
t.assert.strictEqual(result.status, 404)
|
|
60
41
|
})
|
|
61
42
|
|
|
62
43
|
test('allow unsafe regex not safe', (t, done) => {
|
|
@@ -89,8 +70,8 @@ test('allow unsafe regex not safe by default', (t, done) => {
|
|
|
89
70
|
done()
|
|
90
71
|
})
|
|
91
72
|
|
|
92
|
-
test('allow unsafe regex allow unsafe',
|
|
93
|
-
t.plan(
|
|
73
|
+
test('allow unsafe regex allow unsafe', async t => {
|
|
74
|
+
t.plan(3)
|
|
94
75
|
|
|
95
76
|
const fastify = Fastify({
|
|
96
77
|
allowUnsafeRegex: true
|
|
@@ -103,19 +84,9 @@ test('allow unsafe regex allow unsafe', (t, done) => {
|
|
|
103
84
|
})
|
|
104
85
|
})
|
|
105
86
|
|
|
106
|
-
fastify.listen({ port: 0 }
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
url: 'http://localhost:' + fastify.server.address().port + '/1234'
|
|
112
|
-
}, (err, response, body) => {
|
|
113
|
-
t.assert.ifError(err)
|
|
114
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
115
|
-
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
116
|
-
foo: '1234'
|
|
117
|
-
})
|
|
118
|
-
done()
|
|
119
|
-
})
|
|
120
|
-
})
|
|
87
|
+
await fastify.listen({ port: 0 })
|
|
88
|
+
|
|
89
|
+
const result = await fetch(`http://localhost:${fastify.server.address().port}/1234`)
|
|
90
|
+
t.assert.strictEqual(result.status, 200)
|
|
91
|
+
t.assert.deepStrictEqual(await result.json(), { foo: '1234' })
|
|
121
92
|
})
|
package/test/als.test.js
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
const { AsyncLocalStorage } = require('node:async_hooks')
|
|
4
4
|
const { test } = require('node:test')
|
|
5
5
|
const Fastify = require('..')
|
|
6
|
-
const sget = require('simple-get').concat
|
|
7
6
|
|
|
8
|
-
test('Async Local Storage test', (t
|
|
9
|
-
t.plan(
|
|
7
|
+
test('Async Local Storage test', async (t) => {
|
|
8
|
+
t.plan(12)
|
|
10
9
|
if (!AsyncLocalStorage) {
|
|
11
10
|
t.skip('AsyncLocalStorage not available, skipping test')
|
|
12
11
|
process.exit(0)
|
|
@@ -15,6 +14,8 @@ test('Async Local Storage test', (t, done) => {
|
|
|
15
14
|
const storage = new AsyncLocalStorage()
|
|
16
15
|
const app = Fastify({ logger: false })
|
|
17
16
|
|
|
17
|
+
t.after(() => app.close())
|
|
18
|
+
|
|
18
19
|
let counter = 0
|
|
19
20
|
app.addHook('onRequest', (req, reply, next) => {
|
|
20
21
|
const id = counter++
|
|
@@ -33,45 +34,32 @@ test('Async Local Storage test', (t, done) => {
|
|
|
33
34
|
reply.send({ id })
|
|
34
35
|
})
|
|
35
36
|
|
|
36
|
-
app.listen({ port: 0 }
|
|
37
|
-
t.assert.ifError(err)
|
|
37
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
49
|
-
t.assert.deepStrictEqual(body, { id: 0 })
|
|
39
|
+
// First POST request
|
|
40
|
+
const result1 = await fetch(fastifyServer, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'Content-Type': 'application/json' },
|
|
43
|
+
body: JSON.stringify({ hello: 'world' })
|
|
44
|
+
})
|
|
45
|
+
t.assert.ok(result1.ok)
|
|
46
|
+
t.assert.strictEqual(result1.status, 200)
|
|
47
|
+
t.assert.deepStrictEqual(await result1.json(), { id: 0 })
|
|
50
48
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
t.assert.ifError(err)
|
|
60
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
61
|
-
t.assert.deepStrictEqual(body, { id: 1 })
|
|
49
|
+
const result2 = await fetch(fastifyServer, {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
headers: { 'Content-Type': 'application/json' },
|
|
52
|
+
body: JSON.stringify({ hello: 'world' })
|
|
53
|
+
})
|
|
54
|
+
t.assert.ok(result2.ok)
|
|
55
|
+
t.assert.strictEqual(result2.status, 200)
|
|
56
|
+
t.assert.deepStrictEqual(await result2.json(), { id: 1 })
|
|
62
57
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
json: true
|
|
67
|
-
}, (err, response, body) => {
|
|
68
|
-
t.assert.ifError(err)
|
|
69
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
70
|
-
t.assert.deepStrictEqual(body, { id: 2 })
|
|
71
|
-
app.close()
|
|
72
|
-
done()
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
})
|
|
58
|
+
// GET request
|
|
59
|
+
const result3 = await fetch(fastifyServer, {
|
|
60
|
+
method: 'GET'
|
|
76
61
|
})
|
|
62
|
+
t.assert.ok(result3.ok)
|
|
63
|
+
t.assert.strictEqual(result3.status, 200)
|
|
64
|
+
t.assert.deepStrictEqual(await result3.json(), { id: 2 })
|
|
77
65
|
})
|
package/test/async-await.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const sget = require('simple-get').concat
|
|
5
4
|
const Fastify = require('..')
|
|
6
5
|
const split = require('split2')
|
|
7
6
|
const pino = require('pino')
|
|
@@ -43,8 +42,8 @@ const optsWithHostnameAndPort = {
|
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
|
-
test('async await',
|
|
47
|
-
t.plan(
|
|
45
|
+
test('async await', async t => {
|
|
46
|
+
t.plan(15)
|
|
48
47
|
const fastify = Fastify()
|
|
49
48
|
try {
|
|
50
49
|
fastify.get('/', opts, async function awaitMyFunc (req, reply) {
|
|
@@ -75,46 +74,37 @@ test('async await', (t, done) => {
|
|
|
75
74
|
t.assert.fail()
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
fastify.listen({ port: 0 }
|
|
79
|
-
t.assert.ifError(err)
|
|
80
|
-
t.after(() => { fastify.close() })
|
|
81
|
-
|
|
82
|
-
sget({
|
|
83
|
-
method: 'GET',
|
|
84
|
-
url: 'http://localhost:' + fastify.server.address().port
|
|
85
|
-
}, (err, response, body) => {
|
|
86
|
-
t.assert.ifError(err)
|
|
87
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
88
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
89
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
90
|
-
})
|
|
77
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
91
78
|
|
|
92
|
-
|
|
93
|
-
method: 'GET',
|
|
94
|
-
url: 'http://localhost:' + fastify.server.address().port + '/no-await'
|
|
95
|
-
}, (err, response, body) => {
|
|
96
|
-
t.assert.ifError(err)
|
|
97
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
98
|
-
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
99
|
-
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
100
|
-
})
|
|
79
|
+
t.after(() => { fastify.close() })
|
|
101
80
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
81
|
+
const result = await fetch(fastifyServer)
|
|
82
|
+
|
|
83
|
+
const body = await result.text()
|
|
84
|
+
t.assert.ok(result.ok)
|
|
85
|
+
t.assert.strictEqual(result.status, 200)
|
|
86
|
+
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
|
87
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
88
|
+
|
|
89
|
+
const result1 = await fetch(`${fastifyServer}/no-await`)
|
|
90
|
+
|
|
91
|
+
const body1 = await result1.text()
|
|
92
|
+
t.assert.ok(result1.ok)
|
|
93
|
+
t.assert.strictEqual(result1.status, 200)
|
|
94
|
+
t.assert.strictEqual(result1.headers.get('content-length'), '' + body1.length)
|
|
95
|
+
t.assert.deepStrictEqual(JSON.parse(body1), { hello: 'world' })
|
|
96
|
+
|
|
97
|
+
const result2 = await fetch(`http://localhost:${fastify.server.address().port}/await/hostname_port`)
|
|
98
|
+
|
|
99
|
+
const parsedBody = await result2.json()
|
|
100
|
+
t.assert.ok(result2.ok)
|
|
101
|
+
t.assert.strictEqual(result2.status, 200)
|
|
102
|
+
t.assert.strictEqual(parsedBody.hostname, 'localhost')
|
|
103
|
+
t.assert.strictEqual(parseInt(parsedBody.port), fastify.server.address().port)
|
|
114
104
|
})
|
|
115
105
|
|
|
116
|
-
test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t
|
|
117
|
-
t.plan(
|
|
106
|
+
test('ignore the result of the promise if reply.send is called beforehand (undefined)', async (t) => {
|
|
107
|
+
t.plan(3)
|
|
118
108
|
|
|
119
109
|
const server = Fastify()
|
|
120
110
|
const payload = { hello: 'world' }
|
|
@@ -125,22 +115,17 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
|
|
|
125
115
|
|
|
126
116
|
t.after(() => { server.close() })
|
|
127
117
|
|
|
128
|
-
server.listen({ port: 0 }
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
136
|
-
t.assert.strictEqual(res.statusCode, 200)
|
|
137
|
-
done()
|
|
138
|
-
})
|
|
139
|
-
})
|
|
118
|
+
const fastifyServer = await server.listen({ port: 0 })
|
|
119
|
+
|
|
120
|
+
const result = await fetch(fastifyServer)
|
|
121
|
+
|
|
122
|
+
t.assert.ok(result.ok)
|
|
123
|
+
t.assert.deepStrictEqual(payload, await result.json())
|
|
124
|
+
t.assert.strictEqual(result.status, 200)
|
|
140
125
|
})
|
|
141
126
|
|
|
142
|
-
test('ignore the result of the promise if reply.send is called beforehand (object)', (t
|
|
143
|
-
t.plan(
|
|
127
|
+
test('ignore the result of the promise if reply.send is called beforehand (object)', async (t) => {
|
|
128
|
+
t.plan(3)
|
|
144
129
|
|
|
145
130
|
const server = Fastify()
|
|
146
131
|
const payload = { hello: 'world2' }
|
|
@@ -152,18 +137,13 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
|
|
|
152
137
|
|
|
153
138
|
t.after(() => { server.close() })
|
|
154
139
|
|
|
155
|
-
server.listen({ port: 0 }
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
163
|
-
t.assert.strictEqual(res.statusCode, 200)
|
|
164
|
-
done()
|
|
165
|
-
})
|
|
166
|
-
})
|
|
140
|
+
const fastifyServer = await server.listen({ port: 0 })
|
|
141
|
+
|
|
142
|
+
const result = await fetch(fastifyServer)
|
|
143
|
+
|
|
144
|
+
t.assert.ok(result.ok)
|
|
145
|
+
t.assert.deepStrictEqual(payload, await result.json())
|
|
146
|
+
t.assert.strictEqual(result.status, 200)
|
|
167
147
|
})
|
|
168
148
|
|
|
169
149
|
test('server logs an error if reply.send is called and a value is returned via async/await', (t, done) => {
|
|
@@ -197,8 +177,8 @@ test('server logs an error if reply.send is called and a value is returned via a
|
|
|
197
177
|
})
|
|
198
178
|
})
|
|
199
179
|
|
|
200
|
-
test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t
|
|
201
|
-
t.plan(
|
|
180
|
+
test('ignore the result of the promise if reply.send is called beforehand (undefined)', async (t) => {
|
|
181
|
+
t.plan(3)
|
|
202
182
|
|
|
203
183
|
const server = Fastify()
|
|
204
184
|
const payload = { hello: 'world' }
|
|
@@ -209,22 +189,17 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
|
|
|
209
189
|
|
|
210
190
|
t.after(() => { server.close() })
|
|
211
191
|
|
|
212
|
-
server.listen({ port: 0 }
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
220
|
-
t.assert.strictEqual(res.statusCode, 200)
|
|
221
|
-
done()
|
|
222
|
-
})
|
|
223
|
-
})
|
|
192
|
+
const fastifyServer = await server.listen({ port: 0 })
|
|
193
|
+
|
|
194
|
+
const result = await fetch(fastifyServer)
|
|
195
|
+
|
|
196
|
+
t.assert.ok(result.ok)
|
|
197
|
+
t.assert.deepStrictEqual(payload, await result.json())
|
|
198
|
+
t.assert.strictEqual(result.status, 200)
|
|
224
199
|
})
|
|
225
200
|
|
|
226
|
-
test('ignore the result of the promise if reply.send is called beforehand (object)', (t
|
|
227
|
-
t.plan(
|
|
201
|
+
test('ignore the result of the promise if reply.send is called beforehand (object)', async (t) => {
|
|
202
|
+
t.plan(3)
|
|
228
203
|
|
|
229
204
|
const server = Fastify()
|
|
230
205
|
const payload = { hello: 'world2' }
|
|
@@ -236,18 +211,13 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
|
|
|
236
211
|
|
|
237
212
|
t.after(() => { server.close() })
|
|
238
213
|
|
|
239
|
-
server.listen({ port: 0 }
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
247
|
-
t.assert.strictEqual(res.statusCode, 200)
|
|
248
|
-
done()
|
|
249
|
-
})
|
|
250
|
-
})
|
|
214
|
+
const fastifyServer = await server.listen({ port: 0 })
|
|
215
|
+
|
|
216
|
+
const result = await fetch(fastifyServer)
|
|
217
|
+
|
|
218
|
+
t.assert.ok(result.ok)
|
|
219
|
+
t.assert.deepStrictEqual(payload, await result.json())
|
|
220
|
+
t.assert.strictEqual(result.status, 200)
|
|
251
221
|
})
|
|
252
222
|
|
|
253
223
|
test('await reply if we will be calling reply.send in the future', (t, done) => {
|
|
@@ -433,8 +403,8 @@ test('does not call reply.send() twice if 204 response equal already sent', (t,
|
|
|
433
403
|
})
|
|
434
404
|
})
|
|
435
405
|
|
|
436
|
-
test('promise was fulfilled with undefined', (t
|
|
437
|
-
t.plan(
|
|
406
|
+
test('promise was fulfilled with undefined', async (t) => {
|
|
407
|
+
t.plan(3)
|
|
438
408
|
|
|
439
409
|
let fastify = null
|
|
440
410
|
const stream = split(JSON.parse)
|
|
@@ -458,20 +428,13 @@ test('promise was fulfilled with undefined', (t, done) => {
|
|
|
458
428
|
t.assert.fail('should not log an error')
|
|
459
429
|
})
|
|
460
430
|
|
|
461
|
-
fastify.listen({ port: 0 }
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}, (err, res, body) => {
|
|
469
|
-
t.assert.ifError(err)
|
|
470
|
-
t.assert.strictEqual(res.body, undefined)
|
|
471
|
-
t.assert.strictEqual(res.statusCode, 200)
|
|
472
|
-
done()
|
|
473
|
-
})
|
|
474
|
-
})
|
|
431
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
432
|
+
|
|
433
|
+
const result = await fetch(fastifyServer)
|
|
434
|
+
|
|
435
|
+
t.assert.ok(result.ok)
|
|
436
|
+
t.assert.strictEqual(await result.text(), '')
|
|
437
|
+
t.assert.strictEqual(result.status, 200)
|
|
475
438
|
})
|
|
476
439
|
|
|
477
440
|
test('promise was fulfilled with undefined using inject', async (t) => {
|
|
@@ -496,8 +459,8 @@ test('promise was fulfilled with undefined using inject', async (t) => {
|
|
|
496
459
|
t.assert.strictEqual(res.statusCode, 200)
|
|
497
460
|
})
|
|
498
461
|
|
|
499
|
-
test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', (t
|
|
500
|
-
t.plan(
|
|
462
|
+
test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', async (t) => {
|
|
463
|
+
t.plan(3)
|
|
501
464
|
|
|
502
465
|
let fastify = null
|
|
503
466
|
const stream = split(JSON.parse)
|
|
@@ -523,23 +486,16 @@ test('error is not logged because promise was fulfilled with undefined but respo
|
|
|
523
486
|
t.assert.fail('should not log an error')
|
|
524
487
|
})
|
|
525
488
|
|
|
526
|
-
fastify.listen({ port: 0 }
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
t.assert.deepStrictEqual(
|
|
537
|
-
payload,
|
|
538
|
-
JSON.parse(body)
|
|
539
|
-
)
|
|
540
|
-
done()
|
|
541
|
-
})
|
|
542
|
-
})
|
|
489
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
490
|
+
|
|
491
|
+
const result = await fetch(fastifyServer)
|
|
492
|
+
|
|
493
|
+
t.assert.ok(result.ok)
|
|
494
|
+
t.assert.strictEqual(result.status, 200)
|
|
495
|
+
t.assert.deepStrictEqual(
|
|
496
|
+
payload,
|
|
497
|
+
await result.json()
|
|
498
|
+
)
|
|
543
499
|
})
|
|
544
500
|
|
|
545
501
|
test('Thrown Error instance sets HTTP status code', (t, done) => {
|
package/test/async_hooks.test.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const { createHook } = require('node:async_hooks')
|
|
4
4
|
const { test } = require('node:test')
|
|
5
5
|
const Fastify = require('..')
|
|
6
|
-
const sget = require('simple-get').concat
|
|
7
6
|
|
|
8
7
|
const remainingIds = new Set()
|
|
9
8
|
|
|
@@ -20,7 +19,7 @@ createHook({
|
|
|
20
19
|
|
|
21
20
|
const app = Fastify({ logger: false })
|
|
22
21
|
|
|
23
|
-
test('test async hooks', (t
|
|
22
|
+
test('test async hooks', async (t) => {
|
|
24
23
|
app.get('/', function (request, reply) {
|
|
25
24
|
reply.send({ id: 42 })
|
|
26
25
|
})
|
|
@@ -29,43 +28,25 @@ test('test async hooks', (t, done) => {
|
|
|
29
28
|
reply.send({ id: 42 })
|
|
30
29
|
})
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
t.assert.ifError(err)
|
|
31
|
+
t.after(() => app.close())
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
method: 'POST',
|
|
37
|
-
url: 'http://localhost:' + app.server.address().port,
|
|
38
|
-
body: {
|
|
39
|
-
hello: 'world'
|
|
40
|
-
},
|
|
41
|
-
json: true
|
|
42
|
-
}, (err, response, body) => {
|
|
43
|
-
t.assert.ifError(err)
|
|
44
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
33
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
45
34
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
json: true
|
|
53
|
-
}, (err, response, body) => {
|
|
54
|
-
t.assert.ifError(err)
|
|
55
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
35
|
+
const result1 = await fetch(fastifyServer, {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: { 'Content-Type': 'application/json' },
|
|
38
|
+
body: JSON.stringify({ hello: 'world' })
|
|
39
|
+
})
|
|
40
|
+
t.assert.strictEqual(result1.status, 200)
|
|
56
41
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
})
|
|
68
|
-
})
|
|
69
|
-
})
|
|
42
|
+
const result2 = await fetch(fastifyServer, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: { 'Content-Type': 'application/json' },
|
|
45
|
+
body: JSON.stringify({ hello: 'world' })
|
|
70
46
|
})
|
|
47
|
+
t.assert.strictEqual(result2.status, 200)
|
|
48
|
+
|
|
49
|
+
const result3 = await fetch(fastifyServer)
|
|
50
|
+
t.assert.strictEqual(result3.status, 200)
|
|
51
|
+
t.assert.strictEqual(remainingIds.size, 0)
|
|
71
52
|
})
|