From 76d2075ffc137b882df833d61f4eab544dee4cae Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 28 Apr 2012 22:36:23 -0400 Subject: [PATCH] Add ssl option. --- lib/Cake/Routing/Router.php | 12 ++-- lib/Cake/Test/TestCase/Routing/RouterTest.php | 65 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index bb3340b4547..73eea87f71f 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -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', @@ -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 : '/'; @@ -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'])) { diff --git a/lib/Cake/Test/TestCase/Routing/RouterTest.php b/lib/Cake/Test/TestCase/Routing/RouterTest.php index a1064b7a190..372d0eb33f8 100644 --- a/lib/Cake/Test/TestCase/Routing/RouterTest.php +++ b/lib/Cake/Test/TestCase/Routing/RouterTest.php @@ -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 *