Repository: fastify/fastify
File: test/logger/request.test.js (JavaScript)

Code before:
'use strict'

const stream = require('node:stream')

const t = require('node:test')
const split = require('split2')

const Fastify = require('../../fastify')
const helper = require('../helper')
const { on } = stream
const { request } = require('./logger-test-utils')
const { partialDeepStrictEqual } = require('../toolkit')

t.test('request', { timeout: 60000 }, async (t) => {
  let localhost

  t.plan(7)
  t.before(async function () {
    [localhost] = await helper.getLoopbackHost()
  })

  await await t.test('The request id header key can be customized', async (t) => {
    const lines = ['incoming request', 'some log message', 'request completed']
    t.plan(lines.length * 2 + 2)
    const REQUEST_ID = '42'

    const stream = split(JSON.parse)
    const fastify = Fastify({
      logger: { stream, level: 'info' },
      requestIdHeader: 'my-custom-request-id'
    })
    t.after(() => fastify.close())

    fastify.get('/', (req, reply) => {
      t.assert.deepEqual(req.id, REQUEST_ID)
      req.log.info('some log message')
      reply.send({ id: req.id })
    })

    const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'my-custom-request-id': REQUEST_ID } })
    const body = await response.json()
    t.assert.deepEqual(body.id, REQUEST_ID)

    for await (const [line] of on(stream, 'data')) {
      t.assert.deepEqual(line.reqId, REQUEST_ID)
      t.assert.deepEqual(line.msg, lines.shift(), 'message is set')
      if (lines.length === 0) break

Review comment (suggestion):
```suggestion
  await t.test('The request id header key can be customized', async (t) => {
```

Code after:
'use strict'

const stream = require('node:stream')

const t = require('node:test')
const split = require('split2')

const Fastify = require('../../fastify')
const helper = require('../helper')
const { on } = stream
const { request } = require('./logger-test-utils')
const { partialDeepStrictEqual } = require('../toolkit')

t.test('request', { timeout: 60000 }, async (t) => {
  let localhost

  t.plan(7)
  t.before(async function () {
    [localhost] = await helper.getLoopbackHost()
  })

  await t.test('The request id header key can be customized', async (t) => {
    const lines = ['incoming request', 'some log message', 'request completed']
    t.plan(lines.length * 2 + 2)
    const REQUEST_ID = '42'

    const stream = split(JSON.parse)
    const fastify = Fastify({
      logger: { stream, level: 'info' },
      requestIdHeader: 'my-custom-request-id'
    })
    t.after(() => fastify.close())

    fastify.get('/', (req, reply) => {
      t.assert.strictEqual(req.id, REQUEST_ID)
      req.log.info('some log message')
      reply.send({ id: req.id })
    })

    const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'my-custom-request-id': REQUEST_ID } })
    const body = await response.json()
    t.assert.strictEqual(body.id, REQUEST_ID)

    for await (const [line] of on(stream, 'data')) {
      t.assert.strictEqual(line.reqId, REQUEST_ID)
      t.assert.strictEqual(line.msg, lines.shift(), 'message is set')
      if (lines.length === 0) break