CookieCollectionTest.php 9.3 KB

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