Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add x-request-id to the response #96

Merged
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
19 changes: 19 additions & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ export class Response extends Macroable {
return
}

/*
* ----------------------------------------
* SET X-REQUEST-ID HEADER
* ----------------------------------------
*/
this.setRequestId()

/*
* ----------------------------------------
* SET CONTENT-LENGTH HEADER
Expand Down Expand Up @@ -779,6 +786,18 @@ export class Response extends Macroable {
return this
}

/**
* Set X-Request-Id header by copying the header value from the request if it exists.
*
*/
setRequestId(): this {
const requestId = this.request.headers['x-request-id']
if (requestId) {
this.header('X-Request-Id', requestId)
}
return this
}

/**
* Returns a boolean telling if the new response etag evaluates same
* as the request header `if-none-match`. In case of `true`, the
Expand Down
12 changes: 12 additions & 0 deletions tests/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ test.group('Response', (group) => {
})
})

test('set x-request-id header', async () => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-request-id'] = '20241127'
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()

response.send('<p> hello </p>')
response.finish()
})

await supertest(url).get('/').expect(200).expect('x-request-id', '20241127')
})

test('get merged from http res object', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
res.setHeader('content-type', 'application/json')
Expand Down