Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Added a new method Zepto\Router::redirect(), updated test class
Browse files Browse the repository at this point in the history
  • Loading branch information
hassankhan committed Feb 20, 2014
1 parent 497058e commit c2e9c76
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,33 @@ public function run()
catch (\Exception $e) {
$this->error($e);
return FALSE;
}
}

/**
* Redirects to another URL
*
* @param string $url
* @return bool
*/
public function redirect($url)
{
// If no URL is given, throw exception
try {
if (empty($url)) {
throw new \InvalidArgumentException('No URL given');
}
}
catch (\Exception $e) {
$this->error($e);
return FALSE;
}

// Set redirect status codes and location
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_FOUND;
$this->response->setStatusCode($this->current_http_status);
$this->response->headers->set('Location', $url);
return TRUE;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Zepto/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ public function testRunWithError()
$this->assertEquals(500, $this->router->current_http_status());
}

/**
* @covers Zepto\Router::redirect()
*/
public function testRedirect()
{
$actual = $this->router->redirect('http://www.google.com');
$this->assertEquals(302, $this->router->current_http_status());
$this->assertTrue($actual);
}

/**
* @covers Zepto\Router::redirect()
*/
public function testRedirectWithoutUrl()
{
$actual = $this->router->redirect('');
$this->assertEquals(500, $this->router->current_http_status());
$this->assertFalse($actual);
}

/**
* @covers Zepto\Router::routes
*/
Expand Down

0 comments on commit c2e9c76

Please sign in to comment.