mirror of
https://github.com/zebrajr/express.git
synced 2026-01-15 12:15:27 +00:00
Added: res.respondTo() defaults the content-type
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user