UNPKG

fastify

Version:

Fast and low overhead web framework, for Node.js

100 lines (83 loc) 2.17 kB
'use strict' const t = require('tap') const test = t.test const Fastify = require('../fastify') test('same route definition object on multiple prefixes', async t => { t.plan(2) const routeObject = { handler: () => { }, method: 'GET', url: '/simple' } const fastify = Fastify({ exposeHeadRoutes: false }) fastify.register(async function (f) { f.addHook('onRoute', (routeOptions) => { t.equal(routeOptions.url, '/v1/simple') }) f.route(routeObject) }, { prefix: '/v1' }) fastify.register(async function (f) { f.addHook('onRoute', (routeOptions) => { t.equal(routeOptions.url, '/v2/simple') }) f.route(routeObject) }, { prefix: '/v2' }) await fastify.ready() }) test('path can be specified in place of uri', t => { t.plan(3) const fastify = Fastify() fastify.route({ method: 'GET', path: '/path', handler: function (req, reply) { reply.send({ hello: 'world' }) } }) const reqOpts = { method: 'GET', url: '/path' } fastify.inject(reqOpts, (err, res) => { t.error(err) t.equal(res.statusCode, 200) t.same(JSON.parse(res.payload), { hello: 'world' }) }) }) test('invalid bodyLimit option - route', t => { t.plan(2) const fastify = Fastify() try { fastify.route({ bodyLimit: false, method: 'PUT', handler: () => null }) t.fail('bodyLimit must be an integer') } catch (err) { t.equal(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'") } try { fastify.post('/url', { bodyLimit: 10000.1 }, () => null) t.fail('bodyLimit must be an integer') } catch (err) { t.equal(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'") } }) test('handler function in options of shorthand route should works correctly', t => { t.plan(3) const fastify = Fastify() fastify.get('/foo', { handler: (req, reply) => { reply.send({ hello: 'world' }) } }) fastify.inject({ method: 'GET', url: '/foo' }, (err, res) => { t.error(err) t.equal(res.statusCode, 200) t.same(JSON.parse(res.payload), { hello: 'world' }) }) })