Skip to content

Commit

Permalink
fix: Do not response Content-Length if Transfer-Encoding is defined c…
Browse files Browse the repository at this point in the history
…loses #1562
  • Loading branch information
jonathanong committed Aug 18, 2021
1 parent 03ff762 commit 39c0b48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Generators are no longer supported.
Koa no longer asserts if generators are used.

## Fixes

- fix: Do not response Content-Length if Transfer-Encoding is defined #1562 @charlyzeng

2.13.1 / 2021-01-04
==================

Expand Down
26 changes: 26 additions & 0 deletions __tests__/application/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('app.response', () => {
const app3 = new Koa()
const app4 = new Koa()
const app5 = new Koa()
const app6 = new Koa()
const app7 = new Koa()

it('should merge properties', () => {
app1.use((ctx, next) => {
Expand Down Expand Up @@ -71,4 +73,28 @@ describe('app.response', () => {
.get('/')
.expect(204)
})

it('should add Content-Length when Transfer-Encoding is not defined', () => {
app6.use((ctx, next) => {
ctx.body = 'hello world'
})

return request(app6.listen())
.get('/')
.expect('Content-Length', '11')
.expect(200)
})

it('should not add Content-Length when Transfer-Encoding is defined', () => {
app7.use((ctx, next) => {
ctx.set('Transfer-Encoding', 'chunked')
ctx.body = 'hello world'
assert.strictEqual(ctx.response.get('Content-Length'), '')
})

return request(app7.listen())
.get('/')
.expect('Transfer-Encoding', 'chunked')
.expect(200)
})
})
4 changes: 3 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ module.exports = {
*/

set length (n) {
this.set('Content-Length', n)
if (!this.has('Transfer-Encoding')) {
this.set('Content-Length', n)
}
},

/**
Expand Down

0 comments on commit 39c0b48

Please sign in to comment.