Skip to content

Commit

Permalink
Merge pull request expressjs#1747 from sorribas/master
Browse files Browse the repository at this point in the history
res.format() now includes charset.
  • Loading branch information
tj committed Sep 8, 2013
2 parents b66c7da + a887e6a commit 1c87e5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ res.format = function(obj){
this.vary("Accept");

if (key) {
this.set('Content-Type', normalizeType(key).value);
var type = normalizeType(key).value;
var charset = mime.charsets.lookup(type);
if (charset) {
type += '; charset=' + charset;
}
this.set('Content-Type', type);
obj[key](req, this, next);
} else if (fn) {
fn();
Expand Down
19 changes: 18 additions & 1 deletion test/res.format.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,27 @@ function test(app) {
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, text/plain')
.expect('Content-Type', 'text/plain')
.expect('Content-Type', 'text/plain; charset=UTF-8')
.expect('hey', done);
})

it('should set the correct charset for the Content-Type', function() {
request(app)
.get('/')
.set('Accept', 'text/html')
.expect('Content-Type', 'text/html; charset=UTF-8');

request(app)
.get('/')
.set('Accept', 'text/plain')
.expect('Content-Type', 'text/plain; charset=UTF-8');

request(app)
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json');
})

it('should Vary: Accept', function(done){
request(app)
.get('/')
Expand Down

0 comments on commit 1c87e5e

Please sign in to comment.