Home / File/ genReqId.test.js — fastify Source File

genReqId.test.js — fastify Source File

Architecture documentation for genReqId.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { Readable } = require('node:stream')
const { test } = require('node:test')
const fp = require('fastify-plugin')
const Fastify = require('..')

test('Should accept a custom genReqId function', (t, done) => {
  t.plan(4)

  const fastify = Fastify({
    genReqId: function (req) {
      return 'a'
    }
  })

  t.after(() => fastify.close())
  fastify.get('/', (req, reply) => {
    t.assert.ok(req.id)
    reply.send({ id: req.id })
  })

  fastify.listen({ port: 0 }, err => {
    t.assert.ifError(err)
    fastify.inject({
      method: 'GET',
      url: `http://localhost:${fastify.server.address().port}`
    }, (err, res) => {
      t.assert.ifError(err)
      const payload = JSON.parse(res.payload)
      t.assert.strictEqual(payload.id, 'a')
      done()
    })
  })
})

test('Custom genReqId function gets raw request as argument', (t, done) => {
  t.plan(9)

  const REQUEST_ID = 'REQ-1234'

  const fastify = Fastify({
    genReqId: function (req) {
      t.assert.strictEqual('id' in req, false)
      t.assert.strictEqual('raw' in req, false)
      t.assert.ok(req instanceof Readable)
      // http.IncomingMessage does have `rawHeaders` property, but FastifyRequest does not
      const index = req.rawHeaders.indexOf('x-request-id')
      const xReqId = req.rawHeaders[index + 1]
      t.assert.strictEqual(xReqId, REQUEST_ID)
      t.assert.strictEqual(req.headers['x-request-id'], REQUEST_ID)
      return xReqId
    }
  })
  t.after(() => fastify.close())

  fastify.get('/', (req, reply) => {
    t.assert.strictEqual(req.id, REQUEST_ID)
    reply.send({ id: req.id })
  })
// ... (367 more lines)

Frequently Asked Questions

What does genReqId.test.js do?
genReqId.test.js is a source file in the fastify codebase, written in javascript.
Where is genReqId.test.js in the architecture?
genReqId.test.js is located at test/genReqId.test.js (directory: test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free