CookieTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\BrowserKit\Cookie;
  13. class CookieTest extends TestCase
  14. {
  15. public function testToString()
  16. {
  17. $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  18. $this->assertEquals('foo=bar; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
  19. $cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  20. $this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
  21. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  22. $this->assertEquals('foo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=.myfoodomain.com; path=/admin/; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
  23. $cookie = new Cookie('foo', 'bar', 0, '/', '');
  24. $this->assertEquals('foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; httponly', (string) $cookie);
  25. $cookie = new Cookie('foo', 'bar', 2, '/', '', true, true, false, 'lax');
  26. $this->assertEquals('foo=bar; expires=Thu, 01 Jan 1970 00:00:02 GMT; path=/; secure; httponly; samesite=lax', (string) $cookie);
  27. }
  28. /**
  29. * @dataProvider getTestsForToFromString
  30. */
  31. public function testToFromString($cookie, $url = null)
  32. {
  33. $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
  34. }
  35. public function getTestsForToFromString()
  36. {
  37. return [
  38. ['foo=bar; path=/'],
  39. ['foo=bar; path=/foo'],
  40. ['foo=bar; domain=google.com; path=/'],
  41. ['foo=bar; domain=example.com; path=/; secure', 'https://example.com/'],
  42. ['foo=bar; path=/; httponly'],
  43. ['foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'],
  44. ['foo=bar=baz; path=/'],
  45. ['foo=bar%3Dbaz; path=/'],
  46. ];
  47. }
  48. public function testFromStringIgnoreSecureFlag()
  49. {
  50. $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
  51. $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
  52. }
  53. /**
  54. * @dataProvider getExpireCookieStrings
  55. */
  56. public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
  57. {
  58. $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
  59. }
  60. public function getExpireCookieStrings()
  61. {
  62. return [
  63. ['foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'],
  64. ['foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'],
  65. ['foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'],
  66. ['foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'],
  67. ['foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'],
  68. ['foo=bar; expires=Fri Jul 31 08:49:37 2020'],
  69. ['foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''],
  70. ['foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'],
  71. ];
  72. }
  73. public function testFromStringWithCapitalization()
  74. {
  75. $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
  76. $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'));
  77. $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
  78. }
  79. public function testFromStringWithUrl()
  80. {
  81. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com/'));
  82. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com'));
  83. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com?foo'));
  84. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::fromString('foo=bar', 'http://www.example.com/foo/bar'));
  85. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  86. $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::fromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  87. }
  88. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  89. {
  90. $this->expectException('InvalidArgumentException');
  91. Cookie::fromString('foo');
  92. }
  93. public function testFromStringIgnoresInvalidExpiresDate()
  94. {
  95. $cookie = Cookie::fromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
  96. $this->assertFalse($cookie->isExpired());
  97. }
  98. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  99. {
  100. $this->expectException('InvalidArgumentException');
  101. Cookie::fromString('foo=bar', 'foobar');
  102. }
  103. public function testGetName()
  104. {
  105. $cookie = new Cookie('foo', 'bar');
  106. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  107. }
  108. public function testGetValue()
  109. {
  110. $cookie = new Cookie('foo', 'bar');
  111. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  112. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  113. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  114. }
  115. public function testGetRawValue()
  116. {
  117. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  118. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  119. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  120. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  121. }
  122. public function testGetPath()
  123. {
  124. $cookie = new Cookie('foo', 'bar', 0);
  125. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  126. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  127. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  128. }
  129. public function testGetDomain()
  130. {
  131. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  132. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  133. }
  134. public function testIsSecure()
  135. {
  136. $cookie = new Cookie('foo', 'bar');
  137. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  138. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  139. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  140. }
  141. public function testIsHttponly()
  142. {
  143. $cookie = new Cookie('foo', 'bar');
  144. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  145. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  146. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  147. }
  148. public function testGetExpiresTime()
  149. {
  150. $cookie = new Cookie('foo', 'bar');
  151. $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  152. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  153. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  154. }
  155. public function testIsExpired()
  156. {
  157. $cookie = new Cookie('foo', 'bar');
  158. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  159. $cookie = new Cookie('foo', 'bar', time() - 86400);
  160. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  161. $cookie = new Cookie('foo', 'bar', 0);
  162. $this->assertFalse($cookie->isExpired());
  163. }
  164. /**
  165. * @expectedException \UnexpectedValueException
  166. * @expectedExceptionMessage The cookie expiration time "string" is not valid.
  167. */
  168. public function testConstructException()
  169. {
  170. $cookie = new Cookie('foo', 'bar', 'string');
  171. }
  172. public function testSameSite()
  173. {
  174. $cookie = new Cookie('foo', 'bar');
  175. $this->assertNull($cookie->getSameSite());
  176. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true, false, 'lax');
  177. $this->assertSame('lax', $cookie->getSameSite());
  178. }
  179. }