CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
genReqId.test.js476 linesDownload Raw Back to test
1'use strict'2 3const { Readable } = require('node:stream')4const { test } = require('tap')5const fp = require('fastify-plugin')6const Fastify = require('..')7 8test('Should accept a custom genReqId function', t => {9  t.plan(4)10 11  const fastify = Fastify({12    genReqId: function (req) {13      return 'a'14    }15  })16 17  fastify.get('/', (req, reply) => {18    t.ok(req.id)19    reply.send({ id: req.id })20  })21 22  fastify.listen({ port: 0 }, err => {23    t.error(err)24    fastify.inject({25      method: 'GET',26      url: 'http://localhost:' + fastify.server.address().port27    }, (err, res) => {28      t.error(err)29      const payload = JSON.parse(res.payload)30      t.equal(payload.id, 'a')31      fastify.close()32    })33  })34})35 36test('Custom genReqId function gets raw request as argument', t => {37  t.plan(9)38 39  const REQUEST_ID = 'REQ-1234'40 41  const fastify = Fastify({42    genReqId: function (req) {43      t.notOk('id' in req)44      t.notOk('raw' in req)45      t.ok(req instanceof Readable)46      // http.IncomingMessage does have `rawHeaders` property, but FastifyRequest does not47      const index = req.rawHeaders.indexOf('x-request-id')48      const xReqId = req.rawHeaders[index + 1]49      t.equal(xReqId, REQUEST_ID)50      t.equal(req.headers['x-request-id'], REQUEST_ID)51      return xReqId52    }53  })54 55  fastify.get('/', (req, reply) => {56    t.equal(req.id, REQUEST_ID)57    reply.send({ id: req.id })58  })59 60  fastify.listen({ port: 0 }, err => {61    t.error(err)62    fastify.inject({63      method: 'GET',64      headers: {65        'x-request-id': REQUEST_ID66      },67      url: 'http://localhost:' + fastify.server.address().port68    }, (err, res) => {69      t.error(err)70      const payload = JSON.parse(res.payload)71      t.equal(payload.id, REQUEST_ID)72      fastify.close()73    })74  })75})76 77test('Should handle properly requestIdHeader option', t => {78  t.plan(4)79 80  t.equal(Fastify({ requestIdHeader: '' }).initialConfig.requestIdHeader, false)81  t.equal(Fastify({ requestIdHeader: false }).initialConfig.requestIdHeader, false)82  t.equal(Fastify({ requestIdHeader: true }).initialConfig.requestIdHeader, 'request-id')83  t.equal(Fastify({ requestIdHeader: 'x-request-id' }).initialConfig.requestIdHeader, 'x-request-id')84})85 86test('Should accept option to set genReqId with setGenReqId option', t => {87  t.plan(9)88 89  const fastify = Fastify({90    genReqId: function (req) {91      return 'base'92    }93  })94 95  fastify.register(function (instance, opts, next) {96    instance.setGenReqId(function (req) {97      return 'foo'98    })99    instance.get('/', (req, reply) => {100      t.ok(req.id)101      reply.send({ id: req.id })102    })103    next()104  }, { prefix: 'foo' })105 106  fastify.register(function (instance, opts, next) {107    instance.setGenReqId(function (req) {108      return 'bar'109    })110    instance.get('/', (req, reply) => {111      t.ok(req.id)112      reply.send({ id: req.id })113    })114    next()115  }, { prefix: 'bar' })116 117  fastify.get('/', (req, reply) => {118    t.ok(req.id)119    reply.send({ id: req.id })120  })121 122  fastify.inject({123    method: 'GET',124    url: '/'125  }, (err, res) => {126    t.error(err)127    const payload = JSON.parse(res.payload)128    t.equal(payload.id, 'base')129    fastify.close()130  })131 132  fastify.inject({133    method: 'GET',134    url: '/foo'135  }, (err, res) => {136    t.error(err)137    const payload = JSON.parse(res.payload)138    t.equal(payload.id, 'foo')139    fastify.close()140  })141 142  fastify.inject({143    method: 'GET',144    url: '/bar'145  }, (err, res) => {146    t.error(err)147    const payload = JSON.parse(res.payload)148    t.equal(payload.id, 'bar')149    fastify.close()150  })151})152 153test('Should encapsulate setGenReqId', t => {154  t.plan(12)155 156  const fastify = Fastify({157    genReqId: function (req) {158      return 'base'159    }160  })161 162  const bazInstance = function (instance, opts, next) {163    instance.register(barInstance, { prefix: 'baz' })164 165    instance.setGenReqId(function (req) {166      return 'baz'167    })168    instance.get('/', (req, reply) => {169      t.ok(req.id)170      reply.send({ id: req.id })171    })172    next()173  }174 175  const barInstance = function (instance, opts, next) {176    instance.setGenReqId(function (req) {177      return 'bar'178    })179    instance.get('/', (req, reply) => {180      t.ok(req.id)181      reply.send({ id: req.id })182    })183    next()184  }185 186  const fooInstance = function (instance, opts, next) {187    instance.register(bazInstance, { prefix: 'baz' })188    instance.register(barInstance, { prefix: 'bar' })189 190    instance.setGenReqId(function (req) {191      return 'foo'192    })193 194    instance.get('/', (req, reply) => {195      t.ok(req.id)196      reply.send({ id: req.id })197    })198    next()199  }200 201  fastify.register(fooInstance, { prefix: 'foo' })202 203  fastify.get('/', (req, reply) => {204    t.ok(req.id)205    reply.send({ id: req.id })206  })207 208  fastify.inject({209    method: 'GET',210    url: '/'211  }, (err, res) => {212    t.error(err)213    const payload = JSON.parse(res.payload)214    t.equal(payload.id, 'base')215    fastify.close()216  })217 218  fastify.inject({219    method: 'GET',220    url: '/foo'221  }, (err, res) => {222    t.error(err)223    const payload = JSON.parse(res.payload)224    t.equal(payload.id, 'foo')225    fastify.close()226  })227 228  fastify.inject({229    method: 'GET',230    url: '/foo/bar'231  }, (err, res) => {232    t.error(err)233    const payload = JSON.parse(res.payload)234    t.equal(payload.id, 'bar')235    fastify.close()236  })237 238  fastify.inject({239    method: 'GET',240    url: '/foo/baz'241  }, (err, res) => {242    t.error(err)243    const payload = JSON.parse(res.payload)244    t.equal(payload.id, 'baz')245    fastify.close()246  })247})248 249test('Should encapsulate setGenReqId', t => {250  t.plan(12)251 252  const fastify = Fastify({253    genReqId: function (req) {254      return 'base'255    }256  })257 258  const bazInstance = function (instance, opts, next) {259    instance.register(barInstance, { prefix: 'baz' })260 261    instance.setGenReqId(function (req) {262      return 'baz'263    })264    instance.get('/', (req, reply) => {265      t.ok(req.id)266      reply.send({ id: req.id })267    })268    next()269  }270 271  const barInstance = function (instance, opts, next) {272    instance.setGenReqId(function (req) {273      return 'bar'274    })275    instance.get('/', (req, reply) => {276      t.ok(req.id)277      reply.send({ id: req.id })278    })279    next()280  }281 282  const fooInstance = function (instance, opts, next) {283    instance.register(bazInstance, { prefix: 'baz' })284    instance.register(barInstance, { prefix: 'bar' })285 286    instance.setGenReqId(function (req) {287      return 'foo'288    })289 290    instance.get('/', (req, reply) => {291      t.ok(req.id)292      reply.send({ id: req.id })293    })294    next()295  }296 297  fastify.register(fooInstance, { prefix: 'foo' })298 299  fastify.get('/', (req, reply) => {300    t.ok(req.id)301    reply.send({ id: req.id })302  })303 304  fastify.inject({305    method: 'GET',306    url: '/'307  }, (err, res) => {308    t.error(err)309    const payload = JSON.parse(res.payload)310    t.equal(payload.id, 'base')311    fastify.close()312  })313 314  fastify.inject({315    method: 'GET',316    url: '/foo'317  }, (err, res) => {318    t.error(err)319    const payload = JSON.parse(res.payload)320    t.equal(payload.id, 'foo')321    fastify.close()322  })323 324  fastify.inject({325    method: 'GET',326    url: '/foo/bar'327  }, (err, res) => {328    t.error(err)329    const payload = JSON.parse(res.payload)330    t.equal(payload.id, 'bar')331    fastify.close()332  })333 334  fastify.inject({335    method: 'GET',336    url: '/foo/baz'337  }, (err, res) => {338    t.error(err)339    const payload = JSON.parse(res.payload)340    t.equal(payload.id, 'baz')341    fastify.close()342  })343})344 345test('Should not alter parent of genReqId', t => {346  t.plan(6)347 348  const fastify = Fastify()349 350  const fooInstance = function (instance, opts, next) {351    instance.setGenReqId(function (req) {352      return 'foo'353    })354 355    instance.get('/', (req, reply) => {356      t.ok(req.id)357      reply.send({ id: req.id })358    })359    next()360  }361 362  fastify.register(fooInstance, { prefix: 'foo' })363 364  fastify.get('/', (req, reply) => {365    t.ok(req.id)366    reply.send({ id: req.id })367  })368 369  fastify.inject({370    method: 'GET',371    url: '/'372  }, (err, res) => {373    t.error(err)374    const payload = JSON.parse(res.payload)375    t.equal(payload.id, 'req-1')376    fastify.close()377  })378 379  fastify.inject({380    method: 'GET',381    url: '/foo'382  }, (err, res) => {383    t.error(err)384    const payload = JSON.parse(res.payload)385    t.equal(payload.id, 'foo')386    fastify.close()387  })388})389 390test('Should have child instance user parent genReqId', t => {391  t.plan(6)392 393  const fastify = Fastify({394    genReqId: function (req) {395      return 'foo'396    }397  })398 399  const fooInstance = function (instance, opts, next) {400    instance.get('/', (req, reply) => {401      t.ok(req.id)402      reply.send({ id: req.id })403    })404    next()405  }406 407  fastify.register(fooInstance, { prefix: 'foo' })408 409  fastify.get('/', (req, reply) => {410    t.ok(req.id)411    reply.send({ id: req.id })412  })413 414  fastify.inject({415    method: 'GET',416    url: '/'417  }, (err, res) => {418    t.error(err)419    const payload = JSON.parse(res.payload)420    t.equal(payload.id, 'foo')421    fastify.close()422  })423 424  fastify.inject({425    method: 'GET',426    url: '/foo'427  }, (err, res) => {428    t.error(err)429    const payload = JSON.parse(res.payload)430    t.equal(payload.id, 'foo')431    fastify.close()432  })433})434 435test('genReqId set on root scope when using fastify-plugin', t => {436  t.plan(6)437 438  const fastify = Fastify()439 440  fastify.register(fp(function (fastify, options, done) {441    fastify.setGenReqId(function (req) {442      return 'not-encapsulated'443    })444    fastify.get('/not-encapsulated-1', (req, reply) => {445      t.ok(req.id)446      reply.send({ id: req.id })447    })448    done()449  }))450 451  fastify.get('/not-encapsulated-2', (req, reply) => {452    t.ok(req.id)453    reply.send({ id: req.id })454  })455 456  fastify.inject({457    method: 'GET',458    url: '/not-encapsulated-1'459  }, (err, res) => {460    t.error(err)461    const payload = JSON.parse(res.payload)462    t.equal(payload.id, 'not-encapsulated')463    fastify.close()464  })465 466  fastify.inject({467    method: 'GET',468    url: '/not-encapsulated-2'469  }, (err, res) => {470    t.error(err)471    const payload = JSON.parse(res.payload)472    t.equal(payload.id, 'not-encapsulated')473    fastify.close()474  })475})476