Fix error when res.set cannot add charset to Content-Type

fixes #3303
closes #3305
closes #3307
This commit is contained in:
Oz Michaeli 2017-05-10 16:23:55 -04:00 committed by Douglas Christopher Wilson
parent 5ea2a8ff8e
commit ae0b630ac7
3 changed files with 22 additions and 3 deletions

View File

@ -1,6 +1,7 @@
unreleased
==========
* Fix error when `res.set` cannot add charset to `Content-Type`
* deps: debug@2.6.3
- Fix: `DEBUG_MAX_ARRAY_LENGTH`
* deps: finalhandler@~1.0.1

View File

@ -717,9 +717,14 @@ res.header = function header(field, val) {
: String(val);
// add charset to content-type
if (field.toLowerCase() === 'content-type' && !charsetRegExp.test(value)) {
var charset = mime.charsets.lookup(value.split(';')[0]);
if (charset) value += '; charset=' + charset.toLowerCase();
if (field.toLowerCase() === 'content-type') {
if (Array.isArray(value)) {
throw new TypeError('Content-Type cannot be set to an Array');
}
if (!charsetRegExp.test(value)) {
var charset = mime.charsets.lookup(value.split(';')[0]);
if (charset) value += '; charset=' + charset.toLowerCase();
}
}
this.setHeader(field, value);

View File

@ -73,6 +73,19 @@ describe('res', function(){
.expect('Content-Type', 'text/html; charset=lol')
.expect(200, done);
})
it('should throw when Content-Type is an array', function (done) {
var app = express()
app.use(function (req, res) {
res.set('Content-Type', ['text/html'])
res.end()
});
request(app)
.get('/')
.expect(500, /TypeError: Content-Type cannot be set to an Array/, done)
})
})
describe('.set(object)', function(){