Merge pull request #1212 from riadhchtara/Issue944
Before adoption: this commit came in with the history the project adopted, so no landing records it.
issue#944 : Changing Router#match() signature
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parents 4f7c4d1, 8ccd89f · Browse files · 2 files·+51 added−9 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+51 added−9 removed
- tests:+30 added−5 removed
- other:+21 added−4 removed
Provenance
- Commit
e770b67- Tree
03cee60- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent 1
4f7c4d1- Parent 2
8ccd89f
2 files changed+51 added−9 removed
modified:lib/router/index.js+21 added-4 removed
1-1 /**2 * Module dependencies.3 */4 96 unmodified lines101 pass(req._route_index + 1, err);102 }103 104 // match route106- req.route = route = self.match(req, i);105+ req.route = route = self.matchReq(req, i);106 107 // implied OPTIONS108 if (!route && 'OPTIONS' == req.method) return self._options(req, res);109 108 unmodified lines218 * @return {Route}219 * @api private220 */221 223-Router.prototype.match = function(req, i, head){222+Router.prototype.matchReq = function(req, i, head){223 var method = req.method.toLowerCase()224 , url = parse(req)225 , path = url.pathname226 , routes = this.map1 unmodified line228 , route;229 230 // HEAD support231 if (!head && 'head' == method) {233- route = this.match(req, i, true);232+ route = this.matchReq(req, i, true);233 if (route) return route;234 method = 'get';235 }236 11 unmodified lines248 }249 };250 251 /**252+ * Attempt to match a route for `method`253+ * and `url` with optional starting254+ * index of `i` defaulting to 0.255+ *256+ * @param {String} method257+ * @param {String} url258+ * @param {Number} i259+ * @return {Route}260+ * @api private261+ */262+263+Router.prototype.match = function(method, url, i, head){264+ method = method.toLowerCase(); 265+ var req = { method: method, url: url };266+ return this.matchReq(req, i, head);267+};268+269+/**270 * Route `method`, `path`, and one or more callbacks.271 *272 * @param {String} method273 * @param {String} path23 unmodified linesmodified:test/Router.js+30 added-5 removed
10 unmodified lines111213141515161718192021222324252627282930313233343536373839404142434445462247484950512752535455315657585935606162636414 unmodified lines10 unmodified lines router = new Router; app = express(); })
describe('.match(req, i)', function(){ describe('.match(method, url, 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 method = 'GET' , url = '/foo?bar=baz';
var route = router.match(method, url, 0); route.constructor.name.should.equal('Route'); route.method.should.equal('get'); route.path.should.equal('/foo');
var route = router.match(method, url, 1); route.path.should.equal('/foob?');
var route = router.match(method, url, 2); assert(!route);
url = '/bar'; var route = router.match(method, url); route.path.should.equal('/bar'); }) }) describe('.matchReq(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); var route = router.matchReq(req, 0); route.constructor.name.should.equal('Route'); route.method.should.equal('get'); route.path.should.equal('/foo');
var route = router.match(req, 1); var route = router.matchReq(req, 1); req._route_index.should.equal(1); route.path.should.equal('/foob?');
var route = router.match(req, 2); var route = router.matchReq(req, 2); assert(!route);
req.url = '/bar'; var route = router.match(req); var route = router.matchReq(req); route.path.should.equal('/bar'); }) })
14 unmodified lines