Skip to content

Commit

Permalink
fix: allow uppercase requestIdHandler (#4906)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Jul 17, 2023
1 parent c65a6be commit 62d2cc9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function fastify (options) {

validateBodyLimitOption(options.bodyLimit)

const requestIdHeader = (options.requestIdHeader === false) ? false : (options.requestIdHeader || defaultInitOptions.requestIdHeader)
const requestIdHeader = (options.requestIdHeader === false) ? false : (options.requestIdHeader || defaultInitOptions.requestIdHeader).toLowerCase()
const genReqId = reqIdGenFactory(requestIdHeader, options.genReqId)
const requestIdLogLabel = options.requestIdLogLabel || 'reqId'
const bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit
Expand Down
131 changes: 131 additions & 0 deletions test/request-id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'use strict'

const t = require('tap')
const Fastify = require('..')
const sget = require('simple-get').concat

t.test('The request id header key can be customized', async (t) => {
t.plan(2)
const REQUEST_ID = '42'

const fastify = Fastify({
requestIdHeader: 'my-custom-request-id'
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
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.equal(body.id, REQUEST_ID)
})

t.test('The request id header key can be customized', async (t) => {
t.plan(2)
const REQUEST_ID = '42'

const fastify = Fastify({
requestIdHeader: 'my-custom-request-id'
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
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.equal(body.id, REQUEST_ID)
})

t.test('The request id header key can be customized', (t) => {
t.plan(4)
const REQUEST_ID = '42'

const fastify = Fastify({
requestIdHeader: 'my-custom-request-id'
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
reply.send({ id: req.id })
})

fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.teardown(() => fastify.close())

sget({
method: 'GET',
url: address,
headers: {
'my-custom-request-id': REQUEST_ID
}
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), `{"id":"${REQUEST_ID}"}`)
})
})
})

t.test('The request id header key can be customized', (t) => {
t.plan(4)
const REQUEST_ID = '42'

const fastify = Fastify({
requestIdHeader: 'my-custom-request-id'
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
reply.send({ id: req.id })
})

fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.teardown(() => fastify.close())

sget({
method: 'GET',
url: address,
headers: {
'MY-CUSTOM-REQUEST-ID': REQUEST_ID
}
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), `{"id":"${REQUEST_ID}"}`)
})
})
})

t.test('The request id header key can be customized', (t) => {
t.plan(4)
const REQUEST_ID = '42'

const fastify = Fastify({
requestIdHeader: 'MY-CUSTOM-REQUEST-ID'
})

fastify.get('/', (req, reply) => {
t.equal(req.id, REQUEST_ID)
reply.send({ id: req.id })
})

fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.teardown(() => fastify.close())

sget({
method: 'GET',
url: address,
headers: {
'MY-CUSTOM-REQUEST-ID': REQUEST_ID
}
}, (err, response, body) => {
t.error(err)
t.equal(body.toString(), `{"id":"${REQUEST_ID}"}`)
})
})
})

0 comments on commit 62d2cc9

Please sign in to comment.