Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support request method in route matching (close #535).

  • Loading branch information...
commit 10520a1c26d1cd110a09190ef19de723525c3db3 1 parent 3b0c1d5
@jails jails authored
Showing with 103 additions and 3 deletions.
  1. +8 −3 net/http/Route.php
  2. +95 −0 tests/cases/net/http/RouteTest.php
View
11 net/http/Route.php
@@ -256,7 +256,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']) {
@@ -269,6 +269,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;
}
@@ -398,8 +403,6 @@ public function export() {
* @return void
*/
public function compile() {
- $this->_match = $this->_params;
-
foreach ($this->_params as $key => $value) {
if (!strpos($key, ':')) {
continue;
@@ -408,6 +411,8 @@ public function compile() {
$this->_meta[$key] = $value;
}
+ $this->_match = $this->_params;
+
if ($this->_template === '/' || $this->_template === '') {
$this->_pattern = '@^/*$@';
return;
View
95 tests/cases/net/http/RouteTest.php
@@ -544,6 +544,101 @@ public function testMatchingEmptyRoute() {
}
/**
+ * 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')
+ ));
+
+ $result = $route->match(array(
+ 'controller' => 'resource', 'action' => 'create', 'http:method' => 'POST'
+ ));
+ $this->assertEqual('/resource', $result);
+
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create'));
+ $this->assertEqual(false, $result);
+ }
+
+ /**
+ * Test route matching for routes with specified request method (http:method) and a param
+ */
+ public function testMatchWithRequestMethodWithParam() {
+ $parameters = array('controller' => 'resource', 'action' => 'create');
+
+ $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);
+
+ $result = $route->match(array(
+ 'controller' => 'resource', 'action' => 'create', 'param' => 'value'
+ ));
+ $this->assertEqual(false, $result);
+ }
+
+ /**
+ * Test route matching for routes with no request method (http:method)
+ */
+ public function testMatchWithNoRequestMethod() {
+ $parameters = array('controller' => 'resource', 'action' => 'create');
+
+ $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 matching for routes with request method (http:method) GET
+ */
+ public function testMatchWithRequestMethodGet() {
+ $parameters = array('controller' => 'resource', 'action' => 'create');
+
+ $route = new Route(array(
+ 'template' => '/resource',
+ 'params' => $parameters + array('http:method' => 'GET')
+ ));
+
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create', 'http:method' => 'GET'));
+ $this->assertEqual('/resource', $result);
+
+ $result = $route->match(array('controller' => 'resource', 'action' => 'create'));
+ $this->assertEqual('/resource', $result);
+
+ $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.
*/
public function testTrimmingEmptyPathElements() {

0 comments on commit 10520a1

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