CookieCollectionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. * @link http://cakephp.org CakePHP(tm) Project
  10. * @since 3.5.0
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Test\TestCase\Http\Cookie;
  14. use Cake\Http\Cookie\Cookie;
  15. use Cake\Http\Cookie\CookieCollection;
  16. use Cake\Http\ServerRequest;
  17. use Cake\Http\Response;
  18. use Cake\TestSuite\TestCase;
  19. use DateTime;
  20. /**
  21. * Cookie collection test.
  22. */
  23. class CookieCollectionTest extends TestCase
  24. {
  25. /**
  26. * Test constructor
  27. *
  28. * @return void
  29. */
  30. public function testConstructorWithEmptyArray()
  31. {
  32. $collection = new CookieCollection([]);
  33. $this->assertCount(0, $collection);
  34. }
  35. /**
  36. * Test valid cookies
  37. *
  38. * @return void
  39. */
  40. public function testConstructorWithCookieArray()
  41. {
  42. $cookies = [
  43. new Cookie('one', 'one'),
  44. new Cookie('two', 'two')
  45. ];
  46. $collection = new CookieCollection($cookies);
  47. $this->assertCount(2, $collection);
  48. }
  49. /**
  50. * Test iteration
  51. *
  52. * @return void
  53. */
  54. public function testIteration()
  55. {
  56. $cookies = [
  57. new Cookie('remember_me', 'a'),
  58. new Cookie('gtm', 'b'),
  59. new Cookie('three', 'tree')
  60. ];
  61. $collection = new CookieCollection($cookies);
  62. $names = [];
  63. foreach ($collection as $cookie) {
  64. $names[] = $cookie->getName();
  65. }
  66. $this->assertSame(['remember_me', 'gtm', 'three'], $names);
  67. }
  68. /**
  69. * Test adding cookies
  70. *
  71. * @return void
  72. */
  73. public function testAdd()
  74. {
  75. $cookies = [];
  76. $collection = new CookieCollection($cookies);
  77. $this->assertCount(0, $collection);
  78. $remember = new Cookie('remember_me', 'a');
  79. $new = $collection->add($remember);
  80. $this->assertNotSame($new, $collection->add($remember));
  81. $this->assertCount(0, $collection, 'Original instance not modified');
  82. $this->assertCount(1, $new);
  83. $this->assertFalse($collection->has('remember_me'), 'Original instance not modified');
  84. $this->assertTrue($new->has('remember_me'));
  85. $this->assertSame($remember, $new->get('remember_me'));
  86. }
  87. /**
  88. * Cookie collections need to support duplicate cookie names because
  89. * of use cases in Http\Client
  90. *
  91. * @return void
  92. */
  93. public function testAddDuplicates()
  94. {
  95. $remember = new Cookie('remember_me', 'yes');
  96. $rememberNo = new Cookie('remember_me', 'no');
  97. $collection = new CookieCollection([]);
  98. $new = $collection->add($remember)->add($rememberNo);
  99. $this->assertCount(2, $new);
  100. $this->assertNotSame($new, $collection);
  101. $this->assertSame($remember, $new->get('remember_me'), 'get() fetches first cookie');
  102. }
  103. /**
  104. * Test has()
  105. *
  106. * @return void
  107. */
  108. public function testHas()
  109. {
  110. $cookies = [
  111. new Cookie('remember_me', 'a'),
  112. new Cookie('gtm', 'b')
  113. ];
  114. $collection = new CookieCollection($cookies);
  115. $this->assertFalse($collection->has('nope'));
  116. $this->assertTrue($collection->has('remember_me'));
  117. $this->assertTrue($collection->has('REMEMBER_me'), 'case insensitive cookie names');
  118. }
  119. /**
  120. * Test removing cookies
  121. *
  122. * @return void
  123. */
  124. public function testRemove()
  125. {
  126. $cookies = [
  127. new Cookie('remember_me', 'a'),
  128. new Cookie('gtm', 'b')
  129. ];
  130. $collection = new CookieCollection($cookies);
  131. $this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
  132. $new = $collection->remove('remember_me');
  133. $this->assertTrue($collection->has('remember_me'), 'old instance not modified');
  134. $this->assertNotSame($new, $collection);
  135. $this->assertFalse($new->has('remember_me'), 'should be removed');
  136. $this->assertNull($new->get('remember_me'), 'should be removed');
  137. }
  138. /**
  139. * Test getting cookies by name
  140. *
  141. * @return void
  142. */
  143. public function testGetByName()
  144. {
  145. $cookies = [
  146. new Cookie('remember_me', 'a'),
  147. new Cookie('gtm', 'b')
  148. ];
  149. $collection = new CookieCollection($cookies);
  150. $this->assertNull($collection->get('nope'));
  151. $this->assertInstanceOf(Cookie::class, $collection->get('REMEMBER_me'), 'case insensitive cookie names');
  152. $this->assertInstanceOf(Cookie::class, $collection->get('remember_me'));
  153. $this->assertSame($cookies[0], $collection->get('remember_me'));
  154. }
  155. /**
  156. * Test that the constructor takes only an array of objects implementing
  157. * the CookieInterface
  158. *
  159. * @expectedException \InvalidArgumentException
  160. * @expectedExceptionMessage Expected `Cake\Http\Cookie\CookieCollection[]` as $cookies but instead got `array` at index 1
  161. * @return void
  162. */
  163. public function testConstructorWithInvalidCookieObjects()
  164. {
  165. $array = [
  166. new Cookie('one', 'one'),
  167. []
  168. ];
  169. new CookieCollection($array);
  170. }
  171. /**
  172. * Test adding cookies from a response.
  173. *
  174. * @return void
  175. */
  176. public function testAddFromResponse()
  177. {
  178. $collection = new CookieCollection();
  179. $request = new ServerRequest([
  180. 'url' => '/app'
  181. ]);
  182. $response = (new Response())
  183. ->withAddedHeader('Set-Cookie', 'test=value')
  184. ->withAddedHeader('Set-Cookie', 'expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;')
  185. ->withAddedHeader('Set-Cookie', 'session=123abc; Domain=www.example.com');
  186. $new = $collection->addFromResponse($response, $request);
  187. $this->assertNotSame($new, $collection, 'Should clone collection');
  188. $this->assertTrue($new->has('test'));
  189. $this->assertTrue($new->has('session'));
  190. $this->assertTrue($new->has('expiring'));
  191. $this->assertSame('value', $new->get('test')->getValue());
  192. $this->assertSame('123abc', $new->get('session')->getValue());
  193. $this->assertSame('soon', $new->get('expiring')->getValue());
  194. $this->assertSame('/app', $new->get('test')->getPath(), 'cookies should inherit request path');
  195. $this->assertSame('/', $new->get('expiring')->getPath(), 'path attribute should be used.');
  196. $this->assertSame(0, $new->get('test')->getExpiry(), 'No expiry');
  197. $this->assertSame(
  198. '2021-06-09 10:18:14',
  199. date('Y-m-d H:i:s', $new->get('expiring')->getExpiry()),
  200. 'Has expiry'
  201. );
  202. $session = $new->get('session');
  203. $this->assertSame(0, $session->getExpiry(), 'No expiry');
  204. $this->assertSame('www.example.com', $session->getDomain(), 'Has domain');
  205. }
  206. /**
  207. * Test adding cookies that contain URL encoded data
  208. *
  209. * @return void
  210. */
  211. public function testAddFromResponseValueUrldecodeData()
  212. {
  213. $collection = new CookieCollection();
  214. $request = new ServerRequest([
  215. 'url' => '/app'
  216. ]);
  217. $response = (new Response())
  218. ->withAddedHeader('Set-Cookie', 'test=val%3Bue; Path=/example; Secure;');
  219. $new = $collection->addFromResponse($response, $request);
  220. $this->assertTrue($new->has('test'));
  221. $test = $new->get('test');
  222. $this->assertSame('val;ue', $test->getValue());
  223. $this->assertSame('/example', $test->getPath());
  224. }
  225. /**
  226. * Test adding cookies from a response ignores expired cookies
  227. *
  228. * @return void
  229. */
  230. public function testAddFromResponseIgnoreExpired()
  231. {
  232. $collection = new CookieCollection();
  233. $request = new ServerRequest([
  234. 'url' => '/app'
  235. ]);
  236. $response = (new Response())
  237. ->withAddedHeader('Set-Cookie', 'test=value')
  238. ->withAddedHeader('Set-Cookie', 'expired=soon; Expires=Wed, 09-Jun-2012 10:18:14 GMT; Path=/;');
  239. $new = $collection->addFromResponse($response, $request);
  240. $this->assertFalse($new->has('expired'),'Should drop expired cookies');
  241. }
  242. /**
  243. * Test adding cookies from the collection to request.
  244. *
  245. * @return void
  246. */
  247. public function testAddToRequest()
  248. {
  249. $collection = new CookieCollection();
  250. $collection = $collection
  251. ->add(new Cookie('api', 'A', null, '/api', 'example.com'))
  252. ->add(new Cookie('blog', 'b', null, '/blog', 'blog.example.com'))
  253. ->add(new Cookie('expired', 'ex', new DateTime('-2 seconds'), '/', 'example.com'));
  254. $request = new ServerRequest([
  255. 'environment' => [
  256. 'HTTP_HOST' => 'example.com',
  257. 'REQUEST_URI' => '/api'
  258. ]
  259. ]);
  260. $request = $collection->addToRequest($request);
  261. $this->assertCount(1, $request->getCookieParams());
  262. $this->assertSame(['api' => 'A'], $request->getCookieParams());
  263. $request = new ServerRequest([
  264. 'environment' => [
  265. 'HTTP_HOST' => 'example.com',
  266. 'REQUEST_URI' => '/'
  267. ]
  268. ]);
  269. $request = $collection->addToRequest($request);
  270. $this->assertCount(0, $request->getCookieParams());
  271. $request = new ServerRequest([
  272. 'environment' => [
  273. 'HTTP_HOST' => 'example.com',
  274. 'REQUEST_URI' => '/blog'
  275. ]
  276. ]);
  277. $request = $collection->addToRequest($request);
  278. $this->assertCount(0, $request->getCookieParams(), 'domain matching should apply');
  279. $request = new ServerRequest([
  280. 'environment' => [
  281. 'HTTP_HOST' => 'foo.blog.example.com',
  282. 'REQUEST_URI' => '/blog'
  283. ]
  284. ]);
  285. $request = $collection->addToRequest($request);
  286. $this->assertCount(1, $request->getCookieParams(), 'domain matching should apply');
  287. $this->assertSame(['blog' => 'b'], $request->getCookieParams());
  288. }
  289. /**
  290. * Test adding cookies checks the secure crumb
  291. *
  292. * @return void
  293. */
  294. public function testAddToRequestSecureCrumb()
  295. {
  296. $collection = new CookieCollection();
  297. $collection = $collection
  298. ->add(new Cookie('secret', 'A', null, '/', 'example.com', true))
  299. ->add(new Cookie('public', 'b', null, '/', 'example.com', false));
  300. $request = new ServerRequest([
  301. 'environment' => [
  302. 'HTTPS' => 'on',
  303. 'HTTP_HOST' => 'example.com',
  304. 'REQUEST_URI' => '/api'
  305. ]
  306. ]);
  307. $request = $collection->addToRequest($request);
  308. $this->assertSame(['secret' => 'A', 'public' => 'b'], $request->getCookieParams());
  309. // no HTTPS set.
  310. $request = new ServerRequest([
  311. 'environment' => [
  312. 'HTTP_HOST' => 'example.com',
  313. 'REQUEST_URI' => '/api'
  314. ]
  315. ]);
  316. $request = $collection->addToRequest($request);
  317. $this->assertSame(['public' => 'b'], $request->getCookieParams());
  318. }
  319. }