CookieJarTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_CookieJar
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Http/CookieJar.php';
  23. /**
  24. * Zend_Http_CookieJar unit tests
  25. *
  26. * @category Zend
  27. * @package Zend_Http_CookieJar
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Http
  32. * @group Zend_Http_CookieJar
  33. */
  34. class Zend_Http_CookieJarTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function loadResponse($filename)
  37. {
  38. $message = file_get_contents($filename);
  39. // Line endings are sometimes an issue inside the canned responses; the
  40. // following is a negative lookbehind assertion, and replaces any \n
  41. // not preceded by \r with the sequence \r\n, ensuring that the message
  42. // is well-formed.
  43. return preg_replace("#(?<!\r)\n#", "\r\n", $message);
  44. }
  45. /**
  46. * Test we can add cookies to the jar
  47. *
  48. */
  49. public function testAddCookie()
  50. {
  51. $jar = new Zend_Http_CookieJar();
  52. $this->assertEquals(0, count($jar->getAllCookies()), 'Cookie jar is expected to contain 0 cookies');
  53. $jar->addCookie('foo=bar; domain=example.com');
  54. $cookie = $jar->getCookie('http://example.com/', 'foo');
  55. $this->assertTrue($cookie instanceof Zend_Http_Cookie, '$cookie is expected to be a Cookie object');
  56. $this->assertEquals('bar', $cookie->getValue(), 'Cookie value is expected to be "bar"');
  57. $jar->addCookie('cookie=brownie; domain=geekz.co.uk;');
  58. $this->assertEquals(2, count($jar->getAllCookies()), 'Cookie jar is expected to contain 2 cookies');
  59. }
  60. /**
  61. * Check we get an expection if a non-valid cookie is passed to addCookie
  62. *
  63. */
  64. public function testExceptAddInvalidCookie()
  65. {
  66. $jar = new Zend_Http_CookieJar();
  67. try {
  68. $jar->addCookie('garbage');
  69. $this->fail('Expected exception was not thrown');
  70. } catch (Zend_Http_Exception $e) {
  71. // We are ok
  72. }
  73. try {
  74. $jar->addCookie(new Zend_Http_Cookiejar());
  75. $this->fail('Expected exception was not thrown');
  76. } catch (Zend_Http_Exception $e) {
  77. // We are ok
  78. }
  79. }
  80. /**
  81. * Test we can read cookies from a Response object
  82. *
  83. */
  84. public function testAddCookiesFromResponse()
  85. {
  86. $jar = new Zend_Http_Cookiejar();
  87. $res_str = $this->loadResponse(
  88. dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
  89. );
  90. $response = Zend_Http_Response::fromString($res_str);
  91. $jar->addCookiesFromResponse($response, 'http://www.example.com');
  92. $this->assertEquals(3, count($jar->getAllCookies()));
  93. $cookie_str = 'foo=bar;BOFH=Feature+was+not+beta+tested;time=1164234700;';
  94. $this->assertEquals($cookie_str, $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT));
  95. }
  96. /**
  97. * Test we get an exception in case of invalid response objects
  98. *
  99. * @dataProvider invalidResponseProvider
  100. * @expectedException Zend_Http_Exception
  101. */
  102. public function testExceptAddCookiesInvalidResponse($resp)
  103. {
  104. $jar = new Zend_Http_Cookiejar();
  105. $jar->addCookiesFromResponse($resp, 'http://www.example.com');
  106. }
  107. static public function invalidResponseProvider()
  108. {
  109. return array(
  110. array(new stdClass),
  111. array(null),
  112. array(12),
  113. array('hi')
  114. );
  115. }
  116. /**
  117. * Test we can get all cookies as an array of Cookie objects
  118. *
  119. */
  120. public function testGetAllCookies()
  121. {
  122. $jar = new Zend_Http_CookieJar();
  123. $cookies = array(
  124. 'name=Arthur; domain=camelot.gov.uk',
  125. 'quest=holy+grail; domain=forest.euwing.com',
  126. 'swallow=african; domain=bridge-of-death.net'
  127. );
  128. foreach ($cookies as $cookie) {
  129. $jar->addCookie($cookie);
  130. }
  131. $cobjects = $jar->getAllCookies();
  132. foreach ($cobjects as $id => $cookie) {
  133. $this->assertContains((string) $cookie, $cookies[$id]);
  134. }
  135. }
  136. /**
  137. * Test we can get all cookies as a concatenated string
  138. *
  139. */
  140. public function testGetAllCookiesAsConcat()
  141. {
  142. $jar = new Zend_Http_CookieJar();
  143. $cookies = array(
  144. 'name=Arthur; domain=camelot.gov.uk',
  145. 'quest=holy+grail; domain=forest.euwing.com',
  146. 'swallow=african; domain=bridge-of-death.net'
  147. );
  148. foreach ($cookies as $cookie) {
  149. $jar->addCookie($cookie);
  150. }
  151. $expected = 'name=Arthur;quest=holy+grail;swallow=african;';
  152. $real = $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT );
  153. $this->assertEquals($expected, $real, 'Concatenated string is not as expected');
  154. }
  155. /**
  156. * Test we can get all cookies as a concatenated string
  157. * @group ZF-11726
  158. */
  159. public function testGetAllCookiesAsConcatStrictMode()
  160. {
  161. $jar = new Zend_Http_CookieJar();
  162. $cookies = array(
  163. 'name=Arthur; domain=camelot.gov.uk',
  164. 'quest=holy+grail; domain=forest.euwing.com',
  165. 'swallow=african; domain=bridge-of-death.net'
  166. );
  167. foreach ($cookies as $cookie) {
  168. $jar->addCookie($cookie);
  169. }
  170. $expected = 'name=Arthur; quest=holy+grail; swallow=african';
  171. $real = $jar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT_STRICT);
  172. $this->assertEquals($expected, $real, 'Concatenated string is not as expected');
  173. }
  174. /**
  175. * Test we can get a single cookie as an object
  176. *
  177. */
  178. public function testGetCookieAsObject()
  179. {
  180. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  181. $jar = new Zend_Http_CookieJar();
  182. $jar->addCookie($cookie->__toString(), 'http://www.example.com/tests/');
  183. $cobj = $jar->getCookie('http://www.example.com/tests/', 'foo');
  184. $this->assertTrue($cobj instanceof Zend_Http_Cookie, '$cobj is not a Cookie object');
  185. $this->assertEquals($cookie->getName(), $cobj->getName(), 'Cookie name is not as expected');
  186. $this->assertEquals($cookie->getValue(), $cobj->getValue(), 'Cookie value is not as expected');
  187. $this->assertEquals($cookie->getDomain(), $cobj->getDomain(), 'Cookie domain is not as expected');
  188. $this->assertEquals($cookie->getPath(), $cobj->getPath(), 'Cookie path is not as expected');
  189. }
  190. /**
  191. * Check we can get a cookie as a string
  192. */
  193. public function testGetCookieAsString()
  194. {
  195. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  196. $jar = new Zend_Http_CookieJar();
  197. $jar->addCookie($cookie);
  198. $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  199. $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
  200. $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  201. $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
  202. }
  203. /**
  204. * Check we can get false when trying to get a non-existant cookie
  205. */
  206. public function testGetCookieReturnFalse()
  207. {
  208. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  209. $jar = new Zend_Http_CookieJar();
  210. $jar->addCookie($cookie);
  211. $cstr = $jar->getCookie('http://www.example.com/tests/', 'otherfoo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  212. $this->assertFalse($cstr, 'getCookie was expected to return false, no such cookie');
  213. $cstr = $jar->getCookie('http://www.otherexample.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  214. $this->assertFalse($cstr, 'getCookie was expected to return false, no such domain');
  215. $cstr = $jar->getCookie('http://www.example.com/othertests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  216. $this->assertFalse($cstr, 'getCookie was expected to return false, no such path');
  217. }
  218. /**
  219. * Test we get a proper exception when an invalid URI is passed
  220. */
  221. public function testExceptGetCookieInvalidUri()
  222. {
  223. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  224. $jar = new Zend_Http_CookieJar();
  225. $jar->addCookie($cookie);
  226. try {
  227. $jar->getCookie('foo.com', 'foo');
  228. $this->fail('Expected getCookie to throw exception, invalid URI string passed');
  229. } catch (Zend_Exception $e) {
  230. // We are ok!
  231. }
  232. try {
  233. $jar->getCookie(Zend_Uri::factory('mailto:nobody@dev.null.com'), 'foo');
  234. $this->fail('Expected getCookie to throw exception, invalid URI object passed');
  235. } catch (Zend_Exception $e) {
  236. // We are ok!
  237. }
  238. }
  239. /**
  240. * Test we get a proper exception when an invalid return constant is passed
  241. *
  242. */
  243. public function testExceptGetCookieInvalidReturnType()
  244. {
  245. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=example.com;');
  246. $jar = new Zend_Http_CookieJar();
  247. $jar->addCookie($cookie);
  248. try {
  249. $jar->getCookie('http://example.com/', 'foo', 5);
  250. $this->fail('Expected getCookie to throw exception, invalid return type');
  251. } catch (Zend_Http_Exception $e) {
  252. // We are ok!
  253. }
  254. }
  255. /**
  256. * Test we can get all matching cookies for a request, with session cookies
  257. *
  258. * @dataProvider cookieMatchTestProvider
  259. */
  260. public function testGetMatchingCookies($url, $expected)
  261. {
  262. $jar = new Zend_Http_CookieJar();
  263. $cookies = array(
  264. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  265. Zend_Http_Cookie::fromString('foo2=bar2; domain=foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  266. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  267. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  268. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  269. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  270. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  271. Zend_Http_Cookie::fromString('foo7=bar7; domain=newwww.foo.com; path=/;'),
  272. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  273. );
  274. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  275. $cookies = $jar->getMatchingCookies($url);
  276. $this->assertEquals($expected, count($cookies), $jar->getMatchingCookies($url, true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT));
  277. }
  278. static public function cookieMatchTestProvider()
  279. {
  280. return array(
  281. array('http://www.foo.com/path/file.txt', 4),
  282. array('http://foo.com/path/file.txt', 3),
  283. array('https://www.foo.com/path/file.txt', 5),
  284. array('http://subdomain.foo.com/path', 4),
  285. array('http://subdomain.foo.com/otherpath', 3),
  286. array('http://blog.foo.com/news', 2)
  287. );
  288. }
  289. /**
  290. * Test we can get all matching cookies for a request, without session cookies
  291. */
  292. public function testGetMatchingCookiesNoSession()
  293. {
  294. $jar = new Zend_Http_CookieJar();
  295. $cookies = array(
  296. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  297. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  298. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  299. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  300. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  301. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  302. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  303. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  304. );
  305. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  306. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  307. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', false);
  308. $this->assertEquals(3, count($cookies), 'Cookie count is expected to be 3');
  309. $cookies = $jar->getMatchingCookies('https://www.foo.com/path/file.txt', false);
  310. $this->assertEquals(4, count($cookies), 'Cookie count is expected to be 4');
  311. }
  312. /**
  313. * Test we can get all matching cookies for a request, when we set a different time for now
  314. */
  315. public function testGetMatchingCookiesWithTime()
  316. {
  317. $jar = new Zend_Http_CookieJar();
  318. $cookies = array(
  319. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  320. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 7200)),
  321. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  322. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  323. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() - 7200)),
  324. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  325. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  326. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  327. );
  328. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  329. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  330. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() + 3700);
  331. $this->assertEquals(2, count($cookies), 'Cookie count is expected to be 2');
  332. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() - 3700);
  333. $this->assertEquals(5, count($cookies), 'Cookie count is expected to be 5');
  334. }
  335. /**
  336. * Test we can get all matching cookies for a request, and return as strings array / concat
  337. */
  338. public function testGetMatchingCookiesAsStrings()
  339. {
  340. $jar = new Zend_Http_CookieJar();
  341. $cookies = array(
  342. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  343. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  344. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  345. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  346. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  347. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  348. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  349. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  350. );
  351. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  352. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  353. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  354. $this->assertTrue(is_array($cookies), '$cookies is expected to be an array, but it is not');
  355. $this->assertTrue(is_string($cookies[0]), '$cookies[0] is expected to be a string');;
  356. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  357. $this->assertTrue(is_string($cookies), '$cookies is expected to be a string');
  358. $expected = 'foo1=bar1;foo2=bar2;foo4=bar4;foo7=bar7;';
  359. $this->assertEquals($expected, $cookies, 'Concatenated string is not as expected');
  360. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT_STRICT);
  361. $this->assertTrue(is_string($cookies), '$cookies is expected to be a string');
  362. $expected = 'foo1=bar1; foo2=bar2; foo4=bar4; foo7=bar7';
  363. $this->assertEquals($expected, $cookies, 'Concatenated string is not as expected');
  364. }
  365. /**
  366. * Test we get a proper exception when an invalid URI is passed
  367. */
  368. public function testExceptGetMatchingCookiesInvalidUri()
  369. {
  370. $jar = new Zend_Http_CookieJar();
  371. try {
  372. $cookies = $jar->getMatchingCookies('invalid.com', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  373. $this->fail('Expected getMatchingCookies to throw exception, invalid URI string passed');
  374. } catch (Zend_Exception $e) {
  375. // We are ok!
  376. }
  377. try {
  378. $cookies = $jar->getMatchingCookies(new stdClass(), true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  379. $this->fail('Expected getCookie to throw exception, invalid URI object passed');
  380. } catch (Zend_Exception $e) {
  381. // We are ok!
  382. }
  383. }
  384. /**
  385. * Test we can build a new object from a response object (single cookie header)
  386. */
  387. public function testFromResponse()
  388. {
  389. $res_str = $this->loadResponse(
  390. dirname(realpath(__FILE__)) . '/_files/response_with_single_cookie'
  391. );
  392. $response = Zend_Http_Response::fromString($res_str);
  393. $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
  394. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
  395. $this->assertEquals(1, count($jar->getAllCookies()), 'CookieJar expected to contain 1 cookie');
  396. }
  397. /**
  398. * Test we can build a new object from a response object (multiple cookie headers)
  399. */
  400. public function testFromResponseMultiHeader()
  401. {
  402. $res_str = $this->loadResponse(
  403. dirname(realpath(__FILE__)) . '/_files/response_with_cookies'
  404. );
  405. $response = Zend_Http_Response::fromString($res_str);
  406. $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
  407. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
  408. $this->assertEquals(3, count($jar->getAllCookies()), 'CookieJar expected to contain 3 cookies');
  409. }
  410. /**
  411. * Make sure that paths with trailing slashes are matched as well as paths with no trailing slashes
  412. */
  413. public function testMatchPathWithTrailingSlash()
  414. {
  415. $jar = new Zend_Http_CookieJar();
  416. $cookies = array(
  417. Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
  418. Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
  419. );
  420. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  421. $cookies = $jar->getMatchingCookies('http://www.example.com/a/b/file.txt');
  422. $this->assertTrue(is_array($cookies));
  423. $this->assertEquals(2, count($cookies));
  424. }
  425. public function testIteratorAndCountable()
  426. {
  427. $jar = new Zend_Http_CookieJar();
  428. $cookies = array(
  429. Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
  430. Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
  431. );
  432. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  433. foreach ($jar as $cookie) {
  434. $this->assertTrue($cookie instanceof Zend_Http_Cookie);
  435. }
  436. $this->assertEquals(2, count($jar));
  437. $this->assertFalse($jar->isEmpty());
  438. $jar->reset();
  439. $this->assertTrue($jar->isEmpty());
  440. }
  441. }