fastify 4.24.0 → 4.24.1

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.
Files changed (53) hide show
  1. package/fastify.js +1 -1
  2. package/lib/route.js +10 -4
  3. package/package.json +32 -32
  4. package/test/404s.test.js +31 -39
  5. package/test/async-await.test.js +1 -1
  6. package/test/build-certificate.js +90 -1
  7. package/test/close-pipelining.test.js +5 -5
  8. package/test/close.test.js +1 -5
  9. package/test/custom-http-server.test.js +94 -91
  10. package/test/custom-parser.0.test.js +21 -47
  11. package/test/custom-parser.1.test.js +10 -732
  12. package/test/custom-parser.2.test.js +102 -0
  13. package/test/custom-parser.3.test.js +245 -0
  14. package/test/custom-parser.4.test.js +239 -0
  15. package/test/custom-parser.5.test.js +149 -0
  16. package/test/head.test.js +204 -0
  17. package/test/helper.js +30 -8
  18. package/test/hooks-async.test.js +3 -3
  19. package/test/hooks.on-listen.test.js +7 -6
  20. package/test/hooks.test.js +4 -15
  21. package/test/http2/closing.test.js +7 -15
  22. package/test/https/custom-https-server.test.js +43 -40
  23. package/test/listen.1.test.js +101 -0
  24. package/test/listen.2.test.js +103 -0
  25. package/test/listen.3.test.js +87 -0
  26. package/test/listen.4.test.js +164 -0
  27. package/test/listen.deprecated.test.js +3 -9
  28. package/test/logger/instantiation.test.js +25 -16
  29. package/test/logger/logger-test-utils.js +1 -1
  30. package/test/plugin.1.test.js +249 -0
  31. package/test/plugin.2.test.js +328 -0
  32. package/test/plugin.3.test.js +311 -0
  33. package/test/plugin.4.test.js +416 -0
  34. package/test/reply-trailers.test.js +1 -2
  35. package/test/route.1.test.js +309 -0
  36. package/test/route.2.test.js +99 -0
  37. package/test/route.3.test.js +205 -0
  38. package/test/route.4.test.js +131 -0
  39. package/test/route.5.test.js +230 -0
  40. package/test/route.6.test.js +306 -0
  41. package/test/route.7.test.js +370 -0
  42. package/test/route.8.test.js +142 -0
  43. package/test/stream.1.test.js +108 -0
  44. package/test/stream.2.test.js +119 -0
  45. package/test/stream.3.test.js +192 -0
  46. package/test/stream.4.test.js +223 -0
  47. package/test/stream.5.test.js +194 -0
  48. package/test/trust-proxy.test.js +2 -4
  49. package/test/upgrade.test.js +3 -3
  50. package/test/listen.test.js +0 -427
  51. package/test/plugin.test.js +0 -1275
  52. package/test/route.test.js +0 -1762
  53. package/test/stream.test.js +0 -816
