From e08cbe6567ac8204f30df3af4a479e9e5eecc47a Mon Sep 17 00:00:00 2001 From: Harshit Date: Sun, 15 Mar 2026 17:13:32 +0530 Subject: [PATCH] fix: handle unknown Content-Type in res.set() falling back to original value When mime.contentType() returns false for unrecognized types, res.set('Content-Type', value) was setting the header to the literal string 'false'. This change falls back to the original value, matching how res.type() already handles this case. Fixes: #7034 --- lib/response.js | 2 +- test/res.set.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index f965e539dd2..bc2bc6fb438 100644 --- a/lib/response.js +++ b/lib/response.js @@ -673,7 +673,7 @@ 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) + value = mime.contentType(value) || value } this.setHeader(field, value); diff --git a/test/res.set.js b/test/res.set.js index 04511c1c95f..44b7593ec38 100644 --- a/test/res.set.js +++ b/test/res.set.js @@ -87,6 +87,34 @@ describe('res', function(){ .get('/') .expect(500, /TypeError: Content-Type cannot be set to an Array/, done) }) + + it('should fall back to original value for unknown Content-Type', function (done) { + var app = express(); + + app.use(function (req, res) { + res.set('Content-Type', 'some-custom-type'); + res.end(); + }); + + request(app) + .get('/') + .expect('Content-Type', 'some-custom-type') + .expect(200, done); + }) + + it('should resolve known Content-Type shorthands', function (done) { + var app = express(); + + app.use(function (req, res) { + res.set('Content-Type', 'html'); + res.end(); + }); + + request(app) + .get('/') + .expect('Content-Type', 'text/html; charset=utf-8') + .expect(200, done); + }) }) describe('.set(object)', function(){