Route: support request method in route matching and fix _match/_meta in compile #535

Closed
wants to merge 3 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+81 −3
Split
View
@@ -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,11 @@ public function match(array $options = array(), $context = null) {
}
}
+ 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 +407,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 +415,8 @@ public function compile() {
$this->_meta[$key] = $value;
}
+ $this->_match = $this->_params;
+
if ($this->_template === '/' || $this->_template === '') {
$this->_pattern = '@^/*$@';
return;
@@ -542,6 +542,79 @@ 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 without request method matches all
+ $route = new Route(array(
+ 'template' => '/resource',
+ 'params' => $parameters
+ ));
+
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create'));
+ $this->assertEqual('/resource', $result);
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'GET'));
+ $this->assertEqual('/resource', $result);
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'POST'));
+ $this->assertEqual('/resource', $result);
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'PUT'));
+ $this->assertEqual('/resource', $result);
+
+
+ // Test route with request method GET
+ $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);
+
+
@nateabele

nateabele Jun 13, 2012

Owner

Extra newlines?

+ }
+
/**
* Tests that routes with optional trailing elements have unnecessary slashes trimmed.
*/