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
Provenance
- Commit
a8fd8cb- Tree
9cb0335- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
fd06084
4 files changed+72 added−5 removed
modified:lib/request.js+26 added
146 unmodified lines147 return !! ~accept.indexOf(type);148 };149 150 /**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+ */168+169+req.__defineGetter__('accepted', function(){170+ var accept = this.header('Accept');171+ return accept172+ ? utils.parseAccept(accept)173+ : [];174+});175+176+/**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=12225 unmodified linesmodified:lib/utils.js+2 added-2 removed
59 unmodified lines606162636464656667682 unmodified lines7172737475757677787960 unmodified lines59 unmodified lines .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');};
/** * Parse accepts `str`, returning * Parse accept `str`, returning * an array objects containing * `.type` and `.subtype` along * with the values provided by * `parseQuality()`.2 unmodified lines * @return {Type} * @api public */
exports.parseAccepts = function(str){exports.parseAccept = function(str){ return exports .parseQuality(str) .map(function(obj){ var parts = obj.value.split('/');60 unmodified linesadded:test/req.accepted.js+41 added
1234567891011121314151617181920212223242526272829303132333435363738394041
var express = require('../') , request = require('./support/http');
describe('req', function(){ describe('.accepted', function(){ it('should return an array of accepted content-types', function(done){ var app = express();
app.use(function(req, res){ req.accepted[0].value.should.equal('application/json'); req.accepted[1].value.should.equal('text/html'); res.end(); });
request(app) .get('/') .set('Accept', 'text/html;q=.5, application/json') .end(function(){ done(); }); })
describe('when Accept is not present', function(){ it('should default to []', function(done){ var app = express();
app.use(function(req, res){ req.accepted.should.have.length(0); res.end(); });
request(app) .get('/') .end(function(){ done(); }); }) }) })})modified:test/utils.js+3 added-3 removed
64 unmodified lines65 arr[2].value.should.equal('text/plain');66 })67 })68 69-describe('utils.parseAccepts(str)', function(){69+describe('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 })