-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CookieTest.php
571 lines (514 loc) · 16.5 KB
/
CookieTest.php
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Http\Cookie;
use Cake\Chronos\Chronos;
use Cake\Http\Cookie\Cookie;
use Cake\TestSuite\TestCase;
/**
* HTTP cookies test.
*/
class CookieTest extends TestCase
{
/**
* Generate invalid cookie names.
*
* @return array
*/
public function invalidNameProvider()
{
return [
['no='],
["no\rnewline"],
["no\nnewline"],
["no\ttab"],
["no,comma"],
["no;semi"],
];
}
/**
* Test invalid cookie name
*
* @dataProvider invalidNameProvider
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage contains invalid characters.
*/
public function testValidateNameInvalidChars($name)
{
new Cookie($name, 'value');
}
/**
* Test empty cookie name
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The cookie name cannot be empty.
* @return void
*/
public function testValidateNameEmptyName()
{
new Cookie('', '');
}
/**
* Tests the header value
*
* @return void
*/
public function testToHeaderValue()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$result = $cookie->toHeaderValue();
$this->assertEquals('cakephp=cakephp-rocks', $result);
$date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2027 12:00:00');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie = $cookie->withDomain('cakephp.org')
->withExpiry($date)
->withHttpOnly(true)
->withSecure(true);
$result = $cookie->toHeaderValue();
$expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; domain=cakephp.org; secure; httponly';
$this->assertEquals($expected, $result);
}
/**
* Test getting the value from the cookie
*
* @return void
*/
public function testGetValue()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$result = $cookie->getValue();
$this->assertEquals('cakephp-rocks', $result);
$cookie = new Cookie('cakephp', '');
$result = $cookie->getValue();
$this->assertEquals('', $result);
}
/**
* Test setting values in cookies
*
* @return void
*/
public function testWithValue()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withValue('new');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertSame('cakephp-rocks', $cookie->getValue(), 'old instance not modified');
$this->assertSame('new', $new->getValue());
}
/**
* Test setting domain in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `string` but `integer` given
*/
public function testWithDomainInvalidConstructor()
{
new Cookie('cakephp', 'rocks', null, '', 1234);
}
/**
* Test setting domain in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `string` but `array` given
*/
public function testWithDomainInvalid()
{
$cookie = new Cookie('cakephp', 'rocks');
$cookie->withDomain(['oops']);
}
/**
* Test setting domain in cookies
*
* @return void
*/
public function testWithDomain()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withDomain('example.com');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertNotContains('example.com', $cookie->toHeaderValue(), 'old instance not modified');
$this->assertContains('domain=example.com', $new->toHeaderValue());
}
/**
* Test setting path in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `string` but `array` given
*/
public function testWithPathInvalid()
{
$cookie = new Cookie('cakephp', 'rocks');
$cookie->withPath(['oops']);
}
/**
* Test setting path in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `string` but `integer` given
*/
public function testWithPathInvalidConstructor()
{
new Cookie('cakephp', 'rocks', null, 123);
}
/**
* Test setting path in cookies
*
* @return void
*/
public function testWithPath()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withPath('/api');
$this->assertNotSame($new, $cookie, 'Should make a clone');
$this->assertNotContains('path=/api', $cookie->toHeaderValue(), 'old instance not modified');
$this->assertContains('path=/api', $new->toHeaderValue());
}
/**
* Test setting httponly in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
*/
public function testWithHttpOnlyInvalidConstructor()
{
new Cookie('cakephp', 'cakephp-rocks', null, '', '', false, 'invalid');
}
/**
* Test setting httponly in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
*/
public function testWithHttpOnlyInvalid()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie->withHttpOnly('no');
}
/**
* Test setting httponly in cookies
*
* @return void
*/
public function testWithHttpOnly()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withHttpOnly(true);
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertFalse($cookie->isHttpOnly());
$this->assertTrue($new->isHttpOnly());
}
/**
* Test setting secure in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
*/
public function testWithSecureInvalidConstructor()
{
new Cookie('cakephp', 'cakephp-rocks', null, '', '', 'invalid');
}
/**
* Test setting secure in cookies
*
* @return void
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
*/
public function testWithSecureInvalid()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie->withSecure('no');
}
/**
* Test setting secure in cookies
*
* @return void
*/
public function testWithSecure()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withSecure(true);
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertFalse($cookie->isSecure());
$this->assertTrue($new->isSecure());
}
/**
* Test the never expiry method
*
* @return void
*/
public function testWithNeverExpire()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withNeverExpire();
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertContains('01-Jan-2038', $new->toHeaderValue());
}
/**
* Test the expired method
*
* @return void
*/
public function testWithExpired()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withExpired();
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotContains('expiry', $cookie->toHeaderValue());
$now = Chronos::parse('-1 year');
$this->assertContains($now->format('Y'), $new->toHeaderValue());
}
/**
* Test the withExpiry method
*
* @return void
*/
public function testWithExpiry()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withExpiry(Chronos::createFromDate(2022, 6, 15));
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotContains('expires', $cookie->toHeaderValue());
$this->assertContains('expires=Wed, 15-Jun-2022', $new->toHeaderValue());
}
/**
* Test the withExpiry method changes timezone
*
* @return void
*/
public function testWithExpiryChangesTimezone()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$date = Chronos::createFromDate(2022, 6, 15);
$date = $date->setTimezone('America/New_York');
$new = $cookie->withExpiry($date);
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotContains('expires', $cookie->toHeaderValue());
$this->assertContains('expires=Wed, 15-Jun-2022', $new->toHeaderValue());
$this->assertContains('GMT', $new->toHeaderValue());
$this->assertSame($date->format('U'), $new->getExpiresTimestamp());
}
/**
* Test the isExpired method
*
* @return void
*/
public function testIsExpired()
{
$date = Chronos::now();
$cookie = new Cookie('cakephp', 'yay');
$this->assertFalse($cookie->isExpired($date));
$cookie = new Cookie('cakephp', 'yay', $date);
$this->assertFalse($cookie->isExpired($date), 'same time, not expired');
$date = $date->modify('+10 seconds');
$this->assertTrue($cookie->isExpired($date), 'future now');
$date = $date->modify('-1 minute');
$this->assertFalse($cookie->isExpired($date), 'expires later');
}
/**
* Test the withName method
*
* @return void
*/
public function testWithName()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$new = $cookie->withName('user');
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotSame('user', $cookie->getName());
$this->assertSame('user', $new->getName());
}
/**
* Test the withAddedValue method
*
* @return void
*/
public function testWithAddedValue()
{
$cookie = new Cookie('cakephp', '{"type":"mvc", "icing": true}');
$new = $cookie->withAddedValue('type', 'mvc')
->withAddedValue('user.name', 'mark');
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNull($cookie->read('user.name'));
$this->assertSame('mvc', $new->read('type'));
$this->assertSame('mark', $new->read('user.name'));
}
/**
* Test the withoutAddedValue method
*
* @return void
*/
public function testWithoutAddedValue()
{
$cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
$new = $cookie->withoutAddedValue('type', 'mvc')
->withoutAddedValue('user.name');
$this->assertNotSame($new, $cookie, 'Should clone');
$this->assertNotNull($cookie->read('type'));
$this->assertNull($new->read('type'));
$this->assertNull($new->read('user.name'));
}
/**
* Test check() with serialized source data.
*
* @return void
*/
public function testCheckStringSourceData()
{
$cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
$this->assertTrue($cookie->check('type'));
$this->assertTrue($cookie->check('user.name'));
$this->assertFalse($cookie->check('nope'));
$this->assertFalse($cookie->check('user.nope'));
}
/**
* Test check() with array source data.
*
* @return void
*/
public function testCheckArraySourceData()
{
$data = [
'type' => 'mvc',
'user' => ['name' => 'mark']
];
$cookie = new Cookie('cakephp', $data);
$this->assertTrue($cookie->check('type'));
$this->assertTrue($cookie->check('user.name'));
$this->assertFalse($cookie->check('nope'));
$this->assertFalse($cookie->check('user.nope'));
}
/**
* test read() and set on different types
*
* @return void
*/
public function testReadExpandsOnDemand()
{
$data = [
'username' => 'florian',
'profile' => [
'profession' => 'developer'
]
];
$cookie = new Cookie('cakephp', json_encode($data));
$this->assertFalse($cookie->isExpanded());
$this->assertEquals('developer', $cookie->read('profile.profession'));
$this->assertTrue($cookie->isExpanded(), 'Cookies expand when read.');
$cookie = $cookie->withValue(json_encode($data));
$this->assertTrue($cookie->check('profile.profession'), 'Cookies expand when read.');
$this->assertTrue($cookie->isExpanded());
$cookie = $cookie->withValue(json_encode($data))
->withAddedValue('face', 'punch');
$this->assertTrue($cookie->isExpanded());
$this->assertSame('punch', $cookie->read('face'));
}
/**
* test read() on structured data.
*
* @return void
*/
public function testReadComplexData()
{
$data = [
'username' => 'florian',
'profile' => [
'profession' => 'developer'
]
];
$cookie = new Cookie('cakephp', $data);
$result = $cookie->getValue();
$this->assertEquals($data, $result);
$result = $cookie->read('foo');
$this->assertNull($result);
$result = $cookie->read();
$this->assertEquals($data, $result);
$result = $cookie->read('profile.profession');
$this->assertEquals('developer', $result);
}
/**
* Test reading complex data serialized in 1.x and early 2.x
*
* @return void
*/
public function testReadLegacyComplexData()
{
$data = 'key|value,key2|value2';
$cookie = new Cookie('cakephp', $data);
$this->assertEquals('value', $cookie->read('key'));
$this->assertNull($cookie->read('nope'));
}
/**
* Test that toHeaderValue() collapses data.
*
* @return void
*/
public function testToHeaderValueCollapsesComplexData()
{
$data = [
'username' => 'florian',
'profile' => [
'profession' => 'developer'
]
];
$cookie = new Cookie('cakephp', $data);
$this->assertEquals('developer', $cookie->read('profile.profession'));
$expected = '{"username":"florian","profile":{"profession":"developer"}}';
$this->assertContains(urlencode($expected), $cookie->toHeaderValue());
}
/**
* Tests getting the id
*
* @return void
*/
public function testGetId()
{
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$this->assertEquals('cakephp;;', $cookie->getId());
$cookie = new Cookie('CAKEPHP', 'cakephp-rocks');
$this->assertEquals('cakephp;;', $cookie->getId());
$cookie = new Cookie('test', 'val', null, '/path', 'example.com');
$this->assertEquals('test;example.com;/path', $cookie->getId());
}
/**
* Test toArray
*
* @return void
*/
public function testToArray()
{
$date = Chronos::parse('2017-03-31 12:34:56');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie = $cookie->withDomain('cakephp.org')
->withPath('/api')
->withExpiry($date)
->withHttpOnly(true)
->withSecure(true);
$expected = [
'name' => 'cakephp',
'value' => 'cakephp-rocks',
'path' => '/api',
'domain' => 'cakephp.org',
'expires' => $date->format('U'),
'secure' => true,
'httponly' => true
];
$this->assertEquals($expected, $cookie->toArray());
}
}