Skip to content

Commit

Permalink
bug #10762 [BrowserKit] Allow URLs that don't contain a path when cre…
Browse files Browse the repository at this point in the history
…ating a cookie from a string (thewilkybarkid)

This PR was merged into the 2.3 branch.

Discussion
----------

[BrowserKit] Allow URLs that don't contain a path when creating a cookie from a string

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

The Cookie class doesn't require a path, but the `fromString()` static method does, so when URL given is something like http://www.example.com (ie no trailing slash) it currently throws an exception. This PR removes the requirement.

Commits
-------

fc1223f Allow URLs that don't contain a path
  • Loading branch information
fabpot committed May 9, 2014
2 parents d51c9b3 + fc1223f commit 894b4a0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/BrowserKit/Cookie.php
Expand Up @@ -152,12 +152,12 @@ public static function fromString($cookie, $url = null)
);

if (null !== $url) {
if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host']) || !isset($urlParts['path'])) {
if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
}

$values['domain'] = $urlParts['host'];
$values['path'] = substr($urlParts['path'], 0, strrpos($urlParts['path'], '/'));
$values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
}

foreach ($parts as $part) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/CookieTest.php
Expand Up @@ -75,6 +75,8 @@ public function testFromStringWithCapitalization()
public function testFromStringWithUrl()
{
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com'));
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com?foo'));
$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; path=/', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
Expand Down

0 comments on commit 894b4a0

Please sign in to comment.