Skip to content

Commit

Permalink
Fix framework-one#236 by making processRoutes() public and adding a t…
Browse files Browse the repository at this point in the history
…hird, optional, argument that defaults to the current requests HTTP method. This enables testing of processRoutes() more directly by end user code.
  • Loading branch information
seancorfield committed Jun 1, 2014
1 parent 562a2d8 commit 5216e41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
19 changes: 2 additions & 17 deletions framework/one.cfc
Expand Up @@ -1648,7 +1648,7 @@ component {

}

private struct function processRouteMatch( string route, string target, string path ) {
private struct function processRouteMatch( string route, string target, string path, string httpMethod ) {
var regExCache = isFrameworkInitialized() ? application[ variables.framework.applicationKey ].cache.routes.regex : { };
var cacheKey = hash( route & target );
if ( !structKeyExists( regExCache, cacheKey ) ) {
Expand Down Expand Up @@ -1703,7 +1703,7 @@ component {
var routeMatch = { matched = false };
structAppend( routeMatch, regExCache[ cacheKey ] );
if ( !len( path ) || right( path, 1) != '/' ) path &= '/';
var matched = len( routeMatch.method ) ? ( '$' & request._fw1.cgiRequestMethod == routeMatch.method ) : true;
var matched = len( routeMatch.method ) ? ( '$' & httpMethod == routeMatch.method ) : true;
if ( matched && reFind( routeMatch.pattern, path ) ) {
routeMatch.matched = true;
routeMatch.route = route;
Expand Down Expand Up @@ -1769,21 +1769,6 @@ component {
return resourceCache[ cacheKey ];
}

private struct function processRoutes( string path, array routes = getRoutes() ) {
for ( var routePack in routes ) {
for ( var route in routePack ) {
if ( route == 'hint' ) continue;
if ( route == '$RESOURCES' ) {
var routeMatch = processRoutes( path, getResourceRoutes( routePack[ route ] ) );
} else {
var routeMatch = processRouteMatch( route, routePack[ route ], path );
}
if ( routeMatch.matched ) return routeMatch;
}
}
return { matched = false };
}

private void function raiseException( string type, string message, string detail ) {
throw( type = type, message = message, detail = detail );
}
Expand Down
38 changes: 22 additions & 16 deletions tests/frameworkRouteTest.cfc
Expand Up @@ -10,74 +10,80 @@ component extends="tests.InjectableTest" {

public void function testRouteMatchBasics()
{
var match = variables.fw.processRouteMatch("/test", "routed", "/test");
var match = variables.fw.processRouteMatch("/test", "routed", "/test", "GET");
assertTrue(match.matched);
assertEquals("/test/(.*)", match.pattern);
assertEquals("routed/\1", match.target);

match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2/5");
match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2/5", "GET");
assertTrue(match.matched);
assertEquals("/test2/([^/]*)/(.*)", match.pattern);
assertEquals("default.main?id=\1/\2", match.target);

match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2");
match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2", "GET");
assertFalse(match.matched);

match = variables.fw.processRouteMatch("/test/:foo/bar/:baz", "default.main?foo=:foo&baz=:baz", "/test/quux/bar/fnarf");
match = variables.fw.processRouteMatch("/test/:foo/bar/:baz", "default.main?foo=:foo&baz=:baz", "/test/quux/bar/fnarf", "GET");
assertTrue(match.matched);
assertEquals("/test/([^/]*)/bar/([^/]*)/(.*)", match.pattern);
assertEquals("default.main?foo=\1&baz=\2/\3", match.target);
}

public void function testRouteMatchRegex()
{
match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2/5/people");
match = variables.fw.processRouteMatch("/test2/:id", "default.main?id=:id", "/test2/5/people", "GET");
assertTrue(match.matched);

match = variables.fw.processRouteMatch("/(blog|forum|forums)/:action/", "/forum::action/", "/blog/post");
match = variables.fw.processRouteMatch("/(blog|forum|forums)/:action/", "/forum::action/", "/blog/post", "GET");
assertTrue(match.matched);
assertEquals("/forum:post/", rereplace( match.path, match.pattern, match.target ));

match = variables.fw.processRouteMatch("/test2/:id/", "default.main?id=:id", "/test2/5/people");
match = variables.fw.processRouteMatch("/test2/:id/", "default.main?id=:id", "/test2/5/people", "GET");
assertTrue(match.matched, "/test2/:id should match /test2/5/people");

match = variables.fw.processRouteMatch("/test2/:id/$", "default.main?id=:id", "/test2/5/people");
match = variables.fw.processRouteMatch("/test2/:id/$", "default.main?id=:id", "/test2/5/people", "GET");
assertFalse(match.matched, "/test2/:id/$ shouldn't match /test2/5/people");

match = variables.fw.processRouteMatch("/test2/(\d+)/$", "default.main/id/\1/", "/test2/5");
match = variables.fw.processRouteMatch("/test2/(\d+)/$", "default.main/id/\1/", "/test2/5", "GET");
assertTrue(match.matched);
assertEquals("default.main/id/5/", rereplace(match.path, match.pattern, match.target));

match = variables.fw.processRouteMatch("/test2/(\d+)/$", "default.main/id/\1/", "/test2/zz/");
match = variables.fw.processRouteMatch("/test2/(\d+)/$", "default.main/id/\1/", "/test2/zz/", "GET");
assertFalse(match.matched);

var route = "/test/(\d+)/something(\.)?(\w+)?/$";
var target = "default.main/id/\1/type/\3/";
match = variables.fw.processRouteMatch(route, target, "/test/5/something/");
match = variables.fw.processRouteMatch(route, target, "/test/5/something/", "GET");
assertTrue(match.matched, "/test/5/something/ should match");
assertEquals("default.main/id/5/type//", rereplace(match.path, match.pattern, match.target));

match = variables.fw.processRouteMatch(route, target, "/test/5/something.html/");
match = variables.fw.processRouteMatch(route, target, "/test/5/something.html/", "GET");
assertTrue(match.matched);
assertEquals("default.main/id/5/type/html/", rereplace(match.path, match.pattern, match.target));
}

public void function testRouteMatchMethod()
{
match = variables.fw.processRouteMatch("$GET/test/:id", "default.main?id=:id", "/test/5");
match = variables.fw.processRouteMatch("$GET/test/:id", "default.main?id=:id", "/test/5", "GET");
assertTrue(match.matched);

match = variables.fw.processRouteMatch("$POST/test/:id", "default.main?id=:id", "/test/5");
match = variables.fw.processRouteMatch("$POST/test/:id", "default.main?id=:id", "/test/5", "GET");
assertFalse(match.matched);

match = variables.fw.processRouteMatch("$GET/test/:id", "default.main?id=:id", "/test/5", "POST");
assertFalse(match.matched);

match = variables.fw.processRouteMatch("$POST/test/:id", "default.main?id=:id", "/test/5", "POST");
assertTrue(match.matched);
}

public void function testRouteMatchRedirect()
{
match = variables.fw.processRouteMatch("/test/:id", "default.main?id=:id", "/test/5");
match = variables.fw.processRouteMatch("/test/:id", "default.main?id=:id", "/test/5", "GET");
assertTrue(match.matched);
assertFalse(match.redirect);

match = variables.fw.processRouteMatch("/test/:id", "302:default.main?id=:id", "/test/5");
match = variables.fw.processRouteMatch("/test/:id", "302:default.main?id=:id", "/test/5", "GET");
assertTrue(match.matched);
assertTrue(match.redirect);
assertEquals(302, match.statusCode);
Expand Down

0 comments on commit 5216e41

Please sign in to comment.