Skip to content
Browse files

added request method to route matching and fixed different behaviour …

…of for _match/_meta depending of the presence of parameters in route templates.
  • Loading branch information...
1 parent 2d5fd7f commit 5c61d35ab6ff2b8a4efcac72b38bd1d4a7320364 @bquilitz bquilitz committed Jun 11, 2012
Showing with 68 additions and 3 deletions.
  1. +9 −3 net/http/Route.php
  2. +59 −0 tests/cases/net/http/RouteTest.php
View
12 net/http/Route.php
@@ -260,7 +260,7 @@ public function parse($request, array $options = array()) {
* @return mixed
*/
public function match(array $options = array(), $context = null) {
- $defaults = array('action' => 'index');
+ $defaults = array('action' => 'index', 'http:method' => 'GET');
$query = null;
if (!$this->_config['continue']) {
@@ -273,6 +273,12 @@ public function match(array $options = array(), $context = null) {
}
}
+ // check http method
+ if (isset($this->_meta['http:method']) && $options['http:method'] != $this->_meta['http:method'] ) {
+ return false;
+ }
+ unset($options['http:method']);
+
if (!$options = $this->_matchKeys($options)) {
return false;
}
@@ -402,8 +408,6 @@ public function export() {
* @return void
*/
public function compile() {
- $this->_match = $this->_params;
-
foreach ($this->_params as $key => $value) {
if (!strpos($key, ':')) {
continue;
@@ -412,6 +416,8 @@ public function compile() {
$this->_meta[$key] = $value;
}
+ $this->_match = $this->_params;
+
if ($this->_template === '/' || $this->_template === '') {
$this->_pattern = '@^/*$@';
return;
View
59 tests/cases/net/http/RouteTest.php
@@ -542,6 +542,65 @@ public function testMatchingEmptyRoute() {
$this->assertEqual('/', $url);
}
+
+ /**
+ * Test route matching for routes with specified request method (http:method)
+ */
+ public function testMatchWithRequestMethod() {
+ $parameters = array('controller' => 'resource', 'action' => 'create');
+
+ $route = new Route(array(
+ 'template' => '/resource',
+ 'params' => $parameters + array('http:method' => 'POST')
+ ));
+
+ //success
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'POST'));
+ $this->assertEqual('/resource', $result);
+
+ // no match because of missing POST
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create'));
+ $this->assertEqual(false, $result);
+
+
+ // we test params in route here because this made a difference in the original li3 routing implementation
+ $route = new Route(array(
+ 'template' => '/{:param}',
+ 'params' => $parameters + array('http:method' => 'POST')
+ ));
+
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'param' => 'value', 'http:method' => 'POST'));
+ $this->assertEqual('/value', $result);
+
+ // no match because of missing POST
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'param' => 'value'));
+ $this->assertEqual(false, $result);
+
+ $route = new Route(array(
+ 'template' => '/resource',
+ 'params' => $parameters + array('http:method' => 'GET')
+ ));
+
+
+ //success
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'GET'));
+ $this->assertEqual('/resource', $result);
+
+ // success even with missing GET because it's the default
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create'));
+ $this->assertEqual('/resource', $result);
+
+ // other request methods should fail
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'POST'));
+ $this->assertEqual(false, $result);
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'PUT'));
+ $this->assertEqual(false, $result);
+
+
+ }
+
+
+
/**
* Tests that routes with optional trailing elements have unnecessary slashes trimmed.
*/

0 comments on commit 5c61d35

Please sign in to comment.
Something went wrong with that request. Please try again.