CookieCollectionTest.php 6.3 KB

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