From f4282eea98a3cd3ec349116cd7be4b3be0beb026 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 4 Feb 2011 02:21:41 +1100 Subject: [PATCH] [Routing] added support for non-standard port numbers in absolute urls --- .../Routing/Generator/UrlGenerator.php | 8 +- .../Routing/Generator/UrlGeneratorTest.php | 92 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index 759c0c81c5..a67b1f41c6 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -125,7 +125,13 @@ class UrlGenerator implements UrlGeneratorInterface $url = (isset($this->context['base_url']) ? $this->context['base_url'] : '').$url; if ($absolute && isset($this->context['host'])) { - $url = 'http'.(isset($this->context['is_secure']) && $this->context['is_secure'] ? 's' : '').'://'.$this->context['host'].$url; + $isSecure = (isset($this->context['is_secure']) && $this->context['is_secure']); + $port = isset($this->context['port']) ? $this->context['port'] : 80; + $urlBeginning = 'http'.($isSecure ? 's' : '').'://'.$this->context['host']; + if (($isSecure && $port != 443) || (!$isSecure && $port != 80)) { + $urlBeginning .= ':'.$port; + } + $url = $urlBeginning.$url; } return $url; diff --git a/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php new file mode 100644 index 0000000000..58db89b158 --- /dev/null +++ b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php @@ -0,0 +1,92 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Routing\Generator; + +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\Generator\UrlGenerator; + +class UrlGeneratorTest extends \PHPUnit_Framework_TestCase +{ + /** @var RouteCollection */ + private $routeCollection; + /** @var UrlGenerator */ + private $generator; + + protected function setUp() + { + parent::setUp(); + + $this->routeCollection = new RouteCollection(); + $this->generator = new UrlGenerator($this->routeCollection); + } + + public function testAbsoluteUrlWithPort80() + { + $this->routeCollection->add('test', new Route('/testing')); + $this->generator->setContext(array( + 'base_url'=>'/app.php', + 'method'=>'GET', + 'host'=>'localhost', + 'port'=>80, + 'is_secure'=>false)); + + $url = $this->generator->generate('test', array(), true); + + $this->assertEquals('http://localhost/app.php/testing', $url); + } + + public function testAbsoluteSecureUrlWithPort443() + { + $this->routeCollection->add('test', new Route('/testing')); + $this->generator->setContext(array( + 'base_url'=>'/app.php', + 'method'=>'GET', + 'host'=>'localhost', + 'port'=>443, + 'is_secure'=>true)); + + $url = $this->generator->generate('test', array(), true); + + $this->assertEquals('https://localhost/app.php/testing', $url); + } + + public function testAbsoluteUrlWithNonStandardPort() + { + $this->routeCollection->add('test', new Route('/testing')); + $this->generator->setContext(array( + 'base_url'=>'/app.php', + 'method'=>'GET', + 'host'=>'localhost', + 'port'=>8080, + 'is_secure'=>false)); + + $url = $this->generator->generate('test', array(), true); + + $this->assertEquals('http://localhost:8080/app.php/testing', $url); + } + + public function testAbsoluteSecureUrlWithNonStandardPort() + { + $this->routeCollection->add('test', new Route('/testing')); + $this->generator->setContext(array( + 'base_url'=>'/app.php', + 'method'=>'GET', + 'host'=>'localhost', + 'port'=>8080, + 'is_secure'=>true)); + + $url = $this->generator->generate('test', array(), true); + + $this->assertEquals('https://localhost:8080/app.php/testing', $url); + } +} \ No newline at end of file