CookieTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Tests\Component\BrowserKit;
  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'),
  25. array('foo=bar; path=/foo'),
  26. array('foo=bar; domain=google.com'),
  27. array('foo=bar; domain=example.com; secure', 'https://example.com/'),
  28. array('foo=bar; httponly'),
  29. array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
  30. array('foo=bar=baz'),
  31. array('foo=bar%3Dbaz'),
  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=Friday, 31-Jul-20 08:49:37 GMT'),
  52. array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
  53. );
  54. }
  55. public function testFromStringWithUrl()
  56. {
  57. $this->assertEquals('foo=bar; domain=www.example.com', (string) Cookie::FromString('foo=bar', 'http://www.example.com/'));
  58. $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::FromString('foo=bar', 'http://www.example.com/foo/bar'));
  59. $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::FromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
  60. $this->assertEquals('foo=bar; domain=www.myotherexample.com', (string) Cookie::FromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
  61. }
  62. public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
  63. {
  64. $this->setExpectedException('InvalidArgumentException');
  65. Cookie::FromString('foo');
  66. }
  67. public function testFromStringThrowsAnExceptionIfCookieDateIsNotValid()
  68. {
  69. $this->setExpectedException('InvalidArgumentException');
  70. Cookie::FromString('foo=bar; expires=foo');
  71. }
  72. public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
  73. {
  74. $this->setExpectedException('InvalidArgumentException');
  75. Cookie::FromString('foo=bar', 'foobar');
  76. }
  77. public function testGetName()
  78. {
  79. $cookie = new Cookie('foo', 'bar');
  80. $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
  81. }
  82. public function testGetValue()
  83. {
  84. $cookie = new Cookie('foo', 'bar');
  85. $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
  86. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  87. $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
  88. }
  89. public function testGetRawValue()
  90. {
  91. $cookie = new Cookie('foo', 'bar=baz'); // decoded value
  92. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
  93. $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
  94. $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
  95. }
  96. public function testGetPath()
  97. {
  98. $cookie = new Cookie('foo', 'bar', 0);
  99. $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
  100. $cookie = new Cookie('foo', 'bar', 0, '/foo');
  101. $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
  102. }
  103. public function testGetDomain()
  104. {
  105. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
  106. $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
  107. }
  108. public function testIsSecure()
  109. {
  110. $cookie = new Cookie('foo', 'bar');
  111. $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
  112. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
  113. $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
  114. }
  115. public function testIsHttponly()
  116. {
  117. $cookie = new Cookie('foo', 'bar');
  118. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
  119. $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
  120. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
  121. }
  122. public function testGetExpiresTime()
  123. {
  124. $cookie = new Cookie('foo', 'bar');
  125. $this->assertEquals(null, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  126. $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
  127. $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
  128. }
  129. public function testIsExpired()
  130. {
  131. $cookie = new Cookie('foo', 'bar');
  132. $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
  133. $cookie = new Cookie('foo', 'bar', time() - 86400);
  134. $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
  135. $cookie = new Cookie('foo', 'bar', 0);
  136. $this->assertFalse($cookie->isExpired());
  137. }
  138. }