Added: res.respondTo() defaults the content-type

This commit is contained in:
TJ Holowaychuk
2012-02-18 17:23:02 -08:00
parent b565e258cb
commit bdfa6d1fe7
2 changed files with 14 additions and 2 deletions

View File

@@ -340,7 +340,8 @@ res.respondTo = function(obj){
if (!accepted.length) key = keys.shift();
if (key) {
obj[key](req, res, next);
this.set('Content-Type', key);
obj[key](req, this, next);
} else {
var err = new Error('Not Acceptable');
err.status = 406;

View File

@@ -8,7 +8,7 @@ var app = express();
app.use(function(req, res, next){
res.respondTo({
'text/plain': function(){
res.type('text').send('hey');
res.send('hey');
},
'text/html': function(){
@@ -41,6 +41,17 @@ describe('req', function(){
.expect('{"message":"hey"}', done);
})
it('should default the Content-Type', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, text/plain')
.end(function(res){
res.headers['content-type'].should.equal('text/plain');
res.body.should.equal('hey');
done();
});
})
describe('when Accept is not present', function(){
it('should invoke the first callback', function(done){
request(app)