CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
route.5.test.js212 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5 6test('route child logger factory does not affect other routes', async t => {7  t.plan(4)8 9  const fastify = Fastify()10 11  const customRouteChildLogger = (logger, bindings, opts, req) => {12    const child = logger.child(bindings, opts)13    child.customLog = function (message) {14      t.assert.strictEqual(message, 'custom')15    }16    return child17  }18 19  fastify.route({20    method: 'GET',21    path: '/coffee',22    handler: (req, res) => {23      req.log.customLog('custom')24      res.send()25    },26    childLoggerFactory: customRouteChildLogger27  })28 29  fastify.route({30    method: 'GET',31    path: '/tea',32    handler: (req, res) => {33      t.assert.ok(req.log.customLog instanceof Function)34      res.send()35    }36  })37 38  let res = await fastify.inject({39    method: 'GET',40    url: '/coffee'41  })42  t.assert.strictEqual(res.statusCode, 200)43 44  res = await fastify.inject({45    method: 'GET',46    url: '/tea'47  })48  t.assert.strictEqual(res.statusCode, 200)49})50test('route child logger factory overrides global custom error handler', async t => {51  t.plan(4)52 53  const fastify = Fastify()54 55  const customGlobalChildLogger = (logger, bindings, opts, req) => {56    const child = logger.child(bindings, opts)57    child.globalLog = function (message) {58      t.assert.strictEqual(message, 'global')59    }60    return child61  }62  const customRouteChildLogger = (logger, bindings, opts, req) => {63    const child = logger.child(bindings, opts)64    child.customLog = function (message) {65      t.assert.strictEqual(message, 'custom')66    }67    return child68  }69 70  fastify.setChildLoggerFactory(customGlobalChildLogger)71 72  fastify.route({73    method: 'GET',74    path: '/coffee',75    handler: (req, res) => {76      req.log.customLog('custom')77      res.send()78    },79    childLoggerFactory: customRouteChildLogger80  })81  fastify.route({82    method: 'GET',83    path: '/more-coffee',84    handler: (req, res) => {85      req.log.globalLog('global')86      res.send()87    }88  })89 90  let res = await fastify.inject({91    method: 'GET',92    url: '/coffee'93  })94  t.assert.strictEqual(res.statusCode, 200)95 96  res = await fastify.inject({97    method: 'GET',98    url: '/more-coffee'99  })100  t.assert.strictEqual(res.statusCode, 200)101})102 103test('Creates a HEAD route for each GET one (default)', async t => {104  t.plan(6)105 106  const fastify = Fastify()107 108  fastify.route({109    method: 'GET',110    path: '/more-coffee',111    handler: (req, reply) => {112      reply.send({ here: 'is coffee' })113    }114  })115 116  fastify.route({117    method: 'GET',118    path: '/some-light',119    handler: (req, reply) => {120      reply.send('Get some light!')121    }122  })123 124  let res = await fastify.inject({125    method: 'HEAD',126    url: '/more-coffee'127  })128  t.assert.strictEqual(res.statusCode, 200)129  t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')130  t.assert.deepStrictEqual(res.body, '')131 132  res = await fastify.inject({133    method: 'HEAD',134    url: '/some-light'135  })136  t.assert.strictEqual(res.statusCode, 200)137  t.assert.strictEqual(res.headers['content-type'], 'text/plain; charset=utf-8')138  t.assert.strictEqual(res.body, '')139})140 141test('Do not create a HEAD route for each GET one (exposeHeadRoutes: false)', async t => {142  t.plan(2)143 144  const fastify = Fastify({ exposeHeadRoutes: false })145 146  fastify.route({147    method: 'GET',148    path: '/more-coffee',149    handler: (req, reply) => {150      reply.send({ here: 'is coffee' })151    }152  })153 154  fastify.route({155    method: 'GET',156    path: '/some-light',157    handler: (req, reply) => {158      reply.send('Get some light!')159    }160  })161 162  let res = await fastify.inject({163    method: 'HEAD',164    url: '/more-coffee'165  })166  t.assert.strictEqual(res.statusCode, 404)167 168  res = await fastify.inject({169    method: 'HEAD',170    url: '/some-light'171  })172  t.assert.strictEqual(res.statusCode, 404)173})174 175test('Creates a HEAD route for each GET one', async t => {176  t.plan(6)177 178  const fastify = Fastify({ exposeHeadRoutes: true })179 180  fastify.route({181    method: 'GET',182    path: '/more-coffee',183    handler: (req, reply) => {184      reply.send({ here: 'is coffee' })185    }186  })187 188  fastify.route({189    method: 'GET',190    path: '/some-light',191    handler: (req, reply) => {192      reply.send('Get some light!')193    }194  })195 196  let res = await fastify.inject({197    method: 'HEAD',198    url: '/more-coffee'199  })200  t.assert.strictEqual(res.statusCode, 200)201  t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')202  t.assert.deepStrictEqual(res.body, '')203 204  res = await fastify.inject({205    method: 'HEAD',206    url: '/some-light'207  })208  t.assert.strictEqual(res.statusCode, 200)209  t.assert.strictEqual(res.headers['content-type'], 'text/plain; charset=utf-8')210  t.assert.strictEqual(res.body, '')211})212