CookieTest.php 7.7 KB

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