CookieJarTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace GuzzleHttp\Tests\CookieJar;
  3. use GuzzleHttp\Cookie\CookieJar;
  4. use GuzzleHttp\Cookie\SetCookie;
  5. use GuzzleHttp\Psr7\Request;
  6. use GuzzleHttp\Psr7\Response;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @covers GuzzleHttp\Cookie\CookieJar
  10. */
  11. class CookieJarTest extends TestCase
  12. {
  13. /** @var CookieJar */
  14. private $jar;
  15. public function setUp()
  16. {
  17. $this->jar = new CookieJar();
  18. }
  19. protected function getTestCookies()
  20. {
  21. return [
  22. new SetCookie(['Name' => 'foo', 'Value' => 'bar', 'Domain' => 'foo.com', 'Path' => '/', 'Discard' => true]),
  23. new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2]),
  24. new SetCookie(['Name' => 'you', 'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => time() + 1000])
  25. ];
  26. }
  27. public function testCreatesFromArray()
  28. {
  29. $jar = CookieJar::fromArray([
  30. 'foo' => 'bar',
  31. 'baz' => 'bam'
  32. ], 'example.com');
  33. $this->assertCount(2, $jar);
  34. }
  35. public function testEmptyJarIsCountable()
  36. {
  37. $this->assertCount(0, new CookieJar());
  38. }
  39. public function testGetsCookiesByName()
  40. {
  41. $cookies = $this->getTestCookies();
  42. foreach ($this->getTestCookies() as $cookie) {
  43. $this->jar->setCookie($cookie);
  44. }
  45. $testCookie = $cookies[0];
  46. $this->assertEquals($testCookie, $this->jar->getCookieByName($testCookie->getName()));
  47. $this->assertNull($this->jar->getCookieByName("doesnotexist"));
  48. $this->assertNull($this->jar->getCookieByName(""));
  49. }
  50. /**
  51. * Provides test data for cookie cookieJar retrieval
  52. */
  53. public function getCookiesDataProvider()
  54. {
  55. return [
  56. [['foo', 'baz', 'test', 'muppet', 'googoo'], '', '', '', false],
  57. [['foo', 'baz', 'muppet', 'googoo'], '', '', '', true],
  58. [['googoo'], 'www.example.com', '', '', false],
  59. [['muppet', 'googoo'], 'test.y.example.com', '', '', false],
  60. [['foo', 'baz'], 'example.com', '', '', false],
  61. [['muppet'], 'x.y.example.com', '/acme/', '', false],
  62. [['muppet'], 'x.y.example.com', '/acme/test/', '', false],
  63. [['googoo'], 'x.y.example.com', '/test/acme/test/', '', false],
  64. [['foo', 'baz'], 'example.com', '', '', false],
  65. [['baz'], 'example.com', '', 'baz', false],
  66. ];
  67. }
  68. public function testStoresAndRetrievesCookies()
  69. {
  70. $cookies = $this->getTestCookies();
  71. foreach ($cookies as $cookie) {
  72. $this->assertTrue($this->jar->setCookie($cookie));
  73. }
  74. $this->assertCount(3, $this->jar);
  75. $this->assertCount(3, $this->jar->getIterator());
  76. $this->assertEquals($cookies, $this->jar->getIterator()->getArrayCopy());
  77. }
  78. public function testRemovesTemporaryCookies()
  79. {
  80. $cookies = $this->getTestCookies();
  81. foreach ($this->getTestCookies() as $cookie) {
  82. $this->jar->setCookie($cookie);
  83. }
  84. $this->jar->clearSessionCookies();
  85. $this->assertEquals(
  86. [$cookies[1], $cookies[2]],
  87. $this->jar->getIterator()->getArrayCopy()
  88. );
  89. }
  90. public function testRemovesSelectively()
  91. {
  92. foreach ($this->getTestCookies() as $cookie) {
  93. $this->jar->setCookie($cookie);
  94. }
  95. // Remove foo.com cookies
  96. $this->jar->clear('foo.com');
  97. $this->assertCount(2, $this->jar);
  98. // Try again, removing no further cookies
  99. $this->jar->clear('foo.com');
  100. $this->assertCount(2, $this->jar);
  101. // Remove bar.com cookies with path of /boo
  102. $this->jar->clear('bar.com', '/boo');
  103. $this->assertCount(1, $this->jar);
  104. // Remove cookie by name
  105. $this->jar->clear(null, null, 'test');
  106. $this->assertCount(0, $this->jar);
  107. }
  108. public function testDoesNotAddIncompleteCookies()
  109. {
  110. $this->assertFalse($this->jar->setCookie(new SetCookie()));
  111. $this->assertFalse($this->jar->setCookie(new SetCookie(array(
  112. 'Name' => 'foo'
  113. ))));
  114. $this->assertFalse($this->jar->setCookie(new SetCookie(array(
  115. 'Name' => false
  116. ))));
  117. $this->assertFalse($this->jar->setCookie(new SetCookie(array(
  118. 'Name' => true
  119. ))));
  120. $this->assertFalse($this->jar->setCookie(new SetCookie(array(
  121. 'Name' => 'foo',
  122. 'Domain' => 'foo.com'
  123. ))));
  124. }
  125. public function testDoesNotAddEmptyCookies()
  126. {
  127. $this->assertFalse($this->jar->setCookie(new SetCookie(array(
  128. 'Name' => '',
  129. 'Domain' => 'foo.com',
  130. 'Value' => 0
  131. ))));
  132. }
  133. public function testDoesAddValidCookies()
  134. {
  135. $this->assertTrue($this->jar->setCookie(new SetCookie(array(
  136. 'Name' => '0',
  137. 'Domain' => 'foo.com',
  138. 'Value' => 0
  139. ))));
  140. $this->assertTrue($this->jar->setCookie(new SetCookie(array(
  141. 'Name' => 'foo',
  142. 'Domain' => 'foo.com',
  143. 'Value' => 0
  144. ))));
  145. $this->assertTrue($this->jar->setCookie(new SetCookie(array(
  146. 'Name' => 'foo',
  147. 'Domain' => 'foo.com',
  148. 'Value' => 0.0
  149. ))));
  150. $this->assertTrue($this->jar->setCookie(new SetCookie(array(
  151. 'Name' => 'foo',
  152. 'Domain' => 'foo.com',
  153. 'Value' => '0'
  154. ))));
  155. }
  156. public function testOverwritesCookiesThatAreOlderOrDiscardable()
  157. {
  158. $t = time() + 1000;
  159. $data = array(
  160. 'Name' => 'foo',
  161. 'Value' => 'bar',
  162. 'Domain' => '.example.com',
  163. 'Path' => '/',
  164. 'Max-Age' => '86400',
  165. 'Secure' => true,
  166. 'Discard' => true,
  167. 'Expires' => $t
  168. );
  169. // Make sure that the discard cookie is overridden with the non-discard
  170. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  171. $this->assertCount(1, $this->jar);
  172. $data['Discard'] = false;
  173. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  174. $this->assertCount(1, $this->jar);
  175. $c = $this->jar->getIterator()->getArrayCopy();
  176. $this->assertFalse($c[0]->getDiscard());
  177. // Make sure it doesn't duplicate the cookie
  178. $this->jar->setCookie(new SetCookie($data));
  179. $this->assertCount(1, $this->jar);
  180. // Make sure the more future-ful expiration date supersede the other
  181. $data['Expires'] = time() + 2000;
  182. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  183. $this->assertCount(1, $this->jar);
  184. $c = $this->jar->getIterator()->getArrayCopy();
  185. $this->assertNotEquals($t, $c[0]->getExpires());
  186. }
  187. public function testOverwritesCookiesThatHaveChanged()
  188. {
  189. $t = time() + 1000;
  190. $data = array(
  191. 'Name' => 'foo',
  192. 'Value' => 'bar',
  193. 'Domain' => '.example.com',
  194. 'Path' => '/',
  195. 'Max-Age' => '86400',
  196. 'Secure' => true,
  197. 'Discard' => true,
  198. 'Expires' => $t
  199. );
  200. // Make sure that the discard cookie is overridden with the non-discard
  201. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  202. $data['Value'] = 'boo';
  203. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  204. $this->assertCount(1, $this->jar);
  205. // Changing the value plus a parameter also must overwrite the existing one
  206. $data['Value'] = 'zoo';
  207. $data['Secure'] = false;
  208. $this->assertTrue($this->jar->setCookie(new SetCookie($data)));
  209. $this->assertCount(1, $this->jar);
  210. $c = $this->jar->getIterator()->getArrayCopy();
  211. $this->assertSame('zoo', $c[0]->getValue());
  212. }
  213. public function testAddsCookiesFromResponseWithRequest()
  214. {
  215. $response = new Response(200, array(
  216. 'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
  217. ));
  218. $request = new Request('GET', 'http://www.example.com');
  219. $this->jar->extractCookies($request, $response);
  220. $this->assertCount(1, $this->jar);
  221. }
  222. public function getMatchingCookiesDataProvider()
  223. {
  224. return array(
  225. array('https://example.com', 'foo=bar; baz=foobar'),
  226. array('http://example.com', ''),
  227. array('https://example.com:8912', 'foo=bar; baz=foobar'),
  228. array('https://foo.example.com', 'foo=bar; baz=foobar'),
  229. array('http://foo.example.com/test/acme/', 'googoo=gaga')
  230. );
  231. }
  232. /**
  233. * @dataProvider getMatchingCookiesDataProvider
  234. */
  235. public function testReturnsCookiesMatchingRequests($url, $cookies)
  236. {
  237. $bag = [
  238. new SetCookie([
  239. 'Name' => 'foo',
  240. 'Value' => 'bar',
  241. 'Domain' => 'example.com',
  242. 'Path' => '/',
  243. 'Max-Age' => '86400',
  244. 'Secure' => true
  245. ]),
  246. new SetCookie([
  247. 'Name' => 'baz',
  248. 'Value' => 'foobar',
  249. 'Domain' => 'example.com',
  250. 'Path' => '/',
  251. 'Max-Age' => '86400',
  252. 'Secure' => true
  253. ]),
  254. new SetCookie([
  255. 'Name' => 'test',
  256. 'Value' => '123',
  257. 'Domain' => 'www.foobar.com',
  258. 'Path' => '/path/',
  259. 'Discard' => true
  260. ]),
  261. new SetCookie([
  262. 'Name' => 'muppet',
  263. 'Value' => 'cookie_monster',
  264. 'Domain' => '.y.example.com',
  265. 'Path' => '/acme/',
  266. 'Expires' => time() + 86400
  267. ]),
  268. new SetCookie([
  269. 'Name' => 'googoo',
  270. 'Value' => 'gaga',
  271. 'Domain' => '.example.com',
  272. 'Path' => '/test/acme/',
  273. 'Max-Age' => 1500
  274. ])
  275. ];
  276. foreach ($bag as $cookie) {
  277. $this->jar->setCookie($cookie);
  278. }
  279. $request = new Request('GET', $url);
  280. $request = $this->jar->withCookieHeader($request);
  281. $this->assertSame($cookies, $request->getHeaderLine('Cookie'));
  282. }
  283. /**
  284. * @expectedException \RuntimeException
  285. * @expectedExceptionMessage Invalid cookie: Cookie name must not contain invalid characters: ASCII Control characters (0-31;127), space, tab and the following characters: ()<>@,;:\"/?={}
  286. */
  287. public function testThrowsExceptionWithStrictMode()
  288. {
  289. $a = new CookieJar(true);
  290. $a->setCookie(new SetCookie(['Name' => "abc\n", 'Value' => 'foo', 'Domain' => 'bar']));
  291. }
  292. public function testDeletesCookiesByName()
  293. {
  294. $cookies = $this->getTestCookies();
  295. $cookies[] = new SetCookie([
  296. 'Name' => 'other',
  297. 'Value' => '123',
  298. 'Domain' => 'bar.com',
  299. 'Path' => '/boo',
  300. 'Expires' => time() + 1000
  301. ]);
  302. $jar = new CookieJar();
  303. foreach ($cookies as $cookie) {
  304. $jar->setCookie($cookie);
  305. }
  306. $this->assertCount(4, $jar);
  307. $jar->clear('bar.com', '/boo', 'other');
  308. $this->assertCount(3, $jar);
  309. $names = array_map(function (SetCookie $c) {
  310. return $c->getName();
  311. }, $jar->getIterator()->getArrayCopy());
  312. $this->assertSame(['foo', 'test', 'you'], $names);
  313. }
  314. public function testCanConvertToAndLoadFromArray()
  315. {
  316. $jar = new CookieJar(true);
  317. foreach ($this->getTestCookies() as $cookie) {
  318. $jar->setCookie($cookie);
  319. }
  320. $this->assertCount(3, $jar);
  321. $arr = $jar->toArray();
  322. $this->assertCount(3, $arr);
  323. $newCookieJar = new CookieJar(false, $arr);
  324. $this->assertCount(3, $newCookieJar);
  325. $this->assertSame($jar->toArray(), $newCookieJar->toArray());
  326. }
  327. public function testAddsCookiesWithEmptyPathFromResponse()
  328. {
  329. $response = new Response(200, array(
  330. 'Set-Cookie' => "fpc=foobar; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=;"
  331. ));
  332. $request = new Request('GET', 'http://www.example.com');
  333. $this->jar->extractCookies($request, $response);
  334. $newRequest = $this->jar->withCookieHeader(new Request('GET', 'http://www.example.com/foo'));
  335. $this->assertTrue($newRequest->hasHeader('Cookie'));
  336. }
  337. public function getCookiePathsDataProvider()
  338. {
  339. return [
  340. ['', '/'],
  341. ['/', '/'],
  342. ['/foo', '/'],
  343. ['/foo/bar', '/foo'],
  344. ['/foo/bar/', '/foo/bar'],
  345. ['foo', '/'],
  346. ['foo/bar', '/'],
  347. ['foo/bar/', '/'],
  348. ];
  349. }
  350. /**
  351. * @dataProvider getCookiePathsDataProvider
  352. */
  353. public function testCookiePathWithEmptySetCookiePath($uriPath, $cookiePath)
  354. {
  355. $response = (new Response(200))
  356. ->withAddedHeader(
  357. 'Set-Cookie',
  358. "foo=bar; expires=Fri, 02-Mar-2019 02:17:40 GMT; domain=www.example.com; path=;"
  359. )
  360. ->withAddedHeader(
  361. 'Set-Cookie',
  362. "bar=foo; expires=Fri, 02-Mar-2019 02:17:40 GMT; domain=www.example.com; path=foobar;"
  363. )
  364. ;
  365. $request = (new Request('GET', $uriPath))->withHeader('Host', 'www.example.com');
  366. $this->jar->extractCookies($request, $response);
  367. $this->assertSame($cookiePath, $this->jar->toArray()[0]['Path']);
  368. $this->assertSame($cookiePath, $this->jar->toArray()[1]['Path']);
  369. }
  370. }