scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\CookieJar;
4  
5 use GuzzleHttp\Cookie\SetCookie;
6  
7 /**
8 * @covers GuzzleHttp\Cookie\SetCookie
9 */
10 class SetCookieTest extends \PHPUnit_Framework_TestCase
11 {
12 public function testInitializesDefaultValues()
13 {
14 $cookie = new SetCookie();
15 $this->assertEquals('/', $cookie->getPath());
16 }
17  
18 public function testConvertsDateTimeMaxAgeToUnixTimestamp()
19 {
20 $cookie = new SetCookie(['Expires' => 'November 20, 1984']);
21 $this->assertInternalType('integer', $cookie->getExpires());
22 }
23  
24 public function testAddsExpiresBasedOnMaxAge()
25 {
26 $t = time();
27 $cookie = new SetCookie(['Max-Age' => 100]);
28 $this->assertEquals($t + 100, $cookie->getExpires());
29 }
30  
31 public function testHoldsValues()
32 {
33 $t = time();
34 $data = array(
35 'Name' => 'foo',
36 'Value' => 'baz',
37 'Path' => '/bar',
38 'Domain' => 'baz.com',
39 'Expires' => $t,
40 'Max-Age' => 100,
41 'Secure' => true,
42 'Discard' => true,
43 'HttpOnly' => true,
44 'foo' => 'baz',
45 'bar' => 'bam'
46 );
47  
48 $cookie = new SetCookie($data);
49 $this->assertEquals($data, $cookie->toArray());
50  
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']);
62  
63 $cookie->setName('a')
64 ->setValue('b')
65 ->setPath('c')
66 ->setDomain('bar.com')
67 ->setExpires(10)
68 ->setMaxAge(200)
69 ->setSecure(false)
70 ->setHttpOnly(false)
71 ->setDiscard(false);
72  
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());
82 }
83  
84 public function testDeterminesIfExpired()
85 {
86 $c = new SetCookie();
87 $c->setExpires(10);
88 $this->assertTrue($c->isExpired());
89 $c->setExpires(time() + 10000);
90 $this->assertFalse($c->isExpired());
91 }
92  
93 public function testMatchesDomain()
94 {
95 $cookie = new SetCookie();
96 $this->assertTrue($cookie->matchesDomain('baz.com'));
97  
98 $cookie->setDomain('baz.com');
99 $this->assertTrue($cookie->matchesDomain('baz.com'));
100 $this->assertFalse($cookie->matchesDomain('bar.com'));
101  
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'));
107  
108 $cookie->setDomain('.127.0.0.1');
109 $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
110  
111 $cookie->setDomain('127.0.0.1');
112 $this->assertTrue($cookie->matchesDomain('127.0.0.1'));
113  
114 $cookie->setDomain('.com.');
115 $this->assertFalse($cookie->matchesDomain('baz.com'));
116  
117 $cookie->setDomain('.local');
118 $this->assertTrue($cookie->matchesDomain('example.local'));
119 }
120  
121 public function testMatchesPath()
122 {
123 $cookie = new SetCookie();
124 $this->assertTrue($cookie->matchesPath('/foo'));
125  
126 $cookie->setPath('/foo');
127 $this->assertTrue($cookie->matchesPath('/foo'));
128 $this->assertTrue($cookie->matchesPath('/foo/bar'));
129 $this->assertFalse($cookie->matchesPath('/bar'));
130 }
131  
132 public function cookieValidateProvider()
133 {
134 return array(
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'),
141 );
142 }
143  
144 /**
145 * @dataProvider cookieValidateProvider
146 */
147 public function testValidatesCookies($name, $value, $domain, $result)
148 {
149 $cookie = new SetCookie(array(
150 'Name' => $name,
151 'Value' => $value,
152 'Domain' => $domain
153 ));
154 $this->assertSame($result, $cookie->validate());
155 }
156  
157 public function testDoesNotMatchIp()
158 {
159 $cookie = new SetCookie(['Domain' => '192.168.16.']);
160 $this->assertFalse($cookie->matchesDomain('192.168.16.121'));
161 }
162  
163 public function testConvertsToString()
164 {
165 $t = 1382916008;
166 $cookie = new SetCookie([
167 'Name' => 'test',
168 'Value' => '123',
169 'Domain' => 'foo.com',
170 'Expires' => $t,
171 'Path' => '/abc',
172 'HttpOnly' => true,
173 'Secure' => true
174 ]);
175 $this->assertEquals(
176 'test=123; Domain=foo.com; Path=/abc; Expires=Sun, 27 Oct 2013 23:20:08 GMT; Secure; HttpOnly',
177 (string) $cookie
178 );
179 }
180  
181 /**
182 * Provides the parsed information from a cookie
183 *
184 * @return array
185 */
186 public function cookieParserDataProvider()
187 {
188 return array(
189 array(
190 'ASIHTTPRequestTestCookie=This+is+the+value; expires=Sat, 26-Jul-2008 17:00:42 GMT; path=/tests; domain=allseeing-i.com; PHPSESSID=6c951590e7a9359bcedde25cda73e43c; path=/";',
191 array(
192 'Domain' => 'allseeing-i.com',
193 'Path' => '/',
194 'PHPSESSID' => '6c951590e7a9359bcedde25cda73e43c',
195 'Max-Age' => NULL,
196 'Expires' => 'Sat, 26-Jul-2008 17:00:42 GMT',
197 'Secure' => NULL,
198 'Discard' => NULL,
199 'Name' => 'ASIHTTPRequestTestCookie',
200 'Value' => 'This+is+the+value',
201 'HttpOnly' => false
202 )
203 ),
204 array('', []),
205 array('foo', []),
206 // Test setting a blank value for a cookie
207 array(array(
208 'foo=', 'foo =', 'foo =;', 'foo= ;', 'foo =', 'foo= '),
209 array(
210 'Name' => 'foo',
211 'Value' => '',
212 'Discard' => null,
213 'Domain' => null,
214 'Expires' => null,
215 'Max-Age' => null,
216 'Path' => '/',
217 'Secure' => null,
218 'HttpOnly' => false
219 )
220 ),
221 // Test setting a value and removing quotes
222 array(array(
223 'foo=1', 'foo =1', 'foo =1;', 'foo=1 ;', 'foo =1', 'foo= 1', 'foo = 1 ;', 'foo="1"', 'foo="1";', 'foo= "1";'),
224 array(
225 'Name' => 'foo',
226 'Value' => '1',
227 'Discard' => null,
228 'Domain' => null,
229 'Expires' => null,
230 'Max-Age' => null,
231 'Path' => '/',
232 'Secure' => null,
233 'HttpOnly' => false
234 )
235 ),
236 // Some of the following tests are based on http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Http/CookieTest.php
237 array(
238 'justacookie=foo; domain=example.com',
239 array(
240 'Name' => 'justacookie',
241 'Value' => 'foo',
242 'Domain' => 'example.com',
243 'Discard' => null,
244 'Expires' => null,
245 'Max-Age' => null,
246 'Path' => '/',
247 'Secure' => null,
248 'HttpOnly' => false
249 )
250 ),
251 array(
252 'expires=tomorrow; secure; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com',
253 array(
254 'Name' => 'expires',
255 'Value' => 'tomorrow',
256 'Domain' => '.example.com',
257 'Path' => '/Space Out/',
258 'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
259 'Discard' => null,
260 'Secure' => true,
261 'Max-Age' => null,
262 'HttpOnly' => false
263 )
264 ),
265 array(
266 'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com; path=/some value/',
267 array(
268 'Name' => 'domain',
269 'Value' => 'unittests',
270 'Domain' => 'example.com',
271 'Path' => '/some value/',
272 'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
273 'Secure' => false,
274 'Discard' => null,
275 'Max-Age' => null,
276 'HttpOnly' => false
277 )
278 ),
279 array(
280 'path=indexAction; path=/; domain=.foo.com; expires=Tue, 21-Nov-2006 08:33:44 GMT',
281 array(
282 'Name' => 'path',
283 'Value' => 'indexAction',
284 'Domain' => '.foo.com',
285 'Path' => '/',
286 'Expires' => 'Tue, 21-Nov-2006 08:33:44 GMT',
287 'Secure' => false,
288 'Discard' => null,
289 'Max-Age' => null,
290 'HttpOnly' => false
291 )
292 ),
293 array(
294 'secure=sha1; secure; SECURE; domain=some.really.deep.domain.com; version=1; Max-Age=86400',
295 array(
296 'Name' => 'secure',
297 'Value' => 'sha1',
298 'Domain' => 'some.really.deep.domain.com',
299 'Path' => '/',
300 'Secure' => true,
301 'Discard' => null,
302 'Expires' => time() + 86400,
303 'Max-Age' => 86400,
304 'HttpOnly' => false,
305 'version' => '1'
306 )
307 ),
308 array(
309 'PHPSESSID=123456789+abcd%2Cef; secure; discard; domain=.localdomain; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT;',
310 array(
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',
316 'Secure' => true,
317 'Discard' => true,
318 'Max-Age' => null,
319 'HttpOnly' => false
320 )
321 ),
322 );
323 }
324  
325 /**
326 * @dataProvider cookieParserDataProvider
327 */
328 public function testParseCookie($cookie, $parsed)
329 {
330 foreach ((array) $cookie as $v) {
331 $c = SetCookie::fromString($v);
332 $p = $c->toArray();
333  
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']);
339 }
340 }
341  
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));
345 }
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));
348 }
349 } else {
350 $this->assertEquals([
351 'Name' => null,
352 'Value' => null,
353 'Domain' => null,
354 'Path' => '/',
355 'Max-Age' => null,
356 'Expires' => null,
357 'Secure' => false,
358 'Discard' => false,
359 'HttpOnly' => false,
360 ], $p);
361 }
362 }
363 }
364 }