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
14 changes: 7 additions & 7 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ export class Response extends Macroable {
protected writeBody(content: any, generateEtag: boolean, jsonpCallbackName?: string): void {
const hasEmptyBody = content === null || content === undefined || content === ''

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

/**
* Set status to "204" when body is empty. The `safeStatus` method only
* sets the status when no explicit status has been set already
Expand Down Expand Up @@ -407,13 +414,6 @@ export class Response extends Macroable {
return
}

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

/*
* ----------------------------------------
* SET CONTENT-LENGTH HEADER
Expand Down
12 changes: 12 additions & 0 deletions tests/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ test.group('Response', (group) => {
await supertest(url).get('/').expect(200).expect('x-request-id', '20241127')
})

test('set x-request-id header when the response has no body', 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.created()
response.finish()
})

await supertest(url).get('/').expect(201).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