Browse files

Fixing cases where matching routes with default values returns an emp…

…ty string.
  • Loading branch information...
1 parent c3419a4 commit 1c121370e074a7625cacbc08d593c01f88afb492 @nateabele nateabele committed Aug 9, 2012
Showing with 21 additions and 1 deletion.
  1. +1 −1 net/http/Route.php
  2. +20 −0 tests/cases/net/http/RouteTest.php
View
2 net/http/Route.php
@@ -373,7 +373,7 @@ protected function _write($options, $defaults) {
}
$template = str_replace($rpl, $value, $template);
}
- return $template;
+ return $template ?: '/';
}
/**
View
20 tests/cases/net/http/RouteTest.php
@@ -676,6 +676,26 @@ public function testTwoParameterRoutes() {
$this->assertEqual($expected, $actual);
}
+
+ /**
+ * Tests that a single route with default values matches its default parameters, as well as
+ * non-default parameters.
+ */
+ public function testSingleRouteWithDefaultValues() {
+ $defaults = array('controller' => 'Admin', 'action' => 'index');
+
+ $route = new Route(compact('defaults') + array(
+ 'template' => '/{:controller}/{:action}',
+ 'pattern' => '@^(?:/(?P[^\\/]+)?)?(?:/(?P[^\\/]+)?)?$@u',
+ 'params' => array('controller' => 'Admin', 'action' => 'index'),
+ 'keys' => array('controller' => 'controller', 'action' => 'action'),
+ 'match' => array()
+ ));
+ $this->assertIdentical('/', $route->match($defaults));
+
+ $nonDefault = array('controller' => 'Admin', 'action' => 'view');
+ $this->assertIdentical('/Admin/view', $route->match($nonDefault));
+ }
}
?>

0 comments on commit 1c12137

Please sign in to comment.