Skip to content

Commit

Permalink
[BrowserKit] Fixed CookieJar issue being unable to parse multiple coo…
Browse files Browse the repository at this point in the history
…kies from Set-Cookie.
  • Loading branch information
jakzal committed Apr 7, 2012
1 parent 13aa515 commit 87890d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Symfony/Component/BrowserKit/CookieJar.php
Expand Up @@ -96,7 +96,19 @@ public function clear()
*/
public function updateFromResponse(Response $response, $uri = null)
{
$cookies = array();

foreach ($response->getHeader('Set-Cookie', false) as $cookie) {
foreach (explode(',', $cookie) as $i => $part) {
if ($i==0 || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
$cookies[] = ltrim($part);
} else {
$cookies[count($cookies)-1].= ','.$part;
}
}
}

foreach ($cookies as $cookie) {
$this->set(Cookie::fromString($cookie, $uri));
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/CookieJarTest.php
Expand Up @@ -66,10 +66,38 @@ public function testUpdateFromResponse()
$cookieJar->set(new Cookie('bar', 'bar'));
$cookieJar->updateFromResponse($response);

$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
$this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromResponse() keeps existing cookies');
}

public function testUpdateFromResponseWithMultipleCookies()
{
$timestamp = time() + 3600;
$date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
$response = new Response('', 200, array(
'Set-Cookie' => sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date)
));

$cookieJar = new CookieJar();
$cookieJar->updateFromResponse($response);

$fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
$barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
$phpCookie = $cookieJar->get('PHPSESSID');

$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $fooCookie);
$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $barCookie);
$this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $phpCookie);
$this->assertEquals('foo', $fooCookie->getValue());
$this->assertEquals('bar', $barCookie->getValue());
$this->assertEquals('id', $phpCookie->getValue());
$this->assertEquals($timestamp, $fooCookie->getExpiresTime());
$this->assertEquals(null, $barCookie->getExpiresTime());
$this->assertEquals($timestamp, $phpCookie->getExpiresTime());
}

/**
* @dataProvider provideAllValuesValues
*/
Expand Down

0 comments on commit 87890d3

Please sign in to comment.