Skip to content

Commit 894b4a0

Browse files
committed
bug symfony#10762 [BrowserKit] Allow URLs that don't contain a path when creating 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
2 parents d51c9b3 + fc1223f commit 894b4a0

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/Symfony/Component/BrowserKit/Cookie.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ public static function fromString($cookie, $url = null)
152152
);
153153

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

159159
$values['domain'] = $urlParts['host'];
160-
$values['path'] = substr($urlParts['path'], 0, strrpos($urlParts['path'], '/'));
160+
$values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
161161
}
162162

163163
foreach ($parts as $part) {

src/Symfony/Component/BrowserKit/Tests/CookieTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function testFromStringWithCapitalization()
7575
public function testFromStringWithUrl()
7676
{
7777
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
78+
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com'));
79+
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com?foo'));
7880
$this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
7981
$this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
8082
$this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));

0 commit comments

Comments
 (0)