CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
route.4.test.js128 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5 6test('route error handler overrides global custom error handler', async t => {7  t.plan(3)8 9  const fastify = Fastify()10 11  const customGlobalErrorHandler = (error, request, reply) => {12    t.assert.ifError(error)13    reply.code(429).send({ message: 'Too much coffee' })14  }15 16  const customRouteErrorHandler = (error, request, reply) => {17    t.assert.strictEqual(error.message, 'Wrong Pot Error')18    reply.code(418).send({19      message: 'Make a brew',20      statusCode: 418,21      error: 'Wrong Pot Error'22    })23  }24 25  fastify.setErrorHandler(customGlobalErrorHandler)26 27  fastify.route({28    method: 'GET',29    path: '/more-coffee',30    handler: (req, res) => {31      res.send(new Error('Wrong Pot Error'))32    },33    errorHandler: customRouteErrorHandler34  })35 36  const res = await fastify.inject({37    method: 'GET',38    url: '/more-coffee'39  })40  t.assert.strictEqual(res.statusCode, 418)41  t.assert.deepStrictEqual(JSON.parse(res.payload), {42    message: 'Make a brew',43    statusCode: 418,44    error: 'Wrong Pot Error'45  })46})47 48test('throws when route with empty url', async t => {49  t.plan(1)50 51  const fastify = Fastify()52  try {53    fastify.route({54      method: 'GET',55      url: '',56      handler: (req, res) => {57        res.send('hi!')58      }59    })60  } catch (err) {61    t.assert.strictEqual(err.message, 'The path could not be empty')62  }63})64 65test('throws when route with empty url in shorthand declaration', async t => {66  t.plan(1)67 68  const fastify = Fastify()69  try {70    fastify.get(71      '',72      async function handler () { return {} }73    )74  } catch (err) {75    t.assert.strictEqual(err.message, 'The path could not be empty')76  }77})78 79test('throws when route-level error handler is not a function', t => {80  t.plan(1)81 82  const fastify = Fastify()83 84  try {85    fastify.route({86      method: 'GET',87      url: '/tea',88      handler: (req, res) => {89        res.send('hi!')90      },91      errorHandler: 'teapot'92    })93  } catch (err) {94    t.assert.strictEqual(err.message, 'Error Handler for GET:/tea route, if defined, must be a function')95  }96})97 98test('route child logger factory overrides default child logger factory', async t => {99  t.plan(2)100 101  const fastify = Fastify()102 103  const customRouteChildLogger = (logger, bindings, opts, req) => {104    const child = logger.child(bindings, opts)105    child.customLog = function (message) {106      t.assert.strictEqual(message, 'custom')107    }108    return child109  }110 111  fastify.route({112    method: 'GET',113    path: '/coffee',114    handler: (req, res) => {115      req.log.customLog('custom')116      res.send()117    },118    childLoggerFactory: customRouteChildLogger119  })120 121  const res = await fastify.inject({122    method: 'GET',123    url: '/coffee'124  })125 126  t.assert.strictEqual(res.statusCode, 200)127})128