1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386:
<?php
namespace Guzzle\Tests\Plugin\Cookie\CookieJar;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\Request;
class ArrayCookieJarTest extends \Guzzle\Tests\GuzzleTestCase
{
private $jar;
public function setUp()
{
$this->jar = new ArrayCookieJar();
}
protected function getTestCookies()
{
return array(
new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'foo.com', 'path' => '/', 'discard' => true)),
new Cookie(array('name' => 'test', 'value' => '123', 'domain' => 'baz.com', 'path' => '/foo', 'expires' => 2)),
new Cookie(array('name' => 'you', 'value' => '123', 'domain' => 'bar.com', 'path' => '/boo', 'expires' => time() + 1000))
);
}
public function getCookiesDataProvider()
{
return array(
array(array('foo', 'baz', 'test', 'muppet', 'googoo'), '', '', '', false),
array(array('foo', 'baz', 'muppet', 'googoo'), '', '', '', true),
array(array('googoo'), 'www.example.com', '', '', false),
array(array('muppet', 'googoo'), 'test.y.example.com', '', '', false),
array(array('foo', 'baz'), 'example.com', '', '', false),
array(array('muppet'), 'x.y.example.com', '/acme/', '', false),
array(array('muppet'), 'x.y.example.com', '/acme/test/', '', false),
array(array('googoo'), 'x.y.example.com', '/test/acme/test/', '', false),
array(array('foo', 'baz'), 'example.com', '', '', false),
array(array('baz'), 'example.com', '', 'baz', false),
);
}
public function testStoresAndRetrievesCookies()
{
$cookies = $this->getTestCookies();
foreach ($cookies as $cookie) {
$this->assertTrue($this->jar->add($cookie));
}
$this->assertEquals(3, count($this->jar));
$this->assertEquals(3, count($this->jar->getIterator()));
$this->assertEquals($cookies, $this->jar->all(null, null, null, false, false));
}
public function testRemovesExpiredCookies()
{
$cookies = $this->getTestCookies();
foreach ($this->getTestCookies() as $cookie) {
$this->jar->add($cookie);
}
$this->jar->removeExpired();
$this->assertEquals(array($cookies[0], $cookies[2]), $this->jar->all());
}
public function testRemovesTemporaryCookies()
{
$cookies = $this->getTestCookies();
foreach ($this->getTestCookies() as $cookie) {
$this->jar->add($cookie);
}
$this->jar->removeTemporary();
$this->assertEquals(array($cookies[2]), $this->jar->all());
}
public function testIsSerializable()
{
$this->assertEquals('[]', $this->jar->serialize());
$this->jar->unserialize('[]');
$this->assertEquals(array(), $this->jar->all());
$cookies = $this->getTestCookies();
foreach ($this->getTestCookies() as $cookie) {
$this->jar->add($cookie);
}
$serialized = $this->jar->serialize();
$data = json_decode($serialized, true);
$this->assertEquals(1, count($data));
$a = new ArrayCookieJar();
$a->unserialize($serialized);
$this->assertEquals(1, count($a));
}
public function testRemovesSelectively()
{
$cookies = $this->getTestCookies();
foreach ($this->getTestCookies() as $cookie) {
$this->jar->add($cookie);
}
$this->jar->remove('foo.com');
$this->assertEquals(2, count($this->jar));
$this->jar->remove('foo.com');
$this->assertEquals(2, count($this->jar));
$this->jar->remove('bar.com', '/boo');
$this->assertEquals(1, count($this->jar));
$this->jar->remove(null, null, 'test');
$this->assertEquals(0, count($this->jar));
}
public function testDoesNotAddIncompleteCookies()
{
$this->assertEquals(false, $this->jar->add(new Cookie()));
$this->assertFalse($this->jar->add(new Cookie(array(
'name' => 'foo'
))));
$this->assertFalse($this->jar->add(new Cookie(array(
'name' => false
))));
$this->assertFalse($this->jar->add(new Cookie(array(
'name' => true
))));
$this->assertFalse($this->jar->add(new Cookie(array(
'name' => 'foo',
'domain' => 'foo.com'
))));
}
public function testDoesAddValidCookies()
{
$this->assertTrue($this->jar->add(new Cookie(array(
'name' => 'foo',
'domain' => 'foo.com',
'value' => 0
))));
$this->assertTrue($this->jar->add(new Cookie(array(
'name' => 'foo',
'domain' => 'foo.com',
'value' => 0.0
))));
$this->assertTrue($this->jar->add(new Cookie(array(
'name' => 'foo',
'domain' => 'foo.com',
'value' => '0'
))));
}
public function testOverwritesCookiesThatAreOlderOrDiscardable()
{
$t = time() + 1000;
$data = array(
'name' => 'foo',
'value' => 'bar',
'domain' => '.example.com',
'path' => '/',
'max_age' => '86400',
'port' => array(80, 8080),
'version' => '1',
'secure' => true,
'discard' => true,
'expires' => $t
);
$this->assertTrue($this->jar->add(new Cookie($data)));
unset($data['discard']);
$this->assertTrue($this->jar->add(new Cookie($data)));
$this->assertEquals(1, count($this->jar));
$c = $this->jar->all();
$this->assertEquals(false, $c[0]->getDiscard());
$this->jar->add(new Cookie($data));
$this->assertEquals(1, count($this->jar));
$data['expires'] = time() + 2000;
$this->assertTrue($this->jar->add(new Cookie($data)));
$this->assertEquals(1, count($this->jar));
$c = $this->jar->all();
$this->assertNotEquals($t, $c[0]->getExpires());
}
public function testOverwritesCookiesThatHaveChanged()
{
$t = time() + 1000;
$data = array(
'name' => 'foo',
'value' => 'bar',
'domain' => '.example.com',
'path' => '/',
'max_age' => '86400',
'port' => array(80, 8080),
'version' => '1',
'secure' => true,
'discard' => true,
'expires' => $t
);
$this->assertTrue($this->jar->add(new Cookie($data)));
$data['value'] = 'boo';
$this->assertTrue($this->jar->add(new Cookie($data)));
$this->assertEquals(1, count($this->jar));
$data['value'] = 'zoo';
$data['secure'] = false;
$this->assertTrue($this->jar->add(new Cookie($data)));
$this->assertEquals(1, count($this->jar));
$c = $this->jar->all();
$this->assertEquals('zoo', $c[0]->getValue());
}
public function testAddsCookiesFromResponseWithNoRequest()
{
$response = new Response(200, array(
'Set-Cookie' => array(
"fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
"FPCK3=AgBNbvoQAGpGEABZLRAAbFsQAF1tEABkDhAAeO0=; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
"CH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1",
"CH=AgBNbvoQAAEcEAApuhAAMJcQADQvEAAvGxAALe0QAD6uEAATwhAAC1AQAC8t; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1"
)
));
$this->jar->addCookiesFromResponse($response);
$this->assertEquals(3, count($this->jar));
$this->assertEquals(1, count($this->jar->all(null, null, 'fpc')));
$this->assertEquals(1, count($this->jar->all(null, null, 'FPCK3')));
$this->assertEquals(1, count($this->jar->all(null, null, 'CH')));
}
public function testAddsCookiesFromResponseWithRequest()
{
$response = new Response(200, array(
'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
));
$request = new Request('GET', 'http://www.example.com');
$this->jar->addCookiesFromResponse($response, $request);
$this->assertEquals(1, count($this->jar));
}
public function getMatchingCookiesDataProvider()
{
return array(
array('https://example.com', array(0)),
array('http://example.com', array()),
array('https://example.com:8912', array()),
array('https://foo.example.com', array(0)),
array('http://foo.example.com/test/acme/', array(4))
);
}
public function testReturnsCookiesMatchingRequests($url, $cookies)
{
$bag = array(
new Cookie(array(
'name' => 'foo',
'value' => 'bar',
'domain' => 'example.com',
'path' => '/',
'max_age' => '86400',
'port' => array(443, 8080),
'version' => '1',
'secure' => true
)),
new Cookie(array(
'name' => 'baz',
'value' => 'foobar',
'domain' => 'example.com',
'path' => '/',
'max_age' => '86400',
'port' => array(80, 8080),
'version' => '1',
'secure' => true
)),
new Cookie(array(
'name' => 'test',
'value' => '123',
'domain' => 'www.foobar.com',
'path' => '/path/',
'discard' => true
)),
new Cookie(array(
'name' => 'muppet',
'value' => 'cookie_monster',
'domain' => '.y.example.com',
'path' => '/acme/',
'comment' => 'Comment goes here...',
'expires' => time() + 86400
)),
new Cookie(array(
'name' => 'googoo',
'value' => 'gaga',
'domain' => '.example.com',
'path' => '/test/acme/',
'max_age' => 1500,
'version' => 2
))
);
foreach ($bag as $cookie) {
$this->jar->add($cookie);
}
$request = new Request('GET', $url);
$results = $this->jar->getMatchingCookies($request);
$this->assertEquals(count($cookies), count($results));
foreach ($cookies as $i) {
$this->assertContains($bag[$i], $results);
}
}
public function testThrowsExceptionWithStrictMode()
{
$a = new ArrayCookieJar();
$a->setStrictMode(true);
$a->add(new Cookie(array(
'name' => 'abc:@123',
'value' => 'foo',
'domain' => 'bar'
)));
}
public function testRemoveExistingCookieIfEmpty()
{
$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));
$data['value'] = null;
$this->assertFalse($this->jar->add(new Cookie($data)));
$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));
}
}