Skip to content

Commit

Permalink
Fix useFastifyRequestId handling when useHeader is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
puzpuzpuz committed Jun 10, 2020
1 parent 1089901 commit b6ce62f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const fastifyPlugin = ({
requestId = request.headers[headerName.toLowerCase()]
}
if (useFastifyRequestId) {
requestId = request.id
requestId = requestId || request.id
}
requestId = requestId || uuidv1()

Expand Down
24 changes: 23 additions & 1 deletion tests/fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,34 @@ for (const type of types) {
})

return app.ready()
.then(() => request(app.server).get('/test').set('X-Request-Id', ''))
.then(() => request(app.server).get('/test'))
.then(res => {
expect(res.statusCode).toBe(200)
expect(res.body.id).toEqual(1)
})
})

test('uses header instead of fastify id in case of override', () => {
const app = Fastify()
register(type, app, {
useHeader: true,
useFastifyRequestId: true
})

const idInHead = 'id-from-header'

app.get('/test', async (_, reply) => {
const id = rTracer.id()
reply.send({ id })
})

return app.ready()
.then(() => request(app.server).get('/test').set('X-Request-Id', idInHead))
.then(res => {
expect(res.statusCode).toBe(200)
expect(res.body.id).toEqual(idInHead)
})
})
}

test('generates id for request with callback', () => {
Expand Down

0 comments on commit b6ce62f

Please sign in to comment.