Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored utils.accepts()

  • Loading branch information...
commit 86a9e0803aaea5fc4a82b8d4c5dd6eee0941b0c7 1 parent fdf9692
@tj tj authored
Showing with 26 additions and 51 deletions.
  1. +2 −23 lib/utils.js
  2. +24 −28 test/utils.js
View
25 lib/utils.js
@@ -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);
};
/**
View
52 test/utils.js
@@ -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');
})
})
})
Please sign in to comment.
Something went wrong with that request. Please try again.