2
0

CookieJarTest.php 19 KB

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