Browse Source

[Routing] added support for non-standard port numbers in absolute urls

Daniel Holmes 14 years ago
parent
commit
f4282eea98

+ 7 - 1
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;

+ 92 - 0
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -0,0 +1,92 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * 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);
+    }
+}