strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const sget = require('simple-get').concat5const Fastify = require('..')6const {7 FST_ERR_INSTANCE_ALREADY_LISTENING,8 FST_ERR_ROUTE_METHOD_INVALID9} = require('../lib/errors')10const { getServerUrl } = require('./helper')11 12test('route', async t => {13 t.plan(10)14 15 await t.test('route - get', (t, done) => {16 t.plan(4)17 18 const fastify = Fastify()19 t.assert.doesNotThrow(() =>20 fastify.route({21 method: 'GET',22 url: '/',23 schema: {24 response: {25 '2xx': {26 type: 'object',27 properties: {28 hello: {29 type: 'string'30 }31 }32 }33 }34 },35 handler: function (req, reply) {36 reply.send({ hello: 'world' })37 }38 })39 )40 41 fastify.listen({ port: 0 }, function (err) {42 if (err) t.assert.ifError(err)43 t.after(() => { fastify.close() })44 sget({45 method: 'GET',46 url: getServerUrl(fastify) + '/'47 }, (err, response, body) => {48 t.assert.ifError(err)49 t.assert.strictEqual(response.statusCode, 200)50 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })51 done()52 })53 })54 })55 56 await t.test('missing schema - route', (t, done) => {57 t.plan(4)58 59 const fastify = Fastify()60 t.assert.doesNotThrow(() =>61 fastify.route({62 method: 'GET',63 url: '/missing',64 handler: function (req, reply) {65 reply.send({ hello: 'world' })66 }67 })68 )69 70 fastify.listen({ port: 0 }, function (err) {71 if (err) t.assert.ifError(err)72 t.after(() => { fastify.close() })73 sget({74 method: 'GET',75 url: getServerUrl(fastify) + '/missing'76 }, (err, response, body) => {77 t.assert.ifError(err)78 t.assert.strictEqual(response.statusCode, 200)79 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })80 done()81 })82 })83 })84 85 await t.test('invalid handler attribute - route', t => {86 t.plan(1)87 88 const fastify = Fastify()89 t.assert.throws(() => fastify.get('/', { handler: 'not a function' }, () => { }))90 })91 92 await t.test('Add Multiple methods per route all uppercase', (t, done) => {93 t.plan(7)94 95 const fastify = Fastify()96 t.assert.doesNotThrow(() =>97 fastify.route({98 method: ['GET', 'DELETE'],99 url: '/multiple',100 handler: function (req, reply) {101 reply.send({ hello: 'world' })102 }103 }))104 105 fastify.listen({ port: 0 }, function (err) {106 if (err) t.assert.ifError(err)107 t.after(() => { fastify.close() })108 sget({109 method: 'GET',110 url: getServerUrl(fastify) + '/multiple'111 }, (err, response, body) => {112 t.assert.ifError(err)113 t.assert.strictEqual(response.statusCode, 200)114 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })115 })116 117 sget({118 method: 'DELETE',119 url: getServerUrl(fastify) + '/multiple'120 }, (err, response, body) => {121 t.assert.ifError(err)122 t.assert.strictEqual(response.statusCode, 200)123 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })124 done()125 })126 })127 })128 129 await t.test('Add Multiple methods per route all lowercase', (t, done) => {130 t.plan(7)131 132 const fastify = Fastify()133 t.assert.doesNotThrow(() =>134 fastify.route({135 method: ['get', 'delete'],136 url: '/multiple',137 handler: function (req, reply) {138 reply.send({ hello: 'world' })139 }140 }))141 142 fastify.listen({ port: 0 }, function (err) {143 if (err) t.assert.ifError(err)144 t.after(() => { fastify.close() })145 sget({146 method: 'GET',147 url: getServerUrl(fastify) + '/multiple'148 }, (err, response, body) => {149 t.assert.ifError(err)150 t.assert.strictEqual(response.statusCode, 200)151 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })152 })153 154 sget({155 method: 'DELETE',156 url: getServerUrl(fastify) + '/multiple'157 }, (err, response, body) => {158 t.assert.ifError(err)159 t.assert.strictEqual(response.statusCode, 200)160 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })161 done()162 })163 })164 })165 166 await t.test('Add Multiple methods per route mixed uppercase and lowercase', (t, done) => {167 t.plan(7)168 169 const fastify = Fastify()170 t.assert.doesNotThrow(() =>171 fastify.route({172 method: ['GET', 'delete'],173 url: '/multiple',174 handler: function (req, reply) {175 reply.send({ hello: 'world' })176 }177 }))178 179 fastify.listen({ port: 0 }, function (err) {180 if (err) t.assert.ifError(err)181 t.after(() => { fastify.close() })182 sget({183 method: 'GET',184 url: getServerUrl(fastify) + '/multiple'185 }, (err, response, body) => {186 t.assert.ifError(err)187 t.assert.strictEqual(response.statusCode, 200)188 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })189 })190 191 sget({192 method: 'DELETE',193 url: getServerUrl(fastify) + '/multiple'194 }, (err, response, body) => {195 t.assert.ifError(err)196 t.assert.strictEqual(response.statusCode, 200)197 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })198 done()199 })200 })201 })202 203 t.test('Add invalid Multiple methods per route', t => {204 t.plan(1)205 206 const fastify = Fastify()207 t.assert.throws(() =>208 fastify.route({209 method: ['GET', 1],210 url: '/invalid-method',211 handler: function (req, reply) {212 reply.send({ hello: 'world' })213 }214 }), new FST_ERR_ROUTE_METHOD_INVALID())215 })216 217 await t.test('Add method', t => {218 t.plan(1)219 220 const fastify = Fastify()221 t.assert.throws(() =>222 fastify.route({223 method: 1,224 url: '/invalid-method',225 handler: function (req, reply) {226 reply.send({ hello: 'world' })227 }228 }), new FST_ERR_ROUTE_METHOD_INVALID())229 })230 231 await t.test('Add additional multiple methods to existing route', (t, done) => {232 t.plan(7)233 234 const fastify = Fastify()235 t.assert.doesNotThrow(() => {236 fastify.get('/add-multiple', function (req, reply) {237 reply.send({ hello: 'Bob!' })238 })239 fastify.route({240 method: ['PUT', 'DELETE'],241 url: '/add-multiple',242 handler: function (req, reply) {243 reply.send({ hello: 'world' })244 }245 })246 })247 248 fastify.listen({ port: 0 }, function (err) {249 if (err) t.assert.ifError(err)250 t.after(() => { fastify.close() })251 sget({252 method: 'PUT',253 url: getServerUrl(fastify) + '/add-multiple'254 }, (err, response, body) => {255 t.assert.ifError(err)256 t.assert.strictEqual(response.statusCode, 200)257 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })258 })259 260 sget({261 method: 'DELETE',262 url: getServerUrl(fastify) + '/add-multiple'263 }, (err, response, body) => {264 t.assert.ifError(err)265 t.assert.strictEqual(response.statusCode, 200)266 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })267 done()268 })269 })270 })271 272 await t.test('cannot add another route after binding', (t, done) => {273 t.plan(1)274 275 const fastify = Fastify()276 277 fastify.listen({ port: 0 }, function (err) {278 if (err) t.assert.ifError(err)279 t.after(() => { fastify.close() })280 281 t.assert.throws(() => fastify.route({282 method: 'GET',283 url: '/another-get-route',284 handler: function (req, reply) {285 reply.send({ hello: 'world' })286 }287 }), new FST_ERR_INSTANCE_ALREADY_LISTENING('Cannot add route!'))288 289 done()290 })291 })292})293 294test('invalid schema - route', (t, done) => {295 t.plan(3)296 297 const fastify = Fastify()298 fastify.route({299 handler: () => { },300 method: 'GET',301 url: '/invalid',302 schema: {303 querystring: {304 id: 'string'305 }306 }307 })308 fastify.after(err => {309 t.assert.ok(!err, 'the error is throw on preReady')310 })311 fastify.ready(err => {312 t.assert.strictEqual(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')313 t.assert.match(err.message, /Failed building the validation schema for GET: \/invalid/)314 done()315 })316})317 