CookieTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  26. /**
  27. * @dataProvider getTestsForToFromString
  28. */
  29. public function testToFromString($cookie, $url = null)
  30. {
  31. $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
  32. }
  33. public function getTestsForToFromString()
  34. {
  35. return array(
  36. array('foo=bar; path=/'),
  37. array('foo=bar; path=/foo'),
  38. array('foo=bar; domain=google.com; path=/'),
  39. array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
  40. array('foo=bar; path=/; httponly'),
  41. array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
  42. array('foo=bar=baz; path=/'),
  43. array('foo=bar%3Dbaz; path=/'),
  44. );
  45. }
  46. public function testFromStringIgnoreSecureFlag()
  47. {
  48. $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
  49. $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
  50. }
  51. /**
  52. * @dataProvider getExpireCookieStrings
  53. */
  54. public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
  55. {
  56. $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
  57. }
  58. public function getExpireCookieStrings()
  59. {
  60. return array(
  61. array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
  62. array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
  63. array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
  64. array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
  65. array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
  66. array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
  67. array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
  68. array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
  69. );
  70. }
  71. public function testFromStringWithCapitalization()
  72. {
  73. $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
  74. $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'));
  75. $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
  76. }
  77. public function testFromStringWithUrl()
  78. {
  79. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com/'));
  80. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com'));
  81. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com?foo'));
  82. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::fromString('foo=bar', 'http://www.example.com/foo/bar'));
  83. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  84. $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::fromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  85. }
  86. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  87. {
  88. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  89. Cookie::fromString('foo');
  90. }
  91. public function testFromStringIgnoresInvalidExpiresDate()
  92. {
  93. $cookie = Cookie::fromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
  94. $this->assertFalse($cookie->isExpired());
  95. }
  96. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  97. {
  98. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  99. Cookie::fromString('foo=bar', 'foobar');
  100. }
  101. public function testGetName()
  102. {
  103. $cookie = new Cookie('foo', 'bar');
  104. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  105. }
  106. public function testGetValue()
  107. {
  108. $cookie = new Cookie('foo', 'bar');
  109. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  110. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  111. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  112. }
  113. public function testGetRawValue()
  114. {
  115. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  116. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  117. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  118. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  119. }
  120. public function testGetPath()
  121. {
  122. $cookie = new Cookie('foo', 'bar', 0);
  123. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  124. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  125. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  126. }
  127. public function testGetDomain()
  128. {
  129. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  130. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  131. }
  132. public function testIsSecure()
  133. {
  134. $cookie = new Cookie('foo', 'bar');
  135. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  136. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  137. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  138. }
  139. public function testIsHttponly()
  140. {
  141. $cookie = new Cookie('foo', 'bar');
  142. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  143. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  144. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  145. }
  146. public function testGetExpiresTime()
  147. {
  148. $cookie = new Cookie('foo', 'bar');
  149. $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  150. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  151. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  152. }
  153. public function testIsExpired()
  154. {
  155. $cookie = new Cookie('foo', 'bar');
  156. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  157. $cookie = new Cookie('foo', 'bar', time() - 86400);
  158. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  159. $cookie = new Cookie('foo', 'bar', 0);
  160. $this->assertFalse($cookie->isExpired());
  161. }
  162. /**
  163. * @expectedException \UnexpectedValueException
  164. * @expectedExceptionMessage The cookie expiration time "string" is not valid.
  165. */
  166. public function testConstructException()
  167. {
  168. $cookie = new Cookie('foo', 'bar', 'string');
  169. }
  170. }