PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/BrowserKit/Tests/CookieTest.php

http://github.com/fabpot/symfony
PHP | 215 lines | 163 code | 38 blank | 14 comment | 0 complexity | 2d64b947c4d66c26356a952690441003 MD5 | raw file
  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=example.com; path=/'],
  41. ['foo=bar; domain=example.com; path=/; secure', 'https://example.com/'],
  42. ['foo=bar; path=/; httponly'],
  43. ['foo=bar; domain=example.com; path=/foo; secure; httponly', 'https://example.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. public function testConstructException()
  165. {
  166. $this->expectException('UnexpectedValueException');
  167. $this->expectExceptionMessage('The cookie expiration time "string" is not valid.');
  168. new Cookie('foo', 'bar', 'string');
  169. }
  170. public function testSameSite()
  171. {
  172. $cookie = new Cookie('foo', 'bar');
  173. $this->assertNull($cookie->getSameSite());
  174. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true, false, 'lax');
  175. $this->assertSame('lax', $cookie->getSameSite());
  176. }
  177. }