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
3 changes: 2 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ res.type = function contentType(type) {
res.format = function(obj){
var req = this.req;
var next = req.next;
var hasDefault = Object.prototype.hasOwnProperty.call(obj, 'default');

var keys = Object.keys(obj)
.filter(function (v) { return v !== 'default' })
Expand All @@ -584,7 +585,7 @@ res.format = function(obj){
if (key) {
this.set('Content-Type', normalizeType(key).value);
obj[key](req, this, next);
} else if (obj.default) {
} else if (hasDefault && obj.default) {
obj.default(req, this, next)
} else {
next(createError(406, {
Expand Down
27 changes: 27 additions & 0 deletions test/res.format.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,33 @@ describe('res', function(){
.expect('json')
.end(done)
})

it('should ignore inherited default callbacks', function (done) {
var app = express()
var formats = Object.create({
default: function () {
throw new Error('should not be called')
}
})

formats.text = function () {
throw new Error('should not be called')
}

app.use(function (req, res) {
res.format(formats)
})

app.use(function (err, req, res, next) {
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
})

request(app)
.get('/')
.set('Accept', 'application/json')
.expect(406, 'Supports: text/plain', done)
})
})

describe('in router', function(){
Expand Down