fastify 5.3.2 → 5.3.3
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 +22 -0
- package/docs/Guides/Ecosystem.md +7 -2
- package/docs/Guides/Serverless.md +28 -69
- package/docs/Reference/Errors.md +0 -2
- package/docs/Reference/Server.md +17 -1
- package/eslint.config.js +17 -9
- package/fastify.js +6 -2
- package/lib/decorate.js +2 -2
- package/lib/errors.js +0 -8
- package/lib/logger-factory.js +1 -1
- package/lib/logger-pino.js +2 -2
- package/lib/reply.js +2 -2
- package/lib/request.js +1 -1
- package/lib/server.js +30 -51
- package/package.json +4 -4
- package/test/close-pipelining.test.js +5 -4
- package/test/decorator.test.js +422 -341
- package/test/helper.js +107 -69
- package/test/hooks.on-listen.test.js +255 -239
- package/test/hooks.on-ready.test.js +110 -92
- package/test/inject.test.js +114 -97
- package/test/input-validation.js +63 -53
- package/test/internals/errors.test.js +1 -11
- package/test/internals/hooks.test.js +17 -0
- package/test/issue-4959.test.js +2 -2
- package/test/logger/response.test.js +19 -20
- package/test/options.error-handler.test.js +1 -1
- package/test/options.test.js +1 -1
- package/test/output-validation.test.js +49 -70
- 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/promises.test.js +36 -30
- 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/schema-feature.test.js +309 -238
- package/test/schema-validation.test.js +44 -2
- 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/types/errors.test-d.ts +0 -1
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/register.test-d.ts +1 -1
- package/test/use-semicolon-delimiter.test.js +1 -1
- package/types/errors.d.ts +0 -1
- package/test/http2/missing-http2-module.test.js +0 -17
package/test/decorator.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test, describe } = require('node:test')
|
|
5
4
|
const Fastify = require('..')
|
|
6
5
|
const fp = require('fastify-plugin')
|
|
7
6
|
const sget = require('simple-get').concat
|
|
@@ -10,87 +9,96 @@ const symbols = require('../lib/symbols.js')
|
|
|
10
9
|
test('server methods should exist', t => {
|
|
11
10
|
t.plan(2)
|
|
12
11
|
const fastify = Fastify()
|
|
13
|
-
t.ok(fastify.decorate)
|
|
14
|
-
t.ok(fastify.hasDecorator)
|
|
12
|
+
t.assert.ok(fastify.decorate)
|
|
13
|
+
t.assert.ok(fastify.hasDecorator)
|
|
15
14
|
})
|
|
16
15
|
|
|
17
|
-
test('should check if the given decoration already exist when null', t => {
|
|
16
|
+
test('should check if the given decoration already exist when null', (t, done) => {
|
|
18
17
|
t.plan(1)
|
|
19
18
|
const fastify = Fastify()
|
|
20
19
|
fastify.decorate('null', null)
|
|
21
20
|
fastify.ready(() => {
|
|
22
|
-
t.ok(fastify.hasDecorator('null'))
|
|
21
|
+
t.assert.ok(fastify.hasDecorator('null'))
|
|
22
|
+
done()
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
-
test('server methods should be encapsulated via .register', t => {
|
|
26
|
+
test('server methods should be encapsulated via .register', (t, done) => {
|
|
27
27
|
t.plan(2)
|
|
28
28
|
const fastify = Fastify()
|
|
29
29
|
|
|
30
30
|
fastify.register((instance, opts, done) => {
|
|
31
31
|
instance.decorate('test', () => {})
|
|
32
|
-
t.ok(instance.test)
|
|
32
|
+
t.assert.ok(instance.test)
|
|
33
33
|
done()
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
fastify.ready(() => {
|
|
37
|
-
t.
|
|
37
|
+
t.assert.strictEqual(fastify.test, undefined)
|
|
38
|
+
done()
|
|
38
39
|
})
|
|
39
40
|
})
|
|
40
41
|
|
|
41
|
-
test('hasServerMethod should check if the given method already exist', t => {
|
|
42
|
+
test('hasServerMethod should check if the given method already exist', (t, done) => {
|
|
42
43
|
t.plan(2)
|
|
43
44
|
const fastify = Fastify()
|
|
44
45
|
|
|
45
46
|
fastify.register((instance, opts, done) => {
|
|
46
47
|
instance.decorate('test', () => {})
|
|
47
|
-
t.ok(instance.hasDecorator('test'))
|
|
48
|
+
t.assert.ok(instance.hasDecorator('test'))
|
|
48
49
|
done()
|
|
49
50
|
})
|
|
50
51
|
|
|
51
52
|
fastify.ready(() => {
|
|
52
|
-
t.
|
|
53
|
+
t.assert.strictEqual(fastify.hasDecorator('test'), false)
|
|
54
|
+
done()
|
|
53
55
|
})
|
|
54
56
|
})
|
|
55
57
|
|
|
56
|
-
test('decorate should throw if a declared dependency is not present', t => {
|
|
58
|
+
test('decorate should throw if a declared dependency is not present', (t, done) => {
|
|
57
59
|
t.plan(3)
|
|
58
60
|
const fastify = Fastify()
|
|
59
61
|
|
|
60
62
|
fastify.register((instance, opts, done) => {
|
|
61
63
|
try {
|
|
62
64
|
instance.decorate('test', () => {}, ['dependency'])
|
|
63
|
-
t.fail()
|
|
65
|
+
t.assert.fail()
|
|
64
66
|
} catch (e) {
|
|
65
|
-
t.
|
|
66
|
-
t.
|
|
67
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
|
|
68
|
+
t.assert.strictEqual(e.message, 'The decorator is missing dependency \'dependency\'.')
|
|
67
69
|
}
|
|
68
70
|
done()
|
|
69
71
|
})
|
|
70
72
|
|
|
71
|
-
fastify.ready(() =>
|
|
73
|
+
fastify.ready(() => {
|
|
74
|
+
t.assert.ok('ready')
|
|
75
|
+
done()
|
|
76
|
+
})
|
|
72
77
|
})
|
|
73
78
|
|
|
74
|
-
test('decorate should throw if declared dependency is not array', t => {
|
|
79
|
+
test('decorate should throw if declared dependency is not array', (t, done) => {
|
|
75
80
|
t.plan(3)
|
|
76
81
|
const fastify = Fastify()
|
|
77
82
|
|
|
78
83
|
fastify.register((instance, opts, done) => {
|
|
79
84
|
try {
|
|
80
85
|
instance.decorate('test', () => {}, {})
|
|
81
|
-
t.fail()
|
|
86
|
+
t.assert.fail()
|
|
82
87
|
} catch (e) {
|
|
83
|
-
t.
|
|
84
|
-
t.
|
|
88
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_DEPENDENCY_INVALID_TYPE')
|
|
89
|
+
t.assert.strictEqual(e.message, 'The dependencies of decorator \'test\' must be of type Array.')
|
|
85
90
|
}
|
|
86
91
|
done()
|
|
87
92
|
})
|
|
88
93
|
|
|
89
|
-
fastify.ready(() =>
|
|
94
|
+
fastify.ready(() => {
|
|
95
|
+
t.assert.ok('ready')
|
|
96
|
+
done()
|
|
97
|
+
})
|
|
90
98
|
})
|
|
91
99
|
|
|
92
100
|
// issue #777
|
|
93
|
-
test('should pass error for missing request decorator', t => {
|
|
101
|
+
test('should pass error for missing request decorator', (t, done) => {
|
|
94
102
|
t.plan(2)
|
|
95
103
|
const fastify = Fastify()
|
|
96
104
|
|
|
@@ -104,12 +112,13 @@ test('should pass error for missing request decorator', t => {
|
|
|
104
112
|
fastify
|
|
105
113
|
.register(plugin)
|
|
106
114
|
.ready((err) => {
|
|
107
|
-
t.
|
|
108
|
-
t.
|
|
115
|
+
t.assert.ok(err instanceof Error)
|
|
116
|
+
t.assert.ok(err.message.includes("'foo'"))
|
|
117
|
+
done()
|
|
109
118
|
})
|
|
110
119
|
})
|
|
111
120
|
|
|
112
|
-
test('decorateReply inside register', t => {
|
|
121
|
+
test('decorateReply inside register', (t, done) => {
|
|
113
122
|
t.plan(11)
|
|
114
123
|
const fastify = Fastify()
|
|
115
124
|
|
|
@@ -117,7 +126,7 @@ test('decorateReply inside register', t => {
|
|
|
117
126
|
instance.decorateReply('test', 'test')
|
|
118
127
|
|
|
119
128
|
instance.get('/yes', (req, reply) => {
|
|
120
|
-
t.ok(reply.test, 'test exists')
|
|
129
|
+
t.assert.ok(reply.test, 'test exists')
|
|
121
130
|
reply.send({ hello: 'world' })
|
|
122
131
|
})
|
|
123
132
|
|
|
@@ -125,37 +134,47 @@ test('decorateReply inside register', t => {
|
|
|
125
134
|
})
|
|
126
135
|
|
|
127
136
|
fastify.get('/no', (req, reply) => {
|
|
128
|
-
t.
|
|
137
|
+
t.assert.ok(!reply.test)
|
|
129
138
|
reply.send({ hello: 'world' })
|
|
130
139
|
})
|
|
131
140
|
|
|
132
141
|
fastify.listen({ port: 0 }, err => {
|
|
133
|
-
t.
|
|
134
|
-
t.
|
|
142
|
+
t.assert.ifError(err)
|
|
143
|
+
t.after(() => fastify.close())
|
|
144
|
+
|
|
145
|
+
let pending = 2
|
|
146
|
+
|
|
147
|
+
function completed () {
|
|
148
|
+
if (--pending === 0) {
|
|
149
|
+
done()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
135
152
|
|
|
136
153
|
sget({
|
|
137
154
|
method: 'GET',
|
|
138
155
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
139
156
|
}, (err, response, body) => {
|
|
140
|
-
t.
|
|
141
|
-
t.
|
|
142
|
-
t.
|
|
143
|
-
t.
|
|
157
|
+
t.assert.ifError(err)
|
|
158
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
159
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
160
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
161
|
+
completed()
|
|
144
162
|
})
|
|
145
163
|
|
|
146
164
|
sget({
|
|
147
165
|
method: 'GET',
|
|
148
166
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
149
167
|
}, (err, response, body) => {
|
|
150
|
-
t.
|
|
151
|
-
t.
|
|
152
|
-
t.
|
|
153
|
-
t.
|
|
168
|
+
t.assert.ifError(err)
|
|
169
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
170
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
171
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
172
|
+
completed()
|
|
154
173
|
})
|
|
155
174
|
})
|
|
156
175
|
})
|
|
157
176
|
|
|
158
|
-
test('decorateReply as plugin (inside .after)', t => {
|
|
177
|
+
test('decorateReply as plugin (inside .after)', (t, done) => {
|
|
159
178
|
t.plan(11)
|
|
160
179
|
const fastify = Fastify()
|
|
161
180
|
|
|
@@ -165,7 +184,7 @@ test('decorateReply as plugin (inside .after)', t => {
|
|
|
165
184
|
n()
|
|
166
185
|
})).after(() => {
|
|
167
186
|
instance.get('/yes', (req, reply) => {
|
|
168
|
-
t.ok(reply.test)
|
|
187
|
+
t.assert.ok(reply.test)
|
|
169
188
|
reply.send({ hello: 'world' })
|
|
170
189
|
})
|
|
171
190
|
})
|
|
@@ -173,37 +192,47 @@ test('decorateReply as plugin (inside .after)', t => {
|
|
|
173
192
|
})
|
|
174
193
|
|
|
175
194
|
fastify.get('/no', (req, reply) => {
|
|
176
|
-
t.
|
|
195
|
+
t.assert.ok(!reply.test)
|
|
177
196
|
reply.send({ hello: 'world' })
|
|
178
197
|
})
|
|
179
198
|
|
|
180
199
|
fastify.listen({ port: 0 }, err => {
|
|
181
|
-
t.
|
|
182
|
-
t.
|
|
200
|
+
t.assert.ifError(err)
|
|
201
|
+
t.after(() => fastify.close())
|
|
202
|
+
|
|
203
|
+
let pending = 2
|
|
204
|
+
|
|
205
|
+
function completed () {
|
|
206
|
+
if (--pending === 0) {
|
|
207
|
+
done()
|
|
208
|
+
}
|
|
209
|
+
}
|
|
183
210
|
|
|
184
211
|
sget({
|
|
185
212
|
method: 'GET',
|
|
186
213
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
187
214
|
}, (err, response, body) => {
|
|
188
|
-
t.
|
|
189
|
-
t.
|
|
190
|
-
t.
|
|
191
|
-
t.
|
|
215
|
+
t.assert.ifError(err)
|
|
216
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
217
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
218
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
219
|
+
completed()
|
|
192
220
|
})
|
|
193
221
|
|
|
194
222
|
sget({
|
|
195
223
|
method: 'GET',
|
|
196
224
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
197
225
|
}, (err, response, body) => {
|
|
198
|
-
t.
|
|
199
|
-
t.
|
|
200
|
-
t.
|
|
201
|
-
t.
|
|
226
|
+
t.assert.ifError(err)
|
|
227
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
228
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
229
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
230
|
+
completed()
|
|
202
231
|
})
|
|
203
232
|
})
|
|
204
233
|
})
|
|
205
234
|
|
|
206
|
-
test('decorateReply as plugin (outside .after)', t => {
|
|
235
|
+
test('decorateReply as plugin (outside .after)', (t, done) => {
|
|
207
236
|
t.plan(11)
|
|
208
237
|
const fastify = Fastify()
|
|
209
238
|
|
|
@@ -214,44 +243,54 @@ test('decorateReply as plugin (outside .after)', t => {
|
|
|
214
243
|
}))
|
|
215
244
|
|
|
216
245
|
instance.get('/yes', (req, reply) => {
|
|
217
|
-
t.ok(reply.test)
|
|
246
|
+
t.assert.ok(reply.test)
|
|
218
247
|
reply.send({ hello: 'world' })
|
|
219
248
|
})
|
|
220
249
|
done()
|
|
221
250
|
})
|
|
222
251
|
|
|
223
252
|
fastify.get('/no', (req, reply) => {
|
|
224
|
-
t.
|
|
253
|
+
t.assert.ok(!reply.test)
|
|
225
254
|
reply.send({ hello: 'world' })
|
|
226
255
|
})
|
|
227
256
|
|
|
228
257
|
fastify.listen({ port: 0 }, err => {
|
|
229
|
-
t.
|
|
230
|
-
t.
|
|
258
|
+
t.assert.ifError(err)
|
|
259
|
+
t.after(() => fastify.close())
|
|
260
|
+
|
|
261
|
+
let pending = 2
|
|
262
|
+
|
|
263
|
+
function completed () {
|
|
264
|
+
if (--pending === 0) {
|
|
265
|
+
done()
|
|
266
|
+
}
|
|
267
|
+
}
|
|
231
268
|
|
|
232
269
|
sget({
|
|
233
270
|
method: 'GET',
|
|
234
271
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
235
272
|
}, (err, response, body) => {
|
|
236
|
-
t.
|
|
237
|
-
t.
|
|
238
|
-
t.
|
|
239
|
-
t.
|
|
273
|
+
t.assert.ifError(err)
|
|
274
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
275
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
276
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
277
|
+
completed()
|
|
240
278
|
})
|
|
241
279
|
|
|
242
280
|
sget({
|
|
243
281
|
method: 'GET',
|
|
244
282
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
245
283
|
}, (err, response, body) => {
|
|
246
|
-
t.
|
|
247
|
-
t.
|
|
248
|
-
t.
|
|
249
|
-
t.
|
|
284
|
+
t.assert.ifError(err)
|
|
285
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
286
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
287
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
288
|
+
completed()
|
|
250
289
|
})
|
|
251
290
|
})
|
|
252
291
|
})
|
|
253
292
|
|
|
254
|
-
test('decorateRequest inside register', t => {
|
|
293
|
+
test('decorateRequest inside register', (t, done) => {
|
|
255
294
|
t.plan(11)
|
|
256
295
|
const fastify = Fastify()
|
|
257
296
|
|
|
@@ -259,7 +298,7 @@ test('decorateRequest inside register', t => {
|
|
|
259
298
|
instance.decorateRequest('test', 'test')
|
|
260
299
|
|
|
261
300
|
instance.get('/yes', (req, reply) => {
|
|
262
|
-
t.ok(req.test, 'test exists')
|
|
301
|
+
t.assert.ok(req.test, 'test exists')
|
|
263
302
|
reply.send({ hello: 'world' })
|
|
264
303
|
})
|
|
265
304
|
|
|
@@ -267,37 +306,47 @@ test('decorateRequest inside register', t => {
|
|
|
267
306
|
})
|
|
268
307
|
|
|
269
308
|
fastify.get('/no', (req, reply) => {
|
|
270
|
-
t.
|
|
309
|
+
t.assert.ok(!req.test)
|
|
271
310
|
reply.send({ hello: 'world' })
|
|
272
311
|
})
|
|
273
312
|
|
|
274
313
|
fastify.listen({ port: 0 }, err => {
|
|
275
|
-
t.
|
|
276
|
-
t.
|
|
314
|
+
t.assert.ifError(err)
|
|
315
|
+
t.after(() => fastify.close())
|
|
316
|
+
|
|
317
|
+
let pending = 2
|
|
318
|
+
|
|
319
|
+
function completed () {
|
|
320
|
+
if (--pending === 0) {
|
|
321
|
+
done()
|
|
322
|
+
}
|
|
323
|
+
}
|
|
277
324
|
|
|
278
325
|
sget({
|
|
279
326
|
method: 'GET',
|
|
280
327
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
281
328
|
}, (err, response, body) => {
|
|
282
|
-
t.
|
|
283
|
-
t.
|
|
284
|
-
t.
|
|
285
|
-
t.
|
|
329
|
+
t.assert.ifError(err)
|
|
330
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
331
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
332
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
333
|
+
completed()
|
|
286
334
|
})
|
|
287
335
|
|
|
288
336
|
sget({
|
|
289
337
|
method: 'GET',
|
|
290
338
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
291
339
|
}, (err, response, body) => {
|
|
292
|
-
t.
|
|
293
|
-
t.
|
|
294
|
-
t.
|
|
295
|
-
t.
|
|
340
|
+
t.assert.ifError(err)
|
|
341
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
342
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
343
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
344
|
+
completed()
|
|
296
345
|
})
|
|
297
346
|
})
|
|
298
347
|
})
|
|
299
348
|
|
|
300
|
-
test('decorateRequest as plugin (inside .after)', t => {
|
|
349
|
+
test('decorateRequest as plugin (inside .after)', (t, done) => {
|
|
301
350
|
t.plan(11)
|
|
302
351
|
const fastify = Fastify()
|
|
303
352
|
|
|
@@ -307,7 +356,7 @@ test('decorateRequest as plugin (inside .after)', t => {
|
|
|
307
356
|
n()
|
|
308
357
|
})).after(() => {
|
|
309
358
|
instance.get('/yes', (req, reply) => {
|
|
310
|
-
t.ok(req.test)
|
|
359
|
+
t.assert.ok(req.test)
|
|
311
360
|
reply.send({ hello: 'world' })
|
|
312
361
|
})
|
|
313
362
|
})
|
|
@@ -315,37 +364,47 @@ test('decorateRequest as plugin (inside .after)', t => {
|
|
|
315
364
|
})
|
|
316
365
|
|
|
317
366
|
fastify.get('/no', (req, reply) => {
|
|
318
|
-
t.
|
|
367
|
+
t.assert.ok(!req.test)
|
|
319
368
|
reply.send({ hello: 'world' })
|
|
320
369
|
})
|
|
321
370
|
|
|
322
371
|
fastify.listen({ port: 0 }, err => {
|
|
323
|
-
t.
|
|
324
|
-
t.
|
|
372
|
+
t.assert.ifError(err)
|
|
373
|
+
t.after(() => fastify.close())
|
|
374
|
+
|
|
375
|
+
let pending = 2
|
|
376
|
+
|
|
377
|
+
function completed () {
|
|
378
|
+
if (--pending === 0) {
|
|
379
|
+
done()
|
|
380
|
+
}
|
|
381
|
+
}
|
|
325
382
|
|
|
326
383
|
sget({
|
|
327
384
|
method: 'GET',
|
|
328
385
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
329
386
|
}, (err, response, body) => {
|
|
330
|
-
t.
|
|
331
|
-
t.
|
|
332
|
-
t.
|
|
333
|
-
t.
|
|
387
|
+
t.assert.ifError(err)
|
|
388
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
389
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
390
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
391
|
+
completed()
|
|
334
392
|
})
|
|
335
393
|
|
|
336
394
|
sget({
|
|
337
395
|
method: 'GET',
|
|
338
396
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
339
397
|
}, (err, response, body) => {
|
|
340
|
-
t.
|
|
341
|
-
t.
|
|
342
|
-
t.
|
|
343
|
-
t.
|
|
398
|
+
t.assert.ifError(err)
|
|
399
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
400
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
401
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
402
|
+
completed()
|
|
344
403
|
})
|
|
345
404
|
})
|
|
346
405
|
})
|
|
347
406
|
|
|
348
|
-
test('decorateRequest as plugin (outside .after)', t => {
|
|
407
|
+
test('decorateRequest as plugin (outside .after)', (t, done) => {
|
|
349
408
|
t.plan(11)
|
|
350
409
|
const fastify = Fastify()
|
|
351
410
|
|
|
@@ -356,44 +415,54 @@ test('decorateRequest as plugin (outside .after)', t => {
|
|
|
356
415
|
}))
|
|
357
416
|
|
|
358
417
|
instance.get('/yes', (req, reply) => {
|
|
359
|
-
t.ok(req.test)
|
|
418
|
+
t.assert.ok(req.test)
|
|
360
419
|
reply.send({ hello: 'world' })
|
|
361
420
|
})
|
|
362
421
|
done()
|
|
363
422
|
})
|
|
364
423
|
|
|
365
424
|
fastify.get('/no', (req, reply) => {
|
|
366
|
-
t.
|
|
425
|
+
t.assert.ok(!req.test)
|
|
367
426
|
reply.send({ hello: 'world' })
|
|
368
427
|
})
|
|
369
428
|
|
|
370
429
|
fastify.listen({ port: 0 }, err => {
|
|
371
|
-
t.
|
|
372
|
-
t.
|
|
430
|
+
t.assert.ifError(err)
|
|
431
|
+
t.after(() => fastify.close())
|
|
432
|
+
|
|
433
|
+
let pending = 2
|
|
434
|
+
|
|
435
|
+
function completed () {
|
|
436
|
+
if (--pending === 0) {
|
|
437
|
+
done()
|
|
438
|
+
}
|
|
439
|
+
}
|
|
373
440
|
|
|
374
441
|
sget({
|
|
375
442
|
method: 'GET',
|
|
376
443
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
377
444
|
}, (err, response, body) => {
|
|
378
|
-
t.
|
|
379
|
-
t.
|
|
380
|
-
t.
|
|
381
|
-
t.
|
|
445
|
+
t.assert.ifError(err)
|
|
446
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
447
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
448
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
449
|
+
completed()
|
|
382
450
|
})
|
|
383
451
|
|
|
384
452
|
sget({
|
|
385
453
|
method: 'GET',
|
|
386
454
|
url: 'http://localhost:' + fastify.server.address().port + '/no'
|
|
387
455
|
}, (err, response, body) => {
|
|
388
|
-
t.
|
|
389
|
-
t.
|
|
390
|
-
t.
|
|
391
|
-
t.
|
|
456
|
+
t.assert.ifError(err)
|
|
457
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
458
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
459
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
460
|
+
completed()
|
|
392
461
|
})
|
|
393
462
|
})
|
|
394
463
|
})
|
|
395
464
|
|
|
396
|
-
test('decorators should be instance separated', t => {
|
|
465
|
+
test('decorators should be instance separated', (t, done) => {
|
|
397
466
|
t.plan(1)
|
|
398
467
|
|
|
399
468
|
const fastify1 = Fastify()
|
|
@@ -408,140 +477,119 @@ test('decorators should be instance separated', t => {
|
|
|
408
477
|
fastify1.decorateReply('test', 'foo')
|
|
409
478
|
fastify2.decorateReply('test', 'foo')
|
|
410
479
|
|
|
411
|
-
t.
|
|
480
|
+
t.assert.ok('Done')
|
|
481
|
+
done()
|
|
412
482
|
})
|
|
413
483
|
|
|
414
|
-
|
|
484
|
+
describe('hasRequestDecorator', () => {
|
|
415
485
|
const requestDecoratorName = 'my-decorator-name'
|
|
416
486
|
|
|
417
|
-
|
|
418
|
-
t.plan(1)
|
|
487
|
+
test('is a function', async t => {
|
|
419
488
|
const fastify = Fastify()
|
|
420
|
-
t.ok(fastify.hasRequestDecorator)
|
|
489
|
+
t.assert.ok(fastify.hasRequestDecorator)
|
|
421
490
|
})
|
|
422
491
|
|
|
423
|
-
|
|
424
|
-
t.plan(2)
|
|
492
|
+
test('should check if the given request decoration already exist', async t => {
|
|
425
493
|
const fastify = Fastify()
|
|
426
494
|
|
|
427
|
-
t.
|
|
495
|
+
t.assert.ok(!fastify.hasRequestDecorator(requestDecoratorName))
|
|
428
496
|
fastify.decorateRequest(requestDecoratorName, 42)
|
|
429
|
-
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
497
|
+
t.assert.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
430
498
|
})
|
|
431
499
|
|
|
432
|
-
|
|
433
|
-
t.plan(2)
|
|
500
|
+
test('should check if the given request decoration already exist when null', async t => {
|
|
434
501
|
const fastify = Fastify()
|
|
435
502
|
|
|
436
|
-
t.
|
|
503
|
+
t.assert.ok(!fastify.hasRequestDecorator(requestDecoratorName))
|
|
437
504
|
fastify.decorateRequest(requestDecoratorName, null)
|
|
438
|
-
t.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
505
|
+
t.assert.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
439
506
|
})
|
|
440
507
|
|
|
441
|
-
|
|
442
|
-
t.plan(4)
|
|
508
|
+
test('should be plugin encapsulable', async t => {
|
|
443
509
|
const fastify = Fastify()
|
|
444
510
|
|
|
445
|
-
t.
|
|
511
|
+
t.assert.ok(!fastify.hasRequestDecorator(requestDecoratorName))
|
|
446
512
|
|
|
447
|
-
fastify.register(function (fastify2, opts
|
|
513
|
+
await fastify.register(async function (fastify2, opts) {
|
|
448
514
|
fastify2.decorateRequest(requestDecoratorName, 42)
|
|
449
|
-
t.ok(fastify2.hasRequestDecorator(requestDecoratorName))
|
|
450
|
-
done()
|
|
515
|
+
t.assert.ok(fastify2.hasRequestDecorator(requestDecoratorName))
|
|
451
516
|
})
|
|
452
517
|
|
|
453
|
-
t.
|
|
518
|
+
t.assert.ok(!fastify.hasRequestDecorator(requestDecoratorName))
|
|
454
519
|
|
|
455
|
-
fastify.ready(
|
|
456
|
-
|
|
457
|
-
})
|
|
520
|
+
await fastify.ready()
|
|
521
|
+
t.assert.ok(!fastify.hasRequestDecorator(requestDecoratorName))
|
|
458
522
|
})
|
|
459
523
|
|
|
460
|
-
|
|
461
|
-
t.plan(2)
|
|
524
|
+
test('should be inherited', async t => {
|
|
462
525
|
const fastify = Fastify()
|
|
463
526
|
|
|
464
527
|
fastify.decorateRequest(requestDecoratorName, 42)
|
|
465
528
|
|
|
466
|
-
fastify.register(function (fastify2, opts
|
|
467
|
-
t.ok(fastify2.hasRequestDecorator(requestDecoratorName))
|
|
468
|
-
done()
|
|
529
|
+
await fastify.register(async function (fastify2, opts) {
|
|
530
|
+
t.assert.ok(fastify2.hasRequestDecorator(requestDecoratorName))
|
|
469
531
|
})
|
|
470
532
|
|
|
471
|
-
fastify.ready(
|
|
472
|
-
|
|
473
|
-
})
|
|
533
|
+
await fastify.ready()
|
|
534
|
+
t.assert.ok(fastify.hasRequestDecorator(requestDecoratorName))
|
|
474
535
|
})
|
|
475
|
-
|
|
476
|
-
t.end()
|
|
477
536
|
})
|
|
478
537
|
|
|
479
|
-
|
|
538
|
+
describe('hasReplyDecorator', () => {
|
|
480
539
|
const replyDecoratorName = 'my-decorator-name'
|
|
481
540
|
|
|
482
|
-
|
|
483
|
-
t.plan(1)
|
|
541
|
+
test('is a function', async t => {
|
|
484
542
|
const fastify = Fastify()
|
|
485
|
-
t.ok(fastify.hasReplyDecorator)
|
|
543
|
+
t.assert.ok(fastify.hasReplyDecorator)
|
|
486
544
|
})
|
|
487
545
|
|
|
488
|
-
|
|
489
|
-
t.plan(2)
|
|
546
|
+
test('should check if the given reply decoration already exist', async t => {
|
|
490
547
|
const fastify = Fastify()
|
|
491
548
|
|
|
492
|
-
t.
|
|
549
|
+
t.assert.ok(!fastify.hasReplyDecorator(replyDecoratorName))
|
|
493
550
|
fastify.decorateReply(replyDecoratorName, 42)
|
|
494
|
-
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
551
|
+
t.assert.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
495
552
|
})
|
|
496
553
|
|
|
497
|
-
|
|
498
|
-
t.plan(2)
|
|
554
|
+
test('should check if the given reply decoration already exist when null', async t => {
|
|
499
555
|
const fastify = Fastify()
|
|
500
556
|
|
|
501
|
-
t.
|
|
557
|
+
t.assert.ok(!fastify.hasReplyDecorator(replyDecoratorName))
|
|
502
558
|
fastify.decorateReply(replyDecoratorName, null)
|
|
503
|
-
t.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
559
|
+
t.assert.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
504
560
|
})
|
|
505
561
|
|
|
506
|
-
|
|
507
|
-
t.plan(4)
|
|
562
|
+
test('should be plugin encapsulable', async t => {
|
|
508
563
|
const fastify = Fastify()
|
|
509
564
|
|
|
510
|
-
t.
|
|
565
|
+
t.assert.ok(!fastify.hasReplyDecorator(replyDecoratorName))
|
|
511
566
|
|
|
512
|
-
fastify.register(function (fastify2, opts
|
|
567
|
+
await fastify.register(async function (fastify2, opts) {
|
|
513
568
|
fastify2.decorateReply(replyDecoratorName, 42)
|
|
514
|
-
t.ok(fastify2.hasReplyDecorator(replyDecoratorName))
|
|
515
|
-
done()
|
|
569
|
+
t.assert.ok(fastify2.hasReplyDecorator(replyDecoratorName))
|
|
516
570
|
})
|
|
517
571
|
|
|
518
|
-
t.
|
|
572
|
+
t.assert.ok(!fastify.hasReplyDecorator(replyDecoratorName))
|
|
519
573
|
|
|
520
|
-
fastify.ready(
|
|
521
|
-
|
|
522
|
-
})
|
|
574
|
+
await fastify.ready()
|
|
575
|
+
t.assert.ok(!fastify.hasReplyDecorator(replyDecoratorName))
|
|
523
576
|
})
|
|
524
577
|
|
|
525
|
-
|
|
526
|
-
t.plan(2)
|
|
578
|
+
test('should be inherited', async t => {
|
|
527
579
|
const fastify = Fastify()
|
|
528
580
|
|
|
529
581
|
fastify.decorateReply(replyDecoratorName, 42)
|
|
530
582
|
|
|
531
|
-
fastify.register(function (fastify2, opts
|
|
532
|
-
t.ok(fastify2.hasReplyDecorator(replyDecoratorName))
|
|
533
|
-
done()
|
|
583
|
+
await fastify.register(async function (fastify2, opts) {
|
|
584
|
+
t.assert.ok(fastify2.hasReplyDecorator(replyDecoratorName))
|
|
534
585
|
})
|
|
535
586
|
|
|
536
|
-
fastify.ready(
|
|
537
|
-
|
|
538
|
-
})
|
|
587
|
+
await fastify.ready()
|
|
588
|
+
t.assert.ok(fastify.hasReplyDecorator(replyDecoratorName))
|
|
539
589
|
})
|
|
540
|
-
|
|
541
|
-
t.end()
|
|
542
590
|
})
|
|
543
591
|
|
|
544
|
-
test('should register properties via getter/setter objects', t => {
|
|
592
|
+
test('should register properties via getter/setter objects', (t, done) => {
|
|
545
593
|
t.plan(3)
|
|
546
594
|
const fastify = Fastify()
|
|
547
595
|
|
|
@@ -551,17 +599,18 @@ test('should register properties via getter/setter objects', t => {
|
|
|
551
599
|
return 'a getter'
|
|
552
600
|
}
|
|
553
601
|
})
|
|
554
|
-
t.ok(instance.test)
|
|
555
|
-
t.ok(instance.test, 'a getter')
|
|
602
|
+
t.assert.ok(instance.test)
|
|
603
|
+
t.assert.ok(instance.test, 'a getter')
|
|
556
604
|
done()
|
|
557
605
|
})
|
|
558
606
|
|
|
559
607
|
fastify.ready(() => {
|
|
560
|
-
t.
|
|
608
|
+
t.assert.ok(!fastify.test)
|
|
609
|
+
done()
|
|
561
610
|
})
|
|
562
611
|
})
|
|
563
612
|
|
|
564
|
-
test('decorateRequest should work with getter/setter', t => {
|
|
613
|
+
test('decorateRequest should work with getter/setter', (t, done) => {
|
|
565
614
|
t.plan(5)
|
|
566
615
|
const fastify = Fastify()
|
|
567
616
|
|
|
@@ -580,24 +629,34 @@ test('decorateRequest should work with getter/setter', t => {
|
|
|
580
629
|
})
|
|
581
630
|
|
|
582
631
|
fastify.get('/not-decorated', (req, res) => {
|
|
583
|
-
t.
|
|
632
|
+
t.assert.ok(!req.test)
|
|
584
633
|
res.send()
|
|
585
634
|
})
|
|
586
635
|
|
|
636
|
+
let pending = 2
|
|
637
|
+
|
|
638
|
+
function completed () {
|
|
639
|
+
if (--pending === 0) {
|
|
640
|
+
done()
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
587
644
|
fastify.ready(() => {
|
|
588
645
|
fastify.inject({ url: '/req-decorated-get-set' }, (err, res) => {
|
|
589
|
-
t.
|
|
590
|
-
t.
|
|
646
|
+
t.assert.ifError(err)
|
|
647
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { test: 'a getter' })
|
|
648
|
+
completed()
|
|
591
649
|
})
|
|
592
650
|
|
|
593
651
|
fastify.inject({ url: '/not-decorated' }, (err, res) => {
|
|
594
|
-
t.
|
|
595
|
-
t.
|
|
652
|
+
t.assert.ifError(err)
|
|
653
|
+
t.assert.ok('ok', 'not decorated')
|
|
654
|
+
completed()
|
|
596
655
|
})
|
|
597
656
|
})
|
|
598
657
|
})
|
|
599
658
|
|
|
600
|
-
test('decorateReply should work with getter/setter', t => {
|
|
659
|
+
test('decorateReply should work with getter/setter', (t, done) => {
|
|
601
660
|
t.plan(5)
|
|
602
661
|
const fastify = Fastify()
|
|
603
662
|
|
|
@@ -616,39 +675,49 @@ test('decorateReply should work with getter/setter', t => {
|
|
|
616
675
|
})
|
|
617
676
|
|
|
618
677
|
fastify.get('/not-decorated', (req, res) => {
|
|
619
|
-
t.
|
|
678
|
+
t.assert.ok(!res.test)
|
|
620
679
|
res.send()
|
|
621
680
|
})
|
|
622
681
|
|
|
682
|
+
let pending = 2
|
|
683
|
+
|
|
684
|
+
function completed () {
|
|
685
|
+
if (--pending === 0) {
|
|
686
|
+
done()
|
|
687
|
+
}
|
|
688
|
+
}
|
|
623
689
|
fastify.ready(() => {
|
|
624
690
|
fastify.inject({ url: '/res-decorated-get-set' }, (err, res) => {
|
|
625
|
-
t.
|
|
626
|
-
t.
|
|
691
|
+
t.assert.ifError(err)
|
|
692
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { test: 'a getter' })
|
|
693
|
+
completed()
|
|
627
694
|
})
|
|
628
695
|
|
|
629
696
|
fastify.inject({ url: '/not-decorated' }, (err, res) => {
|
|
630
|
-
t.
|
|
631
|
-
t.
|
|
697
|
+
t.assert.ifError(err)
|
|
698
|
+
t.assert.ok('ok')
|
|
699
|
+
completed()
|
|
632
700
|
})
|
|
633
701
|
})
|
|
634
702
|
})
|
|
635
703
|
|
|
636
|
-
test('should register empty values', t => {
|
|
704
|
+
test('should register empty values', (t, done) => {
|
|
637
705
|
t.plan(2)
|
|
638
706
|
const fastify = Fastify()
|
|
639
707
|
|
|
640
708
|
fastify.register((instance, opts, done) => {
|
|
641
709
|
instance.decorate('test', null)
|
|
642
|
-
t.ok(Object.hasOwn(instance, 'test'))
|
|
710
|
+
t.assert.ok(Object.hasOwn(instance, 'test'))
|
|
643
711
|
done()
|
|
644
712
|
})
|
|
645
713
|
|
|
646
714
|
fastify.ready(() => {
|
|
647
|
-
t.
|
|
715
|
+
t.assert.ok(!fastify.test)
|
|
716
|
+
done()
|
|
648
717
|
})
|
|
649
718
|
})
|
|
650
719
|
|
|
651
|
-
test('nested plugins can override things', t => {
|
|
720
|
+
test('nested plugins can override things', (t, done) => {
|
|
652
721
|
t.plan(6)
|
|
653
722
|
const fastify = Fastify()
|
|
654
723
|
|
|
@@ -663,20 +732,21 @@ test('nested plugins can override things', t => {
|
|
|
663
732
|
instance.decorateRequest('test', func)
|
|
664
733
|
instance.decorateReply('test', func)
|
|
665
734
|
|
|
666
|
-
t.
|
|
667
|
-
t.
|
|
668
|
-
t.
|
|
735
|
+
t.assert.strictEqual(instance.test, func)
|
|
736
|
+
t.assert.strictEqual(instance[symbols.kRequest].prototype.test, func)
|
|
737
|
+
t.assert.strictEqual(instance[symbols.kReply].prototype.test, func)
|
|
669
738
|
done()
|
|
670
739
|
})
|
|
671
740
|
|
|
672
741
|
fastify.ready(() => {
|
|
673
|
-
t.
|
|
674
|
-
t.
|
|
675
|
-
t.
|
|
742
|
+
t.assert.strictEqual(fastify.test, rootFunc)
|
|
743
|
+
t.assert.strictEqual(fastify[symbols.kRequest].prototype.test, rootFunc)
|
|
744
|
+
t.assert.strictEqual(fastify[symbols.kReply].prototype.test, rootFunc)
|
|
745
|
+
done()
|
|
676
746
|
})
|
|
677
747
|
})
|
|
678
748
|
|
|
679
|
-
test('a decorator should addSchema to all the encapsulated tree', t => {
|
|
749
|
+
test('a decorator should addSchema to all the encapsulated tree', (t, done) => {
|
|
680
750
|
t.plan(1)
|
|
681
751
|
const fastify = Fastify()
|
|
682
752
|
|
|
@@ -700,10 +770,13 @@ test('a decorator should addSchema to all the encapsulated tree', t => {
|
|
|
700
770
|
done()
|
|
701
771
|
})
|
|
702
772
|
|
|
703
|
-
fastify.ready(
|
|
773
|
+
fastify.ready(() => {
|
|
774
|
+
t.assert.ifError()
|
|
775
|
+
done()
|
|
776
|
+
})
|
|
704
777
|
})
|
|
705
778
|
|
|
706
|
-
test('after can access to a decorated instance and previous plugin decoration', t => {
|
|
779
|
+
test('after can access to a decorated instance and previous plugin decoration', (t, done) => {
|
|
707
780
|
t.plan(11)
|
|
708
781
|
const TEST_VALUE = {}
|
|
709
782
|
const OTHER_TEST_VALUE = {}
|
|
@@ -716,38 +789,39 @@ test('after can access to a decorated instance and previous plugin decoration',
|
|
|
716
789
|
|
|
717
790
|
done()
|
|
718
791
|
})).after(function (err, instance, done) {
|
|
719
|
-
t.
|
|
720
|
-
t.
|
|
792
|
+
t.assert.ifError(err)
|
|
793
|
+
t.assert.strictEqual(instance.test, TEST_VALUE)
|
|
721
794
|
|
|
722
795
|
instance.decorate('test2', OTHER_TEST_VALUE)
|
|
723
796
|
done()
|
|
724
797
|
})
|
|
725
798
|
|
|
726
799
|
fastify.register(fp(function (instance, options, done) {
|
|
727
|
-
t.
|
|
728
|
-
t.
|
|
800
|
+
t.assert.strictEqual(instance.test, TEST_VALUE)
|
|
801
|
+
t.assert.strictEqual(instance.test2, OTHER_TEST_VALUE)
|
|
729
802
|
|
|
730
803
|
instance.decorate('test3', NEW_TEST_VALUE)
|
|
731
804
|
|
|
732
805
|
done()
|
|
733
806
|
})).after(function (err, instance, done) {
|
|
734
|
-
t.
|
|
735
|
-
t.
|
|
736
|
-
t.
|
|
737
|
-
t.
|
|
807
|
+
t.assert.ifError(err)
|
|
808
|
+
t.assert.strictEqual(instance.test, TEST_VALUE)
|
|
809
|
+
t.assert.strictEqual(instance.test2, OTHER_TEST_VALUE)
|
|
810
|
+
t.assert.strictEqual(instance.test3, NEW_TEST_VALUE)
|
|
738
811
|
|
|
739
812
|
done()
|
|
740
813
|
})
|
|
741
814
|
|
|
742
815
|
fastify.get('/', function (req, res) {
|
|
743
|
-
t.
|
|
744
|
-
t.
|
|
816
|
+
t.assert.strictEqual(this.test, TEST_VALUE)
|
|
817
|
+
t.assert.strictEqual(this.test2, OTHER_TEST_VALUE)
|
|
745
818
|
res.send({})
|
|
746
819
|
})
|
|
747
820
|
|
|
748
821
|
fastify.inject('/')
|
|
749
822
|
.then(response => {
|
|
750
|
-
t.
|
|
823
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
824
|
+
done()
|
|
751
825
|
})
|
|
752
826
|
})
|
|
753
827
|
|
|
@@ -764,24 +838,24 @@ test('decorate* should throw if called after ready', async t => {
|
|
|
764
838
|
await fastify.listen({ port: 0 })
|
|
765
839
|
try {
|
|
766
840
|
fastify.decorate('test', true)
|
|
767
|
-
t.fail('should not decorate')
|
|
841
|
+
t.assert.fail('should not decorate')
|
|
768
842
|
} catch (err) {
|
|
769
|
-
t.
|
|
770
|
-
t.
|
|
843
|
+
t.assert.strictEqual(err.code, 'FST_ERR_DEC_AFTER_START')
|
|
844
|
+
t.assert.strictEqual(err.message, "The decorator 'test' has been added after start!")
|
|
771
845
|
}
|
|
772
846
|
try {
|
|
773
847
|
fastify.decorateRequest('test', true)
|
|
774
|
-
t.fail('should not decorate')
|
|
848
|
+
t.assert.fail('should not decorate')
|
|
775
849
|
} catch (e) {
|
|
776
|
-
t.
|
|
777
|
-
t.
|
|
850
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_AFTER_START')
|
|
851
|
+
t.assert.strictEqual(e.message, "The decorator 'test' has been added after start!")
|
|
778
852
|
}
|
|
779
853
|
try {
|
|
780
854
|
fastify.decorateReply('test', true)
|
|
781
|
-
t.fail('should not decorate')
|
|
855
|
+
t.assert.fail('should not decorate')
|
|
782
856
|
} catch (e) {
|
|
783
|
-
t.
|
|
784
|
-
t.
|
|
857
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_AFTER_START')
|
|
858
|
+
t.assert.strictEqual(e.message, "The decorator 'test' has been added after start!")
|
|
785
859
|
}
|
|
786
860
|
await fastify.close()
|
|
787
861
|
})
|
|
@@ -792,10 +866,10 @@ test('decorate* should emit error if an array is passed', t => {
|
|
|
792
866
|
const fastify = Fastify()
|
|
793
867
|
try {
|
|
794
868
|
fastify.decorateRequest('test_array', [])
|
|
795
|
-
t.fail('should not decorate')
|
|
869
|
+
t.assert.fail('should not decorate')
|
|
796
870
|
} catch (err) {
|
|
797
|
-
t.
|
|
798
|
-
t.
|
|
871
|
+
t.assert.strictEqual(err.code, 'FST_ERR_DEC_REFERENCE_TYPE')
|
|
872
|
+
t.assert.strictEqual(err.message, "The decorator 'test_array' of type 'object' is a reference type. Use the { getter, setter } interface instead.")
|
|
799
873
|
}
|
|
800
874
|
})
|
|
801
875
|
|
|
@@ -806,7 +880,7 @@ test('server.decorate should not emit error if reference type is passed', async
|
|
|
806
880
|
fastify.decorate('test_array', [])
|
|
807
881
|
fastify.decorate('test_object', {})
|
|
808
882
|
await fastify.ready()
|
|
809
|
-
t.
|
|
883
|
+
t.assert.ok('Done')
|
|
810
884
|
})
|
|
811
885
|
|
|
812
886
|
test('decorate* should emit warning if object type is passed', t => {
|
|
@@ -815,10 +889,10 @@ test('decorate* should emit warning if object type is passed', t => {
|
|
|
815
889
|
const fastify = Fastify()
|
|
816
890
|
try {
|
|
817
891
|
fastify.decorateRequest('test_object', { foo: 'bar' })
|
|
818
|
-
t.fail('should not decorate')
|
|
892
|
+
t.assert.fail('should not decorate')
|
|
819
893
|
} catch (err) {
|
|
820
|
-
t.
|
|
821
|
-
t.
|
|
894
|
+
t.assert.strictEqual(err.code, 'FST_ERR_DEC_REFERENCE_TYPE')
|
|
895
|
+
t.assert.strictEqual(err.message, "The decorator 'test_object' of type 'object' is a reference type. Use the { getter, setter } interface instead.")
|
|
822
896
|
}
|
|
823
897
|
})
|
|
824
898
|
|
|
@@ -833,7 +907,7 @@ test('decorate* should not emit warning if object with getter/setter is passed',
|
|
|
833
907
|
return 'a getter'
|
|
834
908
|
}
|
|
835
909
|
})
|
|
836
|
-
t.
|
|
910
|
+
t.assert.ok('Done')
|
|
837
911
|
})
|
|
838
912
|
|
|
839
913
|
test('decorateRequest with getter/setter can handle encapsulation', async t => {
|
|
@@ -850,22 +924,22 @@ test('decorateRequest with getter/setter can handle encapsulation', async t => {
|
|
|
850
924
|
})
|
|
851
925
|
|
|
852
926
|
fastify.get('/', async function (req, reply) {
|
|
853
|
-
t.
|
|
927
|
+
t.assert.deepStrictEqual(req.test_getter_setter, {}, 'a getter')
|
|
854
928
|
req.test_getter_setter.a = req.id
|
|
855
|
-
t.
|
|
929
|
+
t.assert.deepStrictEqual(req.test_getter_setter, { a: req.id })
|
|
856
930
|
})
|
|
857
931
|
|
|
858
932
|
fastify.addHook('onResponse', async function hook (req, reply) {
|
|
859
|
-
t.
|
|
933
|
+
t.assert.deepStrictEqual(req.test_getter_setter, { a: req.id })
|
|
860
934
|
})
|
|
861
935
|
|
|
862
936
|
await Promise.all([
|
|
863
|
-
fastify.inject('/').then(res => t.
|
|
864
|
-
fastify.inject('/').then(res => t.
|
|
865
|
-
fastify.inject('/').then(res => t.
|
|
866
|
-
fastify.inject('/').then(res => t.
|
|
867
|
-
fastify.inject('/').then(res => t.
|
|
868
|
-
fastify.inject('/').then(res => t.
|
|
937
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
938
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
939
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
940
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
941
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
942
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200))
|
|
869
943
|
])
|
|
870
944
|
})
|
|
871
945
|
|
|
@@ -883,22 +957,22 @@ test('decorateRequest with getter/setter can handle encapsulation with arrays',
|
|
|
883
957
|
})
|
|
884
958
|
|
|
885
959
|
fastify.get('/', async function (req, reply) {
|
|
886
|
-
t.
|
|
960
|
+
t.assert.deepStrictEqual(req.my_array, [])
|
|
887
961
|
req.my_array.push(req.id)
|
|
888
|
-
t.
|
|
962
|
+
t.assert.deepStrictEqual(req.my_array, [req.id])
|
|
889
963
|
})
|
|
890
964
|
|
|
891
965
|
fastify.addHook('onResponse', async function hook (req, reply) {
|
|
892
|
-
t.
|
|
966
|
+
t.assert.deepStrictEqual(req.my_array, [req.id])
|
|
893
967
|
})
|
|
894
968
|
|
|
895
969
|
await Promise.all([
|
|
896
|
-
fastify.inject('/').then(res => t.
|
|
897
|
-
fastify.inject('/').then(res => t.
|
|
898
|
-
fastify.inject('/').then(res => t.
|
|
899
|
-
fastify.inject('/').then(res => t.
|
|
900
|
-
fastify.inject('/').then(res => t.
|
|
901
|
-
fastify.inject('/').then(res => t.
|
|
970
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
971
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
972
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
973
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
974
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200)),
|
|
975
|
+
fastify.inject('/').then(res => t.assert.strictEqual(res.statusCode, 200))
|
|
902
976
|
])
|
|
903
977
|
})
|
|
904
978
|
|
|
@@ -915,7 +989,7 @@ test('decorate* should not emit error if string,bool,numbers are passed', t => {
|
|
|
915
989
|
fastify.decorateReply('test_number', 42)
|
|
916
990
|
fastify.decorateReply('test_null', null)
|
|
917
991
|
fastify.decorateReply('test_undefined', undefined)
|
|
918
|
-
t.
|
|
992
|
+
t.assert.ok('Done')
|
|
919
993
|
})
|
|
920
994
|
|
|
921
995
|
test('Request/reply decorators should be able to access the server instance', async t => {
|
|
@@ -948,12 +1022,12 @@ test('Request/reply decorators should be able to access the server instance', as
|
|
|
948
1022
|
|
|
949
1023
|
// ----
|
|
950
1024
|
function rootAssert () {
|
|
951
|
-
t.
|
|
1025
|
+
t.assert.strictEqual(this.server, server)
|
|
952
1026
|
}
|
|
953
1027
|
|
|
954
1028
|
function nestedAssert () {
|
|
955
|
-
t.
|
|
956
|
-
t.
|
|
1029
|
+
t.assert.notStrictEqual(this.server, server)
|
|
1030
|
+
t.assert.strictEqual(this.server.foo, 'bar')
|
|
957
1031
|
}
|
|
958
1032
|
})
|
|
959
1033
|
|
|
@@ -990,94 +1064,97 @@ test('plugin required decorators', async t => {
|
|
|
990
1064
|
await app.ready()
|
|
991
1065
|
})
|
|
992
1066
|
|
|
993
|
-
test('decorateRequest/decorateReply empty string', t => {
|
|
1067
|
+
test('decorateRequest/decorateReply empty string', (t, done) => {
|
|
994
1068
|
t.plan(7)
|
|
995
1069
|
const fastify = Fastify()
|
|
996
1070
|
|
|
997
1071
|
fastify.decorateRequest('test', '')
|
|
998
1072
|
fastify.decorateReply('test2', '')
|
|
999
1073
|
fastify.get('/yes', (req, reply) => {
|
|
1000
|
-
t.
|
|
1001
|
-
t.
|
|
1074
|
+
t.assert.strictEqual(req.test, '')
|
|
1075
|
+
t.assert.strictEqual(reply.test2, '')
|
|
1002
1076
|
reply.send({ hello: 'world' })
|
|
1003
1077
|
})
|
|
1004
|
-
t.
|
|
1078
|
+
t.after(() => fastify.close())
|
|
1005
1079
|
|
|
1006
1080
|
fastify.listen({ port: 0 }, err => {
|
|
1007
|
-
t.
|
|
1008
|
-
t.
|
|
1081
|
+
t.assert.ifError(err)
|
|
1082
|
+
t.after(() => fastify.close())
|
|
1009
1083
|
|
|
1010
1084
|
sget({
|
|
1011
1085
|
method: 'GET',
|
|
1012
1086
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
1013
1087
|
}, (err, response, body) => {
|
|
1014
|
-
t.
|
|
1015
|
-
t.
|
|
1016
|
-
t.
|
|
1017
|
-
t.
|
|
1088
|
+
t.assert.ifError(err)
|
|
1089
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1090
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1091
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1092
|
+
done()
|
|
1018
1093
|
})
|
|
1019
1094
|
})
|
|
1020
1095
|
})
|
|
1021
1096
|
|
|
1022
|
-
test('decorateRequest/decorateReply is undefined', t => {
|
|
1097
|
+
test('decorateRequest/decorateReply is undefined', (t, done) => {
|
|
1023
1098
|
t.plan(7)
|
|
1024
1099
|
const fastify = Fastify()
|
|
1025
1100
|
|
|
1026
1101
|
fastify.decorateRequest('test', undefined)
|
|
1027
1102
|
fastify.decorateReply('test2', undefined)
|
|
1028
1103
|
fastify.get('/yes', (req, reply) => {
|
|
1029
|
-
t.
|
|
1030
|
-
t.
|
|
1104
|
+
t.assert.strictEqual(req.test, undefined)
|
|
1105
|
+
t.assert.strictEqual(reply.test2, undefined)
|
|
1031
1106
|
reply.send({ hello: 'world' })
|
|
1032
1107
|
})
|
|
1033
|
-
t.
|
|
1108
|
+
t.after(() => fastify.close())
|
|
1034
1109
|
|
|
1035
1110
|
fastify.listen({ port: 0 }, err => {
|
|
1036
|
-
t.
|
|
1037
|
-
t.
|
|
1111
|
+
t.assert.ifError(err)
|
|
1112
|
+
t.after(() => fastify.close())
|
|
1038
1113
|
|
|
1039
1114
|
sget({
|
|
1040
1115
|
method: 'GET',
|
|
1041
1116
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
1042
1117
|
}, (err, response, body) => {
|
|
1043
|
-
t.
|
|
1044
|
-
t.
|
|
1045
|
-
t.
|
|
1046
|
-
t.
|
|
1118
|
+
t.assert.ifError(err)
|
|
1119
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1120
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1121
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1122
|
+
done()
|
|
1047
1123
|
})
|
|
1048
1124
|
})
|
|
1049
1125
|
})
|
|
1050
1126
|
|
|
1051
|
-
test('decorateRequest/decorateReply is not set to a value', t => {
|
|
1127
|
+
test('decorateRequest/decorateReply is not set to a value', (t, done) => {
|
|
1052
1128
|
t.plan(7)
|
|
1053
1129
|
const fastify = Fastify()
|
|
1054
1130
|
|
|
1055
1131
|
fastify.decorateRequest('test')
|
|
1056
1132
|
fastify.decorateReply('test2')
|
|
1057
1133
|
fastify.get('/yes', (req, reply) => {
|
|
1058
|
-
t.
|
|
1059
|
-
t.
|
|
1134
|
+
t.assert.strictEqual(req.test, undefined)
|
|
1135
|
+
t.assert.strictEqual(reply.test2, undefined)
|
|
1060
1136
|
reply.send({ hello: 'world' })
|
|
1061
1137
|
})
|
|
1062
|
-
t.
|
|
1138
|
+
t.after(() => fastify.close())
|
|
1063
1139
|
|
|
1064
1140
|
fastify.listen({ port: 0 }, err => {
|
|
1065
|
-
t.
|
|
1066
|
-
t.
|
|
1141
|
+
t.assert.ifError(err)
|
|
1142
|
+
t.after(() => fastify.close())
|
|
1067
1143
|
|
|
1068
1144
|
sget({
|
|
1069
1145
|
method: 'GET',
|
|
1070
1146
|
url: 'http://localhost:' + fastify.server.address().port + '/yes'
|
|
1071
1147
|
}, (err, response, body) => {
|
|
1072
|
-
t.
|
|
1073
|
-
t.
|
|
1074
|
-
t.
|
|
1075
|
-
t.
|
|
1148
|
+
t.assert.ifError(err)
|
|
1149
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1150
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1151
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1152
|
+
done()
|
|
1076
1153
|
})
|
|
1077
1154
|
})
|
|
1078
1155
|
})
|
|
1079
1156
|
|
|
1080
|
-
test('decorateRequest with dependencies', (t) => {
|
|
1157
|
+
test('decorateRequest with dependencies', (t, done) => {
|
|
1081
1158
|
t.plan(2)
|
|
1082
1159
|
const app = Fastify()
|
|
1083
1160
|
|
|
@@ -1091,12 +1168,13 @@ test('decorateRequest with dependencies', (t) => {
|
|
|
1091
1168
|
app.hasDecorator('decorator1') &&
|
|
1092
1169
|
app.hasRequestDecorator('decorator1')
|
|
1093
1170
|
) {
|
|
1094
|
-
t.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
|
|
1095
|
-
t.ok(app.hasRequestDecorator('decorator2'))
|
|
1171
|
+
t.assert.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
|
|
1172
|
+
t.assert.ok(app.hasRequestDecorator('decorator2'))
|
|
1173
|
+
done()
|
|
1096
1174
|
}
|
|
1097
1175
|
})
|
|
1098
1176
|
|
|
1099
|
-
test('decorateRequest with dependencies (functions)', (t) => {
|
|
1177
|
+
test('decorateRequest with dependencies (functions)', (t, done) => {
|
|
1100
1178
|
t.plan(2)
|
|
1101
1179
|
const app = Fastify()
|
|
1102
1180
|
|
|
@@ -1110,12 +1188,13 @@ test('decorateRequest with dependencies (functions)', (t) => {
|
|
|
1110
1188
|
app.hasDecorator('decorator1') &&
|
|
1111
1189
|
app.hasRequestDecorator('decorator1')
|
|
1112
1190
|
) {
|
|
1113
|
-
t.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
|
|
1114
|
-
t.ok(app.hasRequestDecorator('decorator2'))
|
|
1191
|
+
t.assert.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
|
|
1192
|
+
t.assert.ok(app.hasRequestDecorator('decorator2'))
|
|
1193
|
+
done()
|
|
1115
1194
|
}
|
|
1116
1195
|
})
|
|
1117
1196
|
|
|
1118
|
-
test('chain of decorators on Request', async
|
|
1197
|
+
test('chain of decorators on Request', async t => {
|
|
1119
1198
|
const fastify = Fastify()
|
|
1120
1199
|
fastify.register(fp(async function (fastify) {
|
|
1121
1200
|
fastify.decorateRequest('foo', 'toto')
|
|
@@ -1159,32 +1238,32 @@ test('chain of decorators on Request', async (t) => {
|
|
|
1159
1238
|
|
|
1160
1239
|
{
|
|
1161
1240
|
const response = await fastify.inject('/foo')
|
|
1162
|
-
t.
|
|
1241
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1163
1242
|
}
|
|
1164
1243
|
|
|
1165
1244
|
{
|
|
1166
1245
|
const response = await fastify.inject('/bar')
|
|
1167
|
-
t.
|
|
1246
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1168
1247
|
}
|
|
1169
1248
|
|
|
1170
1249
|
{
|
|
1171
1250
|
const response = await fastify.inject('/plugin2/foo')
|
|
1172
|
-
t.
|
|
1251
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1173
1252
|
}
|
|
1174
1253
|
|
|
1175
1254
|
{
|
|
1176
1255
|
const response = await fastify.inject('/plugin2/bar')
|
|
1177
|
-
t.
|
|
1256
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1178
1257
|
}
|
|
1179
1258
|
|
|
1180
1259
|
{
|
|
1181
1260
|
const response = await fastify.inject('/plugin2/plugin3/foo')
|
|
1182
|
-
t.
|
|
1261
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1183
1262
|
}
|
|
1184
1263
|
|
|
1185
1264
|
{
|
|
1186
1265
|
const response = await fastify.inject('/plugin2/plugin3/bar')
|
|
1187
|
-
t.
|
|
1266
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1188
1267
|
}
|
|
1189
1268
|
})
|
|
1190
1269
|
|
|
@@ -1232,36 +1311,36 @@ test('chain of decorators on Reply', async (t) => {
|
|
|
1232
1311
|
|
|
1233
1312
|
{
|
|
1234
1313
|
const response = await fastify.inject('/foo')
|
|
1235
|
-
t.
|
|
1314
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1236
1315
|
}
|
|
1237
1316
|
|
|
1238
1317
|
{
|
|
1239
1318
|
const response = await fastify.inject('/bar')
|
|
1240
|
-
t.
|
|
1319
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1241
1320
|
}
|
|
1242
1321
|
|
|
1243
1322
|
{
|
|
1244
1323
|
const response = await fastify.inject('/plugin2/foo')
|
|
1245
|
-
t.
|
|
1324
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1246
1325
|
}
|
|
1247
1326
|
|
|
1248
1327
|
{
|
|
1249
1328
|
const response = await fastify.inject('/plugin2/bar')
|
|
1250
|
-
t.
|
|
1329
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1251
1330
|
}
|
|
1252
1331
|
|
|
1253
1332
|
{
|
|
1254
1333
|
const response = await fastify.inject('/plugin2/plugin3/foo')
|
|
1255
|
-
t.
|
|
1334
|
+
t.assert.strictEqual(response.body, 'toto')
|
|
1256
1335
|
}
|
|
1257
1336
|
|
|
1258
1337
|
{
|
|
1259
1338
|
const response = await fastify.inject('/plugin2/plugin3/bar')
|
|
1260
|
-
t.
|
|
1339
|
+
t.assert.strictEqual(response.body, 'tata')
|
|
1261
1340
|
}
|
|
1262
1341
|
})
|
|
1263
1342
|
|
|
1264
|
-
test('getDecorator should return the decorator', t => {
|
|
1343
|
+
test('getDecorator should return the decorator', (t, done) => {
|
|
1265
1344
|
t.plan(12)
|
|
1266
1345
|
const fastify = Fastify()
|
|
1267
1346
|
|
|
@@ -1269,10 +1348,10 @@ test('getDecorator should return the decorator', t => {
|
|
|
1269
1348
|
fastify.decorateRequest('root', 'from_root_request')
|
|
1270
1349
|
fastify.decorateReply('root', 'from_root_reply')
|
|
1271
1350
|
|
|
1272
|
-
t.
|
|
1351
|
+
t.assert.strictEqual(fastify.getDecorator('root'), 'from_root')
|
|
1273
1352
|
fastify.get('/', async (req, res) => {
|
|
1274
|
-
t.
|
|
1275
|
-
t.
|
|
1353
|
+
t.assert.strictEqual(req.getDecorator('root'), 'from_root_request')
|
|
1354
|
+
t.assert.strictEqual(res.getDecorator('root'), 'from_root_reply')
|
|
1276
1355
|
|
|
1277
1356
|
res.send()
|
|
1278
1357
|
})
|
|
@@ -1280,32 +1359,33 @@ test('getDecorator should return the decorator', t => {
|
|
|
1280
1359
|
fastify.register((child) => {
|
|
1281
1360
|
child.decorate('child', 'from_child')
|
|
1282
1361
|
|
|
1283
|
-
t.
|
|
1284
|
-
t.
|
|
1362
|
+
t.assert.strictEqual(child.getDecorator('child'), 'from_child')
|
|
1363
|
+
t.assert.strictEqual(child.getDecorator('root'), 'from_root')
|
|
1285
1364
|
|
|
1286
1365
|
child.get('/child', async (req, res) => {
|
|
1287
|
-
t.
|
|
1288
|
-
t.
|
|
1366
|
+
t.assert.strictEqual(req.getDecorator('root'), 'from_root_request')
|
|
1367
|
+
t.assert.strictEqual(res.getDecorator('root'), 'from_root_reply')
|
|
1289
1368
|
|
|
1290
1369
|
res.send()
|
|
1291
1370
|
})
|
|
1292
1371
|
})
|
|
1293
1372
|
|
|
1294
1373
|
fastify.ready((err) => {
|
|
1295
|
-
t.
|
|
1374
|
+
t.assert.ifError(err)
|
|
1296
1375
|
fastify.inject({ url: '/' }, (err, res) => {
|
|
1297
|
-
t.
|
|
1298
|
-
t.
|
|
1376
|
+
t.assert.ifError(err)
|
|
1377
|
+
t.assert.ok(true)
|
|
1299
1378
|
})
|
|
1300
1379
|
|
|
1301
1380
|
fastify.inject({ url: '/child' }, (err, res) => {
|
|
1302
|
-
t.
|
|
1303
|
-
t.
|
|
1381
|
+
t.assert.ifError(err)
|
|
1382
|
+
t.assert.ok(true)
|
|
1383
|
+
done()
|
|
1304
1384
|
})
|
|
1305
1385
|
})
|
|
1306
1386
|
})
|
|
1307
1387
|
|
|
1308
|
-
test('getDecorator should return function decorators with expected binded context', t => {
|
|
1388
|
+
test('getDecorator should return function decorators with expected binded context', (t, done) => {
|
|
1309
1389
|
t.plan(12)
|
|
1310
1390
|
const fastify = Fastify()
|
|
1311
1391
|
|
|
@@ -1324,49 +1404,48 @@ test('getDecorator should return function decorators with expected binded contex
|
|
|
1324
1404
|
return this
|
|
1325
1405
|
})
|
|
1326
1406
|
|
|
1327
|
-
t.
|
|
1407
|
+
t.assert.deepEqual(child.getDecorator('a')(), child)
|
|
1328
1408
|
child.get('/child', async (req, res) => {
|
|
1329
|
-
t.
|
|
1330
|
-
t.
|
|
1409
|
+
t.assert.deepEqual(req.getDecorator('b')(), req)
|
|
1410
|
+
t.assert.deepEqual(res.getDecorator('c')(), res)
|
|
1331
1411
|
|
|
1332
1412
|
res.send()
|
|
1333
1413
|
})
|
|
1334
1414
|
})
|
|
1335
1415
|
|
|
1336
|
-
t.
|
|
1416
|
+
t.assert.deepEqual(fastify.getDecorator('a')(), fastify)
|
|
1337
1417
|
fastify.get('/', async (req, res) => {
|
|
1338
|
-
t.
|
|
1339
|
-
t.
|
|
1340
|
-
|
|
1418
|
+
t.assert.deepEqual(req.getDecorator('b')(), req)
|
|
1419
|
+
t.assert.deepEqual(res.getDecorator('c')(), res)
|
|
1341
1420
|
res.send()
|
|
1342
1421
|
})
|
|
1343
1422
|
|
|
1344
1423
|
fastify.ready((err) => {
|
|
1345
|
-
t.
|
|
1424
|
+
t.assert.ifError(err)
|
|
1346
1425
|
fastify.inject({ url: '/' }, (err, res) => {
|
|
1347
|
-
t.
|
|
1348
|
-
t.
|
|
1426
|
+
t.assert.ifError(err)
|
|
1427
|
+
t.assert.ok(true, 'passed')
|
|
1349
1428
|
})
|
|
1350
1429
|
|
|
1351
1430
|
fastify.inject({ url: '/child' }, (err, res) => {
|
|
1352
|
-
t.
|
|
1353
|
-
t.
|
|
1431
|
+
t.assert.ifError(err)
|
|
1432
|
+
t.assert.ok(true, 'passed')
|
|
1433
|
+
done()
|
|
1354
1434
|
})
|
|
1355
|
-
|
|
1356
|
-
t.pass()
|
|
1435
|
+
t.assert.ok(true, 'passed')
|
|
1357
1436
|
})
|
|
1358
1437
|
})
|
|
1359
1438
|
|
|
1360
|
-
test('getDecorator should only return decorators existing in the scope', t => {
|
|
1439
|
+
test('getDecorator should only return decorators existing in the scope', (t, done) => {
|
|
1361
1440
|
t.plan(9)
|
|
1362
1441
|
|
|
1363
1442
|
function assertsThrowOnUndeclaredDecorator (notDecorated, instanceType) {
|
|
1364
1443
|
try {
|
|
1365
1444
|
notDecorated.getDecorator('foo')
|
|
1366
|
-
t.fail()
|
|
1445
|
+
t.assert.fail()
|
|
1367
1446
|
} catch (e) {
|
|
1368
|
-
t.
|
|
1369
|
-
t.
|
|
1447
|
+
t.assert.deepEqual(e.code, 'FST_ERR_DEC_UNDECLARED')
|
|
1448
|
+
t.assert.deepEqual(e.message, `No decorator 'foo' has been declared on ${instanceType}.`)
|
|
1370
1449
|
}
|
|
1371
1450
|
}
|
|
1372
1451
|
|
|
@@ -1385,17 +1464,18 @@ test('getDecorator should only return decorators existing in the scope', t => {
|
|
|
1385
1464
|
})
|
|
1386
1465
|
|
|
1387
1466
|
fastify.ready((err) => {
|
|
1388
|
-
t.
|
|
1467
|
+
t.assert.ifError(err)
|
|
1389
1468
|
|
|
1390
1469
|
assertsThrowOnUndeclaredDecorator(fastify, 'instance')
|
|
1391
1470
|
fastify.inject({ url: '/' }, (err, res) => {
|
|
1392
|
-
t.
|
|
1393
|
-
t.
|
|
1471
|
+
t.assert.ifError(err)
|
|
1472
|
+
t.assert.ok(true, 'passed')
|
|
1473
|
+
done()
|
|
1394
1474
|
})
|
|
1395
1475
|
})
|
|
1396
1476
|
})
|
|
1397
1477
|
|
|
1398
|
-
test('Request.setDecorator should update an existing decorator', t => {
|
|
1478
|
+
test('Request.setDecorator should update an existing decorator', (t, done) => {
|
|
1399
1479
|
t.plan(7)
|
|
1400
1480
|
const fastify = Fastify()
|
|
1401
1481
|
|
|
@@ -1408,25 +1488,26 @@ test('Request.setDecorator should update an existing decorator', t => {
|
|
|
1408
1488
|
})
|
|
1409
1489
|
try {
|
|
1410
1490
|
req.setDecorator('foo', { user: 'Jean' })
|
|
1411
|
-
t.fail()
|
|
1491
|
+
t.assert.fail()
|
|
1412
1492
|
} catch (e) {
|
|
1413
|
-
t.
|
|
1414
|
-
t.
|
|
1493
|
+
t.assert.deepEqual(e.code, 'FST_ERR_DEC_UNDECLARED')
|
|
1494
|
+
t.assert.deepEqual(e.message, "No decorator 'foo' has been declared on request.")
|
|
1415
1495
|
}
|
|
1416
1496
|
})
|
|
1417
1497
|
|
|
1418
1498
|
fastify.get('/', async (req, res) => {
|
|
1419
|
-
t.
|
|
1420
|
-
t.
|
|
1499
|
+
t.assert.deepEqual(req.getDecorator('session'), { user: 'Jean' })
|
|
1500
|
+
t.assert.deepEqual(req.getDecorator('utility')(), req)
|
|
1421
1501
|
|
|
1422
1502
|
res.send()
|
|
1423
1503
|
})
|
|
1424
1504
|
|
|
1425
1505
|
fastify.ready((err) => {
|
|
1426
|
-
t.
|
|
1506
|
+
t.assert.ifError(err)
|
|
1427
1507
|
fastify.inject({ url: '/' }, (err, res) => {
|
|
1428
|
-
t.
|
|
1429
|
-
t.
|
|
1508
|
+
t.assert.ifError(err)
|
|
1509
|
+
t.assert.ok(true, 'passed')
|
|
1510
|
+
done()
|
|
1430
1511
|
})
|
|
1431
1512
|
})
|
|
1432
1513
|
})
|