Ver código fonte

[HttpFoundation] fixed ApacheRequest, added tests

Kris Wallsmith 14 anos atrás
pai
commit
a5d8770a54

+ 8 - 1
src/Symfony/Component/HttpFoundation/ApacheRequest.php

@@ -31,7 +31,14 @@ class ApacheRequest extends Request
      */
     protected function prepareBaseUrl()
     {
-        return $this->server->get('SCRIPT_NAME');
+        $baseUrl = $this->server->get('SCRIPT_NAME');
+
+        if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
+            // assume mod_rewrite
+            return rtrim(dirname($baseUrl), '/');
+        }
+
+        return $baseUrl;
     }
 
     /**

+ 39 - 0
tests/Symfony/Tests/Component/HttpFoundation/ApacheRequestTest.php

@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\HttpFoundation;
+
+use Symfony\Component\HttpFoundation\ApacheRequest;
+
+class ApacheRequestTest extends \PHPUnit_Framework_TestCase
+{
+    public function testGetBaseUrlDoesNotForceScriptName()
+    {
+        $request = new ApacheRequest();
+        $request->server->replace(array(
+            'REQUEST_URI' => '/foo/bar',
+            'SCRIPT_NAME' => '/index.php',
+        ));
+
+        $this->assertEquals('', $request->getBaseUrl(), '->getBaseUrl() does not add the script name');
+    }
+
+    public function testGetBaseUrlIncludesScriptName()
+    {
+        $request = new ApacheRequest();
+        $request->server->replace(array(
+            'REQUEST_URI' => '/index.php/foo/bar',
+            'SCRIPT_NAME' => '/index.php',
+        ));
+
+        $this->assertEquals('/index.php', $request->getBaseUrl(), '->getBaseUrl() includes the script name');
+    }
+}