diff --git a/lib/response.js b/lib/response.js index f965e539dd2..d14e3900e15 100644 --- a/lib/response.js +++ b/lib/response.js @@ -673,7 +673,11 @@ res.header = function header(field, val) { if (Array.isArray(value)) { throw new TypeError('Content-Type cannot be set to an Array'); } - value = mime.contentType(value) + var contentType = mime.contentType(value); + if (contentType === false) { + throw new TypeError('invalid content type'); + } + value = contentType; } this.setHeader(field, value); diff --git a/test/res.set.js b/test/res.set.js index 04511c1c95f..84631ba9879 100644 --- a/test/res.set.js +++ b/test/res.set.js @@ -31,6 +31,19 @@ describe('res', function(){ .expect('X-Number', '123') .expect(200, 'string', done); }) + + it('should throw when Content-Type is invalid', function (done) { + var app = express() + + app.use(function (req, res) { + res.set('Content-Type', 'not-a-real-content-type') + res.end() + }) + + request(app) + .get('/') + .expect(500, /TypeError: invalid content type/, done) + }) }) describe('.set(field, values)', function(){