Merge branch 'master' of github.com:visionmedia/express
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 · parents dd7af29, edfe50e · Browse files · 8 files·+239 added−86 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+239 added−86 removed
- tests:+153 added−1 removed
- other:+86 added−85 removed
Provenance
- Commit
613bdc9- Tree
3103308- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
dd7af29- Parent 2
edfe50e
8 files changed+239 added−86 removed
modified:lib/router/index.js+33 added-28 removed
25 unmodified lines26 27 var methods = exports.methods = require('./methods');28 29 /**30- * Initialize a new `Router` with the given `app`.30+ * Initialize a new `Router` with the given `options`.31 * 32- * @param {express.HTTPServer} app32+ * @param {Object} options33 * @api private34 */35 36-function Router(app) {36+function Router(options) {37+ options = options || {};38 var self = this;38- this.app = app;39 this.routes = new Collection;40 this.map = {};41 this.params = {};42 this._params = [];43+ this.caseSensitive = options.caseSensitive;44+ this.strict = options.strict;45 46 this.middleware = function(req, res, next){47 self._dispatch(req, res, next);48 };92 unmodified lines141 || method == 'all');142 });143 };144 143-/**144- * Return routes with regexps that match the given `url`.145- *146- * @param {String} method147- * @param {String} url148- * @return {Collection}149- * @api public150- */145+// /**146+// * Return routes with regexps that match the given `url`.147+// *148+// * @param {String} method149+// * @param {String} url150+// * @return {Collection}151+// * @api public152+// */153+// 154+// Router.prototype.match = function(method, url){155+// return this.find(function(route){156+// return route.match(url)157+// && (route.method == method158+// || method == 'all');159+// });160+// };161 152-Router.prototype.match = function(method, url){153- return this.find(function(route){154- return route.match(url)155- && (route.method == method156- || method == 'all');157- });158-};159-162 /**163 * Find routes based on the return value of `fn`164 * which is invoked once per route.165 *50 unmodified lines216 pass(req._route_index + 1, err);217 }218 219 // match route218- req.route = route = self._match(req, i);220+ req.route = route = self.match(req, i);221 222 // implied OPTIONS223 if (!route && 'OPTIONS' == req.method) return self._options(req, res);224 98 unmodified lines323 };324 325 /**326 * Attempt to match a route for `req`325- * starting from offset `i`.327+ * with optional starting index of `i`328+ * defaulting to 0.329 *330 * @param {IncomingMessage} req331 * @param {Number} i332 * @return {Route}333 * @api private334 */335 333-Router.prototype._match = function(req, i, head){336+Router.prototype.match = function(req, i, head){337 var method = req.method.toLowerCase()338 , url = parse(req.url)339 , path = url.pathname340 , routes = this.map341+ , i = i || 0342 , captures343 , route344 , keys;345 346 // HEAD support347 // TODO: clean this up348 if (!head && 'head' == method) {349 // attempt lookup346- route = this._match(req, i, true);350+ route = this.match(req, i, true);351 if (route) return route;352 353 // default to GET as res.render() / res.send()354 // etc support HEAD40 unmodified lines395 * @return {Router} for chaining396 * @api private397 */398 395-Router.prototype._route = function(method, path, callbacks){399+Router.prototype.route = function(method, path, callbacks){400 var app = this.app401+ , method = method.toLowerCase()402 , callbacks = utils.flatten([].slice.call(arguments, 2));403 404 // ensure path was given400- if (!path) throw new Error('app.' + method + '() requires a path');405+ if (!path) throw new Error('Router#' + method + '() requires a path');406 407 // create the route408 var route = new Route(method, path, callbacks, {404- sensitive: app.enabled('case sensitive routing')405- , strict: app.enabled('strict routing')409+ sensitive: this.caseSensitive410+ , strict: this.strict411 });412 413 // add it414 (this.map[method] = this.map[method] || []).push(route);3 unmodified linesmodified:lib/application.js+4 added-35 removed
41 unmodified lines42 var self = this;43 this.cache = {};44 this.settings = {};45 this.engines = {};46- this.isCallbacks = {};46 this.viewCallbacks = [];47 this.defaultConfiguration();48 49 // route reflection methods1 unmodified line51 self.lookup[method] = function(path){52 return self._router.lookup(method, path);53 };54 56- self.match[method] = function(path){57- return self._router.match(method, path);58- };59-55 self.remove[method] = function(path){56 return self._router.lookup(method, path).remove();57 };58 });59 60 // del -> delete61 self.lookup.del = self.lookup.delete;67- self.match.del = self.match.delete;62 self.remove.del = self.remove.delete;63 };64 65 /**35 unmodified lines101 this._router = new Router(this);102 this.routes = this._router.routes;103 this.__defineGetter__('router', function(){104 this._usedRouter = true;105+ this._router.caseSensitive = this.enabled('case sensitive routing');106+ this._router.strict = this.enabled('strict routing');111- return self._router.middleware;107+ return this._router.middleware;108 });109 110 // default locals111 this.locals.settings = this.settings;29 unmodified lines141 return this._router.lookup('all', path);142 };143 144 /**149- * Lookup routes matching the given `url`.150- *151- * @param {Stirng} url152- * @return {Array}153- * @api public154- */155-156-app.match = function(url){157- return this._router.match('all', url);158-};159-160-/**145 * Proxy `connect#use()` to apply settings to146 * mounted applications.147 *148 * @param {String|Function|Server} route141 unmodified lines290 return this;291 };292 293 /**310- * Register the given callback `fn` for the given `type`.311- *312- * @param {String} type313- * @param {Function} fn314- * @return {app} for chaining315- * @api public316- */317-318-app.is = function(type, fn){319- if (!fn) return this.isCallbacks[type];320- this.isCallbacks[type] = fn;321- return this;322-};323-324-/**294 * Assign `setting` to `val`, or return `setting`'s value.295 * Mounted servers inherit their parent server's settings.296 *297 * @param {String} setting136 unmodified lines434 app[method] = function(path){435 if ('get' == method && 1 == arguments.length) return this.set(path); 436 var args = [method].concat([].slice.call(arguments));437 if (!this._usedRouter) this.use(this.router);469- return this._router._route.apply(this._router, args);438+ return this._router.route.apply(this._router, args);439 }440 });441 442 /**84 unmodified linesmodified:lib/request.js+11 added-22 removed
265 unmodified lines266 *267 * // With Content-Type: text/html; charset=utf-8268 * req.is('html');269 * req.is('text/html');270+ * req.is('text/*');271 * // => true272 * 273 * // When Content-Type is application/json274 * req.is('json');275 * req.is('application/json');276+ * req.is('application/*');277 * // => true278 * 279 * req.is('html');280 * // => false281 * 280- * Ad-hoc callbacks can also be registered with Express, to perform281- * assertions again the request, for example if we need an expressive282- * way to check if our incoming request is an image, we can register "an image"283- * callback:284- * 285- * app.is('an image', function(req){286- * return 0 == req.headers['content-type'].indexOf('image');287- * });288- *289- * app.is('an attachment', function(req){290- * return 0 == req.headers['content-disposition'].indexOf('attachment');291- * });292- *282 * Now within our route callbacks, we can use to to assert content types283 * such as "image/jpeg", "image/png", etc.284 * 285 * app.post('/image/upload', function(req, res, next){297- * if (req.is('an image')) {286+ * if (req.is('image/*')) {287 * // do something288 * } else {289 * next();290 * }4 unmodified lines295 * @api public296 */297 298 req.is = function(type){310- var fn = this.app.is(type);311- if (fn) return fn(this);312- var contentType = this.headers['content-type'];313- if (!contentType) return;299+ var ct = this.headers['content-type'];300+ if (!ct) return false;301+ ct = ct.split(';')[0];302 if (!~type.indexOf('/')) type = mime.lookup(type);303 if (~type.indexOf('*')) {304 type = type.split('/');317- contentType = contentType.split('/');318- if ('*' == type[0] && type[1] == contentType[1]) return true;319- if ('*' == type[1] && type[0] == contentType[0]) return true;305+ ct = ct.split('/');306+ if ('*' == type[0] && type[1] == ct[1]) return true;307+ if ('*' == type[1] && type[0] == ct[0]) return true;308+ return false;309 }321- return !! ~contentType.indexOf(type);310+ return !! ~ct.indexOf(type);311 };312 313 /**314 * Short-hand for `req.connection.encrypted`.56 unmodified linesadded:test/req.is.js+100 added
1+2+var express = require('../')3+ , request = require('./support/http');4+5+function req(ct) {6+ var req = {7+ headers: { 'content-type': ct }8+ , __proto__: express.request9+ };10+11+ return req;12+}13+14+describe('req.is()', function(){15+ it('should ignore charset', function(){16+ req('application/json; charset=utf-8')17+ .is('json')18+ .should.be.true;19+ })20+21+ describe('when content-type is not present', function(){22+ it('should return false', function(){23+ req('')24+ .is('json')25+ .should.be.false;26+ })27+ })28+29+ describe('when given an extension', function(){30+ it('should lookup the mime type', function(){31+ req('application/json')32+ .is('json')33+ .should.be.true;34+35+ req('text/html')36+ .is('json')37+ .should.be.false;38+ })39+ })40+41+ describe('when given a mime type', function(){42+ it('should match', function(){43+ req('application/json')44+ .is('application/json')45+ .should.be.true;46+47+ req('image/jpeg')48+ .is('application/json')49+ .should.be.false;50+ })51+ })52+53+ describe('when given */subtype', function(){54+ it('should match', function(){55+ req('application/json')56+ .is('*/json')57+ .should.be.true;58+59+ req('image/jpeg')60+ .is('*/json')61+ .should.be.false;62+ })63+64+ describe('with a charset', function(){65+ it('should match', function(){66+ req('text/html; charset=utf-8')67+ .is('*/html')68+ .should.be.true;69+70+ req('text/plain; charset=utf-8')71+ .is('*/html')72+ .should.be.false;73+ })74+ })75+ })76+77+ describe('when given type/*', function(){78+ it('should match', function(){79+ req('image/png')80+ .is('image/*')81+ .should.be.true;82+83+ req('text/html')84+ .is('image/*')85+ .should.be.false;86+ })87+88+ describe('with a charset', function(){89+ it('should match', function(){90+ req('text/html; charset=utf-8')91+ .is('text/*')92+ .should.be.true;93+94+ req('something/html; charset=utf-8')95+ .is('text/*')96+ .should.be.false;97+ })98+ })99+ })100+})added:test/Router.js+53 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
var express = require('../') , Router = express.Router , request = require('./support/http') , assert = require('assert');
describe('Router', function(){ var router, app;
beforeEach(function(){ router = new Router; app = express(); })
describe('.match(req, i)', function(){ it('should match based on index', function(){ router.route('get', '/foo', function(){}); router.route('get', '/foob?', function(){}); router.route('get', '/bar', function(){}); var req = { method: 'GET', url: '/foo?bar=baz' };
var route = router.match(req, 0); route.constructor.name.should.equal('Route'); route.method.should.equal('get'); route.path.should.equal('/foo');
var route = router.match(req, 1); req._route_index.should.equal(1); route.path.should.equal('/foob?');
var route = router.match(req, 2); assert(!route);
req.url = '/bar'; var route = router.match(req); route.path.should.equal('/bar'); }) })
describe('.middleware', function(){ it('should dispatch', function(done){ router.route('get', '/foo', function(req, res){ res.send('foo'); });
app.use(router.middleware);
request(app) .get('/foo') .expect('foo', done); }) })})No newline at end of filemodified:test/utils.js-1 removed
122345158 unmodified linesExpand all
var utils = require('../lib/utils') , assert = require('assert');
describe('utils.isAbsolute()', function(){158 unmodified linesExpand alladded:client.js+25 added
1+2+var http = require('http');3+4+var times = 50;5+6+while (times--) {7+ var req = http.request({8+ port: 30009+ , method: 'POST'10+ , headers: { 'Content-Type': 'application/x-www-form-urlencoded' }11+ });12+13+ req.on('response', function(res){14+ console.log(res.statusCode);15+ });16+17+ var n = 500000;18+ while (n--) {19+ req.write('foo=bar&bar=baz&');20+ }21+22+ req.write('foo=bar&bar=baz');23+24+ req.end();25+}added:test.js+13 added
12345678910111213
var express = require('./');var app = express();
app.use(express.logger('dev'));app.use(express.bodyParser());
app.post('/', function(req, res){ res.send('done\n');});
app.listen(3000);console.log('listening on port 3000');No newline at end of file