Skip to content

Commit

Permalink
Moving tests into CakeRequest from dispatcher test.
Browse files Browse the repository at this point in the history
Dispatcher tests are all working again.
  • Loading branch information
markstory committed May 2, 2010
1 parent e4cf8a6 commit 13cc8e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
72 changes: 30 additions & 42 deletions cake/tests/cases/dispatcher.test.php
Expand Up @@ -1412,8 +1412,7 @@ public function testFullPageCachingDispatch() {
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
), true);

$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$dispatcher = new TestDispatcher();

$url = '/';

Expand All @@ -1434,7 +1433,6 @@ public function testFullPageCachingDispatch() {
$filename = $this->__cachePath($dispatcher->here);
unlink($filename);

$dispatcher->base = false;
$url = 'test_cached_pages/index';

ob_start();
Expand Down Expand Up @@ -1524,6 +1522,7 @@ public function testFullPageCachingDispatch() {
$this->assertEqual($result, $expected);
$filename = $this->__cachePath($dispatcher->here);
$this->assertTrue(file_exists($filename));

unlink($filename);
}

Expand Down Expand Up @@ -1580,66 +1579,57 @@ public function testHttpMethodOverrides() {
Router::mapResources('Posts');

$_SERVER['REQUEST_METHOD'] = 'POST';
$dispatcher =& new Dispatcher();
$dispatcher->base = false;
$dispatcher = new Dispatcher();

$result = $dispatcher->parseParams('/posts');
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'form' => array(), 'url' => array());
$this->assertEqual($result, $expected);
$result = $dispatcher->parseParams(new CakeRequest('/posts'));
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'form' => array());
foreach ($expected as $key => $value) {
$this->assertEqual($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
}

$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';

$result = $dispatcher->parseParams('/posts/5');
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
$this->assertEqual($result, $expected);
$result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array());
foreach ($expected as $key => $value) {
$this->assertEqual($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
}

unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
$_SERVER['REQUEST_METHOD'] = 'GET';

$result = $dispatcher->parseParams('/posts/5');
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'form' => array(), 'url' => array());
$this->assertEqual($result, $expected);
$result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'form' => array());
foreach ($expected as $key => $value) {
$this->assertEqual($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
}

$_POST['_method'] = 'PUT';

$result = $dispatcher->parseParams('/posts/5');
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
$this->assertEqual($result, $expected);
$result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array());
foreach ($expected as $key => $value) {
$this->assertEqual($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
}

$_POST['_method'] = 'POST';
$_POST['data'] = array('Post' => array('title' => 'New Post'));
$_POST['extra'] = 'data';
$_SERVER = array();

$result = $dispatcher->parseParams('/posts');
$result = $dispatcher->parseParams(new CakeRequest('/posts'));
$expected = array(
'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add',
'[method]' => 'POST', 'form' => array('extra' => 'data'), 'data' => array('Post' => array('title' => 'New Post')),
'url' => array()
);
$this->assertEqual($result, $expected);
foreach ($expected as $key => $value) {
$this->assertEqual($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
}

unset($_POST['_method']);
}

/**
* Tests that invalid characters cannot be injected into the application base path.
*
* @return void
*/
public function testBasePathInjection() {
$self = $_SERVER['PHP_SELF'];
$_SERVER['PHP_SELF'] = urldecode(
"/index.php/%22%3E%3Ch1%20onclick=%22alert('xss');%22%3Eheya%3C/h1%3E"
);

$dispatcher =& new Dispatcher();
$result = $dispatcher->baseUrl();
$expected = '/index.php/h1 onclick=alert(xss);heya';
$this->assertEqual($result, $expected);
}

/**
* Tests that the Dispatcher does not return an empty action
*
Expand All @@ -1650,18 +1640,16 @@ public function testTrailingSlash() {
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

Router::reload();
$Dispatcher =& new TestDispatcher();
$Dispatcher = new TestDispatcher();
Router::connect('/myalias/:action/*', array('controller' => 'my_controller', 'action' => null));

$Dispatcher->base = false;
$url = 'myalias/'; //Fails
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$result = $Dispatcher->parseParams($url);
$result = $Dispatcher->parseParams(new CakeRequest($url));
$this->assertEqual('index', $result['action']);

$url = 'myalias'; //Passes
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$result = $Dispatcher->parseParams($url);
$result = $Dispatcher->parseParams(new CakeRequest($url));
$this->assertEqual('index', $result['action']);
}

Expand Down
16 changes: 16 additions & 0 deletions cake/tests/cases/libs/cake_request.test.php
Expand Up @@ -1089,6 +1089,22 @@ public function testEnvironmentDetection() {
$this->__loadEnvironment(array_merge(array('reload' => true), $backup));
}

/**
* test that XSS can't be performed against the base path.
*
* @return void
*/
function testBasePathInjection() {
$self = $_SERVER['PHP_SELF'];
$_SERVER['PHP_SELF'] = urldecode(
"/index.php/%22%3E%3Ch1%20onclick=%22alert('xss');%22%3Eheya%3C/h1%3E"
);

$request = new CakeRequest();
$expected = '/index.php/h1 onclick=alert(xss);heya';
$this->assertEqual($request->base, $expected);
}

/**
* backupEnvironment method
*
Expand Down

0 comments on commit 13cc8e7

Please sign in to comment.