CookieJarTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 Symfony\Component\BrowserKit\CookieJar;
  12. use Symfony\Component\BrowserKit\Cookie;
  13. use Symfony\Component\BrowserKit\Response;
  14. class CookieJarTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSetGet()
  17. {
  18. $cookieJar = new CookieJar();
  19. $cookieJar->set($cookie = new Cookie('foo', 'bar'));
  20. $this->assertEquals($cookie, $cookieJar->get('foo'), '->set() sets a cookie');
  21. $this->assertNull($cookieJar->get('foobar'), '->get() returns null if the cookie does not exist');
  22. $cookieJar->set($cookie = new Cookie('foo', 'bar', time() - 86400));
  23. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  24. }
  25. public function testExpire()
  26. {
  27. $cookieJar = new CookieJar();
  28. $cookieJar->set($cookie = new Cookie('foo', 'bar'));
  29. $cookieJar->expire('foo');
  30. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  31. }
  32. public function testAll()
  33. {
  34. $cookieJar = new CookieJar();
  35. $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
  36. $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
  37. $this->assertEquals(array($cookie1, $cookie2), $cookieJar->all(), '->all() returns all cookies in the jar');
  38. }
  39. public function testClear()
  40. {
  41. $cookieJar = new CookieJar();
  42. $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
  43. $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
  44. $cookieJar->clear();
  45. $this->assertEquals(array(), $cookieJar->all(), '->clear() expires all cookies');
  46. }
  47. public function testUpdateFromResponse()
  48. {
  49. $response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
  50. $cookieJar = new CookieJar();
  51. $cookieJar->updateFromResponse($response);
  52. $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
  53. }
  54. public function testUpdateFromSetCookie()
  55. {
  56. $setCookies = array('foo=foo');
  57. $cookieJar = new CookieJar();
  58. $cookieJar->set(new Cookie('bar', 'bar'));
  59. $cookieJar->updateFromSetCookie($setCookies);
  60. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
  61. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
  62. $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
  63. $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
  64. }
  65. public function testUpdateFromEmptySetCookie()
  66. {
  67. $cookieJar = new CookieJar();
  68. $cookieJar->updateFromSetCookie(array(''));
  69. $this->assertEquals(array(), $cookieJar->all());
  70. }
  71. public function testUpdateFromSetCookieWithMultipleCookies()
  72. {
  73. $timestamp = time() + 3600;
  74. $date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
  75. $setCookies = array(sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%s', $date, $date));
  76. $cookieJar = new CookieJar();
  77. $cookieJar->updateFromSetCookie($setCookies);
  78. $fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
  79. $barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
  80. $phpCookie = $cookieJar->get('PHPSESSID');
  81. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $fooCookie);
  82. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $barCookie);
  83. $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $phpCookie);
  84. $this->assertEquals('foo', $fooCookie->getValue());
  85. $this->assertEquals('bar', $barCookie->getValue());
  86. $this->assertEquals('id', $phpCookie->getValue());
  87. $this->assertEquals($timestamp, $fooCookie->getExpiresTime());
  88. $this->assertNull($barCookie->getExpiresTime());
  89. $this->assertEquals($timestamp, $phpCookie->getExpiresTime());
  90. }
  91. /**
  92. * @dataProvider provideAllValuesValues
  93. */
  94. public function testAllValues($uri, $values)
  95. {
  96. $cookieJar = new CookieJar();
  97. $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
  98. $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
  99. $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
  100. $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
  101. $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
  102. $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
  103. $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
  104. }
  105. public function provideAllValuesValues()
  106. {
  107. return array(
  108. array('http://www.example.com', array('foo_nothing', 'foo_domain')),
  109. array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
  110. array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
  111. array('http://foo.example1.com/', array('foo_nothing')),
  112. array('https://foo.example.com/', array('foo_nothing', 'foo_secure', 'foo_domain')),
  113. array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
  114. array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
  115. );
  116. }
  117. public function testEncodedValues()
  118. {
  119. $cookieJar = new CookieJar();
  120. $cookieJar->set($cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true));
  121. $this->assertEquals(array('foo' => 'bar=baz'), $cookieJar->allValues('/'));
  122. $this->assertEquals(array('foo' => 'bar%3Dbaz'), $cookieJar->allRawValues('/'));
  123. }
  124. public function testCookieExpireWithSameNameButDifferentPaths()
  125. {
  126. $cookieJar = new CookieJar();
  127. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
  128. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
  129. $cookieJar->expire('foo', '/foo');
  130. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  131. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  132. $this->assertEquals(array(), $cookieJar->allValues('http://example.com/foo'));
  133. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
  134. }
  135. public function testCookieExpireWithNullPaths()
  136. {
  137. $cookieJar = new CookieJar();
  138. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/'));
  139. $cookieJar->expire('foo', null);
  140. $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
  141. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  142. }
  143. public function testCookieWithSameNameButDifferentPaths()
  144. {
  145. $cookieJar = new CookieJar();
  146. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
  147. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
  148. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  149. $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://example.com/foo'));
  150. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
  151. }
  152. public function testCookieWithSameNameButDifferentDomains()
  153. {
  154. $cookieJar = new CookieJar();
  155. $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/', 'foo.example.com'));
  156. $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/', 'bar.example.com'));
  157. $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
  158. $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://foo.example.com/'));
  159. $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://bar.example.com/'));
  160. }
  161. }