2
0

CookieJarTest.php 20 KB

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