Skip to content
Closed
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
31 changes: 11 additions & 20 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ res.json = function json(obj) {

// content-type
if (!this.get('Content-Type')) {
this.set('Content-Type', 'application/json');
this.type('json');
}

return this.send(body);
Expand Down Expand Up @@ -271,7 +271,7 @@ res.jsonp = function jsonp(obj) {
// content-type
if (!this.get('Content-Type')) {
this.set('X-Content-Type-Options', 'nosniff');
this.set('Content-Type', 'application/json');
this.type('json');
}

// fixup callback
Expand All @@ -282,7 +282,7 @@ res.jsonp = function jsonp(obj) {
// jsonp
if (typeof callback === 'string' && callback.length !== 0) {
this.set('X-Content-Type-Options', 'nosniff');
this.set('Content-Type', 'text/javascript');
this.type('text/javascript');

// restrict callback charset
callback = callback.replace(/[^\[\]\w$.]/g, '');
Expand Down Expand Up @@ -506,7 +506,7 @@ res.contentType =
res.type = function contentType(type) {
var ct = type.indexOf('/') === -1
? (mime.contentType(type) || 'application/octet-stream')
: type;
: (mime.contentType(type) || type);

return this.set('Content-Type', ct);
};
Expand Down Expand Up @@ -535,25 +535,21 @@ res.type = function contentType(type) {
* res.send('<p>hey</p>');
* },
*
* 'application/json': function () {
* 'application/json': function(){
* res.send({ message: 'hey' });
* }
* });
*
* In addition to canonicalized MIME types you may
* also use extnames mapped to these types:
* In addition to an object of callbacks you may also provide a function
* which will be invoked when no match is made and the "Accept" header is
* present, otherwise 406 "Not Acceptable" is sent.
*
* res.format({
* text: function(){
* res.send('hey');
* },
*
* html: function(){
* res.send('<p>hey</p>');
* },
*
* json: function(){
* res.send({ message: 'hey' });
* default: function(){
* res.send('hey');
* }
* });
*
Expand Down Expand Up @@ -582,7 +578,7 @@ res.format = function(obj){
this.vary("Accept");

if (key) {
this.set('Content-Type', normalizeType(key).value);
this.type(normalizeType(key).value);
obj[key](req, this, next);
} else if (obj.default) {
obj.default(req, this, next)
Expand Down Expand Up @@ -655,9 +651,6 @@ res.append = function append(field, val) {
*
* Aliased as `res.header()`.
*
* When the set header is "Content-Type", the type is expanded to include
* the charset if not present using `mime.contentType()`.
*
* @param {String|Object} field
* @param {String|Array} val
* @return {ServerResponse} for chaining
Expand All @@ -671,12 +664,10 @@ res.header = function header(field, val) {
? val.map(String)
: String(val);

// add charset to content-type
if (field.toLowerCase() === 'content-type') {
if (Array.isArray(value)) {
throw new TypeError('Content-Type cannot be set to an Array');
}
value = mime.contentType(value)
}

this.setHeader(field, value);
Expand Down
17 changes: 17 additions & 0 deletions test/res.format.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ describe('res', function(){

test(app)
})

it('should set Content-Type with charset when formatter uses res.end()', function (done) {
var app = express();

app.use(function (req, res) {
res.format({
'text/plain': function () {
res.end('ok');
}
});
});

request(app)
.get('/')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, 'ok', done);
})
})
})

Expand Down
4 changes: 2 additions & 2 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('res', function(){

request(app)
.get('/')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Type', 'text/plain')
.expect(200, 'hey', done);
})

Expand All @@ -213,7 +213,7 @@ describe('res', function(){

request(app)
.get("/")
.expect("Content-Type", "text/plain; charset=utf-8")
.expect("Content-Type", "text/plain")
.expect(200, "hey", done);
})

Expand Down