CookieTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\BrowserKit\Tests;
  11. use Symfony\Component\BrowserKit\Cookie;
  12. class CookieTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getTestsForToFromString
  16. */
  17. public function testToFromString($cookie, $url = null)
  18. {
  19. $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
  20. }
  21. public function getTestsForToFromString()
  22. {
  23. return array(
  24. array('foo=bar; path=/'),
  25. array('foo=bar; path=/foo'),
  26. array('foo=bar; domain=google.com; path=/'),
  27. array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
  28. array('foo=bar; path=/; httponly'),
  29. array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
  30. array('foo=bar=baz; path=/'),
  31. array('foo=bar%3Dbaz; path=/'),
  32. );
  33. }
  34. public function testFromStringIgnoreSecureFlag()
  35. {
  36. $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
  37. $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
  38. }
  39. /**
  40. * @dataProvider getExpireCookieStrings
  41. */
  42. public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
  43. {
  44. $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
  45. }
  46. public function getExpireCookieStrings()
  47. {
  48. return array(
  49. array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
  50. array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
  51. array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
  52. array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
  53. array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
  54. array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
  55. array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
  56. array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
  57. );
  58. }
  59. public function testFromStringWithCapitalization()
  60. {
  61. $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
  62. $this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
  63. $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
  64. }
  65. public function testFromStringWithUrl()
  66. {
  67. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
  68. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
  69. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  70. $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  71. }
  72. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  73. {
  74. $this->setExpectedException('InvalidArgumentException');
  75. Cookie::FromString('foo');
  76. }
  77. public function testFromStringThrowsAnExceptionIfCookieDateIsNotValid()
  78. {
  79. $this->setExpectedException('InvalidArgumentException');
  80. Cookie::FromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
  81. }
  82. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  83. {
  84. $this->setExpectedException('InvalidArgumentException');
  85. Cookie::FromString('foo=bar', 'foobar');
  86. }
  87. public function testGetName()
  88. {
  89. $cookie = new Cookie('foo', 'bar');
  90. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  91. }
  92. public function testGetValue()
  93. {
  94. $cookie = new Cookie('foo', 'bar');
  95. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  96. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  97. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  98. }
  99. public function testGetRawValue()
  100. {
  101. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  102. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  103. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  104. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  105. }
  106. public function testGetPath()
  107. {
  108. $cookie = new Cookie('foo', 'bar', 0);
  109. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  110. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  111. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  112. }
  113. public function testGetDomain()
  114. {
  115. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  116. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  117. }
  118. public function testIsSecure()
  119. {
  120. $cookie = new Cookie('foo', 'bar');
  121. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  122. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  123. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  124. }
  125. public function testIsHttponly()
  126. {
  127. $cookie = new Cookie('foo', 'bar');
  128. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  129. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  130. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  131. }
  132. public function testGetExpiresTime()
  133. {
  134. $cookie = new Cookie('foo', 'bar');
  135. $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  136. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  137. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  138. }
  139. public function testIsExpired()
  140. {
  141. $cookie = new Cookie('foo', 'bar');
  142. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  143. $cookie = new Cookie('foo', 'bar', time() - 86400);
  144. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  145. $cookie = new Cookie('foo', 'bar', 0);
  146. $this->assertFalse($cookie->isExpired());
  147. }
  148. }