CookieJarTest.php 11 KB

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