diff --git a/History.md b/History.md index 10b15b2b6a..cbf13eb837 100644 --- a/History.md +++ b/History.md @@ -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 diff --git a/lib/response.js b/lib/response.js index f3f043f5cd..b852a60e2f 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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); diff --git a/test/res.set.js b/test/res.set.js index b15bc5d7e3..e46d123947 100644 --- a/test/res.set.js +++ b/test/res.set.js @@ -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(){