Skip to content

Commit

Permalink
Added req.accepts() comma-delimited string support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 24, 2012
1 parent 86a9e08 commit d6d16b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/utils.js
Expand Up @@ -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);
};

Expand Down
13 changes: 13 additions & 0 deletions test/req.accepts.js
Expand Up @@ -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();
Expand Down
28 changes: 27 additions & 1 deletion test/utils.js
Expand Up @@ -97,14 +97,40 @@ 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, */*')
.should.equal('text/html');
})
})

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')
Expand Down

0 comments on commit d6d16b7

Please sign in to comment.