UNPKG

44 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const fp = require('fastify-plugin')
6const httpErrors = require('http-errors')
7const sget = require('simple-get').concat
8const errors = require('http-errors')
9const split = require('split2')
10const Fastify = require('..')
11
12test('default 404', t => {
13 t.plan(3)
14
15 const test = t.test
16 const fastify = Fastify()
17
18 fastify.get('/', function (req, reply) {
19 reply.send({ hello: 'world' })
20 })
21
22 t.tearDown(fastify.close.bind(fastify))
23
24 fastify.listen(0, err => {
25 t.error(err)
26
27 test('unsupported method', t => {
28 t.plan(3)
29 sget({
30 method: 'PUT',
31 url: 'http://localhost:' + fastify.server.address().port,
32 body: {},
33 json: true
34 }, (err, response, body) => {
35 t.error(err)
36 t.strictEqual(response.statusCode, 404)
37 t.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
38 })
39 })
40
41 test('unsupported route', t => {
42 t.plan(3)
43 sget({
44 method: 'GET',
45 url: 'http://localhost:' + fastify.server.address().port + '/notSupported',
46 body: {},
47 json: true
48 }, (err, response, body) => {
49 t.error(err)
50 t.strictEqual(response.statusCode, 404)
51 t.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
52 })
53 })
54 })
55})
56
57test('customized 404', t => {
58 t.plan(5)
59
60 const test = t.test
61 const fastify = Fastify()
62
63 fastify.get('/', function (req, reply) {
64 reply.send({ hello: 'world' })
65 })
66
67 fastify.get('/with-error', function (req, reply) {
68 reply.send(new errors.NotFound())
69 })
70
71 fastify.get('/with-error-custom-header', function (req, reply) {
72 const err = new errors.NotFound()
73 err.headers = { 'x-foo': 'bar' }
74 reply.send(err)
75 })
76
77 fastify.setNotFoundHandler(function (req, reply) {
78 reply.code(404).send('this was not found')
79 })
80
81 t.tearDown(fastify.close.bind(fastify))
82
83 fastify.listen(0, err => {
84 t.error(err)
85
86 test('unsupported method', t => {
87 t.plan(3)
88 sget({
89 method: 'PUT',
90 url: 'http://localhost:' + fastify.server.address().port,
91 body: JSON.stringify({ hello: 'world' }),
92 headers: { 'Content-Type': 'application/json' }
93 }, (err, response, body) => {
94 t.error(err)
95 t.strictEqual(response.statusCode, 404)
96 t.strictEqual(body.toString(), 'this was not found')
97 })
98 })
99
100 test('unsupported route', t => {
101 t.plan(3)
102 sget({
103 method: 'GET',
104 url: 'http://localhost:' + fastify.server.address().port + '/notSupported'
105 }, (err, response, body) => {
106 t.error(err)
107 t.strictEqual(response.statusCode, 404)
108 t.strictEqual(body.toString(), 'this was not found')
109 })
110 })
111
112 test('with error object', t => {
113 t.plan(3)
114 sget({
115 method: 'GET',
116 url: 'http://localhost:' + fastify.server.address().port + '/with-error'
117 }, (err, response, body) => {
118 t.error(err)
119 t.strictEqual(response.statusCode, 404)
120 t.deepEqual(JSON.parse(body), {
121 error: 'Not Found',
122 message: 'Not Found',
123 statusCode: 404
124 })
125 })
126 })
127
128 test('error object with headers property', t => {
129 t.plan(4)
130 sget({
131 method: 'GET',
132 url: 'http://localhost:' + fastify.server.address().port + '/with-error-custom-header'
133 }, (err, response, body) => {
134 t.error(err)
135 t.strictEqual(response.statusCode, 404)
136 t.strictEqual(response.headers['x-foo'], 'bar')
137 t.deepEqual(JSON.parse(body), {
138 error: 'Not Found',
139 message: 'Not Found',
140 statusCode: 404
141 })
142 })
143 })
144 })
145})
146
147test('custom header in notFound handler', t => {
148 t.plan(2)
149
150 const test = t.test
151 const fastify = Fastify()
152
153 fastify.setNotFoundHandler(function (req, reply) {
154 reply.code(404).header('x-foo', 'bar').send('this was not found')
155 })
156
157 t.tearDown(fastify.close.bind(fastify))
158
159 fastify.listen(0, err => {
160 t.error(err)
161
162 test('not found with custom header', t => {
163 t.plan(4)
164 sget({
165 method: 'GET',
166 url: 'http://localhost:' + fastify.server.address().port + '/notSupported'
167 }, (err, response, body) => {
168 t.error(err)
169 t.strictEqual(response.statusCode, 404)
170 t.strictEqual(response.headers['x-foo'], 'bar')
171 t.strictEqual(body.toString(), 'this was not found')
172 })
173 })
174 })
175})
176
177test('setting a custom 404 handler multiple times is an error', t => {
178 t.plan(5)
179
180 t.test('at the root level', t => {
181 t.plan(2)
182
183 const fastify = Fastify()
184
185 fastify.setNotFoundHandler(() => {})
186
187 try {
188 fastify.setNotFoundHandler(() => {})
189 t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
190 } catch (err) {
191 t.type(err, Error)
192 t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
193 }
194 })
195
196 t.test('at the plugin level', t => {
197 t.plan(3)
198
199 const fastify = Fastify()
200
201 fastify.register((instance, options, next) => {
202 instance.setNotFoundHandler(() => {})
203
204 try {
205 instance.setNotFoundHandler(() => {})
206 t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
207 } catch (err) {
208 t.type(err, Error)
209 t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
210 }
211
212 next()
213 }, { prefix: '/prefix' })
214
215 fastify.listen(0, err => {
216 t.error(err)
217 fastify.close()
218 })
219 })
220
221 t.test('at multiple levels', t => {
222 t.plan(3)
223
224 const fastify = Fastify()
225
226 fastify.register((instance, options, next) => {
227 try {
228 instance.setNotFoundHandler(() => {})
229 t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
230 } catch (err) {
231 t.type(err, Error)
232 t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/\'')
233 }
234 next()
235 })
236
237 fastify.setNotFoundHandler(() => {})
238
239 fastify.listen(0, err => {
240 t.error(err)
241 fastify.close()
242 })
243 })
244
245 t.test('at multiple levels / 2', t => {
246 t.plan(3)
247
248 const fastify = Fastify()
249
250 fastify.register((instance, options, next) => {
251 instance.setNotFoundHandler(() => {})
252
253 instance.register((instance2, options, next) => {
254 try {
255 instance2.setNotFoundHandler(() => {})
256 t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
257 } catch (err) {
258 t.type(err, Error)
259 t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
260 }
261 next()
262 })
263
264 next()
265 }, { prefix: '/prefix' })
266
267 fastify.setNotFoundHandler(() => {})
268
269 fastify.listen(0, err => {
270 t.error(err)
271 fastify.close()
272 })
273 })
274
275 t.test('in separate plugins at the same level', t => {
276 t.plan(3)
277
278 const fastify = Fastify()
279
280 fastify.register((instance, options, next) => {
281 instance.register((instance2A, options, next) => {
282 instance2A.setNotFoundHandler(() => {})
283 next()
284 })
285
286 instance.register((instance2B, options, next) => {
287 try {
288 instance2B.setNotFoundHandler(() => {})
289 t.fail('setting multiple 404 handlers at the same prefix encapsulation level should throw')
290 } catch (err) {
291 t.type(err, Error)
292 t.strictEqual(err.message, 'Not found handler already set for Fastify instance with prefix: \'/prefix\'')
293 }
294 next()
295 })
296
297 next()
298 }, { prefix: '/prefix' })
299
300 fastify.setNotFoundHandler(() => {})
301
302 fastify.listen(0, err => {
303 t.error(err)
304 fastify.close()
305 })
306 })
307})
308
309test('encapsulated 404', t => {
310 t.plan(9)
311
312 const test = t.test
313 const fastify = Fastify()
314
315 fastify.get('/', function (req, reply) {
316 reply.send({ hello: 'world' })
317 })
318
319 fastify.setNotFoundHandler(function (req, reply) {
320 reply.code(404).send('this was not found')
321 })
322
323 fastify.register(function (f, opts, next) {
324 f.setNotFoundHandler(function (req, reply) {
325 reply.code(404).send('this was not found 2')
326 })
327 next()
328 }, { prefix: '/test' })
329
330 fastify.register(function (f, opts, next) {
331 f.setNotFoundHandler(function (req, reply) {
332 reply.code(404).send('this was not found 3')
333 })
334 next()
335 }, { prefix: '/test2' })
336
337 fastify.register(function (f, opts, next) {
338 f.setNotFoundHandler(function (request, reply) {
339 reply.code(404).send('this was not found 4')
340 })
341 next()
342 }, { prefix: '/test3/' })
343
344 t.tearDown(fastify.close.bind(fastify))
345
346 fastify.listen(0, err => {
347 t.error(err)
348
349 test('root unsupported method', t => {
350 t.plan(3)
351 sget({
352 method: 'PUT',
353 url: 'http://localhost:' + fastify.server.address().port,
354 body: JSON.stringify({ hello: 'world' }),
355 headers: { 'Content-Type': 'application/json' }
356 }, (err, response, body) => {
357 t.error(err)
358 t.strictEqual(response.statusCode, 404)
359 t.strictEqual(body.toString(), 'this was not found')
360 })
361 })
362
363 test('root insupported route', t => {
364 t.plan(3)
365 sget({
366 method: 'GET',
367 url: 'http://localhost:' + fastify.server.address().port + '/notSupported'
368 }, (err, response, body) => {
369 t.error(err)
370 t.strictEqual(response.statusCode, 404)
371 t.strictEqual(body.toString(), 'this was not found')
372 })
373 })
374
375 test('unsupported method', t => {
376 t.plan(3)
377 sget({
378 method: 'PUT',
379 url: 'http://localhost:' + fastify.server.address().port + '/test',
380 body: JSON.stringify({ hello: 'world' }),
381 headers: { 'Content-Type': 'application/json' }
382 }, (err, response, body) => {
383 t.error(err)
384 t.strictEqual(response.statusCode, 404)
385 t.strictEqual(body.toString(), 'this was not found 2')
386 })
387 })
388
389 test('unsupported route', t => {
390 t.plan(3)
391 sget({
392 method: 'GET',
393 url: 'http://localhost:' + fastify.server.address().port + '/test/notSupported'
394 }, (err, response, body) => {
395 t.error(err)
396 t.strictEqual(response.statusCode, 404)
397 t.strictEqual(body.toString(), 'this was not found 2')
398 })
399 })
400
401 test('unsupported method bis', t => {
402 t.plan(3)
403 sget({
404 method: 'PUT',
405 url: 'http://localhost:' + fastify.server.address().port + '/test2',
406 body: JSON.stringify({ hello: 'world' }),
407 headers: { 'Content-Type': 'application/json' }
408 }, (err, response, body) => {
409 t.error(err)
410 t.strictEqual(response.statusCode, 404)
411 t.strictEqual(body.toString(), 'this was not found 3')
412 })
413 })
414
415 test('unsupported route bis', t => {
416 t.plan(3)
417 sget({
418 method: 'GET',
419 url: 'http://localhost:' + fastify.server.address().port + '/test2/notSupported'
420 }, (err, response, body) => {
421 t.error(err)
422 t.strictEqual(response.statusCode, 404)
423 t.strictEqual(body.toString(), 'this was not found 3')
424 })
425 })
426
427 test('unsupported method 3', t => {
428 t.plan(3)
429 sget({
430 method: 'PUT',
431 url: 'http://localhost:' + fastify.server.address().port + '/test3/',
432 body: JSON.stringify({ hello: 'world' }),
433 headers: { 'Content-Type': 'application/json' }
434 }, (err, response, body) => {
435 t.error(err)
436 t.strictEqual(response.statusCode, 404)
437 t.strictEqual(body.toString(), 'this was not found 4')
438 })
439 })
440
441 test('unsupported route 3', t => {
442 t.plan(3)
443 sget({
444 method: 'GET',
445 url: 'http://localhost:' + fastify.server.address().port + '/test3/notSupported'
446 }, (err, response, body) => {
447 t.error(err)
448 t.strictEqual(response.statusCode, 404)
449 t.strictEqual(body.toString(), 'this was not found 4')
450 })
451 })
452 })
453})
454
455test('custom 404 hook and handler context', t => {
456 t.plan(21)
457
458 const fastify = Fastify()
459
460 fastify.decorate('foo', 42)
461
462 fastify.addHook('onRequest', function (req, res, next) {
463 t.strictEqual(this.foo, 42)
464 next()
465 })
466 fastify.addHook('preHandler', function (request, reply, next) {
467 t.strictEqual(this.foo, 42)
468 next()
469 })
470 fastify.addHook('onSend', function (request, reply, payload, next) {
471 t.strictEqual(this.foo, 42)
472 next()
473 })
474 fastify.addHook('onResponse', function (request, reply, next) {
475 t.strictEqual(this.foo, 42)
476 next()
477 })
478
479 fastify.setNotFoundHandler(function (req, reply) {
480 t.strictEqual(this.foo, 42)
481 reply.code(404).send('this was not found')
482 })
483
484 fastify.register(function (instance, opts, next) {
485 instance.decorate('bar', 84)
486
487 instance.addHook('onRequest', function (req, res, next) {
488 t.strictEqual(this.bar, 84)
489 next()
490 })
491 instance.addHook('preHandler', function (request, reply, next) {
492 t.strictEqual(this.bar, 84)
493 next()
494 })
495 instance.addHook('onSend', function (request, reply, payload, next) {
496 t.strictEqual(this.bar, 84)
497 next()
498 })
499 instance.addHook('onResponse', function (request, reply, next) {
500 t.strictEqual(this.bar, 84)
501 next()
502 })
503
504 instance.setNotFoundHandler(function (req, reply) {
505 t.strictEqual(this.foo, 42)
506 t.strictEqual(this.bar, 84)
507 reply.code(404).send('encapsulated was not found')
508 })
509
510 next()
511 }, { prefix: '/encapsulated' })
512
513 fastify.inject('/not-found', (err, res) => {
514 t.error(err)
515 t.strictEqual(res.statusCode, 404)
516 t.strictEqual(res.payload, 'this was not found')
517 })
518
519 fastify.inject('/encapsulated/not-found', (err, res) => {
520 t.error(err)
521 t.strictEqual(res.statusCode, 404)
522 t.strictEqual(res.payload, 'encapsulated was not found')
523 })
524})
525
526test('encapsulated custom 404 without - prefix hook and handler context', t => {
527 t.plan(13)
528
529 const fastify = Fastify()
530
531 fastify.decorate('foo', 42)
532
533 fastify.register(function (instance, opts, next) {
534 instance.decorate('bar', 84)
535
536 instance.addHook('onRequest', function (req, res, next) {
537 t.strictEqual(this.foo, 42)
538 t.strictEqual(this.bar, 84)
539 next()
540 })
541 instance.addHook('preHandler', function (request, reply, next) {
542 t.strictEqual(this.foo, 42)
543 t.strictEqual(this.bar, 84)
544 next()
545 })
546 instance.addHook('onSend', function (request, reply, payload, next) {
547 t.strictEqual(this.foo, 42)
548 t.strictEqual(this.bar, 84)
549 next()
550 })
551 instance.addHook('onResponse', function (request, reply, next) {
552 t.strictEqual(this.foo, 42)
553 t.strictEqual(this.bar, 84)
554 next()
555 })
556
557 instance.setNotFoundHandler(function (request, reply) {
558 t.strictEqual(this.foo, 42)
559 t.strictEqual(this.bar, 84)
560 reply.code(404).send('custom not found')
561 })
562
563 next()
564 })
565
566 fastify.inject('/not-found', (err, res) => {
567 t.error(err)
568 t.strictEqual(res.statusCode, 404)
569 t.strictEqual(res.payload, 'custom not found')
570 })
571})
572
573test('run hooks and middleware on default 404', t => {
574 t.plan(8)
575
576 const fastify = Fastify()
577
578 fastify.addHook('onRequest', function (req, res, next) {
579 t.pass('onRequest called')
580 next()
581 })
582
583 fastify.use(function (req, res, next) {
584 t.pass('middleware called')
585 next()
586 })
587
588 fastify.addHook('preHandler', function (request, reply, next) {
589 t.pass('preHandler called')
590 next()
591 })
592
593 fastify.addHook('onSend', function (request, reply, payload, next) {
594 t.pass('onSend called')
595 next()
596 })
597
598 fastify.addHook('onResponse', function (request, reply, next) {
599 t.pass('onResponse called')
600 next()
601 })
602
603 fastify.get('/', function (req, reply) {
604 reply.send({ hello: 'world' })
605 })
606
607 t.tearDown(fastify.close.bind(fastify))
608
609 fastify.listen(0, err => {
610 t.error(err)
611
612 sget({
613 method: 'PUT',
614 url: 'http://localhost:' + fastify.server.address().port,
615 body: JSON.stringify({ hello: 'world' }),
616 headers: { 'Content-Type': 'application/json' }
617 }, (err, response, body) => {
618 t.error(err)
619 t.strictEqual(response.statusCode, 404)
620 })
621 })
622})
623
624test('run non-encapsulated plugin hooks and middleware on default 404', t => {
625 t.plan(7)
626
627 const fastify = Fastify()
628
629 fastify.register(fp(function (instance, options, next) {
630 instance.addHook('onRequest', function (req, res, next) {
631 t.pass('onRequest called')
632 next()
633 })
634
635 instance.use(function (req, res, next) {
636 t.pass('middleware called')
637 next()
638 })
639
640 instance.addHook('preHandler', function (request, reply, next) {
641 t.pass('preHandler called')
642 next()
643 })
644
645 instance.addHook('onSend', function (request, reply, payload, next) {
646 t.pass('onSend called')
647 next()
648 })
649
650 instance.addHook('onResponse', function (request, reply, next) {
651 t.pass('onResponse called')
652 next()
653 })
654
655 next()
656 }))
657
658 fastify.get('/', function (req, reply) {
659 reply.send({ hello: 'world' })
660 })
661
662 fastify.inject({
663 method: 'POST',
664 url: '/',
665 payload: { hello: 'world' }
666 }, (err, res) => {
667 t.error(err)
668 t.strictEqual(res.statusCode, 404)
669 })
670})
671
672test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
673 t.plan(13)
674
675 const fastify = Fastify()
676
677 const plugin = fp((instance, opts, next) => {
678 instance.addHook('onRequest', function (req, res, next) {
679 t.pass('onRequest called')
680 next()
681 })
682
683 instance.use(function (req, res, next) {
684 t.pass('middleware called')
685 next()
686 })
687
688 instance.addHook('preHandler', function (request, reply, next) {
689 t.pass('preHandler called')
690 next()
691 })
692
693 instance.addHook('onSend', function (request, reply, payload, next) {
694 t.pass('onSend called')
695 next()
696 })
697
698 instance.addHook('onResponse', function (request, reply, next) {
699 t.pass('onResponse called')
700 next()
701 })
702
703 next()
704 })
705
706 fastify.register(plugin)
707
708 fastify.get('/', function (req, reply) {
709 reply.send({ hello: 'world' })
710 })
711
712 fastify.setNotFoundHandler(function (req, reply) {
713 reply.code(404).send('this was not found')
714 })
715
716 fastify.register(plugin) // Registering plugin after handler also works
717
718 fastify.inject({ url: '/not-found' }, (err, res) => {
719 t.error(err)
720 t.strictEqual(res.statusCode, 404)
721 t.strictEqual(res.payload, 'this was not found')
722 })
723})
724
725test('run hooks and middleware with encapsulated 404', t => {
726 t.plan(13)
727
728 const fastify = Fastify()
729
730 fastify.addHook('onRequest', function (req, res, next) {
731 t.pass('onRequest called')
732 next()
733 })
734
735 fastify.use(function (req, res, next) {
736 t.pass('middleware called')
737 next()
738 })
739
740 fastify.addHook('preHandler', function (request, reply, next) {
741 t.pass('preHandler called')
742 next()
743 })
744
745 fastify.addHook('onSend', function (request, reply, payload, next) {
746 t.pass('onSend called')
747 next()
748 })
749
750 fastify.addHook('onResponse', function (request, reply, next) {
751 t.pass('onResponse called')
752 next()
753 })
754
755 fastify.register(function (f, opts, next) {
756 f.setNotFoundHandler(function (req, reply) {
757 reply.code(404).send('this was not found 2')
758 })
759
760 f.addHook('onRequest', function (req, res, next) {
761 t.pass('onRequest 2 called')
762 next()
763 })
764
765 f.use(function (req, res, next) {
766 t.pass('middleware 2 called')
767 next()
768 })
769
770 f.addHook('preHandler', function (request, reply, next) {
771 t.pass('preHandler 2 called')
772 next()
773 })
774
775 f.addHook('onSend', function (request, reply, payload, next) {
776 t.pass('onSend 2 called')
777 next()
778 })
779
780 f.addHook('onResponse', function (request, reply, next) {
781 t.pass('onResponse 2 called')
782 next()
783 })
784
785 next()
786 }, { prefix: '/test' })
787
788 t.tearDown(fastify.close.bind(fastify))
789
790 fastify.listen(0, err => {
791 t.error(err)
792
793 sget({
794 method: 'PUT',
795 url: 'http://localhost:' + fastify.server.address().port + '/test',
796 body: JSON.stringify({ hello: 'world' }),
797 headers: { 'Content-Type': 'application/json' }
798 }, (err, response, body) => {
799 t.error(err)
800 t.strictEqual(response.statusCode, 404)
801 })
802 })
803})
804
805test('run middlewares on default 404', t => {
806 t.plan(4)
807
808 const fastify = Fastify()
809
810 fastify.use(function (req, res, next) {
811 t.pass('middleware called')
812 next()
813 })
814
815 fastify.get('/', function (req, reply) {
816 reply.send({ hello: 'world' })
817 })
818
819 t.tearDown(fastify.close.bind(fastify))
820
821 fastify.listen(0, err => {
822 t.error(err)
823
824 sget({
825 method: 'PUT',
826 url: 'http://localhost:' + fastify.server.address().port,
827 body: JSON.stringify({ hello: 'world' }),
828 headers: { 'Content-Type': 'application/json' }
829 }, (err, response, body) => {
830 t.error(err)
831 t.strictEqual(response.statusCode, 404)
832 })
833 })
834})
835
836test('run middlewares with encapsulated 404', t => {
837 t.plan(5)
838
839 const fastify = Fastify()
840
841 fastify.use(function (req, res, next) {
842 t.pass('middleware called')
843 next()
844 })
845
846 fastify.register(function (f, opts, next) {
847 f.setNotFoundHandler(function (req, reply) {
848 reply.code(404).send('this was not found 2')
849 })
850
851 f.use(function (req, res, next) {
852 t.pass('middleware 2 called')
853 next()
854 })
855
856 next()
857 }, { prefix: '/test' })
858
859 t.tearDown(fastify.close.bind(fastify))
860
861 fastify.listen(0, err => {
862 t.error(err)
863
864 sget({
865 method: 'PUT',
866 url: 'http://localhost:' + fastify.server.address().port + '/test',
867 body: JSON.stringify({ hello: 'world' }),
868 headers: { 'Content-Type': 'application/json' }
869 }, (err, response, body) => {
870 t.error(err)
871 t.strictEqual(response.statusCode, 404)
872 })
873 })
874})
875
876test('hooks check 404', t => {
877 t.plan(13)
878
879 const fastify = Fastify()
880
881 fastify.get('/', function (req, reply) {
882 reply.send({ hello: 'world' })
883 })
884
885 fastify.addHook('onSend', (req, reply, payload, next) => {
886 t.deepEqual(req.query, { foo: 'asd' })
887 t.ok('called', 'onSend')
888 next()
889 })
890 fastify.addHook('onRequest', (req, res, next) => {
891 t.ok('called', 'onRequest')
892 next()
893 })
894 fastify.addHook('onResponse', (request, reply, next) => {
895 t.ok('called', 'onResponse')
896 next()
897 })
898
899 t.tearDown(fastify.close.bind(fastify))
900
901 fastify.listen(0, err => {
902 t.error(err)
903
904 sget({
905 method: 'PUT',
906 url: 'http://localhost:' + fastify.server.address().port + '?foo=asd',
907 body: JSON.stringify({ hello: 'world' }),
908 headers: { 'Content-Type': 'application/json' }
909 }, (err, response, body) => {
910 t.error(err)
911 t.strictEqual(response.statusCode, 404)
912 })
913
914 sget({
915 method: 'GET',
916 url: 'http://localhost:' + fastify.server.address().port + '/notSupported?foo=asd'
917 }, (err, response, body) => {
918 t.error(err)
919 t.strictEqual(response.statusCode, 404)
920 })
921 })
922})
923
924test('setNotFoundHandler should not suppress duplicated routes checking', t => {
925 t.plan(1)
926
927 const fastify = Fastify()
928
929 fastify.get('/', function (req, reply) {
930 reply.send({ hello: 'world' })
931 })
932
933 fastify.get('/', function (req, reply) {
934 reply.send({ hello: 'world' })
935 })
936
937 fastify.setNotFoundHandler(function (req, reply) {
938 reply.code(404).send('this was not found')
939 })
940
941 fastify.listen(0, err => {
942 t.ok(err)
943 })
944})
945
946test('log debug for 404', t => {
947 t.plan(1)
948
949 const Writable = require('stream').Writable
950
951 const logStream = new Writable()
952 logStream.logs = []
953 logStream._write = function (chunk, encoding, callback) {
954 this.logs.push(chunk.toString())
955 callback()
956 }
957
958 const fastify = Fastify({
959 logger: {
960 level: 'trace',
961 stream: logStream
962 }
963 })
964
965 fastify.get('/', function (req, reply) {
966 reply.send({ hello: 'world' })
967 })
968
969 t.tearDown(fastify.close.bind(fastify))
970
971 t.test('log debug', t => {
972 t.plan(7)
973 fastify.inject({
974 method: 'GET',
975 url: '/not-found'
976 }, (err, response) => {
977 t.error(err)
978 t.strictEqual(response.statusCode, 404)
979
980 const INFO_LEVEL = 30
981 t.strictEqual(JSON.parse(logStream.logs[0]).msg, 'incoming request')
982 t.strictEqual(JSON.parse(logStream.logs[1]).msg, 'Route GET:/not-found not found')
983 t.strictEqual(JSON.parse(logStream.logs[1]).level, INFO_LEVEL)
984 t.strictEqual(JSON.parse(logStream.logs[2]).msg, 'request completed')
985 t.strictEqual(logStream.logs.length, 3)
986 })
987 })
988})
989
990test('Unknown method', t => {
991 t.plan(5)
992
993 const fastify = Fastify()
994
995 fastify.get('/', function (req, reply) {
996 reply.send({ hello: 'world' })
997 })
998
999 t.tearDown(fastify.close.bind(fastify))
1000
1001 fastify.listen(0, err => {
1002 t.error(err)
1003
1004 const handler = () => {}
1005 // See https://github.com/fastify/light-my-request/pull/20
1006 t.throws(() => fastify.inject({
1007 method: 'UNKNWON_METHOD',
1008 url: '/'
1009 }, handler), Error)
1010
1011 sget({
1012 method: 'UNKNWON_METHOD',
1013 url: 'http://localhost:' + fastify.server.address().port
1014 }, (err, response, body) => {
1015 t.error(err)
1016 t.strictEqual(response.statusCode, 400)
1017 t.strictDeepEqual(JSON.parse(body), {
1018 error: 'Bad Request',
1019 message: 'Client Error',
1020 statusCode: 400
1021 })
1022 })
1023 })
1024})
1025
1026test('recognizes errors from the http-errors module', t => {
1027 t.plan(5)
1028
1029 const fastify = Fastify()
1030
1031 fastify.get('/', function (req, reply) {
1032 reply.send(httpErrors.NotFound())
1033 })
1034
1035 t.tearDown(fastify.close.bind(fastify))
1036
1037 fastify.listen(0, err => {
1038 t.error(err)
1039
1040 fastify.inject({
1041 method: 'GET',
1042 url: '/'
1043 }, (err, res) => {
1044 t.error(err)
1045 t.strictEqual(res.statusCode, 404)
1046
1047 sget('http://localhost:' + fastify.server.address().port, (err, response, body) => {
1048 t.error(err)
1049 const obj = JSON.parse(body.toString())
1050 t.strictDeepEqual(obj, {
1051 error: 'Not Found',
1052 message: 'Not Found',
1053 statusCode: 404
1054 })
1055 })
1056 })
1057 })
1058})
1059
1060test('the default 404 handler can be invoked inside a prefixed plugin', t => {
1061 t.plan(3)
1062
1063 const fastify = Fastify()
1064
1065 fastify.register(function (instance, opts, next) {
1066 instance.get('/path', function (request, reply) {
1067 reply.send(httpErrors.NotFound())
1068 })
1069
1070 next()
1071 }, { prefix: '/v1' })
1072
1073 fastify.inject('/v1/path', (err, res) => {
1074 t.error(err)
1075 t.strictEqual(res.statusCode, 404)
1076 t.strictDeepEqual(JSON.parse(res.payload), {
1077 error: 'Not Found',
1078 message: 'Not Found',
1079 statusCode: 404
1080 })
1081 })
1082})
1083
1084test('an inherited custom 404 handler can be invoked inside a prefixed plugin', t => {
1085 t.plan(3)
1086
1087 const fastify = Fastify()
1088
1089 fastify.setNotFoundHandler(function (request, reply) {
1090 reply.code(404).send('custom handler')
1091 })
1092
1093 fastify.register(function (instance, opts, next) {
1094 instance.get('/path', function (request, reply) {
1095 reply.send(httpErrors.NotFound())
1096 })
1097
1098 next()
1099 }, { prefix: '/v1' })
1100
1101 fastify.inject('/v1/path', (err, res) => {
1102 t.error(err)
1103 t.strictEqual(res.statusCode, 404)
1104 t.deepEqual(JSON.parse(res.payload), {
1105 error: 'Not Found',
1106 message: 'Not Found',
1107 statusCode: 404
1108 })
1109 })
1110})
1111
1112test('encapsulated custom 404 handler without a prefix is the handler for the entire 404 level', t => {
1113 t.plan(6)
1114
1115 const fastify = Fastify()
1116
1117 fastify.register(function (instance, opts, next) {
1118 instance.setNotFoundHandler(function (request, reply) {
1119 reply.code(404).send('custom handler')
1120 })
1121
1122 next()
1123 })
1124
1125 fastify.register(function (instance, opts, next) {
1126 instance.register(function (instance2, opts, next) {
1127 instance2.setNotFoundHandler(function (request, reply) {
1128 reply.code(404).send('custom handler 2')
1129 })
1130 next()
1131 })
1132
1133 next()
1134 }, { prefix: 'prefixed' })
1135
1136 fastify.inject('/not-found', (err, res) => {
1137 t.error(err)
1138 t.strictEqual(res.statusCode, 404)
1139 t.strictEqual(res.payload, 'custom handler')
1140 })
1141
1142 fastify.inject('/prefixed/not-found', (err, res) => {
1143 t.error(err)
1144 t.strictEqual(res.statusCode, 404)
1145 t.strictEqual(res.payload, 'custom handler 2')
1146 })
1147})
1148
1149test('cannot set notFoundHandler after binding', t => {
1150 t.plan(2)
1151
1152 const fastify = Fastify()
1153 t.tearDown(fastify.close.bind(fastify))
1154
1155 fastify.listen(0, err => {
1156 t.error(err)
1157
1158 try {
1159 fastify.setNotFoundHandler(() => { })
1160 t.fail()
1161 } catch (e) {
1162 t.pass()
1163 }
1164 })
1165})
1166
1167test('404 inside onSend', t => {
1168 t.plan(3)
1169
1170 const fastify = Fastify()
1171
1172 var called = false
1173
1174 fastify.get('/', function (req, reply) {
1175 reply.send({ hello: 'world' })
1176 })
1177
1178 fastify.addHook('onSend', function (request, reply, payload, next) {
1179 if (!called) {
1180 called = true
1181 next(new errors.NotFound())
1182 } else {
1183 next()
1184 }
1185 })
1186
1187 t.tearDown(fastify.close.bind(fastify))
1188
1189 fastify.listen(0, err => {
1190 t.error(err)
1191
1192 sget({
1193 method: 'GET',
1194 url: 'http://localhost:' + fastify.server.address().port
1195 }, (err, response, body) => {
1196 t.error(err)
1197 t.strictEqual(response.statusCode, 404)
1198 })
1199 })
1200})
1201
1202test('Not found on supported method (should return a 404)', t => {
1203 t.plan(5)
1204
1205 const fastify = Fastify()
1206
1207 fastify.get('/', function (req, reply) {
1208 reply.send({ hello: 'world' })
1209 })
1210
1211 t.tearDown(fastify.close.bind(fastify))
1212
1213 fastify.listen(0, err => {
1214 t.error(err)
1215
1216 fastify.inject({
1217 method: 'POST',
1218 url: '/'
1219 }, (err, res) => {
1220 t.error(err)
1221 t.strictEqual(res.statusCode, 404)
1222
1223 sget({
1224 method: 'POST',
1225 url: 'http://localhost:' + fastify.server.address().port
1226 }, (err, response, body) => {
1227 t.error(err)
1228 t.strictEqual(response.statusCode, 404)
1229 })
1230 })
1231 })
1232})
1233
1234// Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
1235test('Not found on unsupported method (should return a 404)', t => {
1236 t.plan(5)
1237
1238 const fastify = Fastify()
1239
1240 fastify.all('/', function (req, reply) {
1241 reply.send({ hello: 'world' })
1242 })
1243
1244 t.tearDown(fastify.close.bind(fastify))
1245
1246 fastify.listen(0, err => {
1247 t.error(err)
1248
1249 fastify.inject({
1250 method: 'PROPFIND',
1251 url: '/'
1252 }, (err, res) => {
1253 t.error(err)
1254 t.strictEqual(res.statusCode, 404)
1255
1256 sget({
1257 method: 'PROPFIND',
1258 url: 'http://localhost:' + fastify.server.address().port
1259 }, (err, response, body) => {
1260 t.error(err)
1261 t.strictEqual(response.statusCode, 404)
1262 })
1263 })
1264 })
1265})
1266
1267// https://github.com/fastify/fastify/issues/868
1268test('onSend hooks run when an encapsulated route invokes the notFound handler', t => {
1269 t.plan(3)
1270 const fastify = Fastify()
1271
1272 fastify.register((instance, options, done) => {
1273 instance.addHook('onSend', (request, reply, payload, next) => {
1274 t.pass('onSend hook called')
1275 next()
1276 })
1277
1278 instance.get('/', (request, reply) => {
1279 reply.send(new errors.NotFound())
1280 })
1281
1282 done()
1283 })
1284
1285 fastify.inject('/', (err, res) => {
1286 t.error(err)
1287 t.strictEqual(res.statusCode, 404)
1288 })
1289})
1290
1291// https://github.com/fastify/fastify/issues/713
1292test('preHandler option for setNotFoundHandler', t => {
1293 t.plan(10)
1294
1295 t.test('preHandler option', t => {
1296 t.plan(2)
1297 const fastify = Fastify()
1298
1299 fastify.setNotFoundHandler({
1300 preHandler: (req, reply, done) => {
1301 req.body.preHandler = true
1302 done()
1303 }
1304 }, function (req, reply) {
1305 reply.code(404).send(req.body)
1306 })
1307
1308 fastify.inject({
1309 method: 'POST',
1310 url: '/not-found',
1311 payload: { hello: 'world' }
1312 }, (err, res) => {
1313 t.error(err)
1314 var payload = JSON.parse(res.payload)
1315 t.deepEqual(payload, { preHandler: true, hello: 'world' })
1316 })
1317 })
1318
1319 // https://github.com/fastify/fastify/issues/2229
1320 t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', t => {
1321 t.plan(2)
1322 const fastify = Fastify()
1323
1324 fastify.setNotFoundHandler({
1325 preHandler: (req, reply, done) => {
1326 req.body.preHandler = true
1327 done()
1328 }
1329 }, function (req, reply) {
1330 reply.code(404).send(req.body)
1331 })
1332
1333 fastify.post('/', function (req, reply) {
1334 reply.callNotFound()
1335 })
1336
1337 fastify.inject({
1338 method: 'POST',
1339 url: '/',
1340 payload: { hello: 'world' }
1341 }, (err, res) => {
1342 t.error(err)
1343 var payload = JSON.parse(res.payload)
1344 t.deepEqual(payload, { preHandler: true, hello: 'world' })
1345 })
1346 })
1347
1348 t.test('preHandler hook in setNotFoundHandler should accept an array of functions and be called when callNotFound', t => {
1349 t.plan(2)
1350 const fastify = Fastify()
1351
1352 fastify.setNotFoundHandler({
1353 preHandler: [
1354 (req, reply, done) => {
1355 req.body.preHandler1 = true
1356 done()
1357 },
1358 (req, reply, done) => {
1359 req.body.preHandler2 = true
1360 done()
1361 }
1362 ]
1363 }, function (req, reply) {
1364 reply.code(404).send(req.body)
1365 })
1366
1367 fastify.post('/', function (req, reply) {
1368 reply.callNotFound()
1369 })
1370
1371 fastify.inject({
1372 method: 'POST',
1373 url: '/',
1374 payload: { hello: 'world' }
1375 }, (err, res) => {
1376 t.error(err)
1377 var payload = JSON.parse(res.payload)
1378 t.deepEqual(payload, { preHandler1: true, preHandler2: true, hello: 'world' })
1379 })
1380 })
1381
1382 t.test('preHandler option should be called after preHandler hook', t => {
1383 t.plan(2)
1384 const fastify = Fastify()
1385
1386 fastify.addHook('preHandler', (req, reply, next) => {
1387 req.body.check = 'a'
1388 next()
1389 })
1390
1391 fastify.setNotFoundHandler({
1392 preHandler: (req, reply, done) => {
1393 req.body.check += 'b'
1394 done()
1395 }
1396 }, (req, reply) => {
1397 reply.send(req.body)
1398 })
1399
1400 fastify.inject({
1401 method: 'POST',
1402 url: '/',
1403 payload: { hello: 'world' }
1404 }, (err, res) => {
1405 t.error(err)
1406 var payload = JSON.parse(res.payload)
1407 t.deepEqual(payload, { check: 'ab', hello: 'world' })
1408 })
1409 })
1410
1411 t.test('preHandler option should be unique per prefix', t => {
1412 t.plan(4)
1413 const fastify = Fastify()
1414
1415 fastify.setNotFoundHandler({
1416 preHandler: (req, reply, done) => {
1417 req.body.hello = 'earth'
1418 done()
1419 }
1420 }, (req, reply) => {
1421 reply.send(req.body)
1422 })
1423
1424 fastify.register(function (i, o, n) {
1425 i.setNotFoundHandler((req, reply) => {
1426 reply.send(req.body)
1427 })
1428
1429 n()
1430 }, { prefix: '/no' })
1431
1432 fastify.inject({
1433 method: 'POST',
1434 url: '/not-found',
1435 payload: { hello: 'world' }
1436 }, (err, res) => {
1437 t.error(err)
1438 var payload = JSON.parse(res.payload)
1439 t.deepEqual(payload, { hello: 'earth' })
1440 })
1441
1442 fastify.inject({
1443 method: 'POST',
1444 url: '/no/not-found',
1445 payload: { hello: 'world' }
1446 }, (err, res) => {
1447 t.error(err)
1448 var payload = JSON.parse(res.payload)
1449 t.deepEqual(payload, { hello: 'world' })
1450 })
1451 })
1452
1453 t.test('preHandler option should handle errors', t => {
1454 t.plan(3)
1455 const fastify = Fastify()
1456
1457 fastify.setNotFoundHandler({
1458 preHandler: (req, reply, done) => {
1459 done(new Error('kaboom'))
1460 }
1461 }, (req, reply) => {
1462 reply.send(req.body)
1463 })
1464
1465 fastify.inject({
1466 method: 'POST',
1467 url: '/not-found',
1468 payload: { hello: 'world' }
1469 }, (err, res) => {
1470 t.error(err)
1471 var payload = JSON.parse(res.payload)
1472 t.equal(res.statusCode, 500)
1473 t.deepEqual(payload, {
1474 message: 'kaboom',
1475 error: 'Internal Server Error',
1476 statusCode: 500
1477 })
1478 })
1479 })
1480
1481 t.test('preHandler option should handle errors with custom status code', t => {
1482 t.plan(3)
1483 const fastify = Fastify()
1484
1485 fastify.setNotFoundHandler({
1486 preHandler: (req, reply, done) => {
1487 reply.code(401)
1488 done(new Error('go away'))
1489 }
1490 }, (req, reply) => {
1491 reply.send(req.body)
1492 })
1493
1494 fastify.inject({
1495 method: 'POST',
1496 url: '/not-found',
1497 payload: { hello: 'world' }
1498 }, (err, res) => {
1499 t.error(err)
1500 var payload = JSON.parse(res.payload)
1501 t.equal(res.statusCode, 401)
1502 t.deepEqual(payload, {
1503 message: 'go away',
1504 error: 'Unauthorized',
1505 statusCode: 401
1506 })
1507 })
1508 })
1509
1510 t.test('preHandler option could accept an array of functions', t => {
1511 t.plan(2)
1512 const fastify = Fastify()
1513
1514 fastify.setNotFoundHandler({
1515 preHandler: [
1516 (req, reply, done) => {
1517 req.body.preHandler = 'a'
1518 done()
1519 },
1520 (req, reply, done) => {
1521 req.body.preHandler += 'b'
1522 done()
1523 }
1524 ]
1525 }, (req, reply) => {
1526 reply.send(req.body)
1527 })
1528
1529 fastify.inject({
1530 method: 'POST',
1531 url: '/not-found',
1532 payload: { hello: 'world' }
1533 }, (err, res) => {
1534 t.error(err)
1535 var payload = JSON.parse(res.payload)
1536 t.deepEqual(payload, { preHandler: 'ab', hello: 'world' })
1537 })
1538 })
1539
1540 t.test('preHandler option does not interfere with preHandler', t => {
1541 t.plan(4)
1542 const fastify = Fastify()
1543
1544 fastify.addHook('preHandler', (req, reply, next) => {
1545 req.body.check = 'a'
1546 next()
1547 })
1548
1549 fastify.setNotFoundHandler({
1550 preHandler: (req, reply, done) => {
1551 req.body.check += 'b'
1552 done()
1553 }
1554 }, (req, reply) => {
1555 reply.send(req.body)
1556 })
1557
1558 fastify.register(function (i, o, n) {
1559 i.setNotFoundHandler((req, reply) => {
1560 reply.send(req.body)
1561 })
1562
1563 n()
1564 }, { prefix: '/no' })
1565
1566 fastify.inject({
1567 method: 'post',
1568 url: '/not-found',
1569 payload: { hello: 'world' }
1570 }, (err, res) => {
1571 t.error(err)
1572 var payload = JSON.parse(res.payload)
1573 t.deepEqual(payload, { check: 'ab', hello: 'world' })
1574 })
1575
1576 fastify.inject({
1577 method: 'post',
1578 url: '/no/not-found',
1579 payload: { hello: 'world' }
1580 }, (err, res) => {
1581 t.error(err)
1582 var payload = JSON.parse(res.payload)
1583 t.deepEqual(payload, { check: 'a', hello: 'world' })
1584 })
1585 })
1586
1587 t.test('preHandler option should keep the context', t => {
1588 t.plan(3)
1589 const fastify = Fastify()
1590
1591 fastify.decorate('foo', 42)
1592
1593 fastify.setNotFoundHandler({
1594 preHandler: function (req, reply, done) {
1595 t.strictEqual(this.foo, 42)
1596 this.foo += 1
1597 req.body.foo = this.foo
1598 done()
1599 }
1600 }, (req, reply) => {
1601 reply.send(req.body)
1602 })
1603
1604 fastify.inject({
1605 method: 'POST',
1606 url: '/not-found',
1607 payload: { hello: 'world' }
1608 }, (err, res) => {
1609 t.error(err)
1610 var payload = JSON.parse(res.payload)
1611 t.deepEqual(payload, { foo: 43, hello: 'world' })
1612 })
1613 })
1614})
1615
1616test('reply.notFound invoked the notFound handler', t => {
1617 t.plan(3)
1618
1619 const fastify = Fastify()
1620
1621 fastify.setNotFoundHandler((req, reply) => {
1622 reply.code(404).send(new Error('kaboom'))
1623 })
1624
1625 fastify.get('/', function (req, reply) {
1626 reply.callNotFound()
1627 })
1628
1629 fastify.inject({
1630 url: '/',
1631 method: 'GET'
1632 }, (err, res) => {
1633 t.error(err)
1634 t.strictEqual(res.statusCode, 404)
1635 t.deepEqual(JSON.parse(res.payload), {
1636 error: 'Not Found',
1637 message: 'kaboom',
1638 statusCode: 404
1639 })
1640 })
1641})
1642
1643test('The custom error handler should be invoked after the custom not found handler', t => {
1644 t.plan(6)
1645
1646 const fastify = Fastify()
1647 const order = [1, 2]
1648
1649 fastify.setErrorHandler((err, req, reply) => {
1650 t.is(order.shift(), 2)
1651 t.type(err, Error)
1652 reply.send(err)
1653 })
1654
1655 fastify.setNotFoundHandler((req, reply) => {
1656 t.is(order.shift(), 1)
1657 reply.code(404).send(new Error('kaboom'))
1658 })
1659
1660 fastify.get('/', function (req, reply) {
1661 reply.callNotFound()
1662 })
1663
1664 fastify.inject({
1665 url: '/',
1666 method: 'GET'
1667 }, (err, res) => {
1668 t.error(err)
1669 t.strictEqual(res.statusCode, 404)
1670 t.deepEqual(JSON.parse(res.payload), {
1671 error: 'Not Found',
1672 message: 'kaboom',
1673 statusCode: 404
1674 })
1675 })
1676})
1677
1678test('If the custom not found handler does not use an Error, the custom error handler should not be called', t => {
1679 t.plan(3)
1680
1681 const fastify = Fastify()
1682
1683 fastify.setErrorHandler((_err, req, reply) => {
1684 t.fail('Should not be called')
1685 })
1686
1687 fastify.setNotFoundHandler((req, reply) => {
1688 reply.code(404).send('kaboom')
1689 })
1690
1691 fastify.get('/', function (req, reply) {
1692 reply.callNotFound()
1693 })
1694
1695 fastify.inject({
1696 url: '/',
1697 method: 'GET'
1698 }, (err, res) => {
1699 t.error(err)
1700 t.strictEqual(res.statusCode, 404)
1701 t.strictEqual(res.payload, 'kaboom')
1702 })
1703})
1704
1705test('preValidation option', t => {
1706 t.plan(3)
1707 const fastify = Fastify()
1708
1709 fastify.decorate('foo', true)
1710
1711 fastify.setNotFoundHandler({
1712 preValidation: function (req, reply, done) {
1713 t.true(this.foo)
1714 done()
1715 }
1716 }, function (req, reply) {
1717 reply.code(404).send(req.body)
1718 })
1719
1720 fastify.inject({
1721 method: 'POST',
1722 url: '/not-found',
1723 payload: { hello: 'world' }
1724 }, (err, res) => {
1725 t.error(err)
1726 var payload = JSON.parse(res.payload)
1727 t.deepEqual(payload, { hello: 'world' })
1728 })
1729})
1730
1731t.test('preValidation option could accept an array of functions', t => {
1732 t.plan(4)
1733 const fastify = Fastify()
1734
1735 fastify.setNotFoundHandler({
1736 preValidation: [
1737 (req, reply, done) => {
1738 t.ok('called')
1739 done()
1740 },
1741 (req, reply, done) => {
1742 t.ok('called')
1743 done()
1744 }
1745 ]
1746 }, (req, reply) => {
1747 reply.send(req.body)
1748 })
1749
1750 fastify.inject({
1751 method: 'POST',
1752 url: '/not-found',
1753 payload: { hello: 'world' }
1754 }, (err, res) => {
1755 t.error(err)
1756 var payload = JSON.parse(res.payload)
1757 t.deepEqual(payload, { hello: 'world' })
1758 })
1759})
1760
1761test('Should fail to invoke callNotFound inside a 404 handler', t => {
1762 t.plan(5)
1763
1764 let fastify = null
1765 const logStream = split(JSON.parse)
1766 try {
1767 fastify = Fastify({
1768 logger: {
1769 stream: logStream,
1770 level: 'warn'
1771 }
1772 })
1773 } catch (e) {
1774 t.fail()
1775 }
1776
1777 fastify.setNotFoundHandler((req, reply) => {
1778 reply.callNotFound()
1779 })
1780
1781 fastify.get('/', function (req, reply) {
1782 reply.callNotFound()
1783 })
1784
1785 logStream.once('data', line => {
1786 t.is(line.msg, 'Trying to send a NotFound error inside a 404 handler. Sending basic 404 response.')
1787 t.is(line.level, 40)
1788 })
1789
1790 fastify.inject({
1791 url: '/',
1792 method: 'GET'
1793 }, (err, res) => {
1794 t.error(err)
1795 t.is(res.statusCode, 404)
1796 t.is(res.payload, '404 Not Found')
1797 })
1798})
1799
1800test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
1801 t.test('Dyamic route', t => {
1802 t.plan(3)
1803 const fastify = Fastify()
1804 fastify.get('/hello/:id', () => t.fail('we should not be here'))
1805 fastify.inject({
1806 url: '/hello/%world',
1807 method: 'GET'
1808 }, (err, response) => {
1809 t.error(err)
1810 t.strictEqual(response.statusCode, 400)
1811 t.deepEqual(JSON.parse(response.payload), {
1812 error: 'Bad Request',
1813 message: "'%world' is not a valid url component",
1814 statusCode: 400
1815 })
1816 })
1817 })
1818
1819 t.test('Wildcard', t => {
1820 t.plan(3)
1821 const fastify = Fastify()
1822 fastify.get('*', () => t.fail('we should not be here'))
1823 fastify.inject({
1824 url: '/hello/%world',
1825 method: 'GET'
1826 }, (err, response) => {
1827 t.error(err)
1828 t.strictEqual(response.statusCode, 400)
1829 t.deepEqual(JSON.parse(response.payload), {
1830 error: 'Bad Request',
1831 message: "'/hello/%world' is not a valid url component",
1832 statusCode: 400
1833 })
1834 })
1835 })
1836
1837 t.end()
1838})