Skip to content

Commit

Permalink
refactored utils.accepts()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 24, 2012
1 parent fdf9692 commit 86a9e08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 51 deletions.
25 changes: 2 additions & 23 deletions lib/utils.js
Expand Up @@ -115,29 +115,8 @@ exports.acceptsArray = function(types, str){
*/

exports.accepts = function(type, str){
if (Array.isArray(type)) return exports.acceptsArray(type, str);

// accept anything when Accept is not present
if (!str) return true;

// resolve mime
if (!~type.indexOf('/')) type = mime.lookup(type);

// split type/subtype
type = type.split('/');

// parse
var accepted = exports.parseAccept(str)
, len = accepted.length
, obj
, ok;

for (var i = 0; i < len; ++i) {
obj = accepted[i];
if (exports.accept(type, obj)) return true;
}

return false;
if (!Array.isArray(type)) type = [type];
return exports.acceptsArray(type, str);
};

/**
Expand Down
52 changes: 24 additions & 28 deletions test/utils.js
Expand Up @@ -85,79 +85,75 @@ describe('utils.parseAccept(str)', function(){

describe('utils.accepts(type, str)', function(){
describe('when a string is not given', function(){
it('should return true', function(){
it('should return the value', function(){
utils.accepts('text/html')
.should.be.true;
.should.equal('text/html');
})
})

describe('when a string is empty', function(){
it('should return true', function(){
it('should return the value', function(){
utils.accepts('text/html', '')
.should.be.true;
.should.equal('text/html');
})
})

describe('when */* is given', function(){
it('should return true', function(){
it('should return the value', function(){
utils.accepts('text/html', 'text/plain, */*')
.should.be.true;
.should.equal('text/html');
})
})

describe('when accepting type/subtype', function(){
it('should return true when present', function(){
it('should return the value when present', function(){
utils.accepts('text/html', 'text/plain, text/html')
.should.be.true;
.should.equal('text/html');
})

it('should return false otherwise', function(){
utils.accepts('text/html', 'text/plain, application/json')
.should.be.false;
it('should return undefined otherwise', function(){
assert(null == utils.accepts('text/html', 'text/plain, application/json'));
})
})

describe('when accepting */subtype', function(){
it('should return true when present', function(){
it('should return the value when present', function(){
utils.accepts('text/html', 'text/*')
.should.be.true;
.should.equal('text/html');
})

it('should return false otherwise', function(){
utils.accepts('text/html', 'image/*')
.should.be.false;
it('should return undefined otherwise', function(){
assert(null == utils.accepts('text/html', 'image/*'));
})
})

describe('when accepting type/*', function(){
it('should return true when present', function(){
it('should return the value when present', function(){
utils.accepts('text/html', '*/html')
.should.be.true;
.should.equal('text/html');
})

it('should return false otherwise', function(){
utils.accepts('text/html', '*/json')
.should.be.false;
it('should return undefined otherwise', function(){
assert(null == utils.accepts('text/html', '*/json'));
})
})

describe('when an extension is given', function(){
it('should return true when present', function(){
it('should return the value when present', function(){
utils.accepts('html', 'text/html, application/json')
.should.be.true;
.should.equal('html');
})

it('should return false otherwise', function(){
utils.accepts('html', 'text/plain, application/json')
.should.be.false;
it('should return undefined otherwise', function(){
assert(null == utils.accepts('html', 'text/plain, application/json'));
})

it('should support *', function(){
utils.accepts('html', 'text/*')
.should.be.true;
.should.equal('html');

utils.accepts('html', '*/html')
.should.be.true;
.should.equal('html');
})
})
})

0 comments on commit 86a9e08

Please sign in to comment.