Skip to content
Myr

Added req.accepted

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 fd06084 · Browse files · 4 files+72 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+72 added−5 removed

  • tests:+44 added−3 removed
  • other:+28 added−2 removed

4 files+72 added−5 removed

modified:lib/request.js+26 added
146 unmodified lines
147 return !! ~accept.indexOf(type);148};149150/**151 * Return an array of Accepted media types152 * ordered from highest quality to lowest.153 *154 * Examples:155 *156 * [ { value: 'application/json',157 * quality: 1,158 * type: 'application',159 * subtype: 'json' },160 * { value: 'text/html',161 * quality: 0.5,162 * type: 'text',163 * subtype: 'html' } ]164 *165 * @return {Array}166 * @api public167 */168169req.__defineGetter__('accepted', function(){170 var accept = this.header('Accept');171 return accept172 ? utils.parseAccept(accept)173 : [];174});175176/**177 * Return the value of param `name` when present or `defaultValue`.178 *179 * - Checks route placeholders, ex: _/user/:id_180 * - Checks query string params, ex: ?id=12
225 unmodified lines
modified:lib/utils.js+2 added-2 removed
added:test/req.accepted.js+41 added
modified:test/utils.js+3 added-3 removed
64 unmodified lines
65 arr[2].value.should.equal('text/plain');66 })67})6869describe('utils.parseAccepts(str)', function(){69describe('utils.parseAccept(str)', function(){70 it('should provide .type', function(){71 var arr = utils.parseAccepts('text/html');71 var arr = utils.parseAccept('text/html');72 arr[0].type.should.equal('text');73 })74 75 it('should provide .subtype', function(){76 var arr = utils.parseAccepts('text/html');76 var arr = utils.parseAccept('text/html');77 arr[0].subtype.should.equal('html');78 })79})