Added utils.accepts(type, str)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 376a5da · Browse files · 2 files·+121 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+121 added−0 removed
- tests:+79 added−0 removed
- other:+42 added−0 removed
Provenance
- Commit
36be2b0- Tree
ddc8289- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
376a5da
2 files changed+121 added−0 removed
modified:lib/utils.js+42 added
4 unmodified lines5678910111213141516171847 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810972 unmodified lines4 unmodified lines * MIT Licensed */
/** * Module dependencies. */
var mime = require('mime');
/** * Check if `path` looks absolute. * * @param {String} path * @return {Boolean}47 unmodified lines .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');};
/** * Check if `type` is acceptable based on * the given `str`. * * @param {String} type * @param {String} str * @return {Boolean} * @api public */
exports.accepts = function(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]; ok = (type[0] == obj.type || '*' == obj.type) && (type[1] == obj.subtype || '*' == obj.subtype); if (ok) return true; }
return false;};
/** * Parse accept `str`, returning * an array objects containing * `.type` and `.subtype` along * with the values provided by72 unmodified linesmodified:test/utils.js+79 added
74 unmodified lines75 it('should provide .subtype', function(){76 var arr = utils.parseAccept('text/html');77 arr[0].subtype.should.equal('html');78 })79+})80+81+describe('utils.accepts(type, str)', function(){82+ describe('when a string is not given', function(){83+ it('should return true', function(){84+ utils.accepts('text/html')85+ .should.be.true;86+ })87+ })88+ 89+ describe('when a string is empty', function(){90+ it('should return true', function(){91+ utils.accepts('text/html', '')92+ .should.be.true;93+ })94+ })95+ 96+ describe('when */* is given', function(){97+ it('should return true', function(){98+ utils.accepts('text/html', 'text/plain, */*')99+ .should.be.true;100+ })101+ })102+103+ describe('when accepting type/subtype', function(){104+ it('should return true when present', function(){105+ utils.accepts('text/html', 'text/plain, text/html')106+ .should.be.true;107+ })108+ 109+ it('should return false otherwise', function(){110+ utils.accepts('text/html', 'text/plain, application/json')111+ .should.be.false;112+ })113+ })114+ 115+ describe('when accepting */subtype', function(){116+ it('should return true when present', function(){117+ utils.accepts('text/html', 'text/*')118+ .should.be.true;119+ })120+ 121+ it('should return false otherwise', function(){122+ utils.accepts('text/html', 'image/*')123+ .should.be.false;124+ })125+ })126+ 127+ describe('when accepting type/*', function(){128+ it('should return true when present', function(){129+ utils.accepts('text/html', '*/html')130+ .should.be.true;131+ })132+ 133+ it('should return false otherwise', function(){134+ utils.accepts('text/html', '*/json')135+ .should.be.false;136+ })137+ })138+ 139+ describe('when an extension is given', function(){140+ it('should return true when present', function(){141+ utils.accepts('html', 'text/html, application/json')142+ .should.be.true;143+ })144+ 145+ it('should return false otherwise', function(){146+ utils.accepts('html', 'text/plain, application/json')147+ .should.be.false;148+ })149+ 150+ it('should support *', function(){151+ utils.accepts('html', 'text/*')152+ .should.be.true;153+154+ utils.accepts('html', '*/html')155+ .should.be.true;156+ })157+ })158 })