瀏覽代碼

Merge remote branch 'hhamon/cookie_path_fix'

* hhamon/cookie_path_fix:
  [Security] renamed Cookie::isHttponly() to Cookie::isHttpOnly()
  [HttpKernel] renamed Cookie::isHttponly() to Cookie::isHttpOnly()
  [BrowserKit] renamed Cookie::isHttponly() to Cookie::isHttpOnly()
  [HttpFoundation] fix cookie path default value to / and added some new unit tests to cover the class
Fabien Potencier 14 年之前
父節點
當前提交
a137d72351

+ 1 - 1
src/Symfony/Component/BrowserKit/Cookie.php

@@ -223,7 +223,7 @@ class Cookie
      *
      * @return Boolean The cookie httponly flag
      */
-    public function isHttponly()
+    public function isHttpOnly()
     {
         return $this->httponly;
     }

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

@@ -24,9 +24,9 @@ class Cookie
     protected $expire;
     protected $path;
     protected $secure;
-    protected $httponly;
+    protected $httpOnly;
 
-    public function __construct($name, $value = null, $expire = 0, $path = null, $domain = null, $secure = false, $httponly = true)
+    public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
     {
         // from PHP source code
         if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
@@ -52,7 +52,7 @@ class Cookie
         $this->expire = $expire;
         $this->path = $path;
         $this->secure = (Boolean) $secure;
-        $this->httponly = (Boolean) $httponly;
+        $this->httpOnly = (Boolean) $httpOnly;
     }
 
     public function getName()
@@ -85,9 +85,9 @@ class Cookie
         return $this->secure;
     }
 
-    public function isHttponly()
+    public function isHttpOnly()
     {
-        return $this->httponly;
+        return $this->httpOnly;
     }
 
     /**

+ 1 - 1
src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

@@ -33,7 +33,7 @@ class RequestDataCollector extends DataCollector
         $responseHeaders = $response->headers->all();
         $cookies = array();
         foreach ($response->headers->getCookies() as $cookie) {
-            $cookies[] = $this->getCookieHeader($cookie->getName(), $cookie->getValue(), $cookie->getExpire(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttponly());
+            $cookies[] = $this->getCookieHeader($cookie->getName(), $cookie->getValue(), $cookie->getExpire(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
         }
         if (count($cookies) > 0) {
             $responseHeaders['Set-Cookie'] = $cookies;

+ 2 - 2
tests/Symfony/Tests/Component/BrowserKit/CookieTest.php

@@ -99,10 +99,10 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     public function testIsHttponly()
     {
         $cookie = new Cookie('foo', 'bar');
-        $this->assertFalse($cookie->isHttponly(), '->isHttponly() returns false if not defined');
+        $this->assertFalse($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
 
         $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
-        $this->assertTrue($cookie->isHttponly(), '->isHttponly() returns the cookie httponly flag');
+        $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
     }
 
     public function testGetExpiresTime()

+ 50 - 0
tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php

@@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Cookie;
  * CookieTest
  *
  * @author John Kary <john@johnkary.net>
+ * @author Hugo Hamon <hugo.hamon@sensio.com>
  */
 class CookieTest extends \PHPUnit_Framework_TestCase
 {
@@ -87,4 +88,53 @@ class CookieTest extends \PHPUnit_Framework_TestCase
 
         $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
     }
+
+    public function testGetPath()
+    {
+        $cookie = new Cookie('foo', 'bar');
+
+        $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
+    }
+
+    public function testGetExpires()
+    {
+        $cookie = new Cookie('foo', 'bar', 3600);
+
+        $this->assertEquals(3600, $cookie->getExpire(), '->getExpire() returns the expire date');
+    }
+
+    public function testGetDomain()
+    {
+        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com');
+
+        $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
+    }
+
+    public function testIsSecure()
+    {
+        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', true);
+
+        $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
+    }
+
+    public function testIsHttpOnly()
+    {
+        $cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true);
+
+        $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
+    }
+
+    public function testCookieIsNotCleared()
+    {
+        $cookie = new Cookie('foo', 'bar', time()+3600*24);
+
+        $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
+    }
+
+    public function testCookieIsCleared()
+    {
+        $cookie = new Cookie('foo', 'bar', time()-20);
+
+        $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
+    }
 }

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

@@ -299,7 +299,7 @@ class PersistentTokenBasedRememberMeServicesTest extends \PHPUnit_Framework_Test
         $cookie = $response->headers->getCookie('foo');
         $this->assertFalse($cookie->isCleared());
         $this->assertTrue($cookie->isSecure());
-        $this->assertTrue($cookie->isHttponly());
+        $this->assertTrue($cookie->isHttpOnly());
         $this->assertTrue($cookie->getExpire() > time() + 3590 && $cookie->getExpire() < time() + 3610);
         $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
         $this->assertEquals('/foo/path', $cookie->getPath());
@@ -365,7 +365,7 @@ class PersistentTokenBasedRememberMeServicesTest extends \PHPUnit_Framework_Test
         $cookie = $response->headers->getCookie('foo');
         $this->assertFalse($cookie->isCleared());
         $this->assertTrue($cookie->isSecure());
-        $this->assertTrue($cookie->isHttponly());
+        $this->assertTrue($cookie->isHttpOnly());
         $this->assertTrue($cookie->getExpire() > time() + 3590 && $cookie->getExpire() < time() + 3610);
         $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
         $this->assertEquals('/foo/path', $cookie->getPath());

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

@@ -248,7 +248,7 @@ class TokenBasedRememberMeServicesTest extends \PHPUnit_Framework_TestCase
         $cookie = $response->headers->getCookie('foo');
         $this->assertFalse($cookie->isCleared());
         $this->assertTrue($cookie->isSecure());
-        $this->assertTrue($cookie->isHttponly());
+        $this->assertTrue($cookie->isHttpOnly());
         $this->assertTrue($cookie->getExpire() > time() + 3590 && $cookie->getExpire() < time() + 3610);
         $this->assertEquals('myfoodomain.foo', $cookie->getDomain());
         $this->assertEquals('/foo/path', $cookie->getPath());