Skip to content

Commit

Permalink
Add _stop() to RedirectRoute.
Browse files Browse the repository at this point in the history
Moved from CakeResponse to RedirectRoute,
as RedirectRoute is the only place its currently needed.

Refs #2143
  • Loading branch information
markstory committed Oct 23, 2011
1 parent 8e69df9 commit 6d6aa3c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/Cake/Routing/Route/RedirectRoute.php
Expand Up @@ -37,6 +37,13 @@ class RedirectRoute extends CakeRoute {
*/
public $redirect;

/**
* Flag for disabling exit() when this route parses a url.
*
* @var boolean
*/
public $stop = true;

/**
* Constructor
*
Expand Down Expand Up @@ -79,6 +86,7 @@ public function parse($url) {
$this->response->header(array('Location' => Router::url($redirect, true)));
$this->response->statusCode($status);
$this->response->send();
$this->_stop();
}

/**
Expand All @@ -90,4 +98,17 @@ public function parse($url) {
public function match($url) {
return false;
}
}

/**
* Stop execution of the current script. Wraps exit() making
* testing easier.
*
* @param integer|string $status see http://php.net/exit for values
* @return void
*/
protected function _stop($code = 0) {
if ($this->stop) {
exit($code);
}
}
}
8 changes: 8 additions & 0 deletions lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php
Expand Up @@ -45,43 +45,51 @@ public function setUp() {
*/
public function testParsing() {
$route = new RedirectRoute('/home', array('controller' => 'posts'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true)));

$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true)));
$this->assertEqual($route->response->statusCode(), 301);

$route = new RedirectRoute('/google', 'http://google.com');
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/google');
$this->assertEqual($route->response->header(), array('Location' => 'http://google.com'));

$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view', true)));
$this->assertEqual($route->response->statusCode(), 302);

$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view/2', true)));

$route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/test', true)));

$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add/passme/named:param', true)));

$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add', true)));
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Test/Case/Routing/RouterTest.php
Expand Up @@ -2459,6 +2459,7 @@ public function testRouteRedirection() {
Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302));
$this->assertEqual(count(Router::$routes), 1);
Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
Router::$routes[0]->stop = false;
$this->assertEqual(Router::$routes[0]->options['status'], 302);

Router::parse('/blog');
Expand Down

0 comments on commit 6d6aa3c

Please sign in to comment.