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
18 changes: 18 additions & 0 deletions __tests__/response/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ describe('ctx.set(object)', () => {
assert.strictEqual(ctx.response.header.bar, '2')
})
})

describe('ctx.set(object) Content-Type singleton guard', () => {
it('should throw when Content-Type is set to an array via the object form', () => {
const ctx = context()
assert.throws(
() => ctx.set({ 'Content-Type': ['text/html', 'text/plain'] }),
{ message: 'Assign multiple Content-Type for response header is not allowed' }
)
})

it('should throw regardless of header name casing in the object form', () => {
const ctx = context()
assert.throws(
() => ctx.set({ 'content-type': ['application/json', 'text/plain'] }),
{ message: 'Assign multiple Content-Type for response header is not allowed' }
)
})
})
7 changes: 6 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,12 @@ module.exports = {
}
this.res.setHeader(field, val)
} else {
Object.keys(field).forEach(header => this.res.setHeader(header, field[header]))
Object.keys(field).forEach(header => {
if (header.toLowerCase() === 'content-type') {
assert(!Array.isArray(field[header]), 'Assign multiple Content-Type for response header is not allowed')
}
this.res.setHeader(header, field[header])
})
}
},

Expand Down