Added array support. Closes #1053
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 a7a8dcd · Browse files · 5 files·+146 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+146 added−41 removed
- tests:+67 added−0 removed
- other:+79 added−41 removed
Provenance
- Commit
43b35a4- Tree
29bec60- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
a7a8dcd
5 files changed+146 added−41 removed
modified:lib/request.js+10 added-3 removed
56 unmodified lines57 }58 };59 60 /**61- * Check if the given `type` is acceptable,61+ * Check if the given `type(s)` is acceptable,62 * otherwise you should respond with 406 "Not Acceptable".63 *64 * Examples:65 * 11 unmodified lines77 * req.accepts('image/png');78 * req.accepts('png');79 * // => false80 *81+ * // Accept: text/*; application/json82+ * req.accepts(['html', 'json']);83+ * // => "html"84+ *85+ * req.accepts(['text/html', 'application/json']);86+ * // => "text/html"87+ *81- * @param {String} type82- * @return {Boolean}88+ * @param {String|Array} type(s)89+ * @return {Boolean|String}90 * @api public91 */92 93 req.accepts = function(type){292 unmodified linesmodified:lib/response.js+5 added-35 removed
12 unmodified lines13 , http = require('http')14 , path = require('path')15 , connect = require('connect')16 , utils = connect.utils17- , accept = require('./utils').accept17+ , normalizeType = require('./utils').normalizeType18+ , normalizeTypes = require('./utils').normalizeTypes19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , mime = require('mime')22 , basename = path.basename317 unmodified lines340 */341 342 res.format = function(obj){343 var keys = Object.keys(obj)343- , types = []344 , req = this.req345 , next = req.next346- , accepted = req.accepted347- , acceptedlen = accepted.length348- , type349- , key;346+ , key = req.accepts(keys);347 351- // normalize extnames -> mime352- if (acceptedlen) {353- for (var i = 0; i < keys.length; ++i) {354- types.push(~keys[i].indexOf('/')355- ? keys[i]356- : mime.lookup(keys[i]));357- }358- }359-360- // determine most acceptable format361- out:362- for (var i = 0; i < acceptedlen; ++i) {363- for (var j = 0, jlen = types.length; j < jlen; ++j) {364- if (accept(types[j].split('/'), accepted[i])) {365- key = keys[j];366- type = types[j];367- break out;368- }369- }370- }371-372- // default to the first373- if (!acceptedlen) {374- key = keys[0];375- type = types[0];376- }377-348 if (key) {379- this.set('Content-Type', type);349+ this.set('Content-Type', normalizeType(key));350 obj[key](req, this, next);351 } else {352 var err = new Error('Not Acceptable');353 err.status = 406;384- err.types = types;354+ err.types = normalizeTypes(keys);355 next(err);356 }357 358 return this;277 unmodified linesmodified:lib/utils.js+64 added-3 removed
44 unmodified lines45 return ret;46 };47 48 /**49- * Check if `type` is acceptable based on50- * the given `str`.49+ * Normalize the given `type`, for example "html" becomes "text/html".50 *51 * @param {String} type52+ * @return {String}53+ * @api private54+ */55+56+exports.normalizeType = function(type){57+ return ~type.indexOf('/') ? type : mime.lookup(type);58+};59+60+/**61+ * Normalize `types`, for example "html" becomes "text/html".62+ *63+ * @param {Array} types64+ * @return {Array}65+ * @api private66+ */67+68+exports.normalizeTypes = function(types){69+ var ret = [];70+71+ for (var i = 0; i < types.length; ++i) {72+ ret.push(~types[i].indexOf('/')73+ ? types[i]74+ : mime.lookup(types[i]));75+ }76+77+ return ret;78+};79+80+/**81+ * Return the acceptable type in `types`, if any.82+ *83+ * @param {Array} types84 * @param {String} str54- * @return {Boolean}85+ * @return {String}86 * @api private87 */88 89+exports.acceptsArray = function(types, str){90+ // accept anything when Accept is not present91+ if (!str) return types[0];92+93+ // parse94+ var accepted = exports.parseAccept(str)95+ , normalized = exports.normalizeTypes(types)96+ , len = accepted.length;97+98+ for (var i = 0; i < len; ++i) {99+ for (var j = 0, jlen = types.length; j < jlen; ++j) {100+ if (exports.accept(normalized[j].split('/'), accepted[i])) {101+ return types[j];102+ }103+ }104+ }105+};106+107+/**108+ * Check if `type(s)` are acceptable based on109+ * the given `str`.110+ *111+ * @param {String|Array} type(s)112+ * @param {String} str113+ * @return {Boolean|String}114+ * @api private115+ */116+117 exports.accepts = function(type, str){118+ if (Array.isArray(type)) return exports.acceptsArray(type, str);119+120 // accept anything when Accept is not present121 if (!str) return true;122 123 // resolve mime113 unmodified linesmodified:test/req.accepts.js+66 added
40 unmodified lines414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011140 unmodified lines .set('Accept', 'text/html') .expect('no', done); }) })
describe('.accept(types)', function(){ it('should return the first when Accept is not present', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['json', 'html'])); });
request(app) .get('/') .expect('json', done); })
it('should return the first acceptable type', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['json', 'html'])); });
request(app) .get('/') .set('Accept', 'text/html') .expect('html', done); })
it('should return false when no match is made', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['text/html', 'application/json']) ? 'yup' : 'nope'); });
request(app) .get('/') .set('Accept', 'foo/bar, bar/baz') .expect('nope', done); })
it('should take quality into account', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['text/html', 'application/json'])); });
request(app) .get('/') .set('Accept', '*/html; q=.5, application/json') .expect('application/json', done); })
it('should return the first acceptable type with canonical mime types', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['application/json', 'text/html'])); });
request(app) .get('/') .set('Accept', '*/html') .expect('text/html', done); }) })})modified:test/res.format.js+1 added
24 unmodified lines25262728293031323372 unmodified lines24 unmodified lines });});
app.use(function(err, req, res, next){ if (!err.types) throw err; res.send(err.status, 'Supports: ' + err.types.join(', '));})
var app2 = express();72 unmodified lines