[BrowserKit] Remove dependency of CookieJar to Response
This commit is contained in:
parent
8eea5c3d5f
commit
95b8e29f57
@ -89,16 +89,16 @@ class CookieJar
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the cookie jar from a Response object.
|
||||
* Updates the cookie jar from a response Set-Cookie headers.
|
||||
*
|
||||
* @param Response $response A Response object
|
||||
* @param string $uri The base URL
|
||||
* @param array $setCookies Set-Cookie headers from an HTTP response
|
||||
* @param string $uri The base URL
|
||||
*/
|
||||
public function updateFromResponse(Response $response, $uri = null)
|
||||
public function updateFromSetCookie(array $setCookies, $uri = null)
|
||||
{
|
||||
$cookies = array();
|
||||
|
||||
foreach ($response->getHeader('Set-Cookie', false) as $cookie) {
|
||||
foreach ($setCookies as $cookie) {
|
||||
foreach (explode(',', $cookie) as $i => $part) {
|
||||
if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
|
||||
$cookies[] = ltrim($part);
|
||||
@ -113,6 +113,17 @@ class CookieJar
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the cookie jar from a Response object.
|
||||
*
|
||||
* @param Response $response A Response object
|
||||
* @param string $uri The base URL
|
||||
*/
|
||||
public function updateFromResponse(Response $response, $uri = null)
|
||||
{
|
||||
$this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns not yet expired cookies.
|
||||
*
|
||||
|
@ -63,25 +63,33 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
|
||||
$response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
|
||||
|
||||
$cookieJar = new CookieJar();
|
||||
$cookieJar->set(new Cookie('bar', 'bar'));
|
||||
$cookieJar->updateFromResponse($response);
|
||||
|
||||
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
|
||||
}
|
||||
|
||||
public function testUpdateFromSetCookie()
|
||||
{
|
||||
$setCookies = array('foo=foo');
|
||||
|
||||
$cookieJar = new CookieJar();
|
||||
$cookieJar->set(new Cookie('bar', 'bar'));
|
||||
$cookieJar->updateFromSetCookie($setCookies);
|
||||
|
||||
$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');
|
||||
$this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
|
||||
$this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
|
||||
}
|
||||
|
||||
public function testUpdateFromResponseWithMultipleCookies()
|
||||
public function testUpdateFromSetCookieWithMultipleCookies()
|
||||
{
|
||||
$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)
|
||||
));
|
||||
$setCookies = array(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);
|
||||
$cookieJar->updateFromSetCookie($setCookies);
|
||||
|
||||
$fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
|
||||
$barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
|
||||
|
Reference in New Issue
Block a user