CookieCollectionTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http;
  15. use Cake\Network\Http\CookieCollection;
  16. use Cake\Network\Http\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. ],
  58. [
  59. 'name' => 'second',
  60. 'value' => '2',
  61. 'path' => '/',
  62. 'domain' => '.foo.example.com'
  63. ],
  64. ];
  65. $this->assertEquals($expected, $result);
  66. }
  67. /**
  68. * test store secure.
  69. *
  70. * @return void
  71. */
  72. public function testStoreSecure()
  73. {
  74. $headers = [
  75. 'HTTP/1.0 200 Ok',
  76. 'Set-Cookie: first=1',
  77. 'Set-Cookie: second=2; Secure; HttpOnly',
  78. ];
  79. $response = new Response($headers, '');
  80. $result = $this->cookies->store($response, 'http://example.com/some/path');
  81. $this->assertNull($result);
  82. $result = $this->cookies->getAll();
  83. $this->assertCount(2, $result);
  84. $expected = [
  85. [
  86. 'name' => 'first',
  87. 'value' => '1',
  88. 'path' => '/some/path',
  89. 'domain' => 'example.com'
  90. ],
  91. [
  92. 'name' => 'second',
  93. 'value' => '2',
  94. 'path' => '/some/path',
  95. 'domain' => 'example.com',
  96. 'secure' => true,
  97. 'httponly' => true,
  98. ],
  99. ];
  100. $this->assertEquals($expected, $result);
  101. }
  102. /**
  103. * test storing an expired cookie clears existing ones too.
  104. *
  105. * @return void
  106. */
  107. public function testStoreExpiring()
  108. {
  109. $headers = [
  110. 'HTTP/1.0 200 Ok',
  111. 'Set-Cookie: first=1',
  112. 'Set-Cookie: second=2; Path=/',
  113. ];
  114. $response = new Response($headers, '');
  115. $this->cookies->store($response, 'http://example.com/some/path');
  116. $result = $this->cookies->getAll();
  117. $this->assertCount(2, $result);
  118. $headers = [
  119. 'HTTP/1.0 200 Ok',
  120. 'Set-Cookie: first=1; Expires=Wed, 09-Jun-1999 10:18:14 GMT',
  121. ];
  122. $response = new Response($headers, '');
  123. $this->cookies->store($response, 'http://example.com/');
  124. $result = $this->cookies->getAll();
  125. $this->assertCount(2, $result, 'Path does not match, no expiration');
  126. // Use a more common date format that doesn't match
  127. $headers = [
  128. 'HTTP/1.0 200 Ok',
  129. 'Set-Cookie: first=1; Domain=.foo.example.com; Expires=Wed, 09-Jun-1999 10:18:14 GMT',
  130. ];
  131. $response = new Response($headers, '');
  132. $this->cookies->store($response, 'http://example.com/some/path');
  133. $result = $this->cookies->getAll();
  134. $this->assertCount(2, $result, 'Domain does not match, no expiration');
  135. // Use an RFC1123 date
  136. $headers = [
  137. 'HTTP/1.0 200 Ok',
  138. 'Set-Cookie: first=1; Expires=Wed, 09 Jun 1999 10:18:14 GMT',
  139. ];
  140. $response = new Response($headers, '');
  141. $this->cookies->store($response, 'http://example.com/some/path');
  142. $result = $this->cookies->getAll();
  143. $this->assertCount(1, $result, 'Domain does not match, no expiration');
  144. $expected = [
  145. [
  146. 'name' => 'second',
  147. 'value' => '2',
  148. 'path' => '/',
  149. 'domain' => 'example.com'
  150. ],
  151. ];
  152. $this->assertEquals($expected, $result);
  153. }
  154. /**
  155. * test getting cookies with secure flags
  156. *
  157. * @return void
  158. */
  159. public function testGetMatchingSecure()
  160. {
  161. $headers = [
  162. 'HTTP/1.0 200 Ok',
  163. 'Set-Cookie: first=1',
  164. 'Set-Cookie: second=2; Secure; HttpOnly',
  165. ];
  166. $response = new Response($headers, '');
  167. $this->cookies->store($response, 'https://example.com/');
  168. $result = $this->cookies->get('https://example.com/test');
  169. $expected = ['first' => '1', 'second' => '2'];
  170. $this->assertEquals($expected, $result);
  171. $result = $this->cookies->get('http://example.com/test');
  172. $expected = ['first' => '1'];
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * test getting cookies with secure flags
  177. *
  178. * @return void
  179. */
  180. public function testGetMatchingPath()
  181. {
  182. $headers = [
  183. 'HTTP/1.0 200 Ok',
  184. 'Set-Cookie: first=1; Path=/foo',
  185. 'Set-Cookie: second=2; Path=/',
  186. ];
  187. $response = new Response($headers, '');
  188. $this->cookies->store($response, 'http://example.com/foo');
  189. $result = $this->cookies->get('http://example.com/foo');
  190. $expected = ['first' => '1', 'second' => 2];
  191. $this->assertEquals($expected, $result);
  192. $result = $this->cookies->get('http://example.com/');
  193. $expected = ['second' => 2];
  194. $this->assertEquals($expected, $result);
  195. $result = $this->cookies->get('http://example.com/test');
  196. $expected = ['second' => 2];
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * Test getting cookies matching on paths exactly
  201. *
  202. * @return void
  203. */
  204. public function testGetMatchingDomain()
  205. {
  206. $headers = [
  207. 'HTTP/1.0 200 Ok',
  208. 'Set-Cookie: first=1; Domain=.example.com',
  209. 'Set-Cookie: second=2;',
  210. ];
  211. $response = new Response($headers, '');
  212. $this->cookies->store($response, 'http://foo.example.com/');
  213. $result = $this->cookies->get('http://example.com');
  214. $expected = ['first' => 1];
  215. $this->assertEquals($expected, $result);
  216. $result = $this->cookies->get('http://foo.example.com');
  217. $expected = ['first' => 1, 'second' => '2'];
  218. $this->assertEquals($expected, $result);
  219. $result = $this->cookies->get('http://bar.foo.example.com');
  220. $expected = ['first' => 1];
  221. $this->assertEquals($expected, $result);
  222. $result = $this->cookies->get('http://api.example.com');
  223. $expected = ['first' => 1];
  224. $this->assertEquals($expected, $result);
  225. $result = $this->cookies->get('http://google.com');
  226. $expected = [];
  227. $this->assertEquals($expected, $result);
  228. }
  229. }