strong-tie/inbound-calls
0
1'use strict'2 3const { test, describe } = require('node:test')4const Fastify = require('../fastify')5 6const fastify = Fastify()7 8describe('hasRoute', async t => {9 test('hasRoute - invalid options', t => {10 t.plan(3)11 12 t.assert.strictEqual(fastify.hasRoute({ }), false)13 14 t.assert.strictEqual(fastify.hasRoute({ method: 'GET' }), false)15 16 t.assert.strictEqual(fastify.hasRoute({ constraints: [] }), false)17 })18 19 test('hasRoute - primitive method', t => {20 t.plan(2)21 fastify.route({22 method: 'GET',23 url: '/',24 handler: function (req, reply) {25 reply.send({ hello: 'world' })26 }27 })28 29 t.assert.strictEqual(fastify.hasRoute({30 method: 'GET',31 url: '/'32 }), true)33 34 t.assert.strictEqual(fastify.hasRoute({35 method: 'POST',36 url: '/'37 }), false)38 })39 40 test('hasRoute - with constraints', t => {41 t.plan(2)42 fastify.route({43 method: 'GET',44 url: '/',45 constraints: { version: '1.2.0' },46 handler: (req, reply) => {47 reply.send({ hello: 'world' })48 }49 })50 51 t.assert.strictEqual(fastify.hasRoute({52 method: 'GET',53 url: '/',54 constraints: { version: '1.2.0' }55 }), true)56 57 t.assert.strictEqual(fastify.hasRoute({58 method: 'GET',59 url: '/',60 constraints: { version: '1.3.0' }61 }), false)62 })63 64 test('hasRoute - parametric route regexp with constraints', t => {65 t.plan(1)66 // parametric with regexp67 fastify.get('/example/:file(^\\d+).png', function (request, reply) { })68 69 t.assert.strictEqual(fastify.hasRoute({70 method: 'GET',71 url: '/example/:file(^\\d+).png'72 }), true)73 })74 75 test('hasRoute - finds a route even if method is not uppercased', t => {76 t.plan(1)77 fastify.route({78 method: 'GET',79 url: '/equal',80 handler: function (req, reply) {81 reply.send({ hello: 'world' })82 }83 })84 85 t.assert.strictEqual(fastify.hasRoute({86 method: 'get',87 url: '/equal'88 }), true)89 })90})91 