Skip to content
Draft
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
16 changes: 16 additions & 0 deletions __tests__/response/body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'])
})
})
})
2 changes: 2 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
Expand Down
Loading