Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add tests for type + method conditions.
  • Loading branch information
markstory committed Jul 15, 2012
1 parent a08f4e7 commit a203d67
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Routing/Route/Route.php
Expand Up @@ -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.
*/
Expand Down
68 changes: 67 additions & 1 deletion lib/Cake/Test/TestCase/Routing/Route/RouteTest.php
Expand Up @@ -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'));
}

/**
Expand Down

0 comments on commit a203d67

Please sign in to comment.