CookieJarTest.php 21 KB

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