6
* @subpackage UnitTests
7
* @version $Id: CookieTest.php 14420 2009-03-22 11:57:37Z matthew $
8
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
9
* @license http://framework.zend.com/license/new-bsd New BSD License
12
require_once dirname(__FILE__) . '/../../TestHelper.php';
13
require_once 'Zend/Http/Cookie.php';
16
* Zend_Http_Cookie unit tests
20
* @subpackage UnitTests
21
* @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com/)
22
* @license http://framework.zend.com/license/new-bsd New BSD License
24
class Zend_Http_CookieTest extends PHPUnit_Framework_TestCase
27
* Make sure we can't set invalid names
29
public function testSetInvalidName()
31
$invalidcharacters = "=,; \t\r\n\013\014";
32
$l = strlen($invalidcharacters) - 1;
33
for ($i = 0; $i < $l; $i++) {
34
$name = 'cookie_' . $invalidcharacters[$i];
36
$cookie = new Zend_Http_Cookie($name, 'foo', 'example.com');
37
$this->fail('Expected invalid cookie name exception was not thrown for "' . $name . '"');
38
} catch (Zend_Http_Exception $e) {
45
* Test we get the cookie name properly
47
public function testGetName()
49
// Array of cookies and their names. We need to test each 'keyword' in
52
'justacookie' => 'justacookie=foo; domain=example.com',
53
'expires' => 'expires=tomorrow; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com',
54
'domain' => 'domain=unittests; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=example.com',
55
'path' => 'path=indexAction; path=/; domain=.foo.com',
56
'secure' => 'secure=sha1; secure; domain=.foo.com',
57
'PHPSESSID' => 'PHPSESSID=1234567890abcdef; secure; domain=.foo.com; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT;'
60
foreach ($cookies as $name => $cstr) {
61
$cookie = Zend_Http_Cookie::fromString($cstr);
62
if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Cookie ' . $name . ' is not a proper Cookie object');
63
$this->assertEquals($name, $cookie->getName(), 'Cookie name is not as expected');
68
* Make sure we get the correct value if it was set through the constructor
71
public function testGetValueConstructor()
74
'simpleCookie', 'space cookie', '!@#$%^*&()* ][{}?;', "line\n\rbreaks"
77
foreach ($values as $val) {
78
$cookie = new Zend_Http_Cookie('cookie', $val, 'example.com', time(), '/', true);
79
$this->assertEquals($val, $cookie->getValue());
84
* Make sure we get the correct value if it was set through fromString()
87
public function testGetValueFromString()
90
'simpleCookie', 'space cookie', '!@#$%^*&()* ][{}?;', "line\n\rbreaks"
93
foreach ($values as $val) {
94
$cookie = Zend_Http_Cookie::fromString('cookie=' . urlencode($val) . '; domain=example.com');
95
$this->assertEquals($val, $cookie->getValue());
100
* Make sure we get the correct domain when it's set in the cookie string
103
public function testGetDomainInStr()
106
'cookie=foo; domain=example.com' => 'example.com',
107
'cookie=foo; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => '.example.com',
108
'cookie=foo; domain=some.really.deep.domain.com; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT;' => 'some.really.deep.domain.com'
111
foreach ($domains as $cstr => $domain) {
112
$cookie = Zend_Http_Cookie::fromString($cstr);
113
if (! $cookie instanceof Zend_Http_Cookie) $this->fail('We didn\'t get a valid Cookie object');
114
$this->assertEquals($domain, $cookie->getDomain());
119
* Make sure we get the correct domain when it's set in a reference URL
122
public function testGetDomainInRefUrl()
125
'example.com', 'www.example.com', 'some.really.deep.domain.com'
128
foreach ($domains as $domain) {
129
$cookie = Zend_Http_Cookie::fromString('foo=baz; path=/', 'http://' . $domain);
130
if (! $cookie instanceof Zend_Http_Cookie) $this->fail('We didn\'t get a valid Cookie object');
131
$this->assertEquals($domain, $cookie->getDomain());
136
* Make sure we get the correct path when it's set in the cookie string
138
public function testGetPathInStr()
141
'cookie=foo; domain=example.com' => '/',
142
'cookie=foo; path=/foo/baz; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => '/foo/baz',
143
'cookie=foo; domain=some.really.deep.domain.com; path=/Space Out/; expires=Tue, 21-Nov-2006 08:33:44 GMT;' => '/Space Out/'
146
foreach ($cookies as $cstr => $path) {
147
$cookie = Zend_Http_Cookie::fromString($cstr);
148
if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Failed generatic a valid cookie object');
149
$this->assertEquals($path, $cookie->getPath(), 'Cookie path is not as expected');
154
* Make sure we get the correct path when it's set a reference URL
156
public function testGetPathInRefUrl()
159
'http://www.example.com/foo/bar/' => '/foo/bar',
160
'http://foo.com' => '/',
161
'http://qua.qua.co.uk/path/to/very/deep/file.php' => '/path/to/very/deep'
164
foreach ($refUrls as $url => $path) {
165
$cookie = Zend_Http_Cookie::fromString('foo=bar', $url);
166
if (! $cookie instanceof Zend_Http_Cookie) $this->fail('Failed generating a valid cookie object');
167
$this->assertEquals($path, $cookie->getPath(), 'Cookie path is not as expected');
172
* Test we get the correct expiry time
175
public function testGetExpiryTime()
178
$yesterday = $now - (3600 * 24);
180
'cookie=bar; domain=example.com; expires=' . date(DATE_COOKIE, $now) . ';' => $now,
181
'cookie=foo; expires=' . date(DATE_COOKIE, $yesterday) . '; domain=some.really.deep.domain.com; path=/;' => $yesterday,
182
'cookie=baz; domain=foo.com; path=/some/path; secure' => null
185
foreach ($cookies as $cstr => $exp) {
186
$cookie = Zend_Http_Cookie::fromString($cstr);
187
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
188
$this->assertEquals($exp, $cookie->getExpiryTime(), 'Expiry time is not as expected');
193
* Make sure the "is secure" flag is correctly set
195
public function testIsSecure()
198
'cookie=foo; path=/foo/baz; secure; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => true,
199
'cookie=foo; path=/; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => false,
200
'cookie=foo; path=/; SECURE; domain=.example.com;' => true,
201
'cookie=foo; path=/; domain=.example.com; SECURE' => true
204
foreach ($cookies as $cstr => $secure) {
205
$cookie = Zend_Http_Cookie::fromString($cstr);
206
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
207
$this->assertEquals($secure, $cookie->isSecure(), 'isSecure is not as expected');
213
* Make sure we get the correct value for 'isExpired'
215
public function testIsExpired()
217
$notexpired = time() + 3600;
218
$expired = time() - 3600;
221
'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $notexpired) => false,
222
'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $expired) => true,
223
'cookie=foo; domain=example.com;' => false
226
foreach ($cookies as $cstr => $isexp) {
227
$cookie = Zend_Http_Cookie::fromString($cstr);
228
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
229
$this->assertEquals($isexp, $cookie->isExpired(), 'Got the wrong value for isExpired()');
234
* Make sure we get the correct value for 'isExpired', when time is manually set
236
public function testIsExpiredDifferentTime()
238
$notexpired = time() + 3600;
239
$expired = time() - 3600;
240
$now = time() + 7200;
243
'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $notexpired),
244
'cookie=foo; domain=example.com; expires=' . date(DATE_COOKIE, $expired)
247
// Make sure all cookies are expired
248
foreach ($cookies as $cstr) {
249
$cookie = Zend_Http_Cookie::fromString($cstr);
250
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
251
$this->assertTrue($cookie->isExpired($now), 'Cookie is expected to be expired');
254
// Make sure all cookies are not expired
255
$now = time() - 7200;
256
foreach ($cookies as $cstr) {
257
$cookie = Zend_Http_Cookie::fromString($cstr);
258
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
259
$this->assertFalse($cookie->isExpired($now), 'Cookie is expected not to be expired');
264
* Test we can properly check if a cookie is a session cookie (has no expiry time)
266
public function testIsSessionCookie()
269
'cookie=foo; path=/foo/baz; secure; expires=Tue, 21-Nov-2006 08:33:44 GMT; domain=.example.com;' => false,
270
'cookie=foo; path=/; domain=.example.com;' => true,
271
'cookie=foo; path=/; secure; domain=.example.com;' => true,
272
'cookie=foo; path=/; domain=.example.com; secure; expires=' . date(DATE_COOKIE) => false
275
foreach ($cookies as $cstr => $issession) {
276
$cookie = Zend_Http_Cookie::fromString($cstr);
277
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
278
$this->assertEquals($issession, $cookie->isSessionCookie(), 'isSessionCookie is not as expected');
283
* Make sure cookies are properly converted back to strings
285
public function testToString()
290
'urlencodedstuff=' . urlencode('!@#$)(@$%_+{} !@#?^&') . ';',
293
foreach ($cookies as $cstr) {
294
$cookie = Zend_Http_Cookie::fromString($cstr, 'http://example.com');
295
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
296
$this->assertEquals($cstr, $cookie->__toString(), 'Cookie is not converted back to the expected string');
301
public function testGarbageInStrIsIgnored()
304
'name=value; domain=foo.com; silly=place; secure',
305
'foo=value; someCrap; secure; domain=foo.com; ',
306
'anothercookie=value; secure; has some crap; ignore=me; domain=foo.com; '
309
foreach ($cookies as $cstr) {
310
$cookie = Zend_Http_Cookie::fromString($cstr);
311
if (! $cookie) $this->fail('Got no cookie object from a valid cookie string');
312
$this->assertEquals('value', $cookie->getValue(), 'Value is not as expected');
313
$this->assertEquals('foo.com', $cookie->getDomain(), 'Domain is not as expected');
314
$this->assertTrue($cookie->isSecure(), 'Cookie is expected to be secure');
319
* Test the match() method against a domain
322
public function testMatchDomain()
324
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
325
$this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
326
$this->assertFalse($cookie->match('http://www.somexample.com/foo/bar.php'), 'Cookie expected not to match, but did');
328
$uri = Zend_Uri::factory('http://www.foo.com/some/file.txt');
329
$cookie = Zend_Http_Cookie::fromString('cookie=value; domain=www.foo.com');
330
$this->assertTrue($cookie->match($uri), 'Cookie expected to match, but didn\'t');
331
$this->assertTrue($cookie->match('http://il.www.foo.com'), 'Cookie expected to match, but didn\'t');
332
$this->assertFalse($cookie->match('http://bar.foo.com'), 'Cookie expected not to match, but did');
336
* Test the match() method against a domain
339
public function testMatchPath()
341
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; path=/foo');
342
$this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
343
$this->assertFalse($cookie->match('http://www.example.com/bar.php'), 'Cookie expected not to match, but did');
345
$cookie = Zend_Http_Cookie::fromString('cookie=value; domain=www.foo.com; path=/some/long/path');
346
$this->assertTrue($cookie->match('http://www.foo.com/some/long/path/file.txt'), 'Cookie expected to match, but didn\'t');
347
$this->assertTrue($cookie->match('http://www.foo.com/some/long/path/and/even/more'), 'Cookie expected to match, but didn\'t');
348
$this->assertFalse($cookie->match('http://www.foo.com/some/long/file.txt'), 'Cookie expected not to match, but did');
349
$this->assertFalse($cookie->match('http://www.foo.com/some/different/path/file.txt'), 'Cookie expected not to match, but did');
353
* Test the match() method against secure / non secure connections
356
public function testMatchSecure()
358
// A non secure cookie, should match both
359
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
360
$this->assertTrue($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected to match, but didn\'t');
361
$this->assertTrue($cookie->match('https://www.example.com/bar.php'), 'Cookie expected to match, but didn\'t');
363
// A secure cookie, should match secure connections only
364
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; secure');
365
$this->assertFalse($cookie->match('http://www.example.com/foo/bar.php'), 'Cookie expected not to match, but it did');
366
$this->assertTrue($cookie->match('https://www.example.com/bar.php'), 'Cookie expected to match, but didn\'t');
370
* Test the match() method against different expiry times
373
public function testMatchExpire()
375
// A session cookie - should always be valid
376
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com;');
377
$this->assertTrue($cookie->match('http://www.example.com/'), 'Cookie expected to match, but didn\'t');
378
$this->assertTrue($cookie->match('http://www.example.com/', true, time() + 3600), 'Cookie expected to match, but didn\'t');
380
// A session cookie, should not match
381
$this->assertFalse($cookie->match('https://www.example.com/', false), 'Cookie expected not to match, but it did');
382
$this->assertFalse($cookie->match('https://www.example.com/', false, time() - 3600), 'Cookie expected not to match, but it did');
384
// A cookie with expiry time in the future
385
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; expires=' . date(DATE_COOKIE, time() + 3600));
386
$this->assertTrue($cookie->match('http://www.example.com/'), 'Cookie expected to match, but didn\'t');
387
$this->assertFalse($cookie->match('https://www.example.com/', true, time() + 7200), 'Cookie expected not to match, but it did');
389
// A cookie with expiry time in the past
390
$cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; expires=' . date(DATE_COOKIE, time() - 3600));
391
$this->assertFalse($cookie->match('http://www.example.com/'), 'Cookie expected not to match, but it did');
392
$this->assertTrue($cookie->match('https://www.example.com/', true, time() - 7200), 'Cookie expected to match, but didn\'t');
395
public function testFromStringFalse()
397
$cookie = Zend_Http_Cookie::fromString('foo; domain=www.exmaple.com');
398
$this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
400
$cookie = Zend_Http_Cookie::fromString('=bar; secure; domain=foo.nl');
401
$this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
403
$cookie = Zend_Http_Cookie::fromString('fo;o=bar; secure; domain=foo.nl');
404
$this->assertEquals(false, $cookie, 'fromString was expected to fail and return false');
408
* Test that cookies with far future expiry date (beyond the 32 bit unsigned int range) are
409
* not mistakenly marked as 'expired'
411
* @link http://framework.zend.com/issues/browse/ZF-5690
413
public function testZF5690OverflowingExpiryDate()
415
$expTime = "Sat, 29-Jan-2039 00:54:42 GMT";
416
$cookie = Zend_Http_Cookie::fromString("foo=bar; domain=.example.com; expires=$expTime");
417
$this->assertFalse($cookie->isExpired(), 'Expiry: ' . $cookie->getExpiryTime());