CookieTest.php 6.7KB

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