Skip to content

Commit

Permalink
Issue 944:
Browse files Browse the repository at this point in the history
Changing Router#match() signature from (req[, i]) to (method, url[, i]), and making it public
  • Loading branch information
riadhchtara committed Jul 4, 2012
1 parent 136f054 commit f110248
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
24 changes: 21 additions & 3 deletions lib/router/index.js
Expand Up @@ -103,7 +103,7 @@ Router.prototype._dispatch = function(req, res, next){
}

// match route
req.route = route = self.match(req, i);
req.route = route = self.matchReq(req, i);

// implied OPTIONS
if (!route && 'OPTIONS' == req.method) return self._options(req, res);
Expand Down Expand Up @@ -220,7 +220,7 @@ Router.prototype._optionsFor = function(path){
* @api private
*/

Router.prototype.match = function(req, i, head){
Router.prototype.matchReq = function(req, i, head){
var method = req.method.toLowerCase()
, url = parse(req)
, path = url.pathname
Expand All @@ -230,7 +230,7 @@ Router.prototype.match = function(req, i, head){

// HEAD support
if (!head && 'head' == method) {
route = this.match(req, i, true);
route = this.matchReq(req, i, true);
if (route) return route;
method = 'get';
}
Expand All @@ -249,6 +249,24 @@ Router.prototype.match = function(req, i, head){
}
};

/**
* Attempt to match a route for `method`
* and `url` with optional starting
* index of `i` defaulting to 0.
*
* @param {String} method
* @param {String} url
* @param {Number} i
* @return {Route}
* @api public
*/

Router.prototype.match = function(method, url, i, head){
method = method.toLowerCase();
var req = { method: method, url: url };
return this.matchReq(req, i, head);
};

/**
* Route `method`, `path`, and one or more callbacks.
*
Expand Down
35 changes: 30 additions & 5 deletions test/Router.js
Expand Up @@ -12,27 +12,52 @@ describe('Router', function(){
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.matchRe(method, url, 1);
route.path.should.equal('/foob?');

var route = router.match(method, url, 2);
assert(!route);

req.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');
})
})
Expand Down

0 comments on commit f110248

Please sign in to comment.