CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
route.3.test.js214 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const joi = require('joi')5const Fastify = require('..')6 7test('does not mutate joi schemas', (t, done) => {8  t.plan(5)9 10  const fastify = Fastify()11  function validatorCompiler ({ schema, method, url, httpPart }) {12    // Needed to extract the params part,13    // without the JSON-schema encapsulation14    // that is automatically added by the short15    // form of params.16    schema = joi.object(schema.properties)17 18    return validateHttpData19 20    function validateHttpData (data) {21      return schema.validate(data)22    }23  }24 25  fastify.setValidatorCompiler(validatorCompiler)26 27  fastify.route({28    path: '/foo/:an_id',29    method: 'GET',30    schema: {31      params: { an_id: joi.number() }32    },33    handler (req, res) {34      t.assert.strictEqual(Object.keys(req.params).length, 1)35      t.assert.strictEqual(req.params.an_id, '42')36      res.send({ hello: 'world' })37    }38  })39 40  fastify.inject({41    method: 'GET',42    url: '/foo/42'43  }, (err, res) => {44    t.assert.ifError(err)45    t.assert.strictEqual(res.statusCode, 200)46    t.assert.deepStrictEqual(res.json(), { hello: 'world' })47    done()48  })49})50 51test('multiple routes with one schema', (t, done) => {52  t.plan(2)53 54  const fastify = Fastify()55 56  const schema = {57    query: {58      type: 'object',59      properties: {60        id: { type: 'number' }61      }62    }63  }64 65  fastify.route({66    schema,67    method: 'GET',68    path: '/first/:id',69    handler (req, res) {70      res.send({ hello: 'world' })71    }72  })73 74  fastify.route({75    schema,76    method: 'GET',77    path: '/second/:id',78    handler (req, res) {79      res.send({ hello: 'world' })80    }81  })82 83  fastify.ready(error => {84    t.assert.ifError(error)85    t.assert.deepStrictEqual(schema, schema)86    done()87  })88})89 90test('route error handler overrides default error handler', (t, done) => {91  t.plan(4)92 93  const fastify = Fastify()94 95  const customRouteErrorHandler = (error, request, reply) => {96    t.assert.strictEqual(error.message, 'Wrong Pot Error')97 98    reply.code(418).send({99      message: 'Make a brew',100      statusCode: 418,101      error: 'Wrong Pot Error'102    })103  }104 105  fastify.route({106    method: 'GET',107    path: '/coffee',108    handler: (req, res) => {109      res.send(new Error('Wrong Pot Error'))110    },111    errorHandler: customRouteErrorHandler112  })113 114  fastify.inject({115    method: 'GET',116    url: '/coffee'117  }, (error, res) => {118    t.assert.ifError(error)119    t.assert.strictEqual(res.statusCode, 418)120    t.assert.deepStrictEqual(res.json(), {121      message: 'Make a brew',122      statusCode: 418,123      error: 'Wrong Pot Error'124    })125    done()126  })127})128 129test('route error handler does not affect other routes', (t, done) => {130  t.plan(3)131 132  const fastify = Fastify()133 134  const customRouteErrorHandler = (error, request, reply) => {135    t.assert.strictEqual(error.message, 'Wrong Pot Error')136 137    reply.code(418).send({138      message: 'Make a brew',139      statusCode: 418,140      error: 'Wrong Pot Error'141    })142  }143 144  fastify.route({145    method: 'GET',146    path: '/coffee',147    handler: (req, res) => {148      res.send(new Error('Wrong Pot Error'))149    },150    errorHandler: customRouteErrorHandler151  })152 153  fastify.route({154    method: 'GET',155    path: '/tea',156    handler: (req, res) => {157      res.send(new Error('No tea today'))158    }159  })160 161  fastify.inject({162    method: 'GET',163    url: '/tea'164  }, (error, res) => {165    t.assert.ifError(error)166    t.assert.strictEqual(res.statusCode, 500)167    t.assert.deepStrictEqual(res.json(), {168      message: 'No tea today',169      statusCode: 500,170      error: 'Internal Server Error'171    })172    done()173  })174})175 176test('async error handler for a route', (t, done) => {177  t.plan(4)178 179  const fastify = Fastify()180 181  const customRouteErrorHandler = async (error, request, reply) => {182    t.assert.strictEqual(error.message, 'Delayed Pot Error')183    reply.code(418)184    return {185      message: 'Make a brew sometime later',186      statusCode: 418,187      error: 'Delayed Pot Error'188    }189  }190 191  fastify.route({192    method: 'GET',193    path: '/late-coffee',194    handler: (req, res) => {195      res.send(new Error('Delayed Pot Error'))196    },197    errorHandler: customRouteErrorHandler198  })199 200  fastify.inject({201    method: 'GET',202    url: '/late-coffee'203  }, (error, res) => {204    t.assert.ifError(error)205    t.assert.strictEqual(res.statusCode, 418)206    t.assert.deepStrictEqual(res.json(), {207      message: 'Make a brew sometime later',208      statusCode: 418,209      error: 'Delayed Pot Error'210    })211    done()212  })213})214