Added req.accepts() comma-delimited string support

This commit is contained in:
TJ Holowaychuk 2012-03-24 11:36:52 -07:00
parent 86a9e0803a
commit d6d16b7899
3 changed files with 41 additions and 2 deletions

View File

@ -115,7 +115,7 @@ exports.acceptsArray = function(types, str){
*/
exports.accepts = function(type, str){
if (!Array.isArray(type)) type = [type];
if ('string' == typeof type) type = type.split(/ *, */);
return exports.acceptsArray(type, str);
};

View File

@ -43,6 +43,19 @@ describe('req', function(){
})
})
it('should accept a comma-delimited list of types', function(done){
var app = express();
app.use(function(req, res, next){
res.end(req.accepts('json, html'));
});
request(app)
.get('/')
.set('Accept', 'text/html')
.expect('html', done);
})
describe('.accept(types)', function(){
it('should return the first when Accept is not present', function(done){
var app = express();

View File

@ -97,7 +97,7 @@ describe('utils.accepts(type, str)', function(){
.should.equal('text/html');
})
})
describe('when */* is given', function(){
it('should return the value', function(){
utils.accepts('text/html', 'text/plain, */*')
@ -105,6 +105,32 @@ describe('utils.accepts(type, str)', function(){
})
})
describe('when an array is given', function(){
it('should return the best match', function(){
utils.accepts(['html', 'json'], 'text/plain, application/json')
.should.equal('json');
utils.accepts(['html', 'application/json'], 'text/plain, application/json')
.should.equal('application/json');
utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')
.should.equal('text/html');
})
})
describe('when a comma-delimited list is give', function(){
it('should behave like an array', function(){
utils.accepts('html, json', 'text/plain, application/json')
.should.equal('json');
utils.accepts('html, application/json', 'text/plain, application/json')
.should.equal('application/json');
utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')
.should.equal('text/html');
})
})
describe('when accepting type/subtype', function(){
it('should return the value when present', function(){
utils.accepts('text/html', 'text/plain, text/html')