CookieCollectionTest.php 9.3 KB

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