2
0

AppTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Gdata/App.php';
  22. require_once 'Zend/Gdata/HttpClient.php';
  23. require_once 'Zend/Gdata/TestUtility/MockHttpClient.php';
  24. /**
  25. * @package Zend_Gdata
  26. * @subpackage UnitTests
  27. */
  28. class Zend_Gdata_AppTest extends PHPUnit_Framework_TestCase
  29. {
  30. public function setUp()
  31. {
  32. $this->fileName = 'Zend/Gdata/App/_files/FeedSample1.xml';
  33. $this->expectedEtag = 'W/"CkcHQH8_fCp7ImA9WxRTGEw."';
  34. $this->expectedMajorProtocolVersion = 1;
  35. $this->expectedMinorProtocolVersion = 2;
  36. $this->httpEntrySample = file_get_contents(
  37. 'Zend/Gdata/_files/AppSample1.txt',
  38. true);
  39. $this->httpEntrySampleWithoutVersion = file_get_contents(
  40. 'Zend/Gdata/_files/AppSample2.txt',
  41. true);
  42. $this->httpFeedSample = file_get_contents(
  43. 'Zend/Gdata/_files/AppSample3.txt',
  44. true);
  45. $this->httpFeedSampleWithoutVersion = file_get_contents(
  46. 'Zend/Gdata/_files/AppSample4.txt',
  47. true);
  48. $this->adapter = new Test_Zend_Gdata_MockHttpClient();
  49. $this->client = new Zend_Gdata_HttpClient();
  50. $this->client->setAdapter($this->adapter);
  51. $this->service = new Zend_Gdata_App($this->client);
  52. }
  53. public function testImportFile()
  54. {
  55. $feed = Zend_Gdata_App::importFile($this->fileName,
  56. 'Zend_Gdata_App_Feed', true);
  57. $this->assertEquals('dive into mark', $feed->title->text);
  58. }
  59. public function testSetAndGetHttpMethodOverride()
  60. {
  61. Zend_Gdata_App::setHttpMethodOverride(true);
  62. $this->assertEquals(true, Zend_Gdata_App::getHttpMethodOverride());
  63. }
  64. public function testSetAndGetProtocolVersion()
  65. {
  66. $this->service->setMajorProtocolVersion(2);
  67. $this->service->setMinorProtocolVersion(1);
  68. $this->assertEquals(2, $this->service->getMajorProtocolVersion());
  69. $this->assertEquals(1, $this->service->getMinorProtocolVersion());
  70. }
  71. public function testDefaultProtocolVersionIs1X()
  72. {
  73. $this->assertEquals(1, $this->service->getMajorProtocolVersion());
  74. $this->assertEquals(null, $this->service->getMinorProtocolVersion());
  75. }
  76. public function testMajorProtocolVersionCannotBeLessThanOne()
  77. {
  78. $exceptionCaught = false;
  79. try {
  80. $this->service->setMajorProtocolVersion(0);
  81. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  82. $exceptionCaught = true;
  83. }
  84. $this->assertTrue($exceptionCaught, 'Expected exception not caught: '
  85. + 'Zend_Gdata_App_InvalidArgumentException');
  86. }
  87. public function testMajorProtocolVersionCannotBeNull()
  88. {
  89. $exceptionCaught = false;
  90. try {
  91. $this->service->setMajorProtocolVersion(null);
  92. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  93. $exceptionCaught = true;
  94. }
  95. $this->assertTrue($exceptionCaught, 'Expected exception not caught: '
  96. + 'Zend_Gdata_App_InvalidArgumentException');
  97. }
  98. public function testMinorProtocolVersionCannotBeLessThanZero()
  99. {
  100. $exceptionCaught = false;
  101. try {
  102. $this->service->setMinorProtocolVersion(-1);
  103. } catch (Zend_Gdata_App_InvalidArgumentException $e) {
  104. $exceptionCaught = true;
  105. }
  106. $this->assertTrue($exceptionCaught, 'Expected exception not caught: '
  107. + 'Zend_Gdata_App_InvalidArgumentException');
  108. }
  109. public function testNoGdataVersionHeaderSentWhenUsingV1()
  110. {
  111. $this->adapter->setResponse(array('HTTP/1.1 200 OK\r\n\r\n'));
  112. $this->service->setMajorProtocolVersion(1);
  113. $this->service->setMinorProtocolVersion(NULL);
  114. $this->service->get('http://www.example.com');
  115. $headers = $this->adapter->popRequest()->headers;
  116. $found = false;
  117. foreach ($headers as $header) {
  118. if (strstr($header, 'GData-Version:'))
  119. $found = true;
  120. }
  121. $this->assertFalse($found, 'Version header found in V1 feed');
  122. }
  123. public function testNoGdataVersionHeaderSentWhenUsingV1X()
  124. {
  125. $this->adapter->setResponse(array('HTTP/1.1 200 OK\r\n\r\n'));
  126. $this->service->setMajorProtocolVersion(1);
  127. $this->service->setMinorProtocolVersion(1);
  128. $this->service->get('http://www.example.com');
  129. $headers = $this->adapter->popRequest()->headers;
  130. $found = false;
  131. foreach ($headers as $header) {
  132. if (strstr($header, 'GData-Version:'))
  133. $found = true;
  134. }
  135. $this->assertTrue(!$found, 'Version header found in V1 feed');
  136. }
  137. public function testGdataVersionHeaderSentWhenUsingV2()
  138. {
  139. $this->adapter->setResponse(array('HTTP/1.1 200 OK\r\n\r\n'));
  140. $this->service->setMajorProtocolVersion(2);
  141. $this->service->setMinorProtocolVersion(NULL);
  142. $this->service->get('http://www.example.com');
  143. $headers = $this->adapter->popRequest()->headers;
  144. $found = false;
  145. foreach ($headers as $header) {
  146. if ($header == 'GData-Version: 2')
  147. $found = true;
  148. }
  149. $this->assertTrue($found, 'Version header not found or incorrect');
  150. }
  151. public function testGdataVersionHeaderSentWhenUsingV2X()
  152. {
  153. $this->adapter->setResponse(array('HTTP/1.1 200 OK\r\n\r\n'));
  154. $this->service->setMajorProtocolVersion(2);
  155. $this->service->setMinorProtocolVersion(1);
  156. $this->service->get('http://www.example.com');
  157. $headers = $this->adapter->popRequest()->headers;
  158. $found = false;
  159. foreach ($headers as $header) {
  160. if ($header == 'GData-Version: 2')
  161. $found = true;
  162. }
  163. $this->assertTrue($found, 'Version header not found or incorrect');
  164. }
  165. public function testHTTPETagsPropagateToEntriesOnGet()
  166. {
  167. $this->adapter->setResponse($this->httpEntrySample);
  168. $entry = $this->service->getEntry('http://www.example.com');
  169. $this->assertEquals($this->expectedEtag, $entry->getEtag());
  170. }
  171. public function testHTTPETagsPropagateToEntriesOnUpdate()
  172. {
  173. $this->adapter->setResponse($this->httpEntrySample);
  174. $entry = new Zend_Gdata_App_Entry();
  175. $newEntry = $this->service->updateEntry($entry, 'http://www.example.com');
  176. $this->assertEquals($this->expectedEtag, $newEntry->getEtag());
  177. }
  178. public function testHTTPEtagsPropagateToEntriesOnInsert()
  179. {
  180. $this->adapter->setResponse($this->httpEntrySample);
  181. $entry = new Zend_Gdata_App_Entry();
  182. $newEntry = $this->service->insertEntry($entry, 'http://www.example.com');
  183. $this->assertEquals($this->expectedEtag, $newEntry->getEtag());
  184. }
  185. public function testIfMatchHTTPHeaderSetOnUpdate()
  186. {
  187. $etag = 'ABCD1234';
  188. $this->adapter->setResponse("HTTP/1.1 201 Created");
  189. $this->service->setMajorProtocolVersion(2);
  190. $entry = new Zend_Gdata_App_Entry();
  191. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  192. 'http://www.example.com',
  193. 'edit',
  194. 'application/atom+xml'));
  195. $entry->setEtag($etag);
  196. $this->service->updateEntry($entry);
  197. $headers = $this->adapter->popRequest()->headers;
  198. $found = false;
  199. foreach ($headers as $header) {
  200. if ($header == 'If-Match: ' . $etag)
  201. $found = true;
  202. }
  203. $this->assertTrue($found, 'If-Match header not found or incorrect');
  204. }
  205. public function testIfMatchHTTPHeaderSetOnUpdateIfWeak()
  206. {
  207. $etag = 'W/ABCD1234';
  208. $this->adapter->setResponse("HTTP/1.1 201 Created");
  209. $this->service->setMajorProtocolVersion(2);
  210. $entry = new Zend_Gdata_App_Entry();
  211. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  212. 'http://www.example.com',
  213. 'edit',
  214. 'application/atom+xml'));
  215. $entry->setEtag($etag);
  216. $this->service->updateEntry($entry);
  217. $headers = $this->adapter->popRequest()->headers;
  218. $found = false;
  219. foreach ($headers as $header) {
  220. if ($header == 'If-Match: ' . $etag)
  221. $found = true;
  222. }
  223. $this->assertFalse($found, 'If-Match header found');
  224. }
  225. public function testIfMatchHTTPHeaderSetOnSave()
  226. {
  227. $etag = 'ABCD1234';
  228. $this->adapter->setResponse("HTTP/1.1 201 Created");
  229. $this->service->setMajorProtocolVersion(2);
  230. $entry = $this->service->newEntry();
  231. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  232. 'http://www.example.com',
  233. 'edit',
  234. 'application/atom+xml'));
  235. $entry->setEtag($etag);
  236. $entry->setService($this->service);
  237. $entry->save();
  238. $headers = $this->adapter->popRequest()->headers;
  239. $found = false;
  240. foreach ($headers as $header) {
  241. if ($header == 'If-Match: ' . $etag)
  242. $found = true;
  243. }
  244. $this->assertTrue($found, 'If-Match header not found or incorrect');
  245. }
  246. public function testIfMatchHTTPHeaderNotSetOnDelete()
  247. {
  248. $etag = 'ABCD1234';
  249. $this->adapter->setResponse("HTTP/1.1 201 Created");
  250. $this->service->setMajorProtocolVersion(2);
  251. $entry = $this->service->newEntry();
  252. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  253. 'http://www.example.com',
  254. 'edit',
  255. 'application/atom+xml'));
  256. $entry->setEtag($etag);
  257. $entry->setService($this->service);
  258. $entry->delete();
  259. $headers = $this->adapter->popRequest()->headers;
  260. $found = false;
  261. foreach ($headers as $header) {
  262. if ($header == 'If-Match: ' . $etag)
  263. $found = true;
  264. }
  265. $this->assertFalse($found, 'If-Match header found on delete');
  266. }
  267. public function testIfMatchHTTPHeaderSetOnManualPost()
  268. {
  269. $etag = 'ABCD1234';
  270. $this->adapter->setResponse("HTTP/1.1 201 Created");
  271. $this->service->setMajorProtocolVersion(2);
  272. $entry = $this->service->newEntry();
  273. $entry->setEtag($etag);
  274. $entry->setService($this->service);
  275. $this->service->post($entry, 'http://www.example.com');
  276. $headers = $this->adapter->popRequest()->headers;
  277. $found = false;
  278. foreach ($headers as $header) {
  279. if ($header == 'If-Match: ' . $etag)
  280. $found = true;
  281. }
  282. $this->assertTrue($found, 'If-Match header not found or incorrect');
  283. }
  284. public function testIfMatchHTTPHeaderSetOnManualPut()
  285. {
  286. $etag = 'ABCD1234';
  287. $this->adapter->setResponse("HTTP/1.1 201 Created");
  288. $this->service->setMajorProtocolVersion(2);
  289. $entry = $this->service->newEntry();
  290. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  291. 'http://www.example.com',
  292. 'edit',
  293. 'application/atom+xml'));
  294. $entry->setEtag($etag);
  295. $entry->setService($this->service);
  296. $this->service->put($entry);
  297. $headers = $this->adapter->popRequest()->headers;
  298. $found = false;
  299. foreach ($headers as $header) {
  300. if ($header == 'If-Match: ' . $etag)
  301. $found = true;
  302. }
  303. $this->assertTrue($found, 'If-Match header not found or incorrect');
  304. }
  305. public function testIfMatchHTTPHeaderSetOnManualDelete()
  306. {
  307. $etag = 'ABCD1234';
  308. $this->adapter->setResponse("HTTP/1.1 201 Created");
  309. $this->service->setMajorProtocolVersion(2);
  310. $entry = $this->service->newEntry();
  311. $entry->link = array(new Zend_Gdata_App_Extension_Link(
  312. 'http://www.example.com',
  313. 'edit',
  314. 'application/atom+xml'));
  315. $entry->setEtag($etag);
  316. $entry->setService($this->service);
  317. $this->service->delete($entry);
  318. $headers = $this->adapter->popRequest()->headers;
  319. $found = false;
  320. foreach ($headers as $header) {
  321. if ($header == 'If-Match: ' . $etag)
  322. $found = true;
  323. }
  324. $this->assertFalse($found, 'If-Match header found on delete');
  325. }
  326. public function testIfMatchHeaderCanBeSetOnInsert() {
  327. $etagOverride = 'foo';
  328. $etag = 'ABCD1234';
  329. $this->service->setMajorProtocolVersion(2);
  330. $this->adapter->setResponse($this->httpEntrySample);
  331. $entry = new Zend_Gdata_App_Entry();
  332. $entry->setEtag($etag);
  333. $newEntry = $this->service->insertEntry($entry,
  334. 'http://www.example.com',
  335. 'Zend_Gdata_App_Entry',
  336. array('If-Match' => $etagOverride));
  337. $headers = $this->adapter->popRequest()->headers;
  338. $found = false;
  339. foreach ($headers as $header) {
  340. if ($header == 'If-Match: ' . $etagOverride)
  341. $found = true;
  342. }
  343. $this->assertTrue($found, 'If-Match header not found or incorrect');
  344. }
  345. public function testIfNoneMatchHeaderCanBeSetOnInsert() {
  346. $etagOverride = 'foo';
  347. $etag = 'ABCD1234';
  348. $this->service->setMajorProtocolVersion(2);
  349. $this->adapter->setResponse($this->httpEntrySample);
  350. $entry = new Zend_Gdata_App_Entry();
  351. $entry->setEtag($etag);
  352. $newEntry = $this->service->insertEntry($entry,
  353. 'http://www.example.com',
  354. 'Zend_Gdata_App_Entry',
  355. array('If-None-Match' => $etagOverride));
  356. $headers = $this->adapter->popRequest()->headers;
  357. $found = false;
  358. foreach ($headers as $header) {
  359. if ($header == 'If-None-Match: ' . $etagOverride)
  360. $found = true;
  361. }
  362. $this->assertTrue($found, 'If-None-Match header not found or incorrect ');
  363. }
  364. public function testIfMatchHeaderCanBeSetOnUpdate() {
  365. $etagOverride = 'foo';
  366. $etag = 'ABCD1234';
  367. $this->service->setMajorProtocolVersion(2);
  368. $this->adapter->setResponse($this->httpEntrySample);
  369. $entry = new Zend_Gdata_App_Entry();
  370. $entry->setEtag($etag);
  371. $newEntry = $this->service->updateEntry($entry,
  372. 'http://www.example.com',
  373. 'Zend_Gdata_App_Entry',
  374. array('If-Match' => $etagOverride));
  375. $headers = $this->adapter->popRequest()->headers;
  376. $found = false;
  377. foreach ($headers as $header) {
  378. if ($header == 'If-Match: ' . $etagOverride)
  379. $found = true;
  380. }
  381. $this->assertTrue($found, 'If-Match header not found or incorrect or incorrect');
  382. }
  383. public function testIfNoneMatchHeaderCanBeSetOnUpdate() {
  384. $etagOverride = 'foo';
  385. $etag = 'ABCD1234';
  386. $this->service->setMajorProtocolVersion(2);
  387. $this->adapter->setResponse($this->httpEntrySample);
  388. $entry = new Zend_Gdata_App_Entry();
  389. $entry->setEtag($etag);
  390. $newEntry = $this->service->updateEntry($entry,
  391. 'http://www.example.com',
  392. 'Zend_Gdata_App_Entry',
  393. array('If-None-Match' => $etagOverride));
  394. $headers = $this->adapter->popRequest()->headers;
  395. $found = false;
  396. foreach ($headers as $header) {
  397. if ($header == 'If-None-Match: ' . $etagOverride)
  398. $found = true;
  399. }
  400. $this->assertTrue($found, 'If-None-Match header not found or incorrect');
  401. }
  402. public function testGenerateIfMatchHeaderDataReturnsEtagIfV2() {
  403. $etag = 'ABCD1234';
  404. $this->service->setMajorProtocolVersion(2);
  405. $entry = new Zend_Gdata_App_Entry();
  406. $entry->setEtag($etag);
  407. $result = $this->service->generateIfMatchHeaderData($entry, false);
  408. $this->assertEquals($etag, $result);
  409. }
  410. public function testGenerateIfMatchHeaderDataReturnsNullIfV1() {
  411. $etag = 'ABCD1234';
  412. $this->service->setMajorProtocolVersion(1);
  413. $entry = new Zend_Gdata_App_Entry();
  414. $entry->setEtag($etag);
  415. $result = $this->service->generateIfMatchHeaderData($entry, false);
  416. $this->assertEquals(null, $result);
  417. }
  418. public function testGenerateIfMatchHeaderDataReturnsNullIfNotEntry() {
  419. $this->service->setMajorProtocolVersion(2);
  420. $result = $this->service->generateIfMatchHeaderData("Hello world", false);
  421. $this->assertEquals(null, $result);
  422. }
  423. public function testGenerateIfMatchHeaderDataReturnsNullIfWeak() {
  424. $etag = 'W/ABCD1234';
  425. $this->service->setMajorProtocolVersion(2);
  426. $entry = new Zend_Gdata_App_Entry();
  427. $entry->setEtag($etag);
  428. $result = $this->service->generateIfMatchHeaderData($entry, false);
  429. $this->assertEquals(null, $result);
  430. }
  431. public function testGenerateIfMatchHeaderDataReturnsEtagIfWeakAndFlagSet() {
  432. $etag = 'W/ABCD1234';
  433. $this->service->setMajorProtocolVersion(2);
  434. $entry = new Zend_Gdata_App_Entry();
  435. $entry->setEtag($etag);
  436. $result = $this->service->generateIfMatchHeaderData($entry, true);
  437. $this->assertEquals($etag, $result);
  438. }
  439. public function testGenerateIfMatchHeaderDataReturnsEtagIfNotWeakAndFlagSet() {
  440. $etag = 'ABCD1234';
  441. $this->service->setMajorProtocolVersion(2);
  442. $entry = new Zend_Gdata_App_Entry();
  443. $entry->setEtag($etag);
  444. $result = $this->service->generateIfMatchHeaderData($entry, true);
  445. $this->assertEquals($etag, $result);
  446. }
  447. public function testImportUrlSetsMajorProtocolVersionOnEntry() {
  448. $this->adapter->setResponse($this->httpEntrySample);
  449. $entry = $this->service->getEntry('http://www.example.com');
  450. $this->assertEquals($this->expectedMajorProtocolVersion, $entry->getMajorProtocolVersion());
  451. }
  452. public function testImportUrlSetsMinorProtocolVersionOnEntry() {
  453. $this->adapter->setResponse($this->httpEntrySample);
  454. $entry = $this->service->getEntry('http://www.example.com');
  455. $this->assertEquals($this->expectedMinorProtocolVersion, $entry->getMinorProtocolVersion());
  456. }
  457. public function testImportUrlSetsNullVersionIfNoVersionHeaderOnEntry() {
  458. $this->adapter->setResponse($this->httpEntrySampleWithoutVersion);
  459. $entry = $this->service->getEntry('http://www.example.com');
  460. $this->assertEquals(null, $entry->getMinorProtocolVersion());
  461. $this->assertEquals(null, $entry->getMinorProtocolVersion());
  462. }
  463. public function testImportUrlSetsMajorProtocolVersionOnFeed() {
  464. $this->adapter->setResponse($this->httpFeedSample);
  465. $feed = $this->service->getFeed('http://www.example.com');
  466. $this->assertEquals($this->expectedMajorProtocolVersion, $feed->getMajorProtocolVersion());
  467. foreach ($feed as $entry) {
  468. $this->assertEquals($this->expectedMajorProtocolVersion, $entry->getMajorProtocolVersion());
  469. }
  470. }
  471. public function testImportUrlSetsMinorProtocolVersionOnFeed() {
  472. $this->adapter->setResponse($this->httpFeedSample);
  473. $feed = $this->service->getFeed('http://www.example.com');
  474. $this->assertEquals($this->expectedMinorProtocolVersion, $feed->getMinorProtocolVersion());
  475. foreach ($feed as $entry) {
  476. $this->assertEquals($this->expectedMinorProtocolVersion, $entry->getMinorProtocolVersion());
  477. }
  478. }
  479. public function testImportUrlSetsNullVersionIfNoVersionHeaderOnFeed() {
  480. $this->adapter->setResponse($this->httpFeedSampleWithoutVersion);
  481. $feed = $this->service->getFeed('http://www.example.com');
  482. $this->assertEquals(null, $feed->getMajorProtocolVersion());
  483. $this->assertEquals(null, $feed->getMinorProtocolVersion());
  484. foreach ($feed as $entry) {
  485. $this->assertEquals(null, $entry->getMajorProtocolVersion());
  486. $this->assertEquals(null, $entry->getMinorProtocolVersion());
  487. }
  488. }
  489. public function testMagicConstructorsPropogateMajorVersion() {
  490. $v = 42;
  491. $this->service->setMajorProtocolVersion($v);
  492. $feed = $this->service->newFeed();
  493. $this->assertEquals($v, $feed->getMajorProtocolVersion());
  494. }
  495. public function testMagicConstructorsPropogateMinorVersion() {
  496. $v = 84;
  497. $this->service->setMinorProtocolVersion($v);
  498. $feed = $this->service->newFeed();
  499. $this->assertEquals($v, $feed->getMinorProtocolVersion());
  500. }
  501. }