Browse files

[BrowserKit] Fixed CookieJar issue being unable to parse multiple coo…

…kies from Set-Cookie.
  • Loading branch information...
1 parent 13aa515 commit 87890d3a7ce4ca69eacf23f4a337a5c1510fc7e0 @jakzal jakzal committed Apr 7, 2012
View
12 src/Symfony/Component/BrowserKit/CookieJar.php
@@ -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));
}
}
View
28 src/Symfony/Component/BrowserKit/Tests/CookieJarTest.php
@@ -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
*/

0 comments on commit 87890d3

Please sign in to comment.