fastify 5.3.2 → 5.4.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/README.md +2 -0
- package/build/build-validation.js +2 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +16 -7
- package/docs/Guides/Serverless.md +28 -69
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +2 -4
- package/docs/Reference/Hooks.md +14 -14
- 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 +1 -1
- package/docs/Reference/Routes.md +3 -3
- package/docs/Reference/Server.md +40 -10
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/eslint.config.js +17 -9
- package/fastify.d.ts +2 -1
- package/fastify.js +20 -4
- package/lib/configValidator.js +1 -1
- package/lib/decorate.js +2 -2
- package/lib/errors.js +6 -8
- package/lib/logger-factory.js +1 -1
- package/lib/logger-pino.js +2 -2
- package/lib/pluginOverride.js +3 -1
- package/lib/reply.js +9 -13
- package/lib/request.js +4 -11
- package/lib/server.js +30 -51
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +8 -0
- package/package.json +11 -7
- 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 +11 -2
- package/test/body-limit.test.js +41 -65
- package/test/build-certificate.js +1 -1
- package/test/close-pipelining.test.js +5 -4
- package/test/custom-parser-async.test.js +17 -22
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +422 -341
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/helper.js +108 -70
- package/test/hooks-async.test.js +248 -218
- package/test/hooks.on-listen.test.js +255 -239
- package/test/hooks.on-ready.test.js +110 -92
- package/test/hooks.test.js +910 -769
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcol.test.js +5 -9
- 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/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +28 -27
- package/test/https/https.test.js +56 -53
- package/test/inject.test.js +114 -97
- package/test/input-validation.js +63 -53
- package/test/internals/errors.test.js +0 -10
- package/test/internals/handle-request.test.js +49 -66
- package/test/internals/hooks.test.js +17 -0
- package/test/issue-4959.test.js +14 -5
- package/test/listen.4.test.js +31 -43
- package/test/logger/response.test.js +19 -20
- package/test/nullable-validation.test.js +33 -46
- package/test/options.error-handler.test.js +1 -1
- package/test/options.test.js +1 -1
- package/test/output-validation.test.js +49 -72
- package/test/patch.error-handler.test.js +1 -1
- package/test/patch.test.js +1 -1
- package/test/plugin.1.test.js +71 -60
- package/test/plugin.2.test.js +104 -86
- package/test/plugin.3.test.js +56 -35
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +36 -30
- package/test/proto-poisoning.test.js +78 -97
- package/test/put.error-handler.test.js +1 -1
- package/test/put.test.js +1 -1
- package/test/reply-error.test.js +169 -148
- package/test/reply-trailers.test.js +119 -108
- package/test/request-error.test.js +0 -46
- package/test/route-hooks.test.js +112 -92
- package/test/route-prefix.test.js +194 -133
- package/test/schema-feature.test.js +309 -238
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +278 -199
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +30 -27
- package/test/stream.2.test.js +20 -10
- package/test/stream.3.test.js +37 -31
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +0 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/register.test-d.ts +1 -1
- package/test/types/request.test-d.ts +1 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +1 -1
- package/types/errors.d.ts +0 -1
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
- package/test/http2/missing-http2-module.test.js +0 -17
package/test/plugin.2.test.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
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
|
const sget = require('simple-get').concat
|
|
7
6
|
const fp = require('fastify-plugin')
|
|
7
|
+
const { waitForCb } = require('./toolkit')
|
|
8
8
|
|
|
9
|
-
test('check dependencies - should not throw', t => {
|
|
9
|
+
test('check dependencies - should not throw', (t, testDone) => {
|
|
10
10
|
t.plan(12)
|
|
11
11
|
const fastify = Fastify()
|
|
12
12
|
|
|
13
13
|
fastify.register((instance, opts, done) => {
|
|
14
14
|
instance.register(fp((i, o, n) => {
|
|
15
15
|
i.decorate('test', () => {})
|
|
16
|
-
t.ok(i.test)
|
|
16
|
+
t.assert.ok(i.test)
|
|
17
17
|
n()
|
|
18
18
|
}))
|
|
19
19
|
|
|
20
20
|
instance.register(fp((i, o, n) => {
|
|
21
21
|
try {
|
|
22
22
|
i.decorate('otherTest', () => {}, ['test'])
|
|
23
|
-
t.ok(i.test)
|
|
24
|
-
t.ok(i.otherTest)
|
|
23
|
+
t.assert.ok(i.test)
|
|
24
|
+
t.assert.ok(i.otherTest)
|
|
25
25
|
n()
|
|
26
26
|
} catch (e) {
|
|
27
|
-
t.fail()
|
|
27
|
+
t.assert.fail()
|
|
28
28
|
}
|
|
29
29
|
}))
|
|
30
30
|
|
|
31
31
|
instance.get('/', (req, reply) => {
|
|
32
|
-
t.ok(instance.test)
|
|
33
|
-
t.ok(instance.otherTest)
|
|
32
|
+
t.assert.ok(instance.test)
|
|
33
|
+
t.assert.ok(instance.otherTest)
|
|
34
34
|
reply.send({ hello: 'world' })
|
|
35
35
|
})
|
|
36
36
|
|
|
@@ -38,27 +38,28 @@ test('check dependencies - should not throw', t => {
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
fastify.ready(() => {
|
|
41
|
-
t.
|
|
42
|
-
t.
|
|
41
|
+
t.assert.ok(!fastify.test)
|
|
42
|
+
t.assert.ok(!fastify.otherTest)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
45
|
fastify.listen({ port: 0 }, err => {
|
|
46
|
-
t.
|
|
47
|
-
t.
|
|
46
|
+
t.assert.ifError(err)
|
|
47
|
+
t.after(() => { fastify.close() })
|
|
48
48
|
|
|
49
49
|
sget({
|
|
50
50
|
method: 'GET',
|
|
51
51
|
url: 'http://localhost:' + fastify.server.address().port
|
|
52
52
|
}, (err, response, body) => {
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
55
|
-
t.
|
|
56
|
-
t.
|
|
53
|
+
t.assert.ifError(err)
|
|
54
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
55
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
56
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
57
|
+
testDone()
|
|
57
58
|
})
|
|
58
59
|
})
|
|
59
60
|
})
|
|
60
61
|
|
|
61
|
-
test('check dependencies - should throw', t => {
|
|
62
|
+
test('check dependencies - should throw', (t, testDone) => {
|
|
62
63
|
t.plan(12)
|
|
63
64
|
const fastify = Fastify()
|
|
64
65
|
|
|
@@ -66,24 +67,24 @@ test('check dependencies - should throw', t => {
|
|
|
66
67
|
instance.register(fp((i, o, n) => {
|
|
67
68
|
try {
|
|
68
69
|
i.decorate('otherTest', () => {}, ['test'])
|
|
69
|
-
t.fail()
|
|
70
|
+
t.assert.fail()
|
|
70
71
|
} catch (e) {
|
|
71
|
-
t.
|
|
72
|
-
t.
|
|
72
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
|
|
73
|
+
t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')
|
|
73
74
|
}
|
|
74
75
|
n()
|
|
75
76
|
}))
|
|
76
77
|
|
|
77
78
|
instance.register(fp((i, o, n) => {
|
|
78
79
|
i.decorate('test', () => {})
|
|
79
|
-
t.ok(i.test)
|
|
80
|
-
t.
|
|
80
|
+
t.assert.ok(i.test)
|
|
81
|
+
t.assert.ok(!i.otherTest)
|
|
81
82
|
n()
|
|
82
83
|
}))
|
|
83
84
|
|
|
84
85
|
instance.get('/', (req, reply) => {
|
|
85
|
-
t.ok(instance.test)
|
|
86
|
-
t.
|
|
86
|
+
t.assert.ok(instance.test)
|
|
87
|
+
t.assert.ok(!instance.otherTest)
|
|
87
88
|
reply.send({ hello: 'world' })
|
|
88
89
|
})
|
|
89
90
|
|
|
@@ -91,183 +92,192 @@ test('check dependencies - should throw', t => {
|
|
|
91
92
|
})
|
|
92
93
|
|
|
93
94
|
fastify.ready(() => {
|
|
94
|
-
t.
|
|
95
|
+
t.assert.ok(!fastify.test)
|
|
95
96
|
})
|
|
96
97
|
|
|
97
98
|
fastify.listen({ port: 0 }, err => {
|
|
98
|
-
t.
|
|
99
|
-
t.
|
|
99
|
+
t.assert.ifError(err)
|
|
100
|
+
t.after(() => { fastify.close() })
|
|
100
101
|
|
|
101
102
|
sget({
|
|
102
103
|
method: 'GET',
|
|
103
104
|
url: 'http://localhost:' + fastify.server.address().port
|
|
104
105
|
}, (err, response, body) => {
|
|
105
|
-
t.
|
|
106
|
-
t.
|
|
107
|
-
t.
|
|
108
|
-
t.
|
|
106
|
+
t.assert.ifError(err)
|
|
107
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
108
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
109
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
110
|
+
testDone()
|
|
109
111
|
})
|
|
110
112
|
})
|
|
111
113
|
})
|
|
112
114
|
|
|
113
|
-
test('set the plugin name based on the plugin displayName symbol', t => {
|
|
115
|
+
test('set the plugin name based on the plugin displayName symbol', (t, testDone) => {
|
|
114
116
|
t.plan(6)
|
|
115
117
|
const fastify = Fastify()
|
|
118
|
+
t.after(() => fastify.close())
|
|
116
119
|
|
|
117
120
|
fastify.register(fp((fastify, opts, done) => {
|
|
118
|
-
t.
|
|
121
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A')
|
|
119
122
|
fastify.register(fp((fastify, opts, done) => {
|
|
120
|
-
t.
|
|
123
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
|
|
121
124
|
done()
|
|
122
125
|
}, { name: 'plugin-AB' }))
|
|
123
126
|
fastify.register(fp((fastify, opts, done) => {
|
|
124
|
-
t.
|
|
127
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
125
128
|
done()
|
|
126
129
|
}, { name: 'plugin-AC' }))
|
|
127
130
|
done()
|
|
128
131
|
}, { name: 'plugin-A' }))
|
|
129
132
|
|
|
130
133
|
fastify.register(fp((fastify, opts, done) => {
|
|
131
|
-
t.
|
|
134
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
|
|
132
135
|
done()
|
|
133
136
|
}, { name: 'plugin-B' }))
|
|
134
137
|
|
|
135
|
-
t.
|
|
138
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
136
139
|
|
|
137
140
|
fastify.listen({ port: 0 }, err => {
|
|
138
|
-
t.
|
|
139
|
-
|
|
141
|
+
t.assert.ifError(err)
|
|
142
|
+
testDone()
|
|
140
143
|
})
|
|
141
144
|
})
|
|
142
145
|
|
|
143
|
-
test('plugin name will change when using no encapsulation', t => {
|
|
146
|
+
test('plugin name will change when using no encapsulation', (t, testDone) => {
|
|
144
147
|
t.plan(6)
|
|
145
148
|
const fastify = Fastify()
|
|
149
|
+
t.after(() => fastify.close())
|
|
146
150
|
|
|
147
151
|
fastify.register(fp((fastify, opts, done) => {
|
|
148
152
|
// store it in a different variable will hold the correct name
|
|
149
153
|
const pluginName = fastify.pluginName
|
|
150
154
|
fastify.register(fp((fastify, opts, done) => {
|
|
151
|
-
t.
|
|
155
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
|
|
152
156
|
done()
|
|
153
157
|
}, { name: 'plugin-AB' }))
|
|
154
158
|
fastify.register(fp((fastify, opts, done) => {
|
|
155
|
-
t.
|
|
159
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
156
160
|
done()
|
|
157
161
|
}, { name: 'plugin-AC' }))
|
|
158
162
|
setImmediate(() => {
|
|
159
163
|
// normally we would expect the name plugin-A
|
|
160
164
|
// but we operate on the same instance in each plugin
|
|
161
|
-
t.
|
|
162
|
-
t.
|
|
165
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
166
|
+
t.assert.strictEqual(pluginName, 'fastify -> plugin-A')
|
|
163
167
|
})
|
|
164
168
|
done()
|
|
165
169
|
}, { name: 'plugin-A' }))
|
|
166
170
|
|
|
167
|
-
t.
|
|
171
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
168
172
|
|
|
169
173
|
fastify.listen({ port: 0 }, err => {
|
|
170
|
-
t.
|
|
171
|
-
|
|
174
|
+
t.assert.ifError(err)
|
|
175
|
+
testDone()
|
|
172
176
|
})
|
|
173
177
|
})
|
|
174
178
|
|
|
175
|
-
test('plugin name is undefined when accessing in no plugin context', t => {
|
|
179
|
+
test('plugin name is undefined when accessing in no plugin context', (t, testDone) => {
|
|
176
180
|
t.plan(2)
|
|
177
181
|
const fastify = Fastify()
|
|
182
|
+
t.after(() => fastify.close())
|
|
178
183
|
|
|
179
|
-
t.
|
|
184
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
180
185
|
|
|
181
186
|
fastify.listen({ port: 0 }, err => {
|
|
182
|
-
t.
|
|
183
|
-
|
|
187
|
+
t.assert.ifError(err)
|
|
188
|
+
testDone()
|
|
184
189
|
})
|
|
185
190
|
})
|
|
186
191
|
|
|
187
|
-
test('set the plugin name based on the plugin function name', t => {
|
|
192
|
+
test('set the plugin name based on the plugin function name', (t, testDone) => {
|
|
188
193
|
t.plan(5)
|
|
189
194
|
const fastify = Fastify()
|
|
195
|
+
t.after(() => fastify.close())
|
|
190
196
|
|
|
191
197
|
fastify.register(function myPluginA (fastify, opts, done) {
|
|
192
|
-
t.
|
|
198
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginA')
|
|
193
199
|
fastify.register(function myPluginAB (fastify, opts, done) {
|
|
194
|
-
t.
|
|
200
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginAB')
|
|
195
201
|
done()
|
|
196
202
|
})
|
|
197
203
|
setImmediate(() => {
|
|
198
204
|
// exact name due to encapsulation
|
|
199
|
-
t.
|
|
205
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginA')
|
|
200
206
|
})
|
|
201
207
|
done()
|
|
202
208
|
})
|
|
203
209
|
|
|
204
210
|
fastify.register(function myPluginB (fastify, opts, done) {
|
|
205
|
-
t.
|
|
211
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginB')
|
|
206
212
|
done()
|
|
207
213
|
})
|
|
208
214
|
|
|
209
215
|
fastify.listen({ port: 0 }, err => {
|
|
210
|
-
t.
|
|
211
|
-
|
|
216
|
+
t.assert.ifError(err)
|
|
217
|
+
testDone()
|
|
212
218
|
})
|
|
213
219
|
})
|
|
214
220
|
|
|
215
|
-
test('approximate a plugin name when no meta data is available', t => {
|
|
221
|
+
test('approximate a plugin name when no meta data is available', (t, testDone) => {
|
|
216
222
|
t.plan(7)
|
|
217
223
|
const fastify = Fastify()
|
|
224
|
+
t.after(() => fastify.close())
|
|
218
225
|
|
|
219
226
|
fastify.register((fastify, opts, done) => {
|
|
220
227
|
// A
|
|
221
|
-
t.
|
|
222
|
-
t.
|
|
228
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
229
|
+
t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
|
|
223
230
|
fastify.register((fastify, opts, done) => {
|
|
224
231
|
// B
|
|
225
|
-
t.
|
|
226
|
-
t.
|
|
232
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
233
|
+
t.assert.strictEqual(fastify.pluginName.includes('// B'), true)
|
|
227
234
|
done()
|
|
228
235
|
})
|
|
229
236
|
setImmediate(() => {
|
|
230
|
-
t.
|
|
231
|
-
t.
|
|
237
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
238
|
+
t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
|
|
232
239
|
})
|
|
233
240
|
done()
|
|
234
241
|
})
|
|
235
242
|
|
|
236
243
|
fastify.listen({ port: 0 }, err => {
|
|
237
|
-
t.
|
|
238
|
-
|
|
244
|
+
t.assert.ifError(err)
|
|
245
|
+
testDone()
|
|
239
246
|
})
|
|
240
247
|
})
|
|
241
248
|
|
|
242
|
-
test('approximate a plugin name also when fastify-plugin has no meta data', t => {
|
|
249
|
+
test('approximate a plugin name also when fastify-plugin has no meta data', (t, testDone) => {
|
|
243
250
|
t.plan(4)
|
|
244
251
|
const fastify = Fastify()
|
|
252
|
+
t.after(() => fastify.close())
|
|
253
|
+
|
|
245
254
|
// plugin name is got from current file name
|
|
246
255
|
const pluginName = /plugin\.2\.test/
|
|
247
256
|
const pluginNameWithFunction = /plugin\.2\.test-auto-\d+ -> B/
|
|
248
257
|
|
|
249
258
|
fastify.register(fp((fastify, opts, done) => {
|
|
250
|
-
t.match(fastify.pluginName, pluginName)
|
|
259
|
+
t.assert.match(fastify.pluginName, pluginName)
|
|
251
260
|
fastify.register(fp(function B (fastify, opts, done) {
|
|
252
261
|
// function has name
|
|
253
|
-
t.match(fastify.pluginName, pluginNameWithFunction)
|
|
262
|
+
t.assert.match(fastify.pluginName, pluginNameWithFunction)
|
|
254
263
|
done()
|
|
255
264
|
}))
|
|
256
265
|
setImmediate(() => {
|
|
257
|
-
t.match(fastify.pluginName, pluginNameWithFunction)
|
|
266
|
+
t.assert.match(fastify.pluginName, pluginNameWithFunction)
|
|
258
267
|
})
|
|
259
268
|
done()
|
|
260
269
|
}))
|
|
261
270
|
|
|
262
271
|
fastify.listen({ port: 0 }, err => {
|
|
263
|
-
t.
|
|
264
|
-
|
|
272
|
+
t.assert.ifError(err)
|
|
273
|
+
testDone()
|
|
265
274
|
})
|
|
266
275
|
})
|
|
267
276
|
|
|
268
|
-
test('plugin encapsulation', t => {
|
|
277
|
+
test('plugin encapsulation', (t, testDone) => {
|
|
269
278
|
t.plan(10)
|
|
270
279
|
const fastify = Fastify()
|
|
280
|
+
t.after(() => fastify.close())
|
|
271
281
|
|
|
272
282
|
fastify.register((instance, opts, done) => {
|
|
273
283
|
instance.register(fp((i, o, n) => {
|
|
@@ -296,31 +306,39 @@ test('plugin encapsulation', t => {
|
|
|
296
306
|
})
|
|
297
307
|
|
|
298
308
|
fastify.ready(() => {
|
|
299
|
-
t.
|
|
309
|
+
t.assert.ok(!fastify.test)
|
|
300
310
|
})
|
|
301
311
|
|
|
302
312
|
fastify.listen({ port: 0 }, err => {
|
|
303
|
-
t.
|
|
304
|
-
t.
|
|
313
|
+
t.assert.ifError(err)
|
|
314
|
+
t.after(() => { fastify.close() })
|
|
315
|
+
|
|
316
|
+
const completion = waitForCb({
|
|
317
|
+
steps: 2
|
|
318
|
+
})
|
|
305
319
|
|
|
306
320
|
sget({
|
|
307
321
|
method: 'GET',
|
|
308
322
|
url: 'http://localhost:' + fastify.server.address().port + '/first'
|
|
309
323
|
}, (err, response, body) => {
|
|
310
|
-
t.
|
|
311
|
-
t.
|
|
312
|
-
t.
|
|
313
|
-
t.
|
|
324
|
+
t.assert.ifError(err)
|
|
325
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
326
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
327
|
+
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'first' })
|
|
328
|
+
completion.stepIn()
|
|
314
329
|
})
|
|
315
330
|
|
|
316
331
|
sget({
|
|
317
332
|
method: 'GET',
|
|
318
333
|
url: 'http://localhost:' + fastify.server.address().port + '/second'
|
|
319
334
|
}, (err, response, body) => {
|
|
320
|
-
t.
|
|
321
|
-
t.
|
|
322
|
-
t.
|
|
323
|
-
t.
|
|
335
|
+
t.assert.ifError(err)
|
|
336
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
337
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
338
|
+
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'second' })
|
|
339
|
+
completion.stepIn()
|
|
324
340
|
})
|
|
341
|
+
|
|
342
|
+
completion.patience.then(testDone)
|
|
325
343
|
})
|
|
326
344
|
})
|
package/test/plugin.3.test.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
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
|
const sget = require('simple-get').concat
|
|
7
6
|
const fp = require('fastify-plugin')
|
|
7
|
+
const { waitForCb } = require('./toolkit')
|
|
8
8
|
|
|
9
|
-
test('if a plugin raises an error and there is not a callback to handle it, the server must not start', t => {
|
|
9
|
+
test('if a plugin raises an error and there is not a callback to handle it, the server must not start', (t, testDone) => {
|
|
10
10
|
t.plan(2)
|
|
11
11
|
const fastify = Fastify()
|
|
12
12
|
|
|
@@ -15,14 +15,16 @@ test('if a plugin raises an error and there is not a callback to handle it, the
|
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
fastify.listen({ port: 0 }, err => {
|
|
18
|
-
t.ok(err instanceof Error)
|
|
19
|
-
t.
|
|
18
|
+
t.assert.ok(err instanceof Error)
|
|
19
|
+
t.assert.strictEqual(err.message, 'err')
|
|
20
|
+
testDone()
|
|
20
21
|
})
|
|
21
22
|
})
|
|
22
23
|
|
|
23
|
-
test('add hooks after route declaration', t => {
|
|
24
|
+
test('add hooks after route declaration', (t, testDone) => {
|
|
24
25
|
t.plan(3)
|
|
25
26
|
const fastify = Fastify()
|
|
27
|
+
t.after(() => fastify.close())
|
|
26
28
|
|
|
27
29
|
function plugin (instance, opts, done) {
|
|
28
30
|
instance.decorateRequest('check', null)
|
|
@@ -58,25 +60,25 @@ test('add hooks after route declaration', t => {
|
|
|
58
60
|
})
|
|
59
61
|
|
|
60
62
|
fastify.listen({ port: 0 }, err => {
|
|
61
|
-
t.
|
|
63
|
+
t.assert.ifError(err)
|
|
62
64
|
|
|
63
65
|
sget({
|
|
64
66
|
method: 'GET',
|
|
65
67
|
url: 'http://localhost:' + fastify.server.address().port
|
|
66
68
|
}, (err, response, body) => {
|
|
67
|
-
t.
|
|
68
|
-
t.
|
|
69
|
-
|
|
69
|
+
t.assert.ifError(err)
|
|
70
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hook1: true, hook2: true, hook3: true })
|
|
71
|
+
testDone()
|
|
70
72
|
})
|
|
71
73
|
})
|
|
72
74
|
})
|
|
73
75
|
|
|
74
|
-
test('nested plugins', t => {
|
|
76
|
+
test('nested plugins', (t, testDone) => {
|
|
75
77
|
t.plan(5)
|
|
76
78
|
|
|
77
79
|
const fastify = Fastify()
|
|
78
80
|
|
|
79
|
-
t.
|
|
81
|
+
t.after(() => fastify.close())
|
|
80
82
|
|
|
81
83
|
fastify.register(function (fastify, opts, done) {
|
|
82
84
|
fastify.register((fastify, opts, done) => {
|
|
@@ -97,32 +99,39 @@ test('nested plugins', t => {
|
|
|
97
99
|
}, { prefix: '/parent' })
|
|
98
100
|
|
|
99
101
|
fastify.listen({ port: 0 }, err => {
|
|
100
|
-
t.
|
|
102
|
+
t.assert.ifError(err)
|
|
103
|
+
|
|
104
|
+
const completion = waitForCb({
|
|
105
|
+
steps: 2
|
|
106
|
+
})
|
|
101
107
|
|
|
102
108
|
sget({
|
|
103
109
|
method: 'GET',
|
|
104
110
|
url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
|
|
105
111
|
}, (err, response, body) => {
|
|
106
|
-
t.
|
|
107
|
-
t.
|
|
112
|
+
t.assert.ifError(err)
|
|
113
|
+
t.assert.deepStrictEqual(body.toString(), 'I am child 1')
|
|
114
|
+
completion.stepIn()
|
|
108
115
|
})
|
|
109
|
-
|
|
110
116
|
sget({
|
|
111
117
|
method: 'GET',
|
|
112
118
|
url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
|
|
113
119
|
}, (err, response, body) => {
|
|
114
|
-
t.
|
|
115
|
-
t.
|
|
120
|
+
t.assert.ifError(err)
|
|
121
|
+
t.assert.deepStrictEqual(body.toString(), 'I am child 2')
|
|
122
|
+
completion.stepIn()
|
|
116
123
|
})
|
|
124
|
+
|
|
125
|
+
completion.patience.then(testDone)
|
|
117
126
|
})
|
|
118
127
|
})
|
|
119
128
|
|
|
120
|
-
test('nested plugins awaited', t => {
|
|
129
|
+
test('nested plugins awaited', (t, testDone) => {
|
|
121
130
|
t.plan(5)
|
|
122
131
|
|
|
123
132
|
const fastify = Fastify()
|
|
124
133
|
|
|
125
|
-
t.
|
|
134
|
+
t.after(() => fastify.close())
|
|
126
135
|
|
|
127
136
|
fastify.register(async function wrap (fastify, opts) {
|
|
128
137
|
await fastify.register(async function child1 (fastify, opts) {
|
|
@@ -139,27 +148,34 @@ test('nested plugins awaited', t => {
|
|
|
139
148
|
}, { prefix: '/parent' })
|
|
140
149
|
|
|
141
150
|
fastify.listen({ port: 0 }, err => {
|
|
142
|
-
t.
|
|
151
|
+
t.assert.ifError(err)
|
|
152
|
+
|
|
153
|
+
const completion = waitForCb({
|
|
154
|
+
steps: 2
|
|
155
|
+
})
|
|
143
156
|
|
|
144
157
|
sget({
|
|
145
158
|
method: 'GET',
|
|
146
159
|
url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
|
|
147
160
|
}, (err, response, body) => {
|
|
148
|
-
t.
|
|
149
|
-
t.
|
|
161
|
+
t.assert.ifError(err)
|
|
162
|
+
t.assert.deepStrictEqual(body.toString(), 'I am child 1')
|
|
163
|
+
completion.stepIn()
|
|
150
164
|
})
|
|
151
165
|
|
|
152
166
|
sget({
|
|
153
167
|
method: 'GET',
|
|
154
168
|
url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
|
|
155
169
|
}, (err, response, body) => {
|
|
156
|
-
t.
|
|
157
|
-
t.
|
|
170
|
+
t.assert.ifError(err)
|
|
171
|
+
t.assert.deepStrictEqual(body.toString(), 'I am child 2')
|
|
172
|
+
completion.stepIn()
|
|
158
173
|
})
|
|
174
|
+
completion.patience.then(testDone)
|
|
159
175
|
})
|
|
160
176
|
})
|
|
161
177
|
|
|
162
|
-
test('plugin metadata - decorators', t => {
|
|
178
|
+
test('plugin metadata - decorators', (t, testDone) => {
|
|
163
179
|
t.plan(1)
|
|
164
180
|
const fastify = Fastify()
|
|
165
181
|
|
|
@@ -179,7 +195,8 @@ test('plugin metadata - decorators', t => {
|
|
|
179
195
|
fastify.register(plugin)
|
|
180
196
|
|
|
181
197
|
fastify.ready(() => {
|
|
182
|
-
t.ok(fastify.plugin)
|
|
198
|
+
t.assert.ok(fastify.plugin)
|
|
199
|
+
testDone()
|
|
183
200
|
})
|
|
184
201
|
|
|
185
202
|
function plugin (instance, opts, done) {
|
|
@@ -188,7 +205,7 @@ test('plugin metadata - decorators', t => {
|
|
|
188
205
|
}
|
|
189
206
|
})
|
|
190
207
|
|
|
191
|
-
test('plugin metadata - decorators - should throw', t => {
|
|
208
|
+
test('plugin metadata - decorators - should throw', (t, testDone) => {
|
|
192
209
|
t.plan(1)
|
|
193
210
|
const fastify = Fastify()
|
|
194
211
|
|
|
@@ -206,7 +223,8 @@ test('plugin metadata - decorators - should throw', t => {
|
|
|
206
223
|
|
|
207
224
|
fastify.register(plugin)
|
|
208
225
|
fastify.ready((err) => {
|
|
209
|
-
t.
|
|
226
|
+
t.assert.strictEqual(err.message, "The decorator 'plugin1' is not present in Request")
|
|
227
|
+
testDone()
|
|
210
228
|
})
|
|
211
229
|
|
|
212
230
|
function plugin (instance, opts, done) {
|
|
@@ -215,7 +233,7 @@ test('plugin metadata - decorators - should throw', t => {
|
|
|
215
233
|
}
|
|
216
234
|
})
|
|
217
235
|
|
|
218
|
-
test('plugin metadata - decorators - should throw with plugin name', t => {
|
|
236
|
+
test('plugin metadata - decorators - should throw with plugin name', (t, testDone) => {
|
|
219
237
|
t.plan(1)
|
|
220
238
|
const fastify = Fastify()
|
|
221
239
|
|
|
@@ -234,7 +252,8 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
|
|
|
234
252
|
|
|
235
253
|
fastify.register(plugin)
|
|
236
254
|
fastify.ready((err) => {
|
|
237
|
-
t.
|
|
255
|
+
t.assert.strictEqual(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
|
|
256
|
+
testDone()
|
|
238
257
|
})
|
|
239
258
|
|
|
240
259
|
function plugin (instance, opts, done) {
|
|
@@ -243,7 +262,7 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
|
|
|
243
262
|
}
|
|
244
263
|
})
|
|
245
264
|
|
|
246
|
-
test('plugin metadata - dependencies', t => {
|
|
265
|
+
test('plugin metadata - dependencies', (t, testDone) => {
|
|
247
266
|
t.plan(1)
|
|
248
267
|
const fastify = Fastify()
|
|
249
268
|
|
|
@@ -261,7 +280,8 @@ test('plugin metadata - dependencies', t => {
|
|
|
261
280
|
fastify.register(plugin)
|
|
262
281
|
|
|
263
282
|
fastify.ready(() => {
|
|
264
|
-
t.
|
|
283
|
+
t.assert.ok('everything right')
|
|
284
|
+
testDone()
|
|
265
285
|
})
|
|
266
286
|
|
|
267
287
|
function dependency (instance, opts, done) {
|
|
@@ -273,7 +293,7 @@ test('plugin metadata - dependencies', t => {
|
|
|
273
293
|
}
|
|
274
294
|
})
|
|
275
295
|
|
|
276
|
-
test('plugin metadata - dependencies (nested)', t => {
|
|
296
|
+
test('plugin metadata - dependencies (nested)', (t, testDone) => {
|
|
277
297
|
t.plan(1)
|
|
278
298
|
const fastify = Fastify()
|
|
279
299
|
|
|
@@ -291,7 +311,8 @@ test('plugin metadata - dependencies (nested)', t => {
|
|
|
291
311
|
fastify.register(plugin)
|
|
292
312
|
|
|
293
313
|
fastify.ready(() => {
|
|
294
|
-
t.
|
|
314
|
+
t.assert.ok('everything right')
|
|
315
|
+
testDone()
|
|
295
316
|
})
|
|
296
317
|
|
|
297
318
|
function dependency (instance, opts, done) {
|