Kaynağa Gözat

[BrowserKit] fixed cookie management (see RFC 2109)

Fabien Potencier 14 yıl önce
ebeveyn
işleme
aa356e7288

+ 6 - 3
src/Symfony/Component/BrowserKit/CookieJar.php

@@ -108,12 +108,15 @@ class CookieJar
     {
         $this->flushExpiredCookies();
 
-        $parts = parse_url($uri);
+        $parts = array_replace(array('path' => '/'), parse_url($uri));
 
         $cookies = array();
         foreach ($this->cookieJar as $cookie) {
-            if ($cookie->getDomain() && $cookie->getDomain() != substr($parts['host'], -strlen($cookie->getDomain()))) {
-                continue;
+            if ($cookie->getDomain()) {
+                $domain = ltrim($cookie->getDomain(), '.');
+                if ($domain != substr($parts['host'], -strlen($domain))) {
+                    continue;
+                }
             }
 
             if ($cookie->getPath() != substr($parts['path'], 0, strlen($cookie->getPath()))) {

+ 4 - 1
tests/Symfony/Tests/Component/BrowserKit/CookieJarTest.php

@@ -79,7 +79,8 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
         $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
         $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
         $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
-        $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', 'example.com'));
+        $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
+        $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
         $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
 
         $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
@@ -88,11 +89,13 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
     public function provideAllValuesValues()
     {
         return array(
+            array('http://www.example.com', array('foo_nothing', 'foo_domain')),
             array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
             array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
             array('http://foo.example1.com/', array('foo_nothing')),
             array('https://foo.example.com/', array('foo_nothing', 'foo_domain', 'foo_secure')),
             array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
+            array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
         );
     }
 }