2012-02-18 17:13:40 -08:00
|
|
|
|
|
|
|
|
var express = require('../')
|
|
|
|
|
, request = require('./support/http')
|
2012-02-21 09:30:05 -08:00
|
|
|
, utils = require('../lib/utils')
|
|
|
|
|
, assert = require('assert');
|
2012-02-18 17:13:40 -08:00
|
|
|
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res, next){
|
2012-02-22 19:59:52 -08:00
|
|
|
res.format({
|
2012-02-18 17:13:40 -08:00
|
|
|
'text/plain': function(){
|
2012-02-18 17:23:02 -08:00
|
|
|
res.send('hey');
|
2012-02-18 17:13:40 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'text/html': function(){
|
|
|
|
|
res.send('<p>hey</p>');
|
|
|
|
|
},
|
|
|
|
|
|
2012-02-21 10:23:20 -08:00
|
|
|
'application/json': function(a, b, c){
|
2012-02-21 09:30:05 -08:00
|
|
|
assert(req == a);
|
|
|
|
|
assert(res == b);
|
|
|
|
|
assert(next == c);
|
2012-02-18 17:13:40 -08:00
|
|
|
res.send({ message: 'hey' });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(function(err, req, res, next){
|
2012-02-23 10:58:06 -08:00
|
|
|
res.send(err.status, 'Supports: ' + err.types.join(', '));
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var app2 = express();
|
|
|
|
|
|
|
|
|
|
app2.use(function(req, res, next){
|
|
|
|
|
res.format({
|
|
|
|
|
text: function(){ res.send('hey') },
|
|
|
|
|
html: function(){ res.send('<p>hey</p>') },
|
|
|
|
|
json: function(){ res.send({ message: 'hey' }) }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app2.use(function(err, req, res, next){
|
|
|
|
|
res.send(err.status, 'Supports: ' + err.types.join(', '));
|
2012-02-18 17:13:40 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('req', function(){
|
2012-02-22 19:59:52 -08:00
|
|
|
describe('.format(obj)', function(){
|
2012-02-23 10:58:06 -08:00
|
|
|
describe('with canonicalized mime types', function(){
|
|
|
|
|
test(app);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('with extnames', function(){
|
|
|
|
|
test(app2);
|
2012-02-18 17:13:40 -08:00
|
|
|
})
|
2012-02-23 10:58:06 -08:00
|
|
|
})
|
|
|
|
|
})
|
2012-02-18 17:13:40 -08:00
|
|
|
|
2012-02-23 10:58:06 -08:00
|
|
|
function test(app) {
|
|
|
|
|
it('should utilize qvalues in negotiation', function(done){
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
|
|
|
|
|
.expect('{"message":"hey"}', done);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should allow wildcard type/subtypes', function(done){
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
|
|
|
|
|
.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){
|
2012-02-18 17:13:40 -08:00
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2012-02-23 10:58:06 -08:00
|
|
|
.expect('hey', done);
|
2012-02-18 17:13:40 -08:00
|
|
|
})
|
2012-02-23 10:58:06 -08:00
|
|
|
})
|
2012-02-18 17:13:40 -08:00
|
|
|
|
2012-02-23 10:58:06 -08:00
|
|
|
describe('when no match is made', function(){
|
|
|
|
|
it('should should respond with 406 not acceptable', function(done){
|
2012-02-18 17:23:02 -08:00
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2012-02-23 10:58:06 -08:00
|
|
|
.set('Accept', 'foo/bar')
|
2012-02-18 17:23:02 -08:00
|
|
|
.end(function(res){
|
2012-02-23 10:58:06 -08:00
|
|
|
res.should.have.status(406);
|
|
|
|
|
res.body.should.equal('Supports: text/plain, text/html, application/json');
|
2012-02-18 17:23:02 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
})
|
2012-02-18 17:13:40 -08:00
|
|
|
})
|
2012-02-23 10:58:06 -08:00
|
|
|
}
|