Procházet zdrojové kódy

Merge remote branch 'hnw/cookie-raw-value'

* hnw/cookie-raw-value:
  added test for Cookie#getRawValue and CookieJar#AllRawValues
  fixed doc comment
  [BrowserKit] dealing with raw value for cookies
Fabien Potencier před 14 roky
rodič
revize
e0bf310b55

+ 27 - 5
src/Symfony/Component/BrowserKit/Cookie.php

@@ -29,6 +29,7 @@ class Cookie
     protected $domain;
     protected $secure;
     protected $httponly;
+    protected $rawValue;
 
     /**
      * Sets a cookie.
@@ -40,13 +41,20 @@ class Cookie
      * @param  string  $domain   The domain that the cookie is available
      * @param  Boolean $secure   Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
      * @param  Boolean $httponly The cookie httponly flag
+     * @param  Boolean $passedRawValue $value is raw or urldecoded
      *
      * @api
      */
-    public function __construct($name, $value, $expires = null, $path = '/', $domain = '', $secure = false, $httponly = true)
+    public function __construct($name, $value, $expires = null, $path = '/', $domain = '', $secure = false, $httponly = true, $passedRawValue = false)
     {
         $this->name     = $name;
-        $this->value    = $value;
+        if ($passedRawValue) {
+            $this->value    = urldecode($value);
+            $this->rawValue = $value;
+        } else {
+            $this->value    = $value;
+            $this->rawValue = urlencode($value);
+        }
         $this->expires  = null === $expires ? null : (integer) $expires;
         $this->path     = empty($path) ? '/' : $path;
         $this->domain   = $domain;
@@ -63,7 +71,7 @@ class Cookie
      */
     public function __toString()
     {
-        $cookie = sprintf('%s=%s', $this->name, urlencode($this->value));
+        $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
 
         if (null !== $this->expires) {
             $cookie .= '; expires='.substr(\DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('UTC'))->format(static::DATE_FORMAT), 0, -5);
@@ -110,12 +118,13 @@ class Cookie
 
         $values = array(
             'name'     => trim($name),
-            'value'    => urldecode(trim($value)),
+            'value'    => trim($value),
             'expires'  =>  null,
             'path'     => '/',
             'domain'   => '',
             'secure'   => false,
             'httponly' => false,
+            'passedRawValue' => true,
         );
 
         if (null !== $url) {
@@ -162,7 +171,8 @@ class Cookie
             $values['path'],
             $values['domain'],
             $values['secure'],
-            $values['httponly']
+            $values['httponly'],
+            $values['passedRawValue']
         );
     }
 
@@ -190,6 +200,18 @@ class Cookie
         return $this->value;
     }
 
+    /**
+     * Gets the raw value of the cookie.
+     *
+     * @return string The cookie value
+     *
+     * @api
+     */
+    public function getRawValue()
+    {
+        return $this->rawValue;
+    }
+
     /**
      * Gets the expires time of the cookie.
      *

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

@@ -100,11 +100,12 @@ class CookieJar
     /**
      * Returns not yet expired cookie values for the given URI.
      *
-     * @param string $uri A URI
+     * @param string  $uri A URI
+     * @param Boolean $returnsRawValue returnes raw value or urldecoded value
      *
      * @return array An array of cookie values
      */
-    public function allValues($uri)
+    public function allValues($uri, $returnsRawValue = false)
     {
         $this->flushExpiredCookies();
 
@@ -127,12 +128,27 @@ class CookieJar
                 continue;
             }
 
-            $cookies[$cookie->getName()] = $cookie->getValue();
+            if ($returnsRawValue) {
+                $cookies[$cookie->getName()] = $cookie->getRawValue();
+            } else {
+                $cookies[$cookie->getName()] = $cookie->getValue();
+            }
         }
 
         return $cookies;
     }
 
+    /**
+     * Returns not yet expired raw cookie values for the given URI.
+     *
+     * @param string $uri A URI
+     *
+     * @return array An array of cookie values
+     */
+    public function allRawValues($uri) {
+        return $this->allValues($uri, true);
+    }
+
     /**
      * Removes all expired cookies.
      */

+ 16 - 0
tests/Symfony/Tests/Component/BrowserKit/CookieJarTest.php

@@ -98,4 +98,20 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
             array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
         );
     }
+
+    /**
+     * @dataProvider provideAllValuesValues
+     */
+    public function testAllRawValues($uri, $values)
+    {
+        $cookieJar = new CookieJar();
+        $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($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
+
+        $this->assertEquals($values, array_keys($cookieJar->allRawValues($uri)), '->allRawValues() returns the cookie for a given URI');
+    }
+
 }

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

@@ -33,6 +33,8 @@ class CookieTest extends \PHPUnit_Framework_TestCase
             array('foo=bar; secure'),
             array('foo=bar; httponly'),
             array('foo=bar; domain=google.com; path=/foo; secure; httponly'),
+            array('foo=bar=baz'),
+            array('foo=bar%3Dbaz'),
         );
     }
 
@@ -70,6 +72,17 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     {
         $cookie = new Cookie('foo', 'bar');
         $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
+
+        $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
+        $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
+    }
+
+    public function testGetRawValue()
+    {
+        $cookie = new Cookie('foo', 'bar=baz'); // decoded value
+        $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
+        $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
+        $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
     }
 
     public function testGetPath()