Skip to content

Commit

Permalink
Fix error when res.set cannot add charset to Content-Type
Browse files Browse the repository at this point in the history
fixes #3303
closes #3305
closes #3307
  • Loading branch information
oztune authored and dougwilson committed May 15, 2017
1 parent 5ea2a8f commit ae0b630
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/res.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit ae0b630

Please sign in to comment.