From e74a33c8ebddb057df62809a0442520a091a02b4 Mon Sep 17 00:00:00 2001 From: Anna Bocharova Date: Fri, 6 Dec 2024 23:42:09 +0000 Subject: [PATCH] fix(mockResponse): writeHead() sets headersSent. --- lib/mockResponse.js | 1 + package-lock.json | 2 +- test/lib/mockResponse.spec.js | 4 +--- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/mockResponse.js b/lib/mockResponse.js index 38aa53b..9496bae 100644 --- a/lib/mockResponse.js +++ b/lib/mockResponse.js @@ -141,6 +141,7 @@ function createResponse(options = {}) { Object.assign(mockResponse._headers, utils.convertKeysToLowerCase(headers)); } + this.headersSent = true; return this; }; diff --git a/package-lock.json b/package-lock.json index 6a22e93..f358cf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "node-mocks-http", - "version": "1.16.0", + "version": "1.16.1", "license": "MIT", "dependencies": { "accepts": "^1.3.7", diff --git a/test/lib/mockResponse.spec.js b/test/lib/mockResponse.spec.js index 51e920c..ab2fa49 100644 --- a/test/lib/mockResponse.spec.js +++ b/test/lib/mockResponse.spec.js @@ -870,10 +870,8 @@ describe('mockResponse', () => { it('updates the headersSent property of the response', () => { const headers = { 'x-header': 'test llama' }; - response.writeHead(400, headers); - // headers are only sent by node with first body byte expect(response.headersSent).to.equal(false); - response.write('foo'); + response.writeHead(400, headers); expect(response.headersSent).to.equal(true); // further updates to headers shouldn't really be reflected in mock headers // since these would be transmitted as part of the body (probably breaking chunked encoding)