Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ssl option.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 9519ac9 commit 76d2075
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/Cake/Routing/Router.php
Expand Up @@ -584,12 +584,12 @@ public static function promote($which = null) {
*
* There are a few 'special' parameters that can change the final URL string that is generated
*
* - `base` - Set to false to remove the base path from the generated url. If your application
* - `_base` - Set to false to remove the base path from the generated url. If your application
* is not in the root directory, this can be used to generate urls that are 'cake relative'.
* cake relative urls are required when using requestAction.
* - `?` - Takes an array of query string parameters. (Deprecated)
* - `_full` - If true the `FULL_BASE_URL` constant will be prepended to generated urls.
* - `#` - Allows you to set url hash fragments.
* - `full_base` - If true the `FULL_BASE_URL` constant will be prepended to generated urls.
* - `ssl` - Set to true to convert the generated url to https, or false to force http.
*
* @param string|array $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4"
* or an array specifying any of the following: 'controller', 'action',
Expand Down Expand Up @@ -623,7 +623,7 @@ public static function url($url = null, $full = false) {
$here = null;
}

$extension = $output = $q = $frag = null;
$extension = $output = $frag = null;

if (empty($url)) {
$output = isset($here) ? $here : '/';
Expand All @@ -650,6 +650,10 @@ public static function url($url = null, $full = false) {
$extension = '.' . $url['ext'];
unset($url['ext']);
}
if (isset($url['ssl'])) {
$url['_scheme'] = ($url['ssl'] == true) ? 'https' : 'http';
unset($url['ssl']);
}

// Copy the current action if the controller is the current one.
if (empty($url['action'])) {
Expand Down
65 changes: 65 additions & 0 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -1274,6 +1274,71 @@ public function testUrlGenerationWithAutoPrefixes() {
$this->assertEquals($expected, $result);
}

/**
* Test that the ssl option works.
*
* @return void
*/
public function testGenerationWithSslOption() {
Router::connect('/:controller/:action/*');
$_SERVER['HTTP_HOST'] = 'localhost';

$request = new Request();
Router::pushRequest(
$request->addParams(array(
'plugin' => null, 'controller' => 'images', 'action' => 'index'
))->addPaths(array(
'base' => '',
'here' => '/images/index',
'webroot' => '/',
))
);

$result = Router::url(array(
'ssl' => true
));
$this->assertEquals('https://localhost/images/index', $result);

$result = Router::url(array(
'ssl' => false
));
$this->assertEquals('http://localhost/images/index', $result);
}

/**
* Test ssl option when the current request is ssl.
*
* @return void
*/
public function testGenerateWithSslInSsl() {
Router::connect('/:controller/:action/*');
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['HTTPS'] = 'on';

$request = new Request();
Router::pushRequest(
$request->addParams(array(
'plugin' => null,
'controller' => 'images',
'action' => 'index'
))->addPaths(array(
'base' => '',
'here' => '/images/index',
'webroot' => '/',
))
);

$result = Router::url(array(
'ssl' => false
));
$this->assertEquals('http://localhost/images/index', $result);

$result = Router::url(array(
'ssl' => true
));
$this->assertEquals('https://localhost/images/index', $result);
}

/**
* test that auto-generated prefix routes persist
*
Expand Down

0 comments on commit 76d2075

Please sign in to comment.