CookieTest.php 6.5 KB

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