CookieCollectionTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http\Client;
  15. use Cake\Http\Client\CookieCollection;
  16. use Cake\Http\Client\Response;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * HTTP cookies test.
  20. *
  21. * @group deprecated
  22. */
  23. class CookieCollectionTest extends TestCase
  24. {
  25. /**
  26. * setup
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->deprecated(function () {
  34. $this->cookies = new CookieCollection();
  35. });
  36. }
  37. /**
  38. * test store
  39. *
  40. * @return void
  41. */
  42. public function testStore()
  43. {
  44. $headers = [
  45. 'HTTP/1.0 200 Ok',
  46. 'Set-Cookie: first=1',
  47. 'Set-Cookie: second=2; Path=/; Domain=.foo.example.com',
  48. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT',
  49. ];
  50. $response = new Response($headers, '');
  51. $result = $this->cookies->store($response, 'http://example.com/some/path');
  52. $this->assertNull($result);
  53. $result = $this->cookies->getAll();
  54. $this->assertCount(2, $result);
  55. $expected = [
  56. [
  57. 'name' => 'first',
  58. 'value' => '1',
  59. 'path' => '/some/path',
  60. 'domain' => 'example.com',
  61. 'secure' => false,
  62. 'httponly' => false,
  63. 'expires' => 0,
  64. ],
  65. [
  66. 'name' => 'second',
  67. 'value' => '2',
  68. 'path' => '/',
  69. 'domain' => '.foo.example.com',
  70. 'secure' => false,
  71. 'httponly' => false,
  72. 'expires' => 0,
  73. ],
  74. ];
  75. $this->assertEquals($expected, $result);
  76. }
  77. /**
  78. * test store secure.
  79. *
  80. * @return void
  81. */
  82. public function testStoreSecure()
  83. {
  84. $headers = [
  85. 'HTTP/1.0 200 Ok',
  86. 'Set-Cookie: first=1',
  87. 'Set-Cookie: second=2; Secure; HttpOnly',
  88. ];
  89. $response = new Response($headers, '');
  90. $result = $this->cookies->store($response, 'http://example.com/some/path');
  91. $this->assertNull($result);
  92. $result = $this->cookies->getAll();
  93. $this->assertCount(2, $result);
  94. $expected = [
  95. [
  96. 'name' => 'first',
  97. 'value' => '1',
  98. 'path' => '/some/path',
  99. 'domain' => 'example.com',
  100. 'secure' => false,
  101. 'httponly' => false,
  102. 'expires' => 0,
  103. ],
  104. [
  105. 'name' => 'second',
  106. 'value' => '2',
  107. 'path' => '/some/path',
  108. 'domain' => 'example.com',
  109. 'secure' => true,
  110. 'httponly' => true,
  111. 'expires' => 0,
  112. ],
  113. ];
  114. $this->assertEquals($expected, $result);
  115. }
  116. /**
  117. * test storing an expired cookie clears existing ones too.
  118. *
  119. * @return void
  120. */
  121. public function testStoreExpiring()
  122. {
  123. $headers = [
  124. 'HTTP/1.0 200 Ok',
  125. 'Set-Cookie: first=1',
  126. 'Set-Cookie: second=2; Path=/',
  127. ];
  128. $response = new Response($headers, '');
  129. $this->cookies->store($response, 'http://example.com/some/path');
  130. $result = $this->cookies->getAll();
  131. $this->assertCount(2, $result);
  132. $headers = [
  133. 'HTTP/1.0 200 Ok',
  134. 'Set-Cookie: first=1; Expires=Wed, 09-Jun-1999 10:18:14 GMT',
  135. ];
  136. $response = new Response($headers, '');
  137. $this->cookies->store($response, 'http://example.com/');
  138. $result = $this->cookies->getAll();
  139. $this->assertCount(2, $result, 'Path does not match, no expiration');
  140. // Use a more common date format that doesn't match
  141. $headers = [
  142. 'HTTP/1.0 200 Ok',
  143. 'Set-Cookie: first=1; Domain=.foo.example.com; Expires=Wed, 09-Jun-1999 10:18:14 GMT',
  144. ];
  145. $response = new Response($headers, '');
  146. $this->cookies->store($response, 'http://example.com/some/path');
  147. $result = $this->cookies->getAll();
  148. $this->assertCount(2, $result, 'Domain does not match, no expiration');
  149. // Use an RFC1123 date
  150. $headers = [
  151. 'HTTP/1.0 200 Ok',
  152. 'Set-Cookie: first=1; Expires=Wed, 09 Jun 1999 10:18:14 GMT',
  153. ];
  154. $response = new Response($headers, '');
  155. $this->cookies->store($response, 'http://example.com/some/path');
  156. $result = $this->cookies->getAll();
  157. $this->assertCount(1, $result, 'Domain does not match, no expiration');
  158. $expected = [
  159. [
  160. 'name' => 'second',
  161. 'value' => '2',
  162. 'path' => '/',
  163. 'domain' => 'example.com',
  164. 'expires' => 0,
  165. 'secure' => false,
  166. 'httponly' => false,
  167. ],
  168. ];
  169. $this->assertEquals($expected, $result);
  170. }
  171. /**
  172. * test getting cookies with secure flags
  173. *
  174. * @return void
  175. */
  176. public function testGetMatchingSecure()
  177. {
  178. $headers = [
  179. 'HTTP/1.0 200 Ok',
  180. 'Set-Cookie: first=1',
  181. 'Set-Cookie: second=2; Secure; HttpOnly',
  182. ];
  183. $response = new Response($headers, '');
  184. $this->cookies->store($response, 'https://example.com/');
  185. $result = $this->cookies->get('https://example.com/test');
  186. $expected = ['first' => '1', 'second' => '2'];
  187. $this->assertEquals($expected, $result);
  188. $result = $this->cookies->get('http://example.com/test');
  189. $expected = ['first' => '1'];
  190. $this->assertEquals($expected, $result);
  191. }
  192. /**
  193. * test getting cookies with secure flags
  194. *
  195. * @return void
  196. */
  197. public function testGetMatchingPath()
  198. {
  199. $headers = [
  200. 'HTTP/1.0 200 Ok',
  201. 'Set-Cookie: first=1; Path=/foo',
  202. 'Set-Cookie: second=2; Path=/',
  203. ];
  204. $response = new Response($headers, '');
  205. $this->cookies->store($response, 'http://example.com/foo');
  206. $result = $this->cookies->get('http://example.com/foo');
  207. $expected = ['first' => '1', 'second' => 2];
  208. $this->assertEquals($expected, $result);
  209. $result = $this->cookies->get('http://example.com/');
  210. $expected = ['second' => 2];
  211. $this->assertEquals($expected, $result);
  212. $result = $this->cookies->get('http://example.com/test');
  213. $expected = ['second' => 2];
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * Test getting cookies matching on paths exactly
  218. *
  219. * @return void
  220. */
  221. public function testGetMatchingDomain()
  222. {
  223. $headers = [
  224. 'HTTP/1.0 200 Ok',
  225. 'Set-Cookie: first=1; Domain=example.com',
  226. 'Set-Cookie: second=2;',
  227. ];
  228. $response = new Response($headers, '');
  229. $this->cookies->store($response, 'http://foo.example.com/');
  230. $result = $this->cookies->get('http://example.com');
  231. $expected = ['first' => 1];
  232. $this->assertEquals($expected, $result);
  233. $result = $this->cookies->get('http://foo.example.com');
  234. $expected = ['first' => 1, 'second' => '2'];
  235. $this->assertEquals($expected, $result);
  236. $result = $this->cookies->get('http://bar.foo.example.com');
  237. $expected = ['first' => 1, 'second' => '2'];
  238. $this->assertEquals($expected, $result);
  239. $result = $this->cookies->get('http://api.example.com');
  240. $expected = ['first' => 1];
  241. $this->assertEquals($expected, $result);
  242. $result = $this->cookies->get('http://google.com');
  243. $expected = [];
  244. $this->assertEquals($expected, $result);
  245. }
  246. /**
  247. * Test getting cookies matching on paths exactly
  248. *
  249. * @return void
  250. */
  251. public function testGetMatchingDomainWithDot()
  252. {
  253. $headers = [
  254. 'HTTP/1.0 200 Ok',
  255. 'Set-Cookie: first=1; Domain=.example.com',
  256. 'Set-Cookie: second=2;',
  257. ];
  258. $response = new Response($headers, '');
  259. $this->cookies->store($response, 'http://foo.example.com/');
  260. $result = $this->cookies->get('http://example.com');
  261. $expected = ['first' => 1];
  262. $this->assertEquals($expected, $result);
  263. $result = $this->cookies->get('http://foo.example.com');
  264. $expected = ['first' => 1, 'second' => '2'];
  265. $this->assertEquals($expected, $result);
  266. $result = $this->cookies->get('http://bar.foo.example.com');
  267. $expected = ['first' => 1, 'second' => '2'];
  268. $this->assertEquals($expected, $result);
  269. $result = $this->cookies->get('http://api.example.com');
  270. $expected = ['first' => 1];
  271. $this->assertEquals($expected, $result);
  272. $result = $this->cookies->get('http://google.com');
  273. $expected = [];
  274. $this->assertEquals($expected, $result);
  275. }
  276. }