1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const fp = require('fastify-plugin')
|
6 | const httpErrors = require('http-errors')
|
7 | const sget = require('simple-get').concat
|
8 | const errors = require('http-errors')
|
9 | const split = require('split2')
|
10 | const Fastify = require('..')
|
11 |
|
12 | test('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 |
|
57 | test('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 |
|
147 | test('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 |
|
177 | test('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 |
|
309 | test('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 |
|
455 | test('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 |
|
526 | test('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 |
|
573 | test('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 |
|
624 | test('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 |
|
672 | test('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)
|
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 |
|
725 | test('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 |
|
805 | test('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 |
|
836 | test('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 |
|
876 | test('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 |
|
924 | test('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 |
|
946 | test('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 |
|
990 | test('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 |
|
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 |
|
1026 | test('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 |
|
1060 | test('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 |
|
1084 | test('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 |
|
1112 | test('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 |
|
1149 | test('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 |
|
1167 | test('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 |
|
1202 | test('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 |
|
1235 | test('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 |
|
1268 | test('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 |
|
1292 | test('preHandler option for setNotFoundHandler', t => {
|
1293 | t.plan(8)
|
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 | t.test('preHandler option should be called after preHandler hook', t => {
|
1320 | t.plan(2)
|
1321 | const fastify = Fastify()
|
1322 |
|
1323 | fastify.addHook('preHandler', (req, reply, next) => {
|
1324 | req.body.check = 'a'
|
1325 | next()
|
1326 | })
|
1327 |
|
1328 | fastify.setNotFoundHandler({
|
1329 | preHandler: (req, reply, done) => {
|
1330 | req.body.check += 'b'
|
1331 | done()
|
1332 | }
|
1333 | }, (req, reply) => {
|
1334 | reply.send(req.body)
|
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, { check: 'ab', hello: 'world' })
|
1345 | })
|
1346 | })
|
1347 |
|
1348 | t.test('preHandler option should be unique per prefix', t => {
|
1349 | t.plan(4)
|
1350 | const fastify = Fastify()
|
1351 |
|
1352 | fastify.setNotFoundHandler({
|
1353 | preHandler: (req, reply, done) => {
|
1354 | req.body.hello = 'earth'
|
1355 | done()
|
1356 | }
|
1357 | }, (req, reply) => {
|
1358 | reply.send(req.body)
|
1359 | })
|
1360 |
|
1361 | fastify.register(function (i, o, n) {
|
1362 | i.setNotFoundHandler((req, reply) => {
|
1363 | reply.send(req.body)
|
1364 | })
|
1365 |
|
1366 | n()
|
1367 | }, { prefix: '/no' })
|
1368 |
|
1369 | fastify.inject({
|
1370 | method: 'POST',
|
1371 | url: '/not-found',
|
1372 | payload: { hello: 'world' }
|
1373 | }, (err, res) => {
|
1374 | t.error(err)
|
1375 | var payload = JSON.parse(res.payload)
|
1376 | t.deepEqual(payload, { hello: 'earth' })
|
1377 | })
|
1378 |
|
1379 | fastify.inject({
|
1380 | method: 'POST',
|
1381 | url: '/no/not-found',
|
1382 | payload: { hello: 'world' }
|
1383 | }, (err, res) => {
|
1384 | t.error(err)
|
1385 | var payload = JSON.parse(res.payload)
|
1386 | t.deepEqual(payload, { hello: 'world' })
|
1387 | })
|
1388 | })
|
1389 |
|
1390 | t.test('preHandler option should handle errors', t => {
|
1391 | t.plan(3)
|
1392 | const fastify = Fastify()
|
1393 |
|
1394 | fastify.setNotFoundHandler({
|
1395 | preHandler: (req, reply, done) => {
|
1396 | done(new Error('kaboom'))
|
1397 | }
|
1398 | }, (req, reply) => {
|
1399 | reply.send(req.body)
|
1400 | })
|
1401 |
|
1402 | fastify.inject({
|
1403 | method: 'POST',
|
1404 | url: '/not-found',
|
1405 | payload: { hello: 'world' }
|
1406 | }, (err, res) => {
|
1407 | t.error(err)
|
1408 | var payload = JSON.parse(res.payload)
|
1409 | t.equal(res.statusCode, 500)
|
1410 | t.deepEqual(payload, {
|
1411 | message: 'kaboom',
|
1412 | error: 'Internal Server Error',
|
1413 | statusCode: 500
|
1414 | })
|
1415 | })
|
1416 | })
|
1417 |
|
1418 | t.test('preHandler option should handle errors with custom status code', t => {
|
1419 | t.plan(3)
|
1420 | const fastify = Fastify()
|
1421 |
|
1422 | fastify.setNotFoundHandler({
|
1423 | preHandler: (req, reply, done) => {
|
1424 | reply.code(401)
|
1425 | done(new Error('go away'))
|
1426 | }
|
1427 | }, (req, reply) => {
|
1428 | reply.send(req.body)
|
1429 | })
|
1430 |
|
1431 | fastify.inject({
|
1432 | method: 'POST',
|
1433 | url: '/not-found',
|
1434 | payload: { hello: 'world' }
|
1435 | }, (err, res) => {
|
1436 | t.error(err)
|
1437 | var payload = JSON.parse(res.payload)
|
1438 | t.equal(res.statusCode, 401)
|
1439 | t.deepEqual(payload, {
|
1440 | message: 'go away',
|
1441 | error: 'Unauthorized',
|
1442 | statusCode: 401
|
1443 | })
|
1444 | })
|
1445 | })
|
1446 |
|
1447 | t.test('preHandler option could accept an array of functions', t => {
|
1448 | t.plan(2)
|
1449 | const fastify = Fastify()
|
1450 |
|
1451 | fastify.setNotFoundHandler({
|
1452 | preHandler: [
|
1453 | (req, reply, done) => {
|
1454 | req.body.preHandler = 'a'
|
1455 | done()
|
1456 | },
|
1457 | (req, reply, done) => {
|
1458 | req.body.preHandler += 'b'
|
1459 | done()
|
1460 | }
|
1461 | ]
|
1462 | }, (req, reply) => {
|
1463 | reply.send(req.body)
|
1464 | })
|
1465 |
|
1466 | fastify.inject({
|
1467 | method: 'POST',
|
1468 | url: '/not-found',
|
1469 | payload: { hello: 'world' }
|
1470 | }, (err, res) => {
|
1471 | t.error(err)
|
1472 | var payload = JSON.parse(res.payload)
|
1473 | t.deepEqual(payload, { preHandler: 'ab', hello: 'world' })
|
1474 | })
|
1475 | })
|
1476 |
|
1477 | t.test('preHandler option does not interfere with preHandler', t => {
|
1478 | t.plan(4)
|
1479 | const fastify = Fastify()
|
1480 |
|
1481 | fastify.addHook('preHandler', (req, reply, next) => {
|
1482 | req.body.check = 'a'
|
1483 | next()
|
1484 | })
|
1485 |
|
1486 | fastify.setNotFoundHandler({
|
1487 | preHandler: (req, reply, done) => {
|
1488 | req.body.check += 'b'
|
1489 | done()
|
1490 | }
|
1491 | }, (req, reply) => {
|
1492 | reply.send(req.body)
|
1493 | })
|
1494 |
|
1495 | fastify.register(function (i, o, n) {
|
1496 | i.setNotFoundHandler((req, reply) => {
|
1497 | reply.send(req.body)
|
1498 | })
|
1499 |
|
1500 | n()
|
1501 | }, { prefix: '/no' })
|
1502 |
|
1503 | fastify.inject({
|
1504 | method: 'post',
|
1505 | url: '/not-found',
|
1506 | payload: { hello: 'world' }
|
1507 | }, (err, res) => {
|
1508 | t.error(err)
|
1509 | var payload = JSON.parse(res.payload)
|
1510 | t.deepEqual(payload, { check: 'ab', hello: 'world' })
|
1511 | })
|
1512 |
|
1513 | fastify.inject({
|
1514 | method: 'post',
|
1515 | url: '/no/not-found',
|
1516 | payload: { hello: 'world' }
|
1517 | }, (err, res) => {
|
1518 | t.error(err)
|
1519 | var payload = JSON.parse(res.payload)
|
1520 | t.deepEqual(payload, { check: 'a', hello: 'world' })
|
1521 | })
|
1522 | })
|
1523 |
|
1524 | t.test('preHandler option should keep the context', t => {
|
1525 | t.plan(3)
|
1526 | const fastify = Fastify()
|
1527 |
|
1528 | fastify.decorate('foo', 42)
|
1529 |
|
1530 | fastify.setNotFoundHandler({
|
1531 | preHandler: function (req, reply, done) {
|
1532 | t.strictEqual(this.foo, 42)
|
1533 | this.foo += 1
|
1534 | req.body.foo = this.foo
|
1535 | done()
|
1536 | }
|
1537 | }, (req, reply) => {
|
1538 | reply.send(req.body)
|
1539 | })
|
1540 |
|
1541 | fastify.inject({
|
1542 | method: 'POST',
|
1543 | url: '/not-found',
|
1544 | payload: { hello: 'world' }
|
1545 | }, (err, res) => {
|
1546 | t.error(err)
|
1547 | var payload = JSON.parse(res.payload)
|
1548 | t.deepEqual(payload, { foo: 43, hello: 'world' })
|
1549 | })
|
1550 | })
|
1551 | })
|
1552 |
|
1553 | test('reply.notFound invoked the notFound handler', t => {
|
1554 | t.plan(3)
|
1555 |
|
1556 | const fastify = Fastify()
|
1557 |
|
1558 | fastify.setNotFoundHandler((req, reply) => {
|
1559 | reply.code(404).send(new Error('kaboom'))
|
1560 | })
|
1561 |
|
1562 | fastify.get('/', function (req, reply) {
|
1563 | reply.callNotFound()
|
1564 | })
|
1565 |
|
1566 | fastify.inject({
|
1567 | url: '/',
|
1568 | method: 'GET'
|
1569 | }, (err, res) => {
|
1570 | t.error(err)
|
1571 | t.strictEqual(res.statusCode, 404)
|
1572 | t.deepEqual(JSON.parse(res.payload), {
|
1573 | error: 'Not Found',
|
1574 | message: 'kaboom',
|
1575 | statusCode: 404
|
1576 | })
|
1577 | })
|
1578 | })
|
1579 |
|
1580 | test('The custom error handler should be invoked after the custom not found handler', t => {
|
1581 | t.plan(6)
|
1582 |
|
1583 | const fastify = Fastify()
|
1584 | const order = [1, 2]
|
1585 |
|
1586 | fastify.setErrorHandler((err, req, reply) => {
|
1587 | t.is(order.shift(), 2)
|
1588 | t.type(err, Error)
|
1589 | reply.send(err)
|
1590 | })
|
1591 |
|
1592 | fastify.setNotFoundHandler((req, reply) => {
|
1593 | t.is(order.shift(), 1)
|
1594 | reply.code(404).send(new Error('kaboom'))
|
1595 | })
|
1596 |
|
1597 | fastify.get('/', function (req, reply) {
|
1598 | reply.callNotFound()
|
1599 | })
|
1600 |
|
1601 | fastify.inject({
|
1602 | url: '/',
|
1603 | method: 'GET'
|
1604 | }, (err, res) => {
|
1605 | t.error(err)
|
1606 | t.strictEqual(res.statusCode, 404)
|
1607 | t.deepEqual(JSON.parse(res.payload), {
|
1608 | error: 'Not Found',
|
1609 | message: 'kaboom',
|
1610 | statusCode: 404
|
1611 | })
|
1612 | })
|
1613 | })
|
1614 |
|
1615 | test('If the custom not found handler does not use an Error, the custom error handler should not be called', t => {
|
1616 | t.plan(3)
|
1617 |
|
1618 | const fastify = Fastify()
|
1619 |
|
1620 | fastify.setErrorHandler((_err, req, reply) => {
|
1621 | t.fail('Should not be called')
|
1622 | })
|
1623 |
|
1624 | fastify.setNotFoundHandler((req, reply) => {
|
1625 | reply.code(404).send('kaboom')
|
1626 | })
|
1627 |
|
1628 | fastify.get('/', function (req, reply) {
|
1629 | reply.callNotFound()
|
1630 | })
|
1631 |
|
1632 | fastify.inject({
|
1633 | url: '/',
|
1634 | method: 'GET'
|
1635 | }, (err, res) => {
|
1636 | t.error(err)
|
1637 | t.strictEqual(res.statusCode, 404)
|
1638 | t.strictEqual(res.payload, 'kaboom')
|
1639 | })
|
1640 | })
|
1641 |
|
1642 | test('preValidation option', t => {
|
1643 | t.plan(3)
|
1644 | const fastify = Fastify()
|
1645 |
|
1646 | fastify.decorate('foo', true)
|
1647 |
|
1648 | fastify.setNotFoundHandler({
|
1649 | preValidation: function (req, reply, done) {
|
1650 | t.true(this.foo)
|
1651 | done()
|
1652 | }
|
1653 | }, function (req, reply) {
|
1654 | reply.code(404).send(req.body)
|
1655 | })
|
1656 |
|
1657 | fastify.inject({
|
1658 | method: 'POST',
|
1659 | url: '/not-found',
|
1660 | payload: { hello: 'world' }
|
1661 | }, (err, res) => {
|
1662 | t.error(err)
|
1663 | var payload = JSON.parse(res.payload)
|
1664 | t.deepEqual(payload, { hello: 'world' })
|
1665 | })
|
1666 | })
|
1667 |
|
1668 | t.test('preValidation option could accept an array of functions', t => {
|
1669 | t.plan(4)
|
1670 | const fastify = Fastify()
|
1671 |
|
1672 | fastify.setNotFoundHandler({
|
1673 | preValidation: [
|
1674 | (req, reply, done) => {
|
1675 | t.ok('called')
|
1676 | done()
|
1677 | },
|
1678 | (req, reply, done) => {
|
1679 | t.ok('called')
|
1680 | done()
|
1681 | }
|
1682 | ]
|
1683 | }, (req, reply) => {
|
1684 | reply.send(req.body)
|
1685 | })
|
1686 |
|
1687 | fastify.inject({
|
1688 | method: 'POST',
|
1689 | url: '/not-found',
|
1690 | payload: { hello: 'world' }
|
1691 | }, (err, res) => {
|
1692 | t.error(err)
|
1693 | var payload = JSON.parse(res.payload)
|
1694 | t.deepEqual(payload, { hello: 'world' })
|
1695 | })
|
1696 | })
|
1697 |
|
1698 | test('Should fail to invoke callNotFound inside a 404 handler', t => {
|
1699 | t.plan(5)
|
1700 |
|
1701 | let fastify = null
|
1702 | const logStream = split(JSON.parse)
|
1703 | try {
|
1704 | fastify = Fastify({
|
1705 | logger: {
|
1706 | stream: logStream,
|
1707 | level: 'warn'
|
1708 | }
|
1709 | })
|
1710 | } catch (e) {
|
1711 | t.fail()
|
1712 | }
|
1713 |
|
1714 | fastify.setNotFoundHandler((req, reply) => {
|
1715 | reply.callNotFound()
|
1716 | })
|
1717 |
|
1718 | fastify.get('/', function (req, reply) {
|
1719 | reply.callNotFound()
|
1720 | })
|
1721 |
|
1722 | logStream.once('data', line => {
|
1723 | t.is(line.msg, 'Trying to send a NotFound error inside a 404 handler. Sending basic 404 response.')
|
1724 | t.is(line.level, 40)
|
1725 | })
|
1726 |
|
1727 | fastify.inject({
|
1728 | url: '/',
|
1729 | method: 'GET'
|
1730 | }, (err, res) => {
|
1731 | t.error(err)
|
1732 | t.is(res.statusCode, 404)
|
1733 | t.is(res.payload, '404 Not Found')
|
1734 | })
|
1735 | })
|