@fastify/express 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,671 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('fastify')
5
+ const sget = require('simple-get').concat
6
+ const cors = require('cors')
7
+ const passport = require('passport')
8
+ const Strategy = require('passport-http-bearer').Strategy
9
+
10
+ const expressPlugin = require('../index')
11
+
12
+ test('Should support connect style middlewares', t => {
13
+ t.plan(4)
14
+ const fastify = Fastify()
15
+ t.teardown(fastify.close)
16
+
17
+ fastify
18
+ .register(expressPlugin)
19
+ .after(() => { fastify.use(cors()) })
20
+
21
+ fastify.get('/', async (req, reply) => {
22
+ return { hello: 'world' }
23
+ })
24
+
25
+ fastify.listen(0, (err, address) => {
26
+ t.error(err)
27
+ sget({
28
+ method: 'GET',
29
+ url: address
30
+ }, (err, res, data) => {
31
+ t.error(err)
32
+ t.match(res.headers, {
33
+ 'access-control-allow-origin': '*'
34
+ })
35
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
36
+ })
37
+ })
38
+ })
39
+
40
+ test('Should support connect style middlewares (async await)', async t => {
41
+ t.plan(3)
42
+ const fastify = Fastify()
43
+ t.teardown(fastify.close)
44
+
45
+ await fastify.register(expressPlugin)
46
+ fastify.use(cors())
47
+
48
+ fastify.get('/', async (req, reply) => {
49
+ return { hello: 'world' }
50
+ })
51
+
52
+ const address = await fastify.listen(0)
53
+ return new Promise((resolve, reject) => {
54
+ sget({
55
+ method: 'GET',
56
+ url: address
57
+ }, (err, res, data) => {
58
+ t.error(err)
59
+ t.match(res.headers, {
60
+ 'access-control-allow-origin': '*'
61
+ })
62
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
63
+ resolve()
64
+ })
65
+ })
66
+ })
67
+
68
+ test('Should support connect style middlewares (async await after)', async t => {
69
+ t.plan(3)
70
+ const fastify = Fastify()
71
+ t.teardown(fastify.close)
72
+
73
+ fastify.register(expressPlugin)
74
+ await fastify.after()
75
+ fastify.use(cors())
76
+
77
+ fastify.get('/', async (req, reply) => {
78
+ return { hello: 'world' }
79
+ })
80
+
81
+ const address = await fastify.listen(0)
82
+ return new Promise((resolve, reject) => {
83
+ sget({
84
+ method: 'GET',
85
+ url: address
86
+ }, (err, res, data) => {
87
+ t.error(err)
88
+ t.match(res.headers, {
89
+ 'access-control-allow-origin': '*'
90
+ })
91
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
92
+ resolve()
93
+ })
94
+ })
95
+ })
96
+
97
+ test('Should support per path middlewares', t => {
98
+ t.plan(5)
99
+ const fastify = Fastify()
100
+ t.teardown(fastify.close)
101
+
102
+ fastify
103
+ .register(expressPlugin)
104
+ .after(() => { fastify.use('/cors', cors()) })
105
+
106
+ fastify.get('/cors/hello', async (req, reply) => {
107
+ return { hello: 'world' }
108
+ })
109
+
110
+ fastify.get('/', async (req, reply) => {
111
+ return { hello: 'world' }
112
+ })
113
+
114
+ fastify.listen(0, (err, address) => {
115
+ t.error(err)
116
+ sget({
117
+ method: 'GET',
118
+ url: address + '/cors/hello'
119
+ }, (err, res, data) => {
120
+ t.error(err)
121
+ t.match(res.headers, {
122
+ 'access-control-allow-origin': '*'
123
+ })
124
+ })
125
+
126
+ sget({
127
+ method: 'GET',
128
+ url: address
129
+ }, (err, res, data) => {
130
+ t.error(err)
131
+ t.notOk(res.headers['access-control-allow-origin'])
132
+ })
133
+ })
134
+ })
135
+
136
+ test('Should support complex middlewares', t => {
137
+ t.plan(4)
138
+
139
+ const fastify = Fastify()
140
+
141
+ passport.use(new Strategy((token, cb) => {
142
+ t.strictEqual(token, '123456789')
143
+ cb(null, { token })
144
+ }))
145
+
146
+ fastify
147
+ .register(expressPlugin)
148
+ .after(() => { fastify.use(passport.authenticate('bearer', { session: false })) })
149
+ fastify
150
+ .get('/', (req, reply) => {
151
+ t.deepEqual(req.raw.user, { token: '123456789' })
152
+ reply.send('ok')
153
+ })
154
+ .listen(0, (err, address) => {
155
+ t.error(err)
156
+ sget({
157
+ method: 'GET',
158
+ url: address,
159
+ headers: {
160
+ authorization: 'Bearer 123456789'
161
+ }
162
+ }, (err, res, data) => {
163
+ t.error(err)
164
+ fastify.close()
165
+ })
166
+ })
167
+ })
168
+
169
+ test('Encapsulation support / 1', t => {
170
+ t.plan(2)
171
+
172
+ const fastify = Fastify()
173
+
174
+ fastify.register((instance, opts, next) => {
175
+ instance.register(expressPlugin)
176
+ .after(() => { instance.use(middleware) })
177
+
178
+ instance.get('/plugin', (req, reply) => {
179
+ reply.send('ok')
180
+ })
181
+
182
+ next()
183
+ })
184
+
185
+ fastify.get('/', (req, reply) => {
186
+ reply.send('ok')
187
+ })
188
+
189
+ fastify.listen(0, (err, address) => {
190
+ t.error(err)
191
+ sget({
192
+ method: 'GET',
193
+ url: address
194
+ }, (err, res, data) => {
195
+ t.error(err)
196
+ fastify.close()
197
+ })
198
+ })
199
+
200
+ function middleware (req, res, next) {
201
+ t.fail('Shuld not be called')
202
+ }
203
+ })
204
+
205
+ test('Encapsulation support / 2', t => {
206
+ t.plan(2)
207
+
208
+ const fastify = Fastify()
209
+
210
+ fastify.register(expressPlugin)
211
+
212
+ fastify.register((instance, opts, next) => {
213
+ instance.use(middleware)
214
+ instance.get('/plugin', (req, reply) => {
215
+ reply.send('ok')
216
+ })
217
+
218
+ next()
219
+ })
220
+
221
+ fastify.get('/', (req, reply) => {
222
+ reply.send('ok')
223
+ })
224
+
225
+ fastify.listen(0, (err, address) => {
226
+ t.error(err)
227
+ sget({
228
+ method: 'GET',
229
+ url: address
230
+ }, (err, res, data) => {
231
+ t.error(err)
232
+ fastify.close()
233
+ })
234
+ })
235
+
236
+ function middleware (req, res, next) {
237
+ t.fail('Shuld not be called')
238
+ }
239
+ })
240
+
241
+ test('Encapsulation support / 3', t => {
242
+ t.plan(5)
243
+
244
+ const fastify = Fastify()
245
+
246
+ t.teardown(fastify.close)
247
+
248
+ fastify.register(expressPlugin)
249
+
250
+ fastify.register((instance, opts, next) => {
251
+ instance.use(cors())
252
+ instance.get('/plugin', (req, reply) => {
253
+ reply.send('ok')
254
+ })
255
+
256
+ next()
257
+ })
258
+
259
+ fastify.get('/', (req, reply) => {
260
+ reply.send('ok')
261
+ })
262
+
263
+ fastify.listen(0, (err, address) => {
264
+ t.error(err)
265
+ sget({
266
+ method: 'GET',
267
+ url: address + '/plugin'
268
+ }, (err, res, data) => {
269
+ t.error(err)
270
+ t.match(res.headers, {
271
+ 'access-control-allow-origin': '*'
272
+ })
273
+ })
274
+
275
+ sget({
276
+ method: 'GET',
277
+ url: address
278
+ }, (err, res, data) => {
279
+ t.error(err)
280
+ t.notMatch(res.headers, {
281
+ 'access-control-allow-origin': '*'
282
+ })
283
+ })
284
+ })
285
+ })
286
+
287
+ test('Encapsulation support / 4', t => {
288
+ t.plan(5)
289
+
290
+ const fastify = Fastify()
291
+
292
+ t.teardown(fastify.close)
293
+
294
+ fastify.register(expressPlugin)
295
+ fastify.after(() => {
296
+ fastify.use(middleware1)
297
+ })
298
+
299
+ fastify.register((instance, opts, next) => {
300
+ instance.use(middleware2)
301
+ instance.get('/plugin', (req, reply) => {
302
+ reply.send('ok')
303
+ })
304
+
305
+ next()
306
+ })
307
+
308
+ fastify.get('/', (req, reply) => {
309
+ reply.send('ok')
310
+ })
311
+
312
+ fastify.listen(0, (err, address) => {
313
+ t.error(err)
314
+ sget({
315
+ method: 'GET',
316
+ url: address + '/plugin'
317
+ }, (err, res, data) => {
318
+ t.error(err)
319
+ t.match(res.headers, {
320
+ 'x-middleware-1': 'true',
321
+ 'x-middleware-2': 'true'
322
+ })
323
+ })
324
+
325
+ sget({
326
+ method: 'GET',
327
+ url: address
328
+ }, (err, res, data) => {
329
+ t.error(err)
330
+ t.match(res.headers, {
331
+ 'x-middleware-1': 'true'
332
+ })
333
+ })
334
+ })
335
+
336
+ function middleware1 (req, res, next) {
337
+ res.setHeader('x-middleware-1', true)
338
+ next()
339
+ }
340
+
341
+ function middleware2 (req, res, next) {
342
+ res.setHeader('x-middleware-2', true)
343
+ next()
344
+ }
345
+ })
346
+
347
+ test('Encapsulation support / 5', t => {
348
+ t.plan(7)
349
+
350
+ const fastify = Fastify()
351
+
352
+ t.teardown(fastify.close)
353
+
354
+ fastify.register(expressPlugin)
355
+ fastify.after(() => {
356
+ fastify.use(middleware1)
357
+ })
358
+
359
+ fastify.register((instance, opts, next) => {
360
+ instance.use(middleware2)
361
+ instance.get('/', (req, reply) => {
362
+ reply.send('ok')
363
+ })
364
+
365
+ instance.register((i, opts, next) => {
366
+ i.use(middleware3)
367
+ i.get('/nested', (req, reply) => {
368
+ reply.send('ok')
369
+ })
370
+
371
+ next()
372
+ })
373
+
374
+ next()
375
+ }, { prefix: '/plugin' })
376
+
377
+ fastify.get('/', (req, reply) => {
378
+ reply.send('ok')
379
+ })
380
+
381
+ fastify.listen(0, (err, address) => {
382
+ t.error(err)
383
+ sget({
384
+ method: 'GET',
385
+ url: address + '/plugin/nested'
386
+ }, (err, res, data) => {
387
+ t.error(err)
388
+ t.match(res.headers, {
389
+ 'x-middleware-1': 'true',
390
+ 'x-middleware-2': 'true',
391
+ 'x-middleware-3': 'true'
392
+ })
393
+ })
394
+
395
+ sget({
396
+ method: 'GET',
397
+ url: address + '/plugin'
398
+ }, (err, res, data) => {
399
+ t.error(err)
400
+ t.match(res.headers, {
401
+ 'x-middleware-1': 'true',
402
+ 'x-middleware-2': 'true'
403
+ })
404
+ })
405
+
406
+ sget({
407
+ method: 'GET',
408
+ url: address
409
+ }, (err, res, data) => {
410
+ t.error(err)
411
+ t.match(res.headers, {
412
+ 'x-middleware-1': 'true'
413
+ })
414
+ })
415
+ })
416
+
417
+ function middleware1 (req, res, next) {
418
+ res.setHeader('x-middleware-1', true)
419
+ next()
420
+ }
421
+
422
+ function middleware2 (req, res, next) {
423
+ res.setHeader('x-middleware-2', true)
424
+ next()
425
+ }
426
+
427
+ function middleware3 (req, res, next) {
428
+ res.setHeader('x-middleware-3', true)
429
+ next()
430
+ }
431
+ })
432
+
433
+ test('Middleware chain', t => {
434
+ t.plan(5)
435
+
436
+ const order = [1, 2, 3]
437
+ const fastify = Fastify()
438
+
439
+ fastify
440
+ .register(expressPlugin)
441
+ .after(() => {
442
+ fastify
443
+ .use(middleware1)
444
+ .use(middleware2)
445
+ .use(middleware3)
446
+ })
447
+
448
+ fastify.get('/', async (req, reply) => {
449
+ return { hello: 'world' }
450
+ })
451
+
452
+ fastify.listen(0, (err, address) => {
453
+ t.error(err)
454
+ sget({
455
+ method: 'GET',
456
+ url: address
457
+ }, (err, res, data) => {
458
+ t.error(err)
459
+ fastify.close()
460
+ })
461
+ })
462
+
463
+ function middleware1 (req, res, next) {
464
+ t.strictEqual(order.shift(), 1)
465
+ next()
466
+ }
467
+
468
+ function middleware2 (req, res, next) {
469
+ t.strictEqual(order.shift(), 2)
470
+ next()
471
+ }
472
+
473
+ function middleware3 (req, res, next) {
474
+ t.strictEqual(order.shift(), 3)
475
+ next()
476
+ }
477
+ })
478
+
479
+ test('Middleware chain (with errors) / 1', t => {
480
+ t.plan(8)
481
+
482
+ const order = [1, 2, 3]
483
+ const fastify = Fastify()
484
+
485
+ fastify
486
+ .register(expressPlugin)
487
+ .after(() => {
488
+ fastify
489
+ .use(middleware1)
490
+ .use(middleware2)
491
+ .use(middleware3)
492
+ })
493
+
494
+ fastify.get('/', async (req, reply) => {
495
+ return { hello: 'world' }
496
+ })
497
+
498
+ fastify.listen(0, (err, address) => {
499
+ t.error(err)
500
+ sget({
501
+ method: 'GET',
502
+ url: address
503
+ }, (err, res, data) => {
504
+ t.error(err)
505
+ t.strictEqual(res.statusCode, 500)
506
+ fastify.close()
507
+ })
508
+ })
509
+
510
+ function middleware1 (req, res, next) {
511
+ t.strictEqual(order.shift(), 1)
512
+ next(new Error('middleware1'))
513
+ }
514
+
515
+ function middleware2 (err, req, res, next) {
516
+ t.is(err.message, 'middleware1')
517
+ t.strictEqual(order.shift(), 2)
518
+ next(new Error('middleware2'))
519
+ }
520
+
521
+ function middleware3 (err, req, res, next) {
522
+ t.is(err.message, 'middleware2')
523
+ t.strictEqual(order.shift(), 3)
524
+ next(new Error('kaboom'))
525
+ }
526
+ })
527
+
528
+ test('Middleware chain (with errors) / 2', t => {
529
+ t.plan(7)
530
+
531
+ const order = [1, 2]
532
+ const fastify = Fastify()
533
+
534
+ fastify.setErrorHandler((err, req, reply) => {
535
+ t.is(err.message, 'middleware2')
536
+ reply.send(err)
537
+ })
538
+
539
+ fastify
540
+ .register(expressPlugin)
541
+ .after(() => {
542
+ fastify
543
+ .use(middleware1)
544
+ .use(middleware2)
545
+ .use(middleware3)
546
+ })
547
+
548
+ fastify.get('/', async (req, reply) => {
549
+ return { hello: 'world' }
550
+ })
551
+
552
+ fastify.listen(0, (err, address) => {
553
+ t.error(err)
554
+ sget({
555
+ method: 'GET',
556
+ url: address
557
+ }, (err, res, data) => {
558
+ t.error(err)
559
+ t.strictEqual(res.statusCode, 500)
560
+ fastify.close()
561
+ })
562
+ })
563
+
564
+ function middleware1 (req, res, next) {
565
+ t.strictEqual(order.shift(), 1)
566
+ next(new Error('middleware1'))
567
+ }
568
+
569
+ function middleware2 (err, req, res, next) {
570
+ t.is(err.message, 'middleware1')
571
+ t.strictEqual(order.shift(), 2)
572
+ next(new Error('middleware2'))
573
+ }
574
+
575
+ function middleware3 (req, res, next) {
576
+ t.fail('We should not be here')
577
+ }
578
+ })
579
+
580
+ test('Send a response from a middleware', t => {
581
+ t.plan(4)
582
+
583
+ const fastify = Fastify()
584
+
585
+ fastify
586
+ .register(expressPlugin)
587
+ .after(() => {
588
+ fastify
589
+ .use(middleware1)
590
+ .use(middleware2)
591
+ })
592
+
593
+ fastify.addHook('preValidation', (req, reply, next) => {
594
+ t.fail('We should not be here')
595
+ })
596
+
597
+ fastify.addHook('preParsing', (req, reply, payload, next) => {
598
+ t.fail('We should not be here')
599
+ })
600
+
601
+ fastify.addHook('preHandler', (req, reply, next) => {
602
+ t.fail('We should not be here')
603
+ })
604
+
605
+ fastify.addHook('onSend', (req, reply, next) => {
606
+ t.fail('We should not be here')
607
+ })
608
+
609
+ fastify.addHook('onResponse', (req, reply, next) => {
610
+ t.ok('called')
611
+ next()
612
+ })
613
+
614
+ fastify.get('/', (req, reply) => {
615
+ t.fail('We should not be here')
616
+ })
617
+
618
+ fastify.listen(0, (err, address) => {
619
+ t.error(err)
620
+ sget({
621
+ method: 'GET',
622
+ url: address,
623
+ json: true
624
+ }, (err, res, data) => {
625
+ t.error(err)
626
+ t.deepEqual(data, { hello: 'world' })
627
+ fastify.close()
628
+ })
629
+ })
630
+
631
+ function middleware1 (req, res, next) {
632
+ res.send({ hello: 'world' })
633
+ }
634
+
635
+ function middleware2 (req, res, next) {
636
+ t.fail('We should not be here')
637
+ }
638
+ })
639
+
640
+ test('Should support plugin level prefix', t => {
641
+ t.plan(4)
642
+ const fastify = Fastify()
643
+ t.teardown(fastify.close)
644
+
645
+ fastify.register(expressPlugin)
646
+
647
+ fastify.register((instance, opts, next) => {
648
+ instance.use('/world', (req, res, next) => {
649
+ res.setHeader('x-foo', 'bar')
650
+ next()
651
+ })
652
+
653
+ instance.get('/world', (req, reply) => {
654
+ reply.send({ hello: 'world' })
655
+ })
656
+
657
+ next()
658
+ }, { prefix: '/hello' })
659
+
660
+ fastify.listen(0, (err, address) => {
661
+ t.error(err)
662
+ sget({
663
+ method: 'GET',
664
+ url: address + '/hello/world'
665
+ }, (err, res, data) => {
666
+ t.error(err)
667
+ t.strictEqual(res.headers['x-foo'], 'bar')
668
+ t.deepEqual(JSON.parse(data), { hello: 'world' })
669
+ })
670
+ })
671
+ })