3
namespace GuzzleHttp\Tests\CookieJar;
5
use GuzzleHttp\Cookie\CookieJar;
6
use GuzzleHttp\Cookie\SetCookie;
7
use GuzzleHttp\Message\Request;
8
use GuzzleHttp\Message\Response;
11
* @covers GuzzleHttp\Cookie\CookieJar
13
class CookieJarTest extends \PHPUnit_Framework_TestCase
18
public function setUp()
20
$this->jar = new CookieJar();
23
protected function getTestCookies()
26
new SetCookie(['Name' => 'foo', 'Value' => 'bar', 'Domain' => 'foo.com', 'Path' => '/', 'Discard' => true]),
27
new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2]),
28
new SetCookie(['Name' => 'you', 'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => time() + 1000])
32
public function testQuotesBadCookieValues()
34
$this->assertEquals('foo', CookieJar::getCookieValue('foo'));
35
$this->assertEquals('"foo,bar"', CookieJar::getCookieValue('foo,bar'));
38
public function testCreatesFromArray()
40
$jar = CookieJar::fromArray([
44
$this->assertCount(2, $jar);
48
* Provides test data for cookie cookieJar retrieval
50
public function getCookiesDataProvider()
53
[['foo', 'baz', 'test', 'muppet', 'googoo'], '', '', '', false],
54
[['foo', 'baz', 'muppet', 'googoo'], '', '', '', true],
55
[['googoo'], 'www.example.com', '', '', false],
56
[['muppet', 'googoo'], 'test.y.example.com', '', '', false],
57
[['foo', 'baz'], 'example.com', '', '', false],
58
[['muppet'], 'x.y.example.com', '/acme/', '', false],
59
[['muppet'], 'x.y.example.com', '/acme/test/', '', false],
60
[['googoo'], 'x.y.example.com', '/test/acme/test/', '', false],
61
[['foo', 'baz'], 'example.com', '', '', false],
62
[['baz'], 'example.com', '', 'baz', false],
66
public function testStoresAndRetrievesCookies()
68
$cookies = $this->getTestCookies();
69
foreach ($cookies as $cookie) {
70
$this->assertTrue($this->jar->setCookie($cookie));
73
$this->assertEquals(3, count($this->jar));
74
$this->assertEquals(3, count($this->jar->getIterator()));
75
$this->assertEquals($cookies, $this->jar->getIterator()->getArrayCopy());
78
public function testRemovesTemporaryCookies()
80
$cookies = $this->getTestCookies();
81
foreach ($this->getTestCookies() as $cookie) {
82
$this->jar->setCookie($cookie);
84
$this->jar->clearSessionCookies();
86
[$cookies[1], $cookies[2]],
87
$this->jar->getIterator()->getArrayCopy()
91
public function testRemovesSelectively()
93
foreach ($this->getTestCookies() as $cookie) {
94
$this->jar->setCookie($cookie);
97
// Remove foo.com cookies
98
$this->jar->clear('foo.com');
99
$this->assertEquals(2, count($this->jar));
100
// Try again, removing no further cookies
101
$this->jar->clear('foo.com');
102
$this->assertEquals(2, count($this->jar));
104
// Remove bar.com cookies with path of /boo
105
$this->jar->clear('bar.com', '/boo');
106
$this->assertEquals(1, count($this->jar));
108
// Remove cookie by name
109
$this->jar->clear(null, null, 'test');
110
$this->assertEquals(0, count($this->jar));
113
public function testDoesNotAddIncompleteCookies()
115
$this->assertEquals(false, $this->jar->setCookie(new SetCookie()));
116
$this->assertFalse($this->jar->setCookie(new SetCookie(array(
119
$this->assertFalse($this->jar->setCookie(new SetCookie(array(
122
$this->assertFalse($this->jar->setCookie(new SetCookie(array(
125
$this->assertFalse($this->jar->setCookie(new SetCookie(array(
127
'Domain' => 'foo.com'
131
public function testDoesAddValidCookies()
133
$this->assertTrue($this->jar->setCookie(new SetCookie(array(
135
'Domain' => 'foo.com',
138
$this->assertTrue($this->jar->setCookie(new SetCookie(array(
140
'Domain' => 'foo.com',
143
$this->assertTrue($this->jar->setCookie(new SetCookie(array(
145
'Domain' => 'foo.com',
150
public function testOverwritesCookiesThatAreOlderOrDiscardable()
156
'Domain' => '.example.com',
158
'Max-Age' => '86400',
164
// Make sure that the discard cookie is overridden with the non-discard
165
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
166
$this->assertEquals(1, count($this->jar));
168
$data['Discard'] = false;
169
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
170
$this->assertEquals(1, count($this->jar));
172
$c = $this->jar->getIterator()->getArrayCopy();
173
$this->assertEquals(false, $c[0]->getDiscard());
175
// Make sure it doesn't duplicate the cookie
176
$this->jar->setCookie(new SetCookie($data));
177
$this->assertEquals(1, count($this->jar));
179
// Make sure the more future-ful expiration date supersede the other
180
$data['Expires'] = time() + 2000;
181
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
182
$this->assertEquals(1, count($this->jar));
183
$c = $this->jar->getIterator()->getArrayCopy();
184
$this->assertNotEquals($t, $c[0]->getExpires());
187
public function testOverwritesCookiesThatHaveChanged()
193
'Domain' => '.example.com',
195
'Max-Age' => '86400',
201
// Make sure that the discard cookie is overridden with the non-discard
202
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
204
$data['Value'] = 'boo';
205
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
206
$this->assertEquals(1, count($this->jar));
208
// Changing the value plus a parameter also must overwrite the existing one
209
$data['Value'] = 'zoo';
210
$data['Secure'] = false;
211
$this->assertTrue($this->jar->setCookie(new SetCookie($data)));
212
$this->assertEquals(1, count($this->jar));
214
$c = $this->jar->getIterator()->getArrayCopy();
215
$this->assertEquals('zoo', $c[0]->getValue());
218
public function testAddsCookiesFromResponseWithRequest()
220
$response = new Response(200, array(
221
'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
223
$request = new Request('GET', 'http://www.example.com');
224
$this->jar->extractCookies($request, $response);
225
$this->assertEquals(1, count($this->jar));
228
public function getMatchingCookiesDataProvider()
231
array('https://example.com', 'foo=bar; baz=foobar'),
232
array('http://example.com', ''),
233
array('https://example.com:8912', 'foo=bar; baz=foobar'),
234
array('https://foo.example.com', 'foo=bar; baz=foobar'),
235
array('http://foo.example.com/test/acme/', 'googoo=gaga')
240
* @dataProvider getMatchingCookiesDataProvider
242
public function testReturnsCookiesMatchingRequests($url, $cookies)
248
'Domain' => 'example.com',
250
'Max-Age' => '86400',
256
'Domain' => 'example.com',
258
'Max-Age' => '86400',
264
'Domain' => 'www.foobar.com',
270
'Value' => 'cookie_monster',
271
'Domain' => '.y.example.com',
273
'Expires' => time() + 86400
278
'Domain' => '.example.com',
279
'Path' => '/test/acme/',
284
foreach ($bag as $cookie) {
285
$this->jar->setCookie($cookie);
288
$request = new Request('GET', $url);
289
$this->jar->addCookieHeader($request);
290
$this->assertEquals($cookies, $request->getHeader('Cookie'));
294
* @expectedException \RuntimeException
295
* @expectedExceptionMessage Invalid cookie: Cookie name must not cannot invalid characters:
297
public function testThrowsExceptionWithStrictMode()
299
$a = new CookieJar(true);
300
$a->setCookie(new SetCookie(['Name' => "abc\n", 'Value' => 'foo', 'Domain' => 'bar']));
303
public function testDeletesCookiesByName()
305
$cookies = $this->getTestCookies();
306
$cookies[] = new SetCookie([
309
'Domain' => 'bar.com',
311
'Expires' => time() + 1000
313
$jar = new CookieJar();
314
foreach ($cookies as $cookie) {
315
$jar->setCookie($cookie);
317
$this->assertCount(4, $jar);
318
$jar->clear('bar.com', '/boo', 'other');
319
$this->assertCount(3, $jar);
320
$names = array_map(function (SetCookie $c) {
321
return $c->getName();
322
}, $jar->getIterator()->getArrayCopy());
323
$this->assertEquals(['foo', 'test', 'you'], $names);
326
public function testCanConvertToAndLoadFromArray()
328
$jar = new CookieJar(true);
329
foreach ($this->getTestCookies() as $cookie) {
330
$jar->setCookie($cookie);
332
$this->assertCount(3, $jar);
333
$arr = $jar->toArray();
334
$this->assertCount(3, $arr);
335
$newCookieJar = new CookieJar(false, $arr);
336
$this->assertCount(3, $newCookieJar);
337
$this->assertSame($jar->toArray(), $newCookieJar->toArray());