CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
route.2.test.js101 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('../fastify')5 6test('same route definition object on multiple prefixes', async t => {7  t.plan(2)8 9  const routeObject = {10    handler: () => { },11    method: 'GET',12    url: '/simple'13  }14 15  const fastify = Fastify({ exposeHeadRoutes: false })16 17  fastify.register(async function (f) {18    f.addHook('onRoute', (routeOptions) => {19      t.assert.strictEqual(routeOptions.url, '/v1/simple')20    })21    f.route(routeObject)22  }, { prefix: '/v1' })23  fastify.register(async function (f) {24    f.addHook('onRoute', (routeOptions) => {25      t.assert.strictEqual(routeOptions.url, '/v2/simple')26    })27    f.route(routeObject)28  }, { prefix: '/v2' })29 30  await fastify.ready()31})32 33test('path can be specified in place of uri', (t, done) => {34  t.plan(3)35  const fastify = Fastify()36 37  fastify.route({38    method: 'GET',39    path: '/path',40    handler: function (req, reply) {41      reply.send({ hello: 'world' })42    }43  })44 45  const reqOpts = {46    method: 'GET',47    url: '/path'48  }49 50  fastify.inject(reqOpts, (err, res) => {51    t.assert.ifError(err)52    t.assert.strictEqual(res.statusCode, 200)53    t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })54    done()55  })56})57 58test('invalid bodyLimit option - route', t => {59  t.plan(2)60  const fastify = Fastify()61 62  try {63    fastify.route({64      bodyLimit: false,65      method: 'PUT',66      handler: () => null67    })68    t.assert.fail('bodyLimit must be an integer')69  } catch (err) {70    t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")71  }72 73  try {74    fastify.post('/url', { bodyLimit: 10000.1 }, () => null)75    t.assert.fail('bodyLimit must be an integer')76  } catch (err) {77    t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")78  }79})80 81test('handler function in options of shorthand route should works correctly', (t, done) => {82  t.plan(3)83 84  const fastify = Fastify()85  fastify.get('/foo', {86    handler: (req, reply) => {87      reply.send({ hello: 'world' })88    }89  })90 91  fastify.inject({92    method: 'GET',93    url: '/foo'94  }, (err, res) => {95    t.assert.ifError(err)96    t.assert.strictEqual(res.statusCode, 200)97    t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })98    done()99  })100})101