CookieJarTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 a single cookie as an object
  147. *
  148. */
  149. public function testGetCookieAsObject()
  150. {
  151. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  152. $jar = new Zend_Http_CookieJar();
  153. $jar->addCookie($cookie->__toString(), 'http://www.example.com/tests/');
  154. $cobj = $jar->getCookie('http://www.example.com/tests/', 'foo');
  155. $this->assertTrue($cobj instanceof Zend_Http_Cookie, '$cobj is not a Cookie object');
  156. $this->assertEquals($cookie->getName(), $cobj->getName(), 'Cookie name is not as expected');
  157. $this->assertEquals($cookie->getValue(), $cobj->getValue(), 'Cookie value is not as expected');
  158. $this->assertEquals($cookie->getDomain(), $cobj->getDomain(), 'Cookie domain is not as expected');
  159. $this->assertEquals($cookie->getPath(), $cobj->getPath(), 'Cookie path is not as expected');
  160. }
  161. /**
  162. * Check we can get a cookie as a string
  163. */
  164. public function testGetCookieAsString()
  165. {
  166. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  167. $jar = new Zend_Http_CookieJar();
  168. $jar->addCookie($cookie);
  169. $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  170. $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
  171. $cstr = $jar->getCookie('http://www.example.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  172. $this->assertEquals($cookie->__toString(), $cstr, 'Cookie string is not the expected string');
  173. }
  174. /**
  175. * Check we can get false when trying to get a non-existant cookie
  176. */
  177. public function testGetCookieReturnFalse()
  178. {
  179. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  180. $jar = new Zend_Http_CookieJar();
  181. $jar->addCookie($cookie);
  182. $cstr = $jar->getCookie('http://www.example.com/tests/', 'otherfoo', Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  183. $this->assertFalse($cstr, 'getCookie was expected to return false, no such cookie');
  184. $cstr = $jar->getCookie('http://www.otherexample.com/tests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  185. $this->assertFalse($cstr, 'getCookie was expected to return false, no such domain');
  186. $cstr = $jar->getCookie('http://www.example.com/othertests/', 'foo', Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  187. $this->assertFalse($cstr, 'getCookie was expected to return false, no such path');
  188. }
  189. /**
  190. * Test we get a proper exception when an invalid URI is passed
  191. */
  192. public function testExceptGetCookieInvalidUri()
  193. {
  194. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=www.example.com; path=/tests');
  195. $jar = new Zend_Http_CookieJar();
  196. $jar->addCookie($cookie);
  197. try {
  198. $jar->getCookie('foo.com', 'foo');
  199. $this->fail('Expected getCookie to throw exception, invalid URI string passed');
  200. } catch (Zend_Exception $e) {
  201. // We are ok!
  202. }
  203. try {
  204. $jar->getCookie(Zend_Uri::factory('mailto:nobody@dev.null.com'), 'foo');
  205. $this->fail('Expected getCookie to throw exception, invalid URI object passed');
  206. } catch (Zend_Exception $e) {
  207. // We are ok!
  208. }
  209. }
  210. /**
  211. * Test we get a proper exception when an invalid return constant is passed
  212. *
  213. */
  214. public function testExceptGetCookieInvalidReturnType()
  215. {
  216. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=example.com;');
  217. $jar = new Zend_Http_CookieJar();
  218. $jar->addCookie($cookie);
  219. try {
  220. $jar->getCookie('http://example.com/', 'foo', 5);
  221. $this->fail('Expected getCookie to throw exception, invalid return type');
  222. } catch (Zend_Http_Exception $e) {
  223. // We are ok!
  224. }
  225. }
  226. /**
  227. * Test we can get all matching cookies for a request, with session cookies
  228. *
  229. * @dataProvider cookieMatchTestProvider
  230. */
  231. public function testGetMatchingCookies($url, $expected)
  232. {
  233. $jar = new Zend_Http_CookieJar();
  234. $cookies = array(
  235. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  236. Zend_Http_Cookie::fromString('foo2=bar2; domain=foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  237. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  238. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  239. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  240. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  241. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  242. Zend_Http_Cookie::fromString('foo7=bar7; domain=newwww.foo.com; path=/;'),
  243. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  244. );
  245. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  246. $cookies = $jar->getMatchingCookies($url);
  247. $this->assertEquals($expected, count($cookies), $jar->getMatchingCookies($url, true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT));
  248. }
  249. static public function cookieMatchTestProvider()
  250. {
  251. return array(
  252. array('http://www.foo.com/path/file.txt', 4),
  253. array('http://foo.com/path/file.txt', 3),
  254. array('https://www.foo.com/path/file.txt', 5),
  255. array('http://subdomain.foo.com/path', 4),
  256. array('http://subdomain.foo.com/otherpath', 3),
  257. array('http://blog.foo.com/news', 2)
  258. );
  259. }
  260. /**
  261. * Test we can get all matching cookies for a request, without session cookies
  262. */
  263. public function testGetMatchingCookiesNoSession()
  264. {
  265. $jar = new Zend_Http_CookieJar();
  266. $cookies = array(
  267. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  268. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  269. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  270. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  271. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  272. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  273. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  274. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  275. );
  276. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  277. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  278. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', false);
  279. $this->assertEquals(3, count($cookies), 'Cookie count is expected to be 3');
  280. $cookies = $jar->getMatchingCookies('https://www.foo.com/path/file.txt', false);
  281. $this->assertEquals(4, count($cookies), 'Cookie count is expected to be 4');
  282. }
  283. /**
  284. * Test we can get all matching cookies for a request, when we set a different time for now
  285. */
  286. public function testGetMatchingCookiesWithTime()
  287. {
  288. $jar = new Zend_Http_CookieJar();
  289. $cookies = array(
  290. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  291. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 7200)),
  292. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  293. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  294. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() - 7200)),
  295. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  296. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  297. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  298. );
  299. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  300. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  301. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() + 3700);
  302. $this->assertEquals(2, count($cookies), 'Cookie count is expected to be 2');
  303. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_OBJECT, time() - 3700);
  304. $this->assertEquals(5, count($cookies), 'Cookie count is expected to be 5');
  305. }
  306. /**
  307. * Test we can get all matching cookies for a request, and return as strings array / concat
  308. */
  309. public function testGetMatchingCookiesAsStrings()
  310. {
  311. $jar = new Zend_Http_CookieJar();
  312. $cookies = array(
  313. Zend_Http_Cookie::fromString('foo1=bar1; domain=.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  314. Zend_Http_Cookie::fromString('foo2=bar2; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() + 3600)),
  315. Zend_Http_Cookie::fromString('foo3=bar3; domain=.foo.com; path=/; expires=' . date(DATE_COOKIE, time() - 3600)),
  316. Zend_Http_Cookie::fromString('foo4=bar4; domain=.foo.com; path=/;'),
  317. Zend_Http_Cookie::fromString('foo5=bar5; domain=.foo.com; path=/; secure; expires=' . date(DATE_COOKIE, time() + 3600)),
  318. Zend_Http_Cookie::fromString('foo6=bar6; domain=.foo.com; path=/otherpath; expires=' . date(DATE_COOKIE, time() + 3600)),
  319. Zend_Http_Cookie::fromString('foo7=bar7; domain=www.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  320. Zend_Http_Cookie::fromString('foo8=bar8; domain=subdomain.foo.com; path=/path; expires=' . date(DATE_COOKIE, time() + 3600)),
  321. );
  322. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  323. $this->assertEquals(8, count($jar->getAllCookies()), 'Cookie count is expected to be 8');
  324. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  325. $this->assertType('array', $cookies, '$cookies is expected to be an array, but it is not');
  326. $this->assertType('string', $cookies[0], '$cookies[0] is expected to be a string');;
  327. $cookies = $jar->getMatchingCookies('http://www.foo.com/path/file.txt', true, Zend_Http_CookieJar::COOKIE_STRING_CONCAT);
  328. $this->assertType('string', $cookies, '$cookies is expected to be a string');
  329. }
  330. /**
  331. * Test we get a proper exception when an invalid URI is passed
  332. */
  333. public function testExceptGetMatchingCookiesInvalidUri()
  334. {
  335. $jar = new Zend_Http_CookieJar();
  336. try {
  337. $cookies = $jar->getMatchingCookies('invalid.com', true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  338. $this->fail('Expected getMatchingCookies to throw exception, invalid URI string passed');
  339. } catch (Zend_Exception $e) {
  340. // We are ok!
  341. }
  342. try {
  343. $cookies = $jar->getMatchingCookies(new stdClass(), true, Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
  344. $this->fail('Expected getCookie to throw exception, invalid URI object passed');
  345. } catch (Zend_Exception $e) {
  346. // We are ok!
  347. }
  348. }
  349. /**
  350. * Test we can build a new object from a response object (single cookie header)
  351. */
  352. public function testFromResponse()
  353. {
  354. $res_str = file_get_contents(dirname(realpath(__FILE__)) .
  355. DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_single_cookie');
  356. $response = Zend_Http_Response::fromString($res_str);
  357. $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
  358. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
  359. $this->assertEquals(1, count($jar->getAllCookies()), 'CookieJar expected to contain 1 cookie');
  360. }
  361. /**
  362. * Test we can build a new object from a response object (multiple cookie headers)
  363. */
  364. public function testFromResponseMultiHeader()
  365. {
  366. $res_str = file_get_contents(dirname(realpath(__FILE__)) .
  367. DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'response_with_cookies');
  368. $response = Zend_Http_Response::fromString($res_str);
  369. $jar = Zend_Http_CookieJar::fromResponse($response, 'http://www.example.com');
  370. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of CookieJar as expected');
  371. $this->assertEquals(3, count($jar->getAllCookies()), 'CookieJar expected to contain 3 cookies');
  372. }
  373. /**
  374. * Make sure that paths with trailing slashes are matched as well as paths with no trailing slashes
  375. */
  376. public function testMatchPathWithTrailingSlash()
  377. {
  378. $jar = new Zend_Http_CookieJar();
  379. $cookies = array(
  380. Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
  381. Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
  382. );
  383. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  384. $cookies = $jar->getMatchingCookies('http://www.example.com/a/b/file.txt');
  385. $this->assertType('array', $cookies);
  386. $this->assertEquals(2, count($cookies));
  387. }
  388. public function testIteratorAndCountable()
  389. {
  390. $jar = new Zend_Http_CookieJar();
  391. $cookies = array(
  392. Zend_Http_Cookie::fromString('foo1=bar1; domain=.example.com; path=/a/b'),
  393. Zend_Http_Cookie::fromString('foo2=bar2; domain=.example.com; path=/a/b/')
  394. );
  395. foreach ($cookies as $cookie) $jar->addCookie($cookie);
  396. foreach ($jar as $cookie) {
  397. $this->assertType('Zend_Http_Cookie', $cookie);
  398. }
  399. $this->assertEquals(2, count($jar));
  400. $this->assertFalse($jar->isEmpty());
  401. $jar->reset();
  402. $this->assertTrue($jar->isEmpty());
  403. }
  404. }