Skip to content
Open
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
14 changes: 14 additions & 0 deletions __tests__/application/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('app.response', () => {
const app5 = new Koa()
const app6 = new Koa()
const app7 = new Koa()
const app8 = new Koa()

it('should merge properties', () => {
app1.use((ctx, next) => {
Expand Down Expand Up @@ -97,4 +98,17 @@ describe('app.response', () => {
.expect('Transfer-Encoding', 'chunked')
.expect(200)
})

it('should not assign multiple content-type for response header', () => {
app8.use((ctx, next) => {
assert.throws(() => {
ctx.set('Content-Type', ['image/jpg', 'application/json'])
}, Error)
ctx.body = {}
})

return request(app8.callback())
.get('/')
.expect(200)
})
})
3 changes: 3 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ module.exports = {
if (this.headerSent || !field) return

if (typeof field === 'string') {
if (field.toLowerCase() === 'content-type') {
assert(!Array.isArray(val), 'Assign multiple Content-Type for response header is not allowed')
}
this.res.setHeader(field, val)
} else {
Object.keys(field).forEach(header => this.res.setHeader(header, field[header]))
Expand Down