mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 02:22:57 +01:00
Guarding against the case of a cookie with the same name but with different attributes like domain, path, etc...
This commit is contained in:
parent
190bb2bd76
commit
476bfb12ac
@ -85,28 +85,6 @@ class ArrayCookieJar implements CookieJarInterface, \Serializable
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* If a cookie already exists and the server asks to set it again with a null value, the
|
||||
* cookie must be deleted.
|
||||
*
|
||||
* @param \Guzzle\Plugin\Cookie\Cookie $cookie
|
||||
*/
|
||||
public function removeCookieIfEmpty(Cookie $cookie)
|
||||
{
|
||||
$cookieValue = $cookie->getValue();
|
||||
if (!empty($cookieValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$foundCookie = array_filter($this->cookies, function (Cookie $savedCookie) use ($cookie) {
|
||||
return $savedCookie->getName() === $cookie->getName();
|
||||
});
|
||||
if ($foundCookie) {
|
||||
$index = array_search($foundCookie, $this->cookies);
|
||||
unset($this->cookies[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public function add(Cookie $cookie)
|
||||
{
|
||||
// Only allow cookies with set and valid domain, name, value
|
||||
@ -242,4 +220,18 @@ class ArrayCookieJar implements CookieJarInterface, \Serializable
|
||||
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a cookie already exists and the server asks to set it again with a null value, the
|
||||
* cookie must be deleted.
|
||||
*
|
||||
* @param \Guzzle\Plugin\Cookie\Cookie $cookie
|
||||
*/
|
||||
private function removeCookieIfEmpty(Cookie $cookie)
|
||||
{
|
||||
$cookieValue = $cookie->getValue();
|
||||
if ($cookieValue === null || $cookieValue === '') {
|
||||
$this->remove($cookie->getDomain(), $cookie->getPath(), $cookie->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -353,13 +353,33 @@ class ArrayCookieJarTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
public function testRemoveExistingCookieIfEmpty()
|
||||
{
|
||||
// first set a valid cookie
|
||||
$validCookie = new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'foo.com', 'path' => '/', 'discard' => false));
|
||||
$this->assertTrue($this->jar->add($validCookie));
|
||||
// then try to re-set the same cookie with no value: assert that cookie is not added
|
||||
$emptyCookie = new Cookie(array('name' => 'foo', 'value' => NULL, 'domain' => 'foo.com', 'path' => '/', 'discard' => false));
|
||||
$this->assertFalse($this->jar->add($emptyCookie));
|
||||
// Add a cookie that should not be affected
|
||||
$a = new Cookie(array(
|
||||
'name' => 'foo',
|
||||
'value' => 'nope',
|
||||
'domain' => 'foo.com',
|
||||
'path' => '/abc'
|
||||
));
|
||||
$this->jar->add($a);
|
||||
|
||||
$data = array(
|
||||
'name' => 'foo',
|
||||
'value' => 'bar',
|
||||
'domain' => 'foo.com',
|
||||
'path' => '/'
|
||||
);
|
||||
|
||||
$b = new Cookie($data);
|
||||
$this->assertTrue($this->jar->add($b));
|
||||
$this->assertEquals(2, count($this->jar));
|
||||
|
||||
// Try to re-set the same cookie with no value: assert that cookie is not added
|
||||
$data['value'] = null;
|
||||
$this->assertFalse($this->jar->add(new Cookie($data)));
|
||||
// assert that original cookie has been deleted
|
||||
$this->assertEquals(0, count($this->jar));
|
||||
$cookies = $this->jar->all('foo.com');
|
||||
$this->assertTrue(in_array($a, $cookies, true));
|
||||
$this->assertFalse(in_array($b, $cookies, true));
|
||||
$this->assertEquals(1, count($this->jar));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user