diff --git a/lib/Cake/Routing/Route/Route.php b/lib/Cake/Routing/Route/Route.php index eb3d5e734bc..96d244ee349 100644 --- a/lib/Cake/Routing/Route/Route.php +++ b/lib/Cake/Routing/Route/Route.php @@ -141,8 +141,8 @@ public function compiled() { } /** - * Compiles the route's regular expression. Modifies defaults property so all necessary keys are set - * and populates $this->names with the named routing elements. + * Compiles the route's regular expression. Modifies defaults property so all necessary + * keys are set and populates $this->names with the named routing elements. * * @return array Returns a string regular expression of the compiled route. */ diff --git a/lib/Cake/Test/TestCase/Routing/Route/RouteTest.php b/lib/Cake/Test/TestCase/Routing/Route/RouteTest.php index 9630911aba5..1e9e93387bc 100644 --- a/lib/Cake/Test/TestCase/Routing/Route/RouteTest.php +++ b/lib/Cake/Test/TestCase/Routing/Route/RouteTest.php @@ -634,9 +634,75 @@ public function testParseWithPassDefaults() { */ public function testParseWithHttpHeaderConditions() { $_SERVER['REQUEST_METHOD'] = 'GET'; - $route = new Route('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST')); + $route = new Route('/sample', ['controller' => 'posts', 'action' => 'index', '[method]' => 'POST']); + $this->assertFalse($route->parse('/sample')); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $expected = [ + 'controller' => 'posts', + 'action' => 'index', + 'pass' => [], + '[method]' => 'POST', + ]; + $this->assertEquals($expected, $route->parse('/sample')); + } + +/** + * test that http header conditions can cause route failures. + * + * @return void + */ + public function testParseWithMultipleHttpMethodConditions() { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $route = new Route('/sample', [ + 'controller' => 'posts', + 'action' => 'index', + '[method]' => ['PUT', 'POST'] + ]); $this->assertFalse($route->parse('/sample')); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $expected = [ + 'controller' => 'posts', + 'action' => 'index', + 'pass' => [], + '[method]' => ['PUT', 'POST'], + ]; + $this->assertEquals($expected, $route->parse('/sample')); + } + + +/** + * Test that the [type] condition works. + * + * @return void + */ + public function testParseWithContentTypeCondition() { + $_SERVER['REQUEST_METHOD'] = 'POST'; + unset($_SERVER['CONTENT_TYPE']); + $route = new Route('/sample', [ + 'controller' => 'posts', + 'action' => 'index', + '[method]' => 'POST', + '[type]' => 'application/xml' + ]); + $this->assertFalse($route->parse('/sample'), 'No content type set.'); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SERVER['CONTENT_TYPE'] = 'application/json'; + $this->assertFalse($route->parse('/sample'), 'Wrong content type set.'); + + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SERVER['CONTENT_TYPE'] = 'application/xml'; + $expected = [ + 'controller' => 'posts', + 'action' => 'index', + 'pass' => [], + '[method]' => 'POST', + '[type]' => 'application/xml', + ]; + $this->assertEquals($expected, $route->parse('/sample')); } /**