[HttpFoundation] Fix cookie to string conversion for raw cookies

This commit is contained in:
Roland Franssen 2016-12-13 18:58:15 +00:00 committed by Fabien Potencier
parent 298452da31
commit 5e899cd2a7
2 changed files with 10 additions and 8 deletions

View File

@ -80,20 +80,20 @@ class Cookie
*/ */
public function __toString() public function __toString()
{ {
$str = urlencode($this->getName()).'='; $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
if ('' === (string) $this->getValue()) { if ('' === (string) $this->getValue()) {
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001); $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
} else { } else {
$str .= urlencode($this->getValue()); $str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());
if ($this->getExpiresTime() !== 0) { if ($this->getExpiresTime() !== 0) {
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()); $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
} }
} }
if ($this->path) { if ($this->getPath()) {
$str .= '; path='.$this->path; $str .= '; path='.$this->getPath();
} }
if ($this->getDomain()) { if ($this->getDomain()) {
@ -124,7 +124,7 @@ class Cookie
/** /**
* Gets the value of the cookie. * Gets the value of the cookie.
* *
* @return string * @return string|null
*/ */
public function getValue() public function getValue()
{ {
@ -134,7 +134,7 @@ class Cookie
/** /**
* Gets the domain that the cookie is available to. * Gets the domain that the cookie is available to.
* *
* @return string * @return string|null
*/ */
public function getDomain() public function getDomain()
{ {

View File

@ -154,10 +154,12 @@ class CookieTest extends \PHPUnit_Framework_TestCase
public function testRawCookie() public function testRawCookie()
{ {
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true); $cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
$this->assertFalse($cookie->isRaw()); $this->assertFalse($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
$cookie = new Cookie('foo', 'bar', 3600, '/', '.myfoodomain.com', false, true, true); $cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
$this->assertTrue($cookie->isRaw()); $this->assertTrue($cookie->isRaw());
$this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
} }
} }