Sfoglia il codice sorgente

[HttpFoundation] standardized cookie paths (an empty path is equivalent to /)

Fabien Potencier 13 anni fa
parent
commit
b4028350d2

+ 4 - 4
src/Symfony/Component/HttpFoundation/Cookie.php

@@ -71,7 +71,7 @@ class Cookie
         $this->value = $value;
         $this->domain = $domain;
         $this->expire = $expire;
-        $this->path = $path;
+        $this->path = empty($path) ? '/' : $path;
         $this->secure = (Boolean) $secure;
         $this->httpOnly = (Boolean) $httpOnly;
     }
@@ -90,8 +90,8 @@ class Cookie
             }
         }
 
-        if ('/' !== $this->getPath()) {
-            $str .= '; path='.$this->getPath();
+        if ('/' !== $this->path) {
+            $str .= '; path='.$this->path;
         }
 
         if (null !== $this->getDomain()) {
@@ -166,7 +166,7 @@ class Cookie
      */
     public function getPath()
     {
-        return null === $this->path ? '/' : $this->path;
+        return $this->path;
     }
 
     /**

+ 3 - 3
tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php

@@ -142,10 +142,10 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     {
         $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
 
-        $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
+        $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; domain=.myfoodomain.com; secure; httponly', $cookie->__toString(), '->__toString() returns string representation of the cookie');
 
-        $cookie = new Cookie('foo', null, 1, '/', '.myfoodomain.com');
+        $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
 
-        $this->assertEquals('foo=deleted; expires=' . gmdate("D, d-M-Y H:i:s T", time()-31536001) . '; path=/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
+        $this->assertEquals('foo=deleted; expires=' . gmdate("D, d-M-Y H:i:s T", time()-31536001) . '; path=/admin/; domain=.myfoodomain.com; httponly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL');
     }
 }

+ 2 - 2
tests/Symfony/Tests/Component/HttpFoundation/ResponseHeaderBagTest.php

@@ -69,7 +69,7 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
         $bag = new ResponseHeaderBag(array());
         $bag->setCookie(new Cookie('foo', 'bar'));
 
-        $this->assertContains("Set-Cookie: foo=bar; path=/; httponly", explode("\r\n", $bag->__toString()));
+        $this->assertContains("Set-Cookie: foo=bar; httponly", explode("\r\n", $bag->__toString()));
 
         $bag->clearCookie('foo');
 
@@ -90,7 +90,7 @@ class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
         $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
         $this->assertContains("Set-Cookie: foo=bar; path=/path/foo; domain=foo.bar; httponly", $headers);
         $this->assertContains("Set-Cookie: foo=bar; path=/path/bar; domain=bar.foo; httponly", $headers);
-        $this->assertContains("Set-Cookie: foo=bar; path=/; httponly", $headers);
+        $this->assertContains("Set-Cookie: foo=bar; httponly", $headers);
 
         $cookies = $bag->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
         $this->assertTrue(isset($cookies['foo.bar']['/path/foo']['foo']));

+ 2 - 2
tests/Symfony/Tests/Component/Security/Http/Logout/CookieClearingLogoutHandlerTest.php

@@ -40,9 +40,9 @@ class CookieClearingLogoutHandlerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('foo.foo', $cookie->getDomain());
         $this->assertTrue($cookie->isCleared());
 
-        $cookie = $cookies['']['']['foo2'];
+        $cookie = $cookies['']['/']['foo2'];
         $this->assertStringStartsWith('foo2', $cookie->getName());
-        $this->assertNull($cookie->getPath());
+        $this->assertEquals('/', $cookie->getPath());
         $this->assertNull($cookie->getDomain());
         $this->assertTrue($cookie->isCleared());
     }

+ 1 - 1
tests/Symfony/Tests/Component/Security/Http/RememberMe/PersistentTokenBasedRememberMeServicesTest.php

@@ -222,7 +222,7 @@ class PersistentTokenBasedRememberMeServicesTest extends \PHPUnit_Framework_Test
 
         $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
         $this->assertTrue($cookie->isCleared());
-        $this->assertNull($cookie->getPath());
+        $this->assertEquals('/', $cookie->getPath());
         $this->assertNull($cookie->getDomain());
     }
 

+ 1 - 1
tests/Symfony/Tests/Component/Security/Http/RememberMe/TokenBasedRememberMeServicesTest.php

@@ -155,7 +155,7 @@ class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
 
         $cookie = $request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME);
         $this->assertTrue($cookie->isCleared());
-        $this->assertNull($cookie->getPath());
+        $this->assertEquals('/', $cookie->getPath());
         $this->assertNull($cookie->getDomain());
     }