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
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,13 @@ exports.setCharset = function setCharset(type, charset) {
return type;
}

// parse type
var parsed = contentType.parse(type);
// preserve unparseable values instead of throwing when callers set
// a non-standard Content-Type manually.
try {
var parsed = contentType.parse(type);
} catch {
return type;
}

// set charset
parsed.parameters.charset = charset;
Expand Down
28 changes: 28 additions & 0 deletions test/res.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ describe('res', function(){
.expect('X-Number', '123')
.expect(200, 'string', done);
})

it('should preserve unknown Content-Type values', function (done) {
var app = express();

app.use(function (req, res) {
res.set('Content-Type', 'some-custom-type');
res.end('hello');
});

request(app)
.get('/')
.expect('Content-Type', 'some-custom-type')
.expect(200, 'hello', done);
})

it('should preserve unknown Content-Type values when sending strings', function (done) {
var app = express();

app.use(function (req, res) {
res.set('Content-Type', 'some-custom-type');
res.send('hello');
});

request(app)
.get('/')
.expect('Content-Type', 'some-custom-type')
.expect(200, 'hello', done);
})
})

describe('.set(field, values)', function(){
Expand Down
4 changes: 4 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe('utils.setCharset(type, charset)', function () {
it('should override charset', function () {
assert.strictEqual(utils.setCharset('text/html; charset=iso-8859-1', 'utf-8'), 'text/html; charset=utf-8');
});

it('should preserve unparseable types', function () {
assert.strictEqual(utils.setCharset('some-custom-type', 'utf-8'), 'some-custom-type');
});
});

describe('utils.wetag(body, encoding)', function(){
Expand Down