strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const Fastify = require('..')6 7test('Prefix options should add a prefix for all the routes inside a register / 1', t => {8 t.plan(6)9 const fastify = Fastify()10 11 fastify.get('/first', (req, reply) => {12 reply.send({ route: '/first' })13 })14 15 fastify.register(function (fastify, opts, done) {16 fastify.get('/first', (req, reply) => {17 reply.send({ route: '/v1/first' })18 })19 20 fastify.register(function (fastify, opts, done) {21 fastify.get('/first', (req, reply) => {22 reply.send({ route: '/v1/v2/first' })23 })24 done()25 }, { prefix: '/v2' })26 27 done()28 }, { prefix: '/v1' })29 30 fastify.inject({31 method: 'GET',32 url: '/first'33 }, (err, res) => {34 t.error(err)35 t.same(JSON.parse(res.payload), { route: '/first' })36 })37 38 fastify.inject({39 method: 'GET',40 url: '/v1/first'41 }, (err, res) => {42 t.error(err)43 t.same(JSON.parse(res.payload), { route: '/v1/first' })44 })45 46 fastify.inject({47 method: 'GET',48 url: '/v1/v2/first'49 }, (err, res) => {50 t.error(err)51 t.same(JSON.parse(res.payload), { route: '/v1/v2/first' })52 })53})54 55test('Prefix options should add a prefix for all the routes inside a register / 2', t => {56 t.plan(4)57 const fastify = Fastify()58 59 fastify.register(function (fastify, opts, done) {60 fastify.get('/first', (req, reply) => {61 reply.send({ route: '/v1/first' })62 })63 64 fastify.get('/second', (req, reply) => {65 reply.send({ route: '/v1/second' })66 })67 done()68 }, { prefix: '/v1' })69 70 fastify.inject({71 method: 'GET',72 url: '/v1/first'73 }, (err, res) => {74 t.error(err)75 t.same(JSON.parse(res.payload), { route: '/v1/first' })76 })77 78 fastify.inject({79 method: 'GET',80 url: '/v1/second'81 }, (err, res) => {82 t.error(err)83 t.same(JSON.parse(res.payload), { route: '/v1/second' })84 })85})86 87test('Prefix options should add a prefix for all the chained routes inside a register / 3', t => {88 t.plan(4)89 90 const fastify = Fastify()91 92 fastify.register(function (fastify, opts, done) {93 fastify94 .get('/first', (req, reply) => {95 reply.send({ route: '/v1/first' })96 })97 .get('/second', (req, reply) => {98 reply.send({ route: '/v1/second' })99 })100 done()101 }, { prefix: '/v1' })102 103 fastify.inject({104 method: 'GET',105 url: '/v1/first'106 }, (err, res) => {107 t.error(err)108 t.same(JSON.parse(res.payload), { route: '/v1/first' })109 })110 111 fastify.inject({112 method: 'GET',113 url: '/v1/second'114 }, (err, res) => {115 t.error(err)116 t.same(JSON.parse(res.payload), { route: '/v1/second' })117 })118})119 120test('Prefix should support parameters as well', t => {121 t.plan(2)122 const fastify = Fastify()123 124 fastify.register(function (fastify, opts, done) {125 fastify.get('/hello', (req, reply) => {126 reply.send({ id: req.params.id })127 })128 done()129 }, { prefix: '/v1/:id' })130 131 fastify.inject({132 method: 'GET',133 url: '/v1/param/hello'134 }, (err, res) => {135 t.error(err)136 t.same(JSON.parse(res.payload), { id: 'param' })137 })138})139 140test('Prefix should support /', t => {141 t.plan(2)142 const fastify = Fastify()143 144 fastify.register(function (fastify, opts, done) {145 fastify.get('/', (req, reply) => {146 reply.send({ hello: 'world' })147 })148 done()149 }, { prefix: '/v1' })150 151 fastify.inject({152 method: 'GET',153 url: '/v1'154 }, (err, res) => {155 t.error(err)156 t.same(JSON.parse(res.payload), { hello: 'world' })157 })158})159 160test('Prefix without /', t => {161 t.plan(2)162 const fastify = Fastify()163 164 fastify.register(function (fastify, opts, done) {165 fastify.get('/', (req, reply) => {166 reply.send({ hello: 'world' })167 })168 done()169 }, { prefix: 'v1' })170 171 fastify.inject({172 method: 'GET',173 url: '/v1'174 }, (err, res) => {175 t.error(err)176 t.same(JSON.parse(res.payload), { hello: 'world' })177 })178})179 180test('Prefix with trailing /', t => {181 t.plan(6)182 const fastify = Fastify()183 184 fastify.register(function (fastify, opts, done) {185 fastify.get('/route1', (req, reply) => {186 reply.send({ hello: 'world1' })187 })188 fastify.get('route2', (req, reply) => {189 reply.send({ hello: 'world2' })190 })191 192 fastify.register(function (fastify, opts, done) {193 fastify.get('/route3', (req, reply) => {194 reply.send({ hello: 'world3' })195 })196 done()197 }, { prefix: '/inner/' })198 199 done()200 }, { prefix: '/v1/' })201 202 fastify.inject({203 method: 'GET',204 url: '/v1/route1'205 }, (err, res) => {206 t.error(err)207 t.same(JSON.parse(res.payload), { hello: 'world1' })208 })209 210 fastify.inject({211 method: 'GET',212 url: '/v1/route2'213 }, (err, res) => {214 t.error(err)215 t.same(JSON.parse(res.payload), { hello: 'world2' })216 })217 218 fastify.inject({219 method: 'GET',220 url: '/v1/inner/route3'221 }, (err, res) => {222 t.error(err)223 t.same(JSON.parse(res.payload), { hello: 'world3' })224 })225})226 227test('Prefix works multiple levels deep', t => {228 t.plan(2)229 const fastify = Fastify()230 231 fastify.register(function (fastify, opts, done) {232 fastify.register(function (fastify, opts, done) {233 fastify.register(function (fastify, opts, done) {234 fastify.register(function (fastify, opts, done) {235 fastify.get('/', (req, reply) => {236 reply.send({ hello: 'world' })237 })238 done()239 }, { prefix: '/v3' })240 done()241 }) // No prefix on this level242 done()243 }, { prefix: 'v2' })244 done()245 }, { prefix: '/v1' })246 247 fastify.inject({248 method: 'GET',249 url: '/v1/v2/v3'250 }, (err, res) => {251 t.error(err)252 t.same(JSON.parse(res.payload), { hello: 'world' })253 })254})255 256test('Different register - encapsulation check', t => {257 t.plan(4)258 const fastify = Fastify()259 260 fastify.get('/first', (req, reply) => {261 reply.send({ route: '/first' })262 })263 264 fastify.register(function (instance, opts, done) {265 instance.register(function (f, opts, done) {266 f.get('/', (req, reply) => {267 reply.send({ route: '/v1/v2' })268 })269 done()270 }, { prefix: '/v2' })271 done()272 }, { prefix: '/v1' })273 274 fastify.register(function (instance, opts, done) {275 instance.register(function (f, opts, done) {276 f.get('/', (req, reply) => {277 reply.send({ route: '/v3/v4' })278 })279 done()280 }, { prefix: '/v4' })281 done()282 }, { prefix: '/v3' })283 284 fastify.inject({285 method: 'GET',286 url: '/v1/v2'287 }, (err, res) => {288 t.error(err)289 t.same(JSON.parse(res.payload), { route: '/v1/v2' })290 })291 292 fastify.inject({293 method: 'GET',294 url: '/v3/v4'295 }, (err, res) => {296 t.error(err)297 t.same(JSON.parse(res.payload), { route: '/v3/v4' })298 })299})300 301test('Can retrieve prefix within encapsulated instances', t => {302 t.plan(4)303 const fastify = Fastify()304 305 fastify.register(function (instance, opts, done) {306 instance.get('/one', function (req, reply) {307 reply.send(instance.prefix)308 })309 310 instance.register(function (instance, opts, done) {311 instance.get('/two', function (req, reply) {312 reply.send(instance.prefix)313 })314 done()315 }, { prefix: '/v2' })316 317 done()318 }, { prefix: '/v1' })319 320 fastify.inject({321 method: 'GET',322 url: '/v1/one'323 }, (err, res) => {324 t.error(err)325 t.equal(res.payload, '/v1')326 })327 328 fastify.inject({329 method: 'GET',330 url: '/v1/v2/two'331 }, (err, res) => {332 t.error(err)333 t.equal(res.payload, '/v1/v2')334 })335})336 337test('matches both /prefix and /prefix/ with a / route', t => {338 t.plan(4)339 const fastify = Fastify()340 341 fastify.register(function (fastify, opts, done) {342 fastify.get('/', (req, reply) => {343 reply.send({ hello: 'world' })344 })345 346 done()347 }, { prefix: '/prefix' })348 349 fastify.inject({350 method: 'GET',351 url: '/prefix'352 }, (err, res) => {353 t.error(err)354 t.same(JSON.parse(res.payload), { hello: 'world' })355 })356 357 fastify.inject({358 method: 'GET',359 url: '/prefix/'360 }, (err, res) => {361 t.error(err)362 t.same(JSON.parse(res.payload), { hello: 'world' })363 })364})365 366test('prefix "/prefix/" does not match "/prefix" with a / route', t => {367 t.plan(4)368 const fastify = Fastify()369 370 fastify.register(function (fastify, opts, done) {371 fastify.get('/', (req, reply) => {372 reply.send({ hello: 'world' })373 })374 375 done()376 }, { prefix: '/prefix/' })377 378 fastify.inject({379 method: 'GET',380 url: '/prefix'381 }, (err, res) => {382 t.error(err)383 t.equal(res.statusCode, 404)384 })385 386 fastify.inject({387 method: 'GET',388 url: '/prefix/'389 }, (err, res) => {390 t.error(err)391 t.same(JSON.parse(res.payload), { hello: 'world' })392 })393})394 395test('matches both /prefix and /prefix/ with a / route - ignoreTrailingSlash: true', t => {396 t.plan(4)397 const fastify = Fastify({398 ignoreTrailingSlash: true399 })400 401 fastify.register(function (fastify, opts, done) {402 fastify.get('/', (req, reply) => {403 reply.send({ hello: 'world' })404 })405 406 done()407 }, { prefix: '/prefix' })408 409 fastify.inject({410 method: 'GET',411 url: '/prefix'412 }, (err, res) => {413 t.error(err)414 t.same(JSON.parse(res.payload), { hello: 'world' })415 })416 417 fastify.inject({418 method: 'GET',419 url: '/prefix/'420 }, (err, res) => {421 t.error(err)422 t.same(JSON.parse(res.payload), { hello: 'world' })423 })424})425 426test('matches both /prefix and /prefix/ with a / route - ignoreDuplicateSlashes: true', t => {427 t.plan(4)428 const fastify = Fastify({429 ignoreDuplicateSlashes: true430 })431 432 fastify.register(function (fastify, opts, done) {433 fastify.get('/', (req, reply) => {434 reply.send({ hello: 'world' })435 })436 437 done()438 }, { prefix: '/prefix' })439 440 fastify.inject({441 method: 'GET',442 url: '/prefix'443 }, (err, res) => {444 t.error(err)445 t.same(JSON.parse(res.payload), { hello: 'world' })446 })447 448 fastify.inject({449 method: 'GET',450 url: '/prefix/'451 }, (err, res) => {452 t.error(err)453 t.same(JSON.parse(res.payload), { hello: 'world' })454 })455})456 457test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreTrailingSlash: false', t => {458 t.plan(4)459 const fastify = Fastify({460 ignoreTrailingSlash: false461 })462 463 fastify.register(function (fastify, opts, done) {464 fastify.route({465 method: 'GET',466 url: '/',467 prefixTrailingSlash: 'both',468 handler: (req, reply) => {469 reply.send({ hello: 'world' })470 }471 })472 473 done()474 }, { prefix: '/prefix' })475 476 fastify.inject({477 method: 'GET',478 url: '/prefix'479 }, (err, res) => {480 t.error(err)481 t.same(JSON.parse(res.payload), { hello: 'world' })482 })483 484 fastify.inject({485 method: 'GET',486 url: '/prefix/'487 }, (err, res) => {488 t.error(err)489 t.same(JSON.parse(res.payload), { hello: 'world' })490 })491})492 493test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreDuplicateSlashes: false', t => {494 t.plan(4)495 const fastify = Fastify({496 ignoreDuplicateSlashes: false497 })498 499 fastify.register(function (fastify, opts, done) {500 fastify.route({501 method: 'GET',502 url: '/',503 prefixTrailingSlash: 'both',504 handler: (req, reply) => {505 reply.send({ hello: 'world' })506 }507 })508 509 done()510 }, { prefix: '/prefix' })511 512 fastify.inject({513 method: 'GET',514 url: '/prefix'515 }, (err, res) => {516 t.error(err)517 t.same(JSON.parse(res.payload), { hello: 'world' })518 })519 520 fastify.inject({521 method: 'GET',522 url: '/prefix/'523 }, (err, res) => {524 t.error(err)525 t.same(JSON.parse(res.payload), { hello: 'world' })526 })527})528 529test('matches both /prefix and /prefix/ with a / route - ignoreTrailingSlash: true, ignoreDuplicateSlashes: true', t => {530 t.plan(4)531 const fastify = Fastify({532 ignoreTrailingSlash: true,533 ignoreDuplicateSlashes: true534 })535 536 fastify.register(function (fastify, opts, done) {537 fastify.get('/', (req, reply) => {538 reply.send({ hello: 'world' })539 })540 541 done()542 }, { prefix: '/prefix' })543 544 fastify.inject({545 method: 'GET',546 url: '/prefix'547 }, (err, res) => {548 t.error(err)549 t.same(JSON.parse(res.payload), { hello: 'world' })550 })551 552 fastify.inject({553 method: 'GET',554 url: '/prefix/'555 }, (err, res) => {556 t.error(err)557 t.same(JSON.parse(res.payload), { hello: 'world' })558 })559})560 561test('matches both /prefix and /prefix/ with a / route - ignoreTrailingSlash: true, ignoreDuplicateSlashes: false', t => {562 t.plan(4)563 const fastify = Fastify({564 ignoreTrailingSlash: true,565 ignoreDuplicateSlashes: false566 })567 568 fastify.register(function (fastify, opts, done) {569 fastify.get('/', (req, reply) => {570 reply.send({ hello: 'world' })571 })572 573 done()574 }, { prefix: '/prefix' })575 576 fastify.inject({577 method: 'GET',578 url: '/prefix'579 }, (err, res) => {580 t.error(err)581 t.same(JSON.parse(res.payload), { hello: 'world' })582 })583 584 fastify.inject({585 method: 'GET',586 url: '/prefix/'587 }, (err, res) => {588 t.error(err)589 t.same(JSON.parse(res.payload), { hello: 'world' })590 })591})592 593test('returns 404 status code with /prefix/ and / route - prefixTrailingSlash: "both" (default), ignoreTrailingSlash: true', t => {594 t.plan(2)595 const fastify = Fastify({596 ignoreTrailingSlash: true597 })598 599 fastify.register(function (fastify, opts, done) {600 fastify.route({601 method: 'GET',602 url: '/',603 handler: (req, reply) => {604 reply.send({ hello: 'world' })605 }606 })607 608 done()609 }, { prefix: '/prefix/' })610 611 fastify.inject({612 method: 'GET',613 url: '/prefix//'614 }, (err, res) => {615 t.error(err)616 t.same(JSON.parse(res.payload), {617 error: 'Not Found',618 message: 'Route GET:/prefix// not found',619 statusCode: 404620 })621 })622})623 624test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreDuplicateSlashes: true', t => {625 t.plan(2)626 const fastify = Fastify({627 ignoreDuplicateSlashes: true628 })629 630 fastify.register(function (fastify, opts, done) {631 fastify.route({632 method: 'GET',633 url: '/',634 handler: (req, reply) => {635 reply.send({ hello: 'world' })636 }637 })638 639 done()640 }, { prefix: '/prefix/' })641 642 fastify.inject({643 method: 'GET',644 url: '/prefix//'645 }, (err, res) => {646 t.error(err)647 t.same(JSON.parse(res.payload), { hello: 'world' })648 })649})650 651test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreTrailingSlash: true, ignoreDuplicateSlashes: true', t => {652 t.plan(2)653 const fastify = Fastify({654 ignoreTrailingSlash: true,655 ignoreDuplicateSlashes: true656 })657 658 fastify.register(function (fastify, opts, done) {659 fastify.route({660 method: 'GET',661 url: '/',662 handler: (req, reply) => {663 reply.send({ hello: 'world' })664 }665 })666 667 done()668 }, { prefix: '/prefix/' })669 670 fastify.inject({671 method: 'GET',672 url: '/prefix//'673 }, (err, res) => {674 t.error(err)675 t.same(JSON.parse(res.payload), { hello: 'world' })676 })677})678 679test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreDuplicateSlashes: true', t => {680 t.plan(2)681 const fastify = Fastify({682 ignoreTrailingSlash: true,683 ignoreDuplicateSlashes: true684 })685 686 fastify.register(function (fastify, opts, done) {687 fastify.route({688 method: 'GET',689 url: '/',690 handler: (req, reply) => {691 reply.send({ hello: 'world' })692 }693 })694 695 done()696 }, { prefix: '/prefix/' })697 698 fastify.inject({699 method: 'GET',700 url: '/prefix//'701 }, (err, res) => {702 t.error(err)703 t.same(JSON.parse(res.payload), { hello: 'world' })704 })705})706 707test('matches only /prefix with a / route - prefixTrailingSlash: "no-slash", ignoreTrailingSlash: false', t => {708 t.plan(4)709 const fastify = Fastify({710 ignoreTrailingSlash: false711 })712 713 fastify.register(function (fastify, opts, done) {714 fastify.route({715 method: 'GET',716 url: '/',717 prefixTrailingSlash: 'no-slash',718 handler: (req, reply) => {719 reply.send({ hello: 'world' })720 }721 })722 723 done()724 }, { prefix: '/prefix' })725 726 fastify.inject({727 method: 'GET',728 url: '/prefix'729 }, (err, res) => {730 t.error(err)731 t.same(JSON.parse(res.payload), { hello: 'world' })732 })733 734 fastify.inject({735 method: 'GET',736 url: '/prefix/'737 }, (err, res) => {738 t.error(err)739 t.equal(JSON.parse(res.payload).statusCode, 404)740 })741})742 743test('matches only /prefix with a / route - prefixTrailingSlash: "no-slash", ignoreDuplicateSlashes: false', t => {744 t.plan(4)745 const fastify = Fastify({746 ignoreDuplicateSlashes: false747 })748 749 fastify.register(function (fastify, opts, done) {750 fastify.route({751 method: 'GET',752 url: '/',753 prefixTrailingSlash: 'no-slash',754 handler: (req, reply) => {755 reply.send({ hello: 'world' })756 }757 })758 759 done()760 }, { prefix: '/prefix' })761 762 fastify.inject({763 method: 'GET',764 url: '/prefix'765 }, (err, res) => {766 t.error(err)767 t.same(JSON.parse(res.payload), { hello: 'world' })768 })769 770 fastify.inject({771 method: 'GET',772 url: '/prefix/'773 }, (err, res) => {774 t.error(err)775 t.equal(JSON.parse(res.payload).statusCode, 404)776 })777})778 779test('matches only /prefix/ with a / route - prefixTrailingSlash: "slash", ignoreTrailingSlash: false', t => {780 t.plan(4)781 const fastify = Fastify({782 ignoreTrailingSlash: false783 })784 785 fastify.register(function (fastify, opts, done) {786 fastify.route({787 method: 'GET',788 url: '/',789 prefixTrailingSlash: 'slash',790 handler: (req, reply) => {791 reply.send({ hello: 'world' })792 }793 })794 795 done()796 }, { prefix: '/prefix' })797 798 fastify.inject({799 method: 'GET',800 url: '/prefix/'801 }, (err, res) => {802 t.error(err)803 t.same(JSON.parse(res.payload), { hello: 'world' })804 })805 806 fastify.inject({807 method: 'GET',808 url: '/prefix'809 }, (err, res) => {810 t.error(err)811 t.equal(JSON.parse(res.payload).statusCode, 404)812 })813})814 815test('calls onRoute only once when prefixing', async t => {816 t.plan(1)817 const fastify = Fastify({818 ignoreTrailingSlash: false,819 exposeHeadRoutes: false820 })821 822 let onRouteCalled = 0823 fastify.register(function (fastify, opts, next) {824 fastify.addHook('onRoute', () => {825 onRouteCalled++826 })827 828 fastify.route({829 method: 'GET',830 url: '/',831 prefixTrailingSlash: 'both',832 handler: (req, reply) => {833 reply.send({ hello: 'world' })834 }835 })836 837 next()838 }, { prefix: '/prefix' })839 840 await fastify.ready()841 842 t.same(onRouteCalled, 1)843})844 