Skip to content

Commit

Permalink
[BrowserKit] fixed explicit cookie params being overriden by url
Browse files Browse the repository at this point in the history
  • Loading branch information
danielholmes committed May 26, 2011
1 parent 0b9f3d8 commit 3c372d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Symfony/Component/BrowserKit/Cookie.php
Expand Up @@ -45,7 +45,7 @@ class Cookie
*
* @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) {
$this->value = urldecode($value);
Expand All @@ -56,7 +56,7 @@ public function __construct($name, $value, $expires = null, $path = '/', $domain
}
$this->name = $name;
$this->expires = null === $expires ? null : (integer) $expires;
$this->path = empty($path) ? '/' : $path;
$this->path = empty($path) ? null : $path;
$this->domain = $domain;
$this->secure = (Boolean) $secure;
$this->httponly = (Boolean) $httponly;
Expand All @@ -81,7 +81,7 @@ public function __toString()
$cookie .= '; domain='.$this->domain;
}

if ('/' !== $this->path) {
if (null !== $this->path) {
$cookie .= '; path='.$this->path;
}

Expand Down Expand Up @@ -120,18 +120,19 @@ static public function fromString($cookie, $url = null)
'name' => trim($name),
'value' => trim($value),
'expires' => null,
'path' => '/',
'path' => null,
'domain' => '',
'secure' => false,
'httponly' => false,
'passedRawValue' => true,
);

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));
}

$parts = array_merge($urlParts, $parts);

$values['domain'] = $parts['host'];
$values['path'] = substr($parts['path'], 0, strrpos($parts['path'], '/'));
}
Expand Down Expand Up @@ -233,7 +234,7 @@ public function getExpiresTime()
*/
public function getPath()
{
return $this->path;
return (null !== $this->path)?$this->path:'/';
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Symfony/Tests/Component/BrowserKit/CookieTest.php
Expand Up @@ -42,6 +42,8 @@ public function testFromStringWithUrl()
{
$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=/', (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()
Expand Down

0 comments on commit 3c372d3

Please sign in to comment.