CookieJarTest.php 20 KB

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