Przeglądaj źródła

[BrowserKit] fixed explicit cookie params being overriden by url

Daniel Holmes 14 lat temu
rodzic
commit
3c372d3773

+ 8 - 7
src/Symfony/Component/BrowserKit/Cookie.php

@@ -45,7 +45,7 @@ class Cookie
      *
      *
      * @api
      * @api
      */
      */
-    public function __construct($name, $value, $expires = null, $path = '/', $domain = '', $secure = false, $httponly = true, $encodedValue = false)
+    public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
     {
     {
         if ($encodedValue) {
         if ($encodedValue) {
             $this->value    = urldecode($value);
             $this->value    = urldecode($value);
@@ -56,7 +56,7 @@ class Cookie
         }
         }
         $this->name     = $name;
         $this->name     = $name;
         $this->expires  = null === $expires ? null : (integer) $expires;
         $this->expires  = null === $expires ? null : (integer) $expires;
-        $this->path     = empty($path) ? '/' : $path;
+        $this->path     = empty($path) ? null : $path;
         $this->domain   = $domain;
         $this->domain   = $domain;
         $this->secure   = (Boolean) $secure;
         $this->secure   = (Boolean) $secure;
         $this->httponly = (Boolean) $httponly;
         $this->httponly = (Boolean) $httponly;
@@ -81,7 +81,7 @@ class Cookie
             $cookie .= '; domain='.$this->domain;
             $cookie .= '; domain='.$this->domain;
         }
         }
 
 
-        if ('/' !== $this->path) {
+        if (null !== $this->path) {
             $cookie .= '; path='.$this->path;
             $cookie .= '; path='.$this->path;
         }
         }
 
 
@@ -120,7 +120,7 @@ class Cookie
             'name'     => trim($name),
             'name'     => trim($name),
             'value'    => trim($value),
             'value'    => trim($value),
             'expires'  =>  null,
             'expires'  =>  null,
-            'path'     => '/',
+            'path'     => null,
             'domain'   => '',
             'domain'   => '',
             'secure'   => false,
             'secure'   => false,
             'httponly' => false,
             'httponly' => false,
@@ -128,10 +128,11 @@ class Cookie
         );
         );
 
 
         if (null !== $url) {
         if (null !== $url) {
-            if ((false === $parts = parse_url($url)) || !isset($parts['host']) || !isset($parts['path'])) {
+            if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host']) || !isset($urlParts['path'])) {
                 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
                 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
             }
             }
-
+            $parts = array_merge($urlParts, $parts);
+            
             $values['domain'] = $parts['host'];
             $values['domain'] = $parts['host'];
             $values['path'] = substr($parts['path'], 0, strrpos($parts['path'], '/'));
             $values['path'] = substr($parts['path'], 0, strrpos($parts['path'], '/'));
         }
         }
@@ -233,7 +234,7 @@ class Cookie
      */
      */
     public function getPath()
     public function getPath()
     {
     {
-        return $this->path;
+        return (null !== $this->path)?$this->path:'/';
     }
     }
 
 
     /**
     /**

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

@@ -42,6 +42,8 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     {
     {
         $this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
         $this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
         $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
         $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
+        $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
+        $this->assertEquals('foo=bar; domain=www.myotherexample.com', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
     }
     }
 
 
     public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
     public function testFromStringThrowsAnExceptionIfCookieIsNotValid()