3
namespace GuzzleHttp\Tests\CookieJar;
5
use GuzzleHttp\Cookie\SetCookie;
8
* @covers GuzzleHttp\Cookie\SetCookie
10
class SetCookieTest extends \PHPUnit_Framework_TestCase
12
public function testInitializesDefaultValues()
14
$cookie = new SetCookie();
15
$this->assertEquals('/', $cookie->getPath());
18
public function testConvertsDateTimeMaxAgeToUnixTimestamp()
20
$cookie = new SetCookie(['Expires' => 'November 20, 1984']);
21
$this->assertInternalType('integer', $cookie->getExpires());
24
public function testAddsExpiresBasedOnMaxAge()
27
$cookie = new SetCookie(['Max-Age' => 100]);
28
$this->assertEquals($t + 100, $cookie->getExpires());
31
public function testHoldsValues()
38
'Domain' => 'baz.com',
48
$cookie = new SetCookie($data);
49
$this->assertEquals($data, $cookie->toArray());
51
$this->assertEquals('foo', $cookie->getName());
52
$this->assertEquals('baz', $cookie->getValue());
53
$this->assertEquals('baz.com', $cookie->getDomain());
54
$this->assertEquals('/bar', $cookie->getPath());
55
$this->assertEquals($t, $cookie->getExpires());
56
$this->assertEquals(100, $cookie->getMaxAge());
57
$this->assertTrue($cookie->getSecure());
58
$this->assertTrue($cookie->getDiscard());
59
$this->assertTrue($cookie->getHttpOnly());
60
$this->assertEquals('baz', $cookie->toArray()['foo']);
61
$this->assertEquals('bam', $cookie->toArray()['bar']);
66
->setDomain('bar.com')
73
$this->assertEquals('a', $cookie->getName());
74
$this->assertEquals('b', $cookie->getValue());
75
$this->assertEquals('c', $cookie->getPath());
76
$this->assertEquals('bar.com', $cookie->getDomain());
77
$this->assertEquals(10, $cookie->getExpires());
78
$this->assertEquals(200, $cookie->getMaxAge());
79
$this->assertFalse($cookie->getSecure());
80
$this->assertFalse($cookie->getDiscard());
81
$this->assertFalse($cookie->getHttpOnly());
84
public function testDeterminesIfExpired()
88
$this->assertTrue($c->isExpired());
89
$c->setExpires(time() + 10000);
90
$this->assertFalse($c->isExpired());
93
public function testMatchesDomain()
95
$cookie = new SetCookie();
96
$this->assertTrue($cookie->matchesDomain('baz.com'));
98
$cookie->setDomain('baz.com');
99
$this->assertTrue($cookie->matchesDomain('baz.com'));
100
$this->assertFalse($cookie->matchesDomain('bar.com'));
102
$cookie->setDomain('.baz.com');
103
$this->assertTrue($cookie->matchesDomain('.baz.com'));
104
$this->assertTrue($cookie->matchesDomain('foo.baz.com'));
105
$this->assertFalse($cookie->matchesDomain('baz.bar.com'));
106
$this->assertTrue($cookie->matchesDomain('baz.com'));
108
$cookie->setDomain('.127.0.0.1');
109
$this->assertTrue($cookie->matchesDomain('127.0.0.1'));
111
$cookie->setDomain('127.0.0.1');
112
$this->assertTrue($cookie->matchesDomain('127.0.0.1'));
114
$cookie->setDomain('.com.');
115
$this->assertFalse($cookie->matchesDomain('baz.com'));
117
$cookie->setDomain('.local');
118
$this->assertTrue($cookie->matchesDomain('example.local'));
121
public function testMatchesPath()
123
$cookie = new SetCookie();
124
$this->assertTrue($cookie->matchesPath('/foo'));
126
$cookie->setPath('/foo');
127
$this->assertTrue($cookie->matchesPath('/foo'));
128
$this->assertTrue($cookie->matchesPath('/foo/bar'));
129
$this->assertFalse($cookie->matchesPath('/bar'));
132
public function cookieValidateProvider()
135
array('foo', 'baz', 'bar', true),
136
array('0', '0', '0', true),
137
array('', 'baz', 'bar', 'The cookie name must not be empty'),
138
array('foo', '', 'bar', 'The cookie value must not be empty'),
139
array('foo', 'baz', '', 'The cookie domain must not be empty'),
140
array("foo\r", 'baz', '0', 'Cookie name must not cannot invalid characters: =,; \t\r\n\013\014'),
145
* @dataProvider cookieValidateProvider
147
public function testValidatesCookies($name, $value, $domain, $result)
149
$cookie = new SetCookie(array(
154
$this->assertSame($result, $cookie->validate());
157
public function testDoesNotMatchIp()
159
$cookie = new SetCookie(['Domain' => '192.168.16.']);
160
$this->assertFalse($cookie->matchesDomain('192.168.16.121'));
163
public function testConvertsToString()
166
$cookie = new SetCookie([
169
'Domain' => 'foo.com',
176
'test=123; Domain=foo.com; Path=/abc; Expires=Sun, 27 Oct 2013 23:20:08 GMT; Secure; HttpOnly',
182
* Provides the parsed information from a cookie
186
public function cookieParserDataProvider()
190
'ASIHTTPRequestTestCookie=This+is+the+value; expires=Sat, 26-Jul-2008 17:00:42 GMT; path=/tests; domain=allseeing-i.com; PHPSESSID=6c951590e7a9359bcedde25cda73e43c; path=/";',
192
'Domain' => 'allseeing-i.com',
194
'PHPSESSID' => '6c951590e7a9359bcedde25cda73e43c',
196
'Expires' => 'Sat, 26-Jul-2008 17:00:42 GMT',
199
'Name' => 'ASIHTTPRequestTestCookie',
200
'Value' => 'This+is+the+value',
206
// Test setting a blank value for a cookie
208
'foo=', 'foo =', 'foo =;', 'foo= ;', 'foo =', 'foo= '),
221
// Test setting a value and removing quotes
223
'foo=1', 'foo =1', 'foo =1;', 'foo=1 ;', 'foo =1', 'foo= 1', 'foo = 1 ;', 'foo="1"', 'foo="1";', 'foo= "1";'),
236
// Some of the following tests are based on http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Http/CookieTest.php
238
'justacookie=foo; domain=example.com',
240
'Name' => 'justacookie',
242
'Domain' => 'example.com',
252
'expires=tomorrow; secure; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com',
255
'Value' => 'tomorrow',
256
'Domain' => '.example.com',
257
'Path' => '/Space Out/',
258
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
266
'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com; path=/some value/',
269
'Value' => 'unittests',
270
'Domain' => 'example.com',
271
'Path' => '/some value/',
272
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
280
'path=indexAction; path=/; domain=.foo.com; expires=Tue, 21-Nov-2006 08:33:44 GMT',
283
'Value' => 'indexAction',
284
'Domain' => '.foo.com',
286
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
294
'secure=sha1; secure; SECURE; domain=some.really.deep.domain.com; version=1; Max-Age=86400',
298
'Domain' => 'some.really.deep.domain.com',
302
'Expires' => time() + 86400,
309
'PHPSESSID=123456789+abcd%2Cef; secure; discard; domain=.localdomain; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT;',
311
'Name' => 'PHPSESSID',
312
'Value' => '123456789+abcd%2Cef',
313
'Domain' => '.localdomain',
314
'Path' => '/foo/baz',
315
'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
326
* @dataProvider cookieParserDataProvider
328
public function testParseCookie($cookie, $parsed)
330
foreach ((array) $cookie as $v) {
331
$c = SetCookie::fromString($v);
334
if (isset($p['Expires'])) {
335
// Remove expires values from the assertion if they are relatively equal
336
if (abs($p['Expires'] != strtotime($parsed['Expires'])) < 40) {
337
unset($p['Expires']);
338
unset($parsed['Expires']);
342
if (!empty($parsed)) {
343
foreach ($parsed as $key => $value) {
344
$this->assertEquals($parsed[$key], $p[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
346
foreach ($p as $key => $value) {
347
$this->assertEquals($p[$key], $parsed[$key], 'Comparing ' . $key . ' ' . var_export($value, true) . ' : ' . var_export($parsed, true) . ' | ' . var_export($p, true));
350
$this->assertEquals([