Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/middleware/request-id/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ describe('Request ID Middleware', () => {
expect(await res.text()).toBe('hono-is-hot')
})

it('Should return custom request id with all valid characters', async () => {
const validRequestId = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_='
const res = await app.request('http://localhost/requestId', {
headers: {
'X-Request-Id': validRequestId,
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe(validRequestId)
expect(await res.text()).toBe(validRequestId)
})

it('Should return random request id without using request header', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/request-id/request-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const requestId = ({
return async function requestId(c, next) {
// If `headerName` is empty string, req.header will return the object
let reqId = headerName ? c.req.header(headerName) : undefined
if (!reqId || reqId.length > limitLength || /[^\w\-]/.test(reqId)) {
if (!reqId || reqId.length > limitLength || /[^\w\-=]/.test(reqId)) {
reqId = generator(c)
}

Expand Down
Loading