@@ -1,1275 +0,0 @@
1
- 'use strict'
2
-
3
- /* eslint no-prototype-builtins: 0 */
4
-
5
- const t = require('tap')
6
- const test = t.test
7
- const Fastify = require('..')
8
- const sget = require('simple-get').concat
9
- const fp = require('fastify-plugin')
10
- const fakeTimer = require('@sinonjs/fake-timers')
11
-
12
- test('require a plugin', t => {
13
- t.plan(1)
14
- const fastify = Fastify()
15
- fastify.register(require('./plugin.helper'))
16
- fastify.ready(() => {
17
- t.ok(fastify.test)
18
- })
19
- })
20
-
21
- test('plugin metadata - ignore prefix', t => {
22
- t.plan(2)
23
- const fastify = Fastify()
24
-
25
- plugin[Symbol.for('skip-override')] = true
26
- fastify.register(plugin, { prefix: 'foo' })
27
-
28
- fastify.inject({
29
- method: 'GET',
30
- url: '/'
31
- }, function (err, res) {
32
- t.error(err)
33
- t.equal(res.payload, 'hello')
34
- })
35
-
36
- function plugin (instance, opts, done) {
37
- instance.get('/', function (request, reply) {
38
- reply.send('hello')
39
- })
40
- done()
41
- }
42
- })
43
-
44
- test('plugin metadata - naming plugins', async t => {
45
- t.plan(2)
46
- const fastify = Fastify()
47
-
48
- fastify.register(require('./plugin.name.display'))
49
- fastify.register(function (fastify, opts, done) {
50
- // one line
51
- t.equal(fastify.pluginName, 'function (fastify, opts, done) { -- // one line')
52
- done()
53
- })
54
- fastify.register(function fooBar (fastify, opts, done) {
55
- t.equal(fastify.pluginName, 'fooBar')
56
- done()
57
- })
58
-
59
- await fastify.ready()
60
- })
61
-
62
- test('fastify.register with fastify-plugin should not encapsulate his code', t => {
63
- t.plan(10)
64
- const fastify = Fastify()
65
-
66
- fastify.register((instance, opts, done) => {
67
- instance.register(fp((i, o, n) => {
68
- i.decorate('test', () => {})
69
- t.ok(i.test)
70
- n()
71
- }))
72
-
73
- t.notOk(instance.test)
74
-
75
- // the decoration is added at the end
76
- instance.after(() => {
77
- t.ok(instance.test)
78
- })
79
-
80
- instance.get('/', (req, reply) => {
81
- t.ok(instance.test)
82
- reply.send({ hello: 'world' })
83
- })
84
-
85
- done()
86
- })
87
-
88
- fastify.ready(() => {
89
- t.notOk(fastify.test)
90
- })
91
-
92
- fastify.listen({ port: 0 }, err => {
93
- t.error(err)
94
- t.teardown(() => { fastify.close() })
95
-
96
- sget({
97
- method: 'GET',
98
- url: 'http://localhost:' + fastify.server.address().port
99
- }, (err, response, body) => {
100
- t.error(err)
101
- t.equal(response.statusCode, 200)
102
- t.equal(response.headers['content-length'], '' + body.length)
103
- t.same(JSON.parse(body), { hello: 'world' })
104
- })
105
- })
106
- })
107
-
108
- test('fastify.register with fastify-plugin should provide access to external fastify instance if opts argument is a function', t => {
109
- t.plan(22)
110
- const fastify = Fastify()
111
-
112
- fastify.register((instance, opts, done) => {
113
- instance.register(fp((i, o, n) => {
114
- i.decorate('global', () => {})
115
- t.ok(i.global)
116
- n()
117
- }))
118
-
119
- instance.register((i, o, n) => n(), p => {
120
- t.notOk(p === instance || p === fastify)
121
- t.ok(instance.isPrototypeOf(p))
122
- t.ok(fastify.isPrototypeOf(p))
123
- t.ok(p.global)
124
- })
125
-
126
- instance.register((i, o, n) => {
127
- i.decorate('local', () => {})
128
- n()
129
- })
130
-
131
- instance.register((i, o, n) => n(), p => t.notOk(p.local))
132
-
133
- instance.register((i, o, n) => {
134
- t.ok(i.local)
135
- n()
136
- }, p => p.decorate('local', () => {}))
137
-
138
- instance.register((i, o, n) => n(), p => t.notOk(p.local))
139
-
140
- instance.register(fp((i, o, n) => {
141
- t.ok(i.global_2)
142
- n()
143
- }), p => p.decorate('global_2', () => 'hello'))
144
-
145
- instance.register((i, o, n) => {
146
- i.decorate('global_2', () => 'world')
147
- n()
148
- }, p => p.get('/', (req, reply) => {
149
- t.ok(p.global_2)
150
- reply.send({ hello: p.global_2() })
151
- }))
152
-
153
- t.notOk(instance.global)
154
- t.notOk(instance.global_2)
155
- t.notOk(instance.local)
156
-
157
- // the decoration is added at the end
158
- instance.after(() => {
159
- t.ok(instance.global)
160
- t.equal(instance.global_2(), 'hello')
161
- t.notOk(instance.local)
162
- })
163
-
164
- done()
165
- })
166
-
167
- fastify.ready(() => {
168
- t.notOk(fastify.global)
169
- })
170
-
171
- fastify.listen({ port: 0 }, err => {
172
- t.error(err)
173
- t.teardown(() => { fastify.close() })
174
-
175
- sget({
176
- method: 'GET',
177
- url: 'http://localhost:' + fastify.server.address().port
178
- }, (err, response, body) => {
179
- t.error(err)
180
- t.equal(response.statusCode, 200)
181
- t.equal(response.headers['content-length'], '' + body.length)
182
- t.same(JSON.parse(body), { hello: 'world' })
183
- })
184
- })
185
- })
186
-
187
- test('fastify.register with fastify-plugin registers fastify level plugins', t => {
188
- t.plan(15)
189
- const fastify = Fastify()
190
-
191
- function fastifyPlugin (instance, opts, done) {
192
- instance.decorate('test', 'first')
193
- t.ok(instance.test)
194
- done()
195
- }
196
-
197
- function innerPlugin (instance, opts, done) {
198
- instance.decorate('test2', 'second')
199
- done()
200
- }
201
-
202
- fastify.register(fp(fastifyPlugin))
203
-
204
- fastify.register((instance, opts, done) => {
205
- t.ok(instance.test)
206
- instance.register(fp(innerPlugin))
207
-
208
- instance.get('/test2', (req, reply) => {
209
- t.ok(instance.test2)
210
- reply.send({ test2: instance.test2 })
211
- })
212
-
213
- done()
214
- })
215
-
216
- fastify.ready(() => {
217
- t.ok(fastify.test)
218
- t.notOk(fastify.test2)
219
- })
220
-
221
- fastify.get('/', (req, reply) => {
222
- t.ok(fastify.test)
223
- reply.send({ test: fastify.test })
224
- })
225
-
226
- fastify.listen({ port: 0 }, err => {
227
- t.error(err)
228
- t.teardown(() => { fastify.close() })
229
-
230
- sget({
231
- method: 'GET',
232
- url: 'http://localhost:' + fastify.server.address().port
233
- }, (err, response, body) => {
234
- t.error(err)
235
- t.equal(response.statusCode, 200)
236
- t.equal(response.headers['content-length'], '' + body.length)
237
- t.same(JSON.parse(body), { test: 'first' })
238
- })
239
-
240
- sget({
241
- method: 'GET',
242
- url: 'http://localhost:' + fastify.server.address().port + '/test2'
243
- }, (err, response, body) => {
244
- t.error(err)
245
- t.equal(response.statusCode, 200)
246
- t.equal(response.headers['content-length'], '' + body.length)
247
- t.same(JSON.parse(body), { test2: 'second' })
248
- })
249
- })
250
- })
251
-
252
- test('check dependencies - should not throw', t => {
253
- t.plan(12)
254
- const fastify = Fastify()
255
-
256
- fastify.register((instance, opts, done) => {
257
- instance.register(fp((i, o, n) => {
258
- i.decorate('test', () => {})
259
- t.ok(i.test)
260
- n()
261
- }))
262
-
263
- instance.register(fp((i, o, n) => {
264
- try {
265
- i.decorate('otherTest', () => {}, ['test'])
266
- t.ok(i.test)
267
- t.ok(i.otherTest)
268
- n()
269
- } catch (e) {
270
- t.fail()
271
- }
272
- }))
273
-
274
- instance.get('/', (req, reply) => {
275
- t.ok(instance.test)
276
- t.ok(instance.otherTest)
277
- reply.send({ hello: 'world' })
278
- })
279
-
280
- done()
281
- })
282
-
283
- fastify.ready(() => {
284
- t.notOk(fastify.test)
285
- t.notOk(fastify.otherTest)
286
- })
287
-
288
- fastify.listen({ port: 0 }, err => {
289
- t.error(err)
290
- t.teardown(() => { fastify.close() })
291
-
292
- sget({
293
- method: 'GET',
294
- url: 'http://localhost:' + fastify.server.address().port
295
- }, (err, response, body) => {
296
- t.error(err)
297
- t.equal(response.statusCode, 200)
298
- t.equal(response.headers['content-length'], '' + body.length)
299
- t.same(JSON.parse(body), { hello: 'world' })
300
- })
301
- })
302
- })
303
-
304
- test('check dependencies - should throw', t => {
305
- t.plan(12)
306
- const fastify = Fastify()
307
-
308
- fastify.register((instance, opts, done) => {
309
- instance.register(fp((i, o, n) => {
310
- try {
311
- i.decorate('otherTest', () => {}, ['test'])
312
- t.fail()
313
- } catch (e) {
314
- t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
315
- t.equal(e.message, 'The decorator is missing dependency \'test\'.')
316
- }
317
- n()
318
- }))
319
-
320
- instance.register(fp((i, o, n) => {
321
- i.decorate('test', () => {})
322
- t.ok(i.test)
323
- t.notOk(i.otherTest)
324
- n()
325
- }))
326
-
327
- instance.get('/', (req, reply) => {
328
- t.ok(instance.test)
329
- t.notOk(instance.otherTest)
330
- reply.send({ hello: 'world' })
331
- })
332
-
333
- done()
334
- })
335
-
336
- fastify.ready(() => {
337
- t.notOk(fastify.test)
338
- })
339
-
340
- fastify.listen({ port: 0 }, err => {
341
- t.error(err)
342
- t.teardown(() => { fastify.close() })
343
-
344
- sget({
345
- method: 'GET',
346
- url: 'http://localhost:' + fastify.server.address().port
347
- }, (err, response, body) => {
348
- t.error(err)
349
- t.equal(response.statusCode, 200)
350
- t.equal(response.headers['content-length'], '' + body.length)
351
- t.same(JSON.parse(body), { hello: 'world' })
352
- })
353
- })
354
- })
355
-
356
- test('set the plugin name based on the plugin displayName symbol', t => {
357
- t.plan(6)
358
- const fastify = Fastify()
359
-
360
- fastify.register(fp((fastify, opts, done) => {
361
- t.equal(fastify.pluginName, 'fastify -> plugin-A')
362
- fastify.register(fp((fastify, opts, done) => {
363
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
364
- done()
365
- }, { name: 'plugin-AB' }))
366
- fastify.register(fp((fastify, opts, done) => {
367
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
368
- done()
369
- }, { name: 'plugin-AC' }))
370
- done()
371
- }, { name: 'plugin-A' }))
372
-
373
- fastify.register(fp((fastify, opts, done) => {
374
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
375
- done()
376
- }, { name: 'plugin-B' }))
377
-
378
- t.equal(fastify.pluginName, 'fastify')
379
-
380
- fastify.listen({ port: 0 }, err => {
381
- t.error(err)
382
- fastify.close()
383
- })
384
- })
385
-
386
- test('plugin name will change when using no encapsulation', t => {
387
- t.plan(6)
388
- const fastify = Fastify()
389
-
390
- fastify.register(fp((fastify, opts, done) => {
391
- // store it in a different variable will hold the correct name
392
- const pluginName = fastify.pluginName
393
- fastify.register(fp((fastify, opts, done) => {
394
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
395
- done()
396
- }, { name: 'plugin-AB' }))
397
- fastify.register(fp((fastify, opts, done) => {
398
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
399
- done()
400
- }, { name: 'plugin-AC' }))
401
- setImmediate(() => {
402
- // normally we would expect the name plugin-A
403
- // but we operate on the same instance in each plugin
404
- t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
405
- t.equal(pluginName, 'fastify -> plugin-A')
406
- })
407
- done()
408
- }, { name: 'plugin-A' }))
409
-
410
- t.equal(fastify.pluginName, 'fastify')
411
-
412
- fastify.listen({ port: 0 }, err => {
413
- t.error(err)
414
- fastify.close()
415
- })
416
- })
417
-
418
- test('plugin name is undefined when accessing in no plugin context', t => {
419
- t.plan(2)
420
- const fastify = Fastify()
421
-
422
- t.equal(fastify.pluginName, 'fastify')
423
-
424
- fastify.listen({ port: 0 }, err => {
425
- t.error(err)
426
- fastify.close()
427
- })
428
- })
429
-
430
- test('set the plugin name based on the plugin function name', t => {
431
- t.plan(5)
432
- const fastify = Fastify()
433
-
434
- fastify.register(function myPluginA (fastify, opts, done) {
435
- t.equal(fastify.pluginName, 'myPluginA')
436
- fastify.register(function myPluginAB (fastify, opts, done) {
437
- t.equal(fastify.pluginName, 'myPluginAB')
438
- done()
439
- })
440
- setImmediate(() => {
441
- // exact name due to encapsulation
442
- t.equal(fastify.pluginName, 'myPluginA')
443
- })
444
- done()
445
- })
446
-
447
- fastify.register(function myPluginB (fastify, opts, done) {
448
- t.equal(fastify.pluginName, 'myPluginB')
449
- done()
450
- })
451
-
452
- fastify.listen({ port: 0 }, err => {
453
- t.error(err)
454
- fastify.close()
455
- })
456
- })
457
-
458
- test('approximate a plugin name when no meta data is available', t => {
459
- t.plan(7)
460
- const fastify = Fastify()
461
-
462
- fastify.register((fastify, opts, done) => {
463
- // A
464
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
465
- t.equal(fastify.pluginName.includes('// A'), true)
466
- fastify.register((fastify, opts, done) => {
467
- // B
468
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
469
- t.equal(fastify.pluginName.includes('// B'), true)
470
- done()
471
- })
472
- setImmediate(() => {
473
- t.equal(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
474
- t.equal(fastify.pluginName.includes('// A'), true)
475
- })
476
- done()
477
- })
478
-
479
- fastify.listen({ port: 0 }, err => {
480
- t.error(err)
481
- fastify.close()
482
- })
483
- })
484
-
485
- test('approximate a plugin name also when fastify-plugin has no meta data', t => {
486
- t.plan(4)
487
- const fastify = Fastify()
488
-
489
- fastify.register(fp((fastify, opts, done) => {
490
- t.match(fastify.pluginName, /plugin\.test/)
491
- fastify.register(fp(function B (fastify, opts, done) {
492
- // function has name
493
- t.match(fastify.pluginName, /plugin\.test-auto-\d+ -> B/)
494
- done()
495
- }))
496
- setImmediate(() => {
497
- t.match(fastify.pluginName, /plugin\.test-auto-\d+ -> B/)
498
- })
499
- done()
500
- }))
501
-
502
- fastify.listen({ port: 0 }, err => {
503
- t.error(err)
504
- fastify.close()
505
- })
506
- })
507
-
508
- test('plugin encapsulation', t => {
509
- t.plan(10)
510
- const fastify = Fastify()
511
-
512
- fastify.register((instance, opts, done) => {
513
- instance.register(fp((i, o, n) => {
514
- i.decorate('test', 'first')
515
- n()
516
- }))
517
-
518
- instance.get('/first', (req, reply) => {
519
- reply.send({ plugin: instance.test })
520
- })
521
-
522
- done()
523
- })
524
-
525
- fastify.register((instance, opts, done) => {
526
- instance.register(fp((i, o, n) => {
527
- i.decorate('test', 'second')
528
- n()
529
- }))
530
-
531
- instance.get('/second', (req, reply) => {
532
- reply.send({ plugin: instance.test })
533
- })
534
-
535
- done()
536
- })
537
-
538
- fastify.ready(() => {
539
- t.notOk(fastify.test)
540
- })
541
-
542
- fastify.listen({ port: 0 }, err => {
543
- t.error(err)
544
- t.teardown(() => { fastify.close() })
545
-
546
- sget({
547
- method: 'GET',
548
- url: 'http://localhost:' + fastify.server.address().port + '/first'
549
- }, (err, response, body) => {
550
- t.error(err)
551
- t.equal(response.statusCode, 200)
552
- t.equal(response.headers['content-length'], '' + body.length)
553
- t.same(JSON.parse(body), { plugin: 'first' })
554
- })
555
-
556
- sget({
557
- method: 'GET',
558
- url: 'http://localhost:' + fastify.server.address().port + '/second'
559
- }, (err, response, body) => {
560
- t.error(err)
561
- t.equal(response.statusCode, 200)
562
- t.equal(response.headers['content-length'], '' + body.length)
563
- t.same(JSON.parse(body), { plugin: 'second' })
564
- })
565
- })
566
- })
567
-
568
- test('if a plugin raises an error and there is not a callback to handle it, the server must not start', t => {
569
- t.plan(2)
570
- const fastify = Fastify()
571
-
572
- fastify.register((instance, opts, done) => {
573
- done(new Error('err'))
574
- })
575
-
576
- fastify.listen({ port: 0 }, err => {
577
- t.ok(err instanceof Error)
578
- t.equal(err.message, 'err')
579
- })
580
- })
581
-
582
- test('add hooks after route declaration', t => {
583
- t.plan(3)
584
- const fastify = Fastify()
585
-
586
- function plugin (instance, opts, done) {
587
- instance.decorateRequest('check', null)
588
- instance.addHook('onRequest', (req, reply, done) => {
589
- req.check = {}
590
- done()
591
- })
592
- setImmediate(done)
593
- }
594
- fastify.register(fp(plugin))
595
-
596
- fastify.register((instance, options, done) => {
597
- instance.addHook('preHandler', function b (req, res, done) {
598
- req.check.hook2 = true
599
- done()
600
- })
601
-
602
- instance.get('/', (req, reply) => {
603
- reply.send(req.check)
604
- })
605
-
606
- instance.addHook('preHandler', function c (req, res, done) {
607
- req.check.hook3 = true
608
- done()
609
- })
610
-
611
- done()
612
- })
613
-
614
- fastify.addHook('preHandler', function a (req, res, done) {
615
- req.check.hook1 = true
616
- done()
617
- })
618
-
619
- fastify.listen({ port: 0 }, err => {
620
- t.error(err)
621
-
622
- sget({
623
- method: 'GET',
624
- url: 'http://localhost:' + fastify.server.address().port
625
- }, (err, response, body) => {
626
- t.error(err)
627
- t.same(JSON.parse(body), { hook1: true, hook2: true, hook3: true })
628
- fastify.close()
629
- })
630
- })
631
- })
632
-
633
- test('nested plugins', t => {
634
- t.plan(5)
635
-
636
- const fastify = Fastify()
637
-
638
- t.teardown(fastify.close.bind(fastify))
639
-
640
- fastify.register(function (fastify, opts, done) {
641
- fastify.register((fastify, opts, done) => {
642
- fastify.get('/', function (req, reply) {
643
- reply.send('I am child 1')
644
- })
645
- done()
646
- }, { prefix: '/child1' })
647
-
648
- fastify.register((fastify, opts, done) => {
649
- fastify.get('/', function (req, reply) {
650
- reply.send('I am child 2')
651
- })
652
- done()
653
- }, { prefix: '/child2' })
654
-
655
- done()
656
- }, { prefix: '/parent' })
657
-
658
- fastify.listen({ port: 0 }, err => {
659
- t.error(err)
660
-
661
- sget({
662
- method: 'GET',
663
- url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
664
- }, (err, response, body) => {
665
- t.error(err)
666
- t.same(body.toString(), 'I am child 1')
667
- })
668
-
669
- sget({
670
- method: 'GET',
671
- url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
672
- }, (err, response, body) => {
673
- t.error(err)
674
- t.same(body.toString(), 'I am child 2')
675
- })
676
- })
677
- })
678
-
679
- test('nested plugins awaited', t => {
680
- t.plan(5)
681
-
682
- const fastify = Fastify()
683
-
684
- t.teardown(fastify.close.bind(fastify))
685
-
686
- fastify.register(async function wrap (fastify, opts) {
687
- await fastify.register(async function child1 (fastify, opts) {
688
- fastify.get('/', function (req, reply) {
689
- reply.send('I am child 1')
690
- })
691
- }, { prefix: '/child1' })
692
-
693
- await fastify.register(async function child2 (fastify, opts) {
694
- fastify.get('/', function (req, reply) {
695
- reply.send('I am child 2')
696
- })
697
- }, { prefix: '/child2' })
698
- }, { prefix: '/parent' })
699
-
700
- fastify.listen({ port: 0 }, err => {
701
- t.error(err)
702
-
703
- sget({
704
- method: 'GET',
705
- url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
706
- }, (err, response, body) => {
707
- t.error(err)
708
- t.same(body.toString(), 'I am child 1')
709
- })
710
-
711
- sget({
712
- method: 'GET',
713
- url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
714
- }, (err, response, body) => {
715
- t.error(err)
716
- t.same(body.toString(), 'I am child 2')
717
- })
718
- })
719
- })
720
-
721
- test('plugin metadata - decorators', t => {
722
- t.plan(1)
723
- const fastify = Fastify()
724
-
725
- fastify.decorate('plugin1', true)
726
- fastify.decorateReply('plugin1', true)
727
- fastify.decorateRequest('plugin1', true)
728
-
729
- plugin[Symbol.for('skip-override')] = true
730
- plugin[Symbol.for('plugin-meta')] = {
731
- decorators: {
732
- fastify: ['plugin1'],
733
- reply: ['plugin1'],
734
- request: ['plugin1']
735
- }
736
- }
737
-
738
- fastify.register(plugin)
739
-
740
- fastify.ready(() => {
741
- t.ok(fastify.plugin)
742
- })
743
-
744
- function plugin (instance, opts, done) {
745
- instance.decorate('plugin', true)
746
- done()
747
- }
748
- })
749
-
750
- test('plugin metadata - decorators - should throw', t => {
751
- t.plan(1)
752
- const fastify = Fastify()
753
-
754
- fastify.decorate('plugin1', true)
755
- fastify.decorateReply('plugin1', true)
756
-
757
- plugin[Symbol.for('skip-override')] = true
758
- plugin[Symbol.for('plugin-meta')] = {
759
- decorators: {
760
- fastify: ['plugin1'],
761
- reply: ['plugin1'],
762
- request: ['plugin1']
763
- }
764
- }
765
-
766
- fastify.register(plugin)
767
- fastify.ready((err) => {
768
- t.equal(err.message, "The decorator 'plugin1' is not present in Request")
769
- })
770
-
771
- function plugin (instance, opts, done) {
772
- instance.decorate('plugin', true)
773
- done()
774
- }
775
- })
776
-
777
- test('plugin metadata - decorators - should throw with plugin name', t => {
778
- t.plan(1)
779
- const fastify = Fastify()
780
-
781
- fastify.decorate('plugin1', true)
782
- fastify.decorateReply('plugin1', true)
783
-
784
- plugin[Symbol.for('skip-override')] = true
785
- plugin[Symbol.for('plugin-meta')] = {
786
- name: 'the-plugin',
787
- decorators: {
788
- fastify: ['plugin1'],
789
- reply: ['plugin1'],
790
- request: ['plugin1']
791
- }
792
- }
793
-
794
- fastify.register(plugin)
795
- fastify.ready((err) => {
796
- t.equal(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
797
- })
798
-
799
- function plugin (instance, opts, done) {
800
- instance.decorate('plugin', true)
801
- done()
802
- }
803
- })
804
-
805
- test('plugin metadata - dependencies', t => {
806
- t.plan(1)
807
- const fastify = Fastify()
808
-
809
- dependency[Symbol.for('skip-override')] = true
810
- dependency[Symbol.for('plugin-meta')] = {
811
- name: 'plugin'
812
- }
813
-
814
- plugin[Symbol.for('skip-override')] = true
815
- plugin[Symbol.for('plugin-meta')] = {
816
- dependencies: ['plugin']
817
- }
818
-
819
- fastify.register(dependency)
820
- fastify.register(plugin)
821
-
822
- fastify.ready(() => {
823
- t.pass('everything right')
824
- })
825
-
826
- function dependency (instance, opts, done) {
827
- done()
828
- }
829
-
830
- function plugin (instance, opts, done) {
831
- done()
832
- }
833
- })
834
-
835
- test('plugin metadata - dependencies (nested)', t => {
836
- t.plan(1)
837
- const fastify = Fastify()
838
-
839
- dependency[Symbol.for('skip-override')] = true
840
- dependency[Symbol.for('plugin-meta')] = {
841
- name: 'plugin'
842
- }
843
-
844
- nested[Symbol.for('skip-override')] = true
845
- nested[Symbol.for('plugin-meta')] = {
846
- dependencies: ['plugin']
847
- }
848
-
849
- fastify.register(dependency)
850
- fastify.register(plugin)
851
-
852
- fastify.ready(() => {
853
- t.pass('everything right')
854
- })
855
-
856
- function dependency (instance, opts, done) {
857
- done()
858
- }
859
-
860
- function plugin (instance, opts, done) {
861
- instance.register(nested)
862
- done()
863
- }
864
-
865
- function nested (instance, opts, done) {
866
- done()
867
- }
868
- })
869
-
870
- test('pluginTimeout', t => {
871
- t.plan(5)
872
- const fastify = Fastify({
873
- pluginTimeout: 10
874
- })
875
- fastify.register(function (app, opts, done) {
876
- // to no call done on purpose
877
- })
878
- fastify.ready((err) => {
879
- t.ok(err)
880
- t.equal(err.message,
881
- "fastify-plugin: Plugin did not start in time: 'function (app, opts, done) { -- // to no call done on purpose'. You may have forgotten to call 'done' function or to resolve a Promise")
882
- t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
883
- t.ok(err.cause)
884
- t.equal(err.cause.code, 'AVV_ERR_READY_TIMEOUT')
885
- })
886
- })
887
-
888
- test('pluginTimeout - named function', t => {
889
- t.plan(5)
890
- const fastify = Fastify({
891
- pluginTimeout: 10
892
- })
893
- fastify.register(function nameFunction (app, opts, done) {
894
- // to no call done on purpose
895
- })
896
- fastify.ready((err) => {
897
- t.ok(err)
898
- t.equal(err.message,
899
- "fastify-plugin: Plugin did not start in time: 'nameFunction'. You may have forgotten to call 'done' function or to resolve a Promise")
900
- t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
901
- t.ok(err.cause)
902
- t.equal(err.cause.code, 'AVV_ERR_READY_TIMEOUT')
903
- })
904
- })
905
-
906
- test('pluginTimeout default', t => {
907
- t.plan(5)
908
- const clock = fakeTimer.install({ shouldClearNativeTimers: true })
909
-
910
- const fastify = Fastify()
911
- fastify.register(function (app, opts, done) {
912
- // default time elapsed without calling done
913
- clock.tick(10000)
914
- })
915
-
916
- fastify.ready((err) => {
917
- t.ok(err)
918
- t.equal(err.message,
919
- "fastify-plugin: Plugin did not start in time: 'function (app, opts, done) { -- // default time elapsed without calling done'. You may have forgotten to call 'done' function or to resolve a Promise")
920
- t.equal(err.code, 'FST_ERR_PLUGIN_TIMEOUT')
921
- t.ok(err.cause)
922
- t.equal(err.cause.code, 'AVV_ERR_READY_TIMEOUT')
923
- })
924
-
925
- t.teardown(clock.uninstall)
926
- })
927
-
928
- test('plugin metadata - version', t => {
929
- t.plan(1)
930
- const fastify = Fastify()
931
-
932
- plugin[Symbol.for('skip-override')] = true
933
- plugin[Symbol.for('plugin-meta')] = {
934
- name: 'plugin',
935
- fastify: '2.0.0'
936
- }
937
-
938
- fastify.register(plugin)
939
-
940
- fastify.ready(() => {
941
- t.pass('everything right')
942
- })
943
-
944
- function plugin (instance, opts, done) {
945
- done()
946
- }
947
- })
948
-
949
- test('plugin metadata - version range', t => {
950
- t.plan(1)
951
- const fastify = Fastify()
952
-
953
- plugin[Symbol.for('skip-override')] = true
954
- plugin[Symbol.for('plugin-meta')] = {
955
- name: 'plugin',
956
- fastify: '>=2.0.0'
957
- }
958
-
959
- fastify.register(plugin)
960
-
961
- fastify.ready(() => {
962
- t.pass('everything right')
963
- })
964
-
965
- function plugin (instance, opts, done) {
966
- done()
967
- }
968
- })
969
-
970
- test('plugin metadata - version not matching requirement', t => {
971
- t.plan(2)
972
- const fastify = Fastify()
973
-
974
- plugin[Symbol.for('skip-override')] = true
975
- plugin[Symbol.for('plugin-meta')] = {
976
- name: 'plugin',
977
- fastify: '99.0.0'
978
- }
979
-
980
- fastify.register(plugin)
981
-
982
- fastify.ready((err) => {
983
- t.ok(err)
984
- t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
985
- })
986
-
987
- function plugin (instance, opts, done) {
988
- done()
989
- }
990
- })
991
-
992
- test('plugin metadata - version not matching requirement 2', t => {
993
- t.plan(2)
994
- const fastify = Fastify()
995
-
996
- plugin[Symbol.for('skip-override')] = true
997
- plugin[Symbol.for('plugin-meta')] = {
998
- name: 'plugin',
999
- fastify: '<=3.0.0'
1000
- }
1001
-
1002
- fastify.register(plugin)
1003
-
1004
- fastify.ready((err) => {
1005
- t.ok(err)
1006
- t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
1007
- })
1008
-
1009
- function plugin (instance, opts, done) {
1010
- done()
1011
- }
1012
- })
1013
-
1014
- test('plugin metadata - version not matching requirement 3', t => {
1015
- t.plan(2)
1016
- const fastify = Fastify()
1017
-
1018
- plugin[Symbol.for('skip-override')] = true
1019
- plugin[Symbol.for('plugin-meta')] = {
1020
- name: 'plugin',
1021
- fastify: '>=99.0.0'
1022
- }
1023
-
1024
- fastify.register(plugin)
1025
-
1026
- fastify.ready((err) => {
1027
- t.ok(err)
1028
- t.equal(err.code, 'FST_ERR_PLUGIN_VERSION_MISMATCH')
1029
- })
1030
-
1031
- function plugin (instance, opts, done) {
1032
- done()
1033
- }
1034
- })
1035
-
1036
- test('plugin metadata - release candidate', t => {
1037
- t.plan(2)
1038
- const fastify = Fastify()
1039
- Object.defineProperty(fastify, 'version', {
1040
- value: '99.0.0-rc.1'
1041
- })
1042
-
1043
- plugin[Symbol.for('plugin-meta')] = {
1044
- name: 'plugin',
1045
- fastify: '99.x'
1046
- }
1047
-
1048
- fastify.register(plugin)
1049
-
1050
- fastify.ready((err) => {
1051
- t.error(err)
1052
- t.pass('everything right')
1053
- })
1054
-
1055
- function plugin (instance, opts, done) {
1056
- done()
1057
- }
1058
- })
1059
-
1060
- test('fastify-rc loads prior version plugins', t => {
1061
- t.plan(2)
1062
- const fastify = Fastify()
1063
- Object.defineProperty(fastify, 'version', {
1064
- value: '99.0.0-rc.1'
1065
- })
1066
-
1067
- plugin[Symbol.for('plugin-meta')] = {
1068
- name: 'plugin',
1069
- fastify: '^98.1.0'
1070
- }
1071
- plugin2[Symbol.for('plugin-meta')] = {
1072
- name: 'plugin2',
1073
- fastify: '98.x'
1074
- }
1075
-
1076
- fastify.register(plugin)
1077
-
1078
- fastify.ready((err) => {
1079
- t.error(err)
1080
- t.pass('everything right')
1081
- })
1082
-
1083
- function plugin (instance, opts, done) {
1084
- done()
1085
- }
1086
-
1087
- function plugin2 (instance, opts, done) {
1088
- done()
1089
- }
1090
- })
1091
-
1092
- test('hasPlugin method exists as a function', t => {
1093
- t.plan(1)
1094
-
1095
- const fastify = Fastify()
1096
- t.equal(typeof fastify.hasPlugin, 'function')
1097
- })
1098
-
1099
- test('hasPlugin returns true if the specified plugin has been registered', async t => {
1100
- t.plan(4)
1101
-
1102
- const fastify = Fastify()
1103
-
1104
- function pluginA (fastify, opts, done) {
1105
- t.ok(fastify.hasPlugin('plugin-A'))
1106
- done()
1107
- }
1108
- pluginA[Symbol.for('fastify.display-name')] = 'plugin-A'
1109
- fastify.register(pluginA)
1110
-
1111
- fastify.register(function pluginB (fastify, opts, done) {
1112
- t.ok(fastify.hasPlugin('pluginB'))
1113
- done()
1114
- })
1115
-
1116
- fastify.register(function (fastify, opts, done) {
1117
- // one line
1118
- t.ok(fastify.hasPlugin('function (fastify, opts, done) { -- // one line'))
1119
- done()
1120
- })
1121
-
1122
- await fastify.ready()
1123
-
1124
- t.ok(fastify.hasPlugin('fastify'))
1125
- })
1126
-
1127
- test('hasPlugin returns false if the specified plugin has not been registered', t => {
1128
- t.plan(1)
1129
-
1130
- const fastify = Fastify()
1131
- t.notOk(fastify.hasPlugin('pluginFoo'))
1132
- })
1133
-
1134
- test('hasPlugin returns false when using encapsulation', async t => {
1135
- t.plan(25)
1136
-
1137
- const fastify = Fastify()
1138
-
1139
- fastify.register(function pluginA (fastify, opts, done) {
1140
- t.ok(fastify.hasPlugin('pluginA'))
1141
- t.notOk(fastify.hasPlugin('pluginAA'))
1142
- t.notOk(fastify.hasPlugin('pluginAAA'))
1143
- t.notOk(fastify.hasPlugin('pluginAB'))
1144
- t.notOk(fastify.hasPlugin('pluginB'))
1145
-
1146
- fastify.register(function pluginAA (fastify, opts, done) {
1147
- t.notOk(fastify.hasPlugin('pluginA'))
1148
- t.ok(fastify.hasPlugin('pluginAA'))
1149
- t.notOk(fastify.hasPlugin('pluginAAA'))
1150
- t.notOk(fastify.hasPlugin('pluginAB'))
1151
- t.notOk(fastify.hasPlugin('pluginB'))
1152
-
1153
- fastify.register(function pluginAAA (fastify, opts, done) {
1154
- t.notOk(fastify.hasPlugin('pluginA'))
1155
- t.notOk(fastify.hasPlugin('pluginAA'))
1156
- t.ok(fastify.hasPlugin('pluginAAA'))
1157
- t.notOk(fastify.hasPlugin('pluginAB'))
1158
- t.notOk(fastify.hasPlugin('pluginB'))
1159
-
1160
- done()
1161
- })
1162
-
1163
- done()
1164
- })
1165
-
1166
- fastify.register(function pluginAB (fastify, opts, done) {
1167
- t.notOk(fastify.hasPlugin('pluginA'))
1168
- t.notOk(fastify.hasPlugin('pluginAA'))
1169
- t.notOk(fastify.hasPlugin('pluginAAA'))
1170
- t.ok(fastify.hasPlugin('pluginAB'))
1171
- t.notOk(fastify.hasPlugin('pluginB'))
1172
-
1173
- done()
1174
- })
1175
-
1176
- done()
1177
- })
1178
-
1179
- fastify.register(function pluginB (fastify, opts, done) {
1180
- t.notOk(fastify.hasPlugin('pluginA'))
1181
- t.notOk(fastify.hasPlugin('pluginAA'))
1182
- t.notOk(fastify.hasPlugin('pluginAAA'))
1183
- t.notOk(fastify.hasPlugin('pluginAB'))
1184
- t.ok(fastify.hasPlugin('pluginB'))
1185
-
1186
- done()
1187
- })
1188
-
1189
- await fastify.ready()
1190
- })
1191
-
1192
- test('hasPlugin returns true when using no encapsulation', async t => {
1193
- t.plan(26)
1194
-
1195
- const fastify = Fastify()
1196
-
1197
- fastify.register(fp((fastify, opts, done) => {
1198
- t.equal(fastify.pluginName, 'fastify -> plugin-AA')
1199
- t.ok(fastify.hasPlugin('plugin-AA'))
1200
- t.notOk(fastify.hasPlugin('plugin-A'))
1201
- t.notOk(fastify.hasPlugin('plugin-AAA'))
1202
- t.notOk(fastify.hasPlugin('plugin-AB'))
1203
- t.notOk(fastify.hasPlugin('plugin-B'))
1204
-
1205
- fastify.register(fp((fastify, opts, done) => {
1206
- t.ok(fastify.hasPlugin('plugin-AA'))
1207
- t.ok(fastify.hasPlugin('plugin-A'))
1208
- t.notOk(fastify.hasPlugin('plugin-AAA'))
1209
- t.notOk(fastify.hasPlugin('plugin-AB'))
1210
- t.notOk(fastify.hasPlugin('plugin-B'))
1211
-
1212
- fastify.register(fp((fastify, opts, done) => {
1213
- t.ok(fastify.hasPlugin('plugin-AA'))
1214
- t.ok(fastify.hasPlugin('plugin-A'))
1215
- t.ok(fastify.hasPlugin('plugin-AAA'))
1216
- t.notOk(fastify.hasPlugin('plugin-AB'))
1217
- t.notOk(fastify.hasPlugin('plugin-B'))
1218
-
1219
- done()
1220
- }, { name: 'plugin-AAA' }))
1221
-
1222
- done()
1223
- }, { name: 'plugin-A' }))
1224
-
1225
- fastify.register(fp((fastify, opts, done) => {
1226
- t.ok(fastify.hasPlugin('plugin-AA'))
1227
- t.ok(fastify.hasPlugin('plugin-A'))
1228
- t.ok(fastify.hasPlugin('plugin-AAA'))
1229
- t.ok(fastify.hasPlugin('plugin-AB'))
1230
- t.notOk(fastify.hasPlugin('plugin-B'))
1231
-
1232
- done()
1233
- }, { name: 'plugin-AB' }))
1234
-
1235
- done()
1236
- }, { name: 'plugin-AA' }))
1237
-
1238
- fastify.register(fp((fastify, opts, done) => {
1239
- t.ok(fastify.hasPlugin('plugin-AA'))
1240
- t.ok(fastify.hasPlugin('plugin-A'))
1241
- t.ok(fastify.hasPlugin('plugin-AAA'))
1242
- t.ok(fastify.hasPlugin('plugin-AB'))
1243
- t.ok(fastify.hasPlugin('plugin-B'))
1244
-
1245
- done()
1246
- }, { name: 'plugin-B' }))
1247
-
1248
- await fastify.ready()
1249
- })
1250
-
1251
- test('hasPlugin returns true when using encapsulation', async t => {
1252
- t.plan(2)
1253
-
1254
- const fastify = Fastify()
1255
-
1256
- const pluginCallback = function (server, options, done) {
1257
- done()
1258
- }
1259
- const pluginName = 'awesome-plugin'
1260
- const plugin = fp(pluginCallback, { name: pluginName })
1261
-
1262
- fastify.register(plugin)
1263
-
1264
- fastify.register(async (server) => {
1265
- t.ok(server.hasPlugin(pluginName))
1266
- })
1267
-
1268
- fastify.register(async function foo (server) {
1269
- server.register(async function bar (server) {
1270
- t.ok(server.hasPlugin(pluginName))
1271
- })
1272
- })
1273
-
1274
- await fastify.ready()
1275
- })