diff --git a/__tests__/response/body.test.js b/__tests__/response/body.test.js index c63cb4d75..8f684a328 100644 --- a/__tests__/response/body.test.js +++ b/__tests__/response/body.test.js @@ -288,6 +288,14 @@ describe('res.body=', () => { res.body = new ReadableStream() assert.strictEqual('application/octet-stream', res.header['content-type']) }) + + it('should remove stale Content-Length when replacing a body', () => { + const res = response() + res.body = 'hello' + assert.strictEqual(5, res.length) + res.body = new ReadableStream() + assert.strictEqual(undefined, res.header['content-length']) + }) }) describe('when a Blob is given', () => { @@ -324,5 +332,13 @@ describe('res.body=', () => { assert.strictEqual(301, res.status) assert.strictEqual('https://www.example.com/', res.header.location) }) + + it('should remove stale Content-Length when replacing a body', () => { + const res = response() + res.body = 'hello' + assert.strictEqual(5, res.length) + res.body = new Response('different body') + assert.strictEqual(undefined, res.header['content-length']) + }) }) }) diff --git a/lib/response.js b/lib/response.js index 846a8beed..e1aa2b03e 100644 --- a/lib/response.js +++ b/lib/response.js @@ -201,6 +201,7 @@ module.exports = { // ReadableStream if (val instanceof ReadableStream) { if (setType) this.type = 'bin' + if (original != null) this.remove('Content-Length') cleanupPreviousStream() return } @@ -217,6 +218,7 @@ module.exports = { if (val instanceof Response) { this.status = val.status if (setType) this.type = 'bin' + if (original != null) this.remove('Content-Length') const headers = val.headers for (const key of headers.keys()) { this.set(key, headers.get(key))