diff --git a/src/response.ts b/src/response.ts index 949f027..42455e6 100644 --- a/src/response.ts +++ b/src/response.ts @@ -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 @@ -407,13 +414,6 @@ export class Response extends Macroable { return } - /* - * ---------------------------------------- - * SET X-REQUEST-ID HEADER - * ---------------------------------------- - */ - this.setRequestId() - /* * ---------------------------------------- * SET CONTENT-LENGTH HEADER diff --git a/tests/response.spec.ts b/tests/response.spec.ts index a0f659e..475ba9f 100644 --- a/tests/response.spec.ts +++ b/tests/response.spec.ts @@ -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')