ClientTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_Rest
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. /** Zend_Rest_Client */
  23. require_once 'Zend/Rest/Client.php';
  24. /** Zend_Http_Client_Adapter_Test */
  25. require_once 'Zend/Http/Client/Adapter/Test.php';
  26. /**
  27. * Test cases for Zend_Rest_Client
  28. *
  29. * @category Zend
  30. * @package Zend_Rest
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Rest
  35. * @group Zend_Rest_Client
  36. */
  37. class Zend_Rest_ClientTest extends PHPUnit_Framework_TestCase
  38. {
  39. public function setUp()
  40. {
  41. $this->path = dirname(__FILE__) . '/responses/';
  42. $this->adapter = new Zend_Http_Client_Adapter_Test();
  43. $client = new Zend_Http_Client(null, array(
  44. 'adapter' => $this->adapter
  45. ));
  46. Zend_Rest_Client::setHttpClient($client);
  47. $this->rest = new Zend_Rest_Client('http://framework.zend.com/');
  48. }
  49. /**
  50. * @group ZF-10664
  51. *
  52. * Test that you can post a file using a preset
  53. * Zend_Http_Client that has a file to post,
  54. * by calling $restClient->setNoReset() prior to issuing the
  55. * restPost() call.
  56. */
  57. public function testCanPostFileInPresetHttpClient()
  58. {
  59. if (!defined('TESTS_ZEND_REST_ONLINE_ENABLED')
  60. || !constant('TESTS_ZEND_REST_ONLINE_ENABLED')
  61. ) {
  62. $this->markTestSkipped('Define TESTS_ZEND_REST_ONLINE_ENABLED to test Zend_Rest_ClientTest online.');
  63. }
  64. $client = new Zend_Rest_Client('http://framework.zend.com');
  65. $httpClient = new Zend_Http_Client();
  66. $text = 'this is some plain text';
  67. $httpClient->setFileUpload('some_text.txt', 'upload', $text, 'text/plain');
  68. $client->setHttpClient($httpClient);
  69. $client->setNoReset();
  70. $client->restPost('/file');
  71. $request = $httpClient->getLastRequest();
  72. $this->assertTrue(strpos($request, $text) !== false, 'The file is not in the request');
  73. }
  74. public function testUri()
  75. {
  76. if (!defined('TESTS_ZEND_REST_ONLINE_ENABLED')
  77. || !constant('TESTS_ZEND_REST_ONLINE_ENABLED')
  78. ) {
  79. $this->markTestSkipped('Define TESTS_ZEND_REST_ONLINE_ENABLED to test Zend_Rest_ClientTest online.');
  80. }
  81. $client = new Zend_Rest_Client('http://framework.zend.com/rest/');
  82. $uri = $client->getUri();
  83. $this->assertTrue($uri instanceof Zend_Uri_Http);
  84. $this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
  85. $client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
  86. $uri = $client->getUri();
  87. $this->assertTrue($uri instanceof Zend_Uri_Http);
  88. $this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
  89. $client->setUri('http://framework.zend.com/xmlrpc/');
  90. $uri = $client->getUri();
  91. $this->assertTrue($uri instanceof Zend_Uri_Http);
  92. $this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
  93. }
  94. public function testRestGetThrowsExceptionWithNoUri()
  95. {
  96. $expXml = file_get_contents($this->path . 'returnString.xml');
  97. $response = "HTTP/1.0 200 OK\r\n"
  98. . "X-powered-by: PHP/5.2.0\r\n"
  99. . "Content-type: text/xml\r\n"
  100. . "Content-length: " . strlen($expXml) . "\r\n"
  101. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  102. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  103. . "Connection: close\r\n"
  104. . "\r\n"
  105. . $expXml;
  106. $this->adapter->setResponse($response);
  107. $rest = new Zend_Rest_Client();
  108. try {
  109. $response = $rest->restGet('/rest/');
  110. $this->fail('Should throw exception if no URI in object');
  111. } catch (Exception $e) {
  112. // success
  113. }
  114. }
  115. public function testRestFixesPathWithMissingSlashes()
  116. {
  117. $expXml = file_get_contents($this->path . 'returnString.xml');
  118. $response = "HTTP/1.0 200 OK\r\n"
  119. . "X-powered-by: PHP/5.2.0\r\n"
  120. . "Content-type: text/xml\r\n"
  121. . "Content-length: " . strlen($expXml) . "\r\n"
  122. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  123. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  124. . "Connection: close\r\n"
  125. . "\r\n"
  126. . $expXml;
  127. $this->adapter->setResponse($response);
  128. $rest = new Zend_Rest_Client('http://framework.zend.com');
  129. $response = $rest->restGet('rest');
  130. $this->assertTrue($response instanceof Zend_Http_Response);
  131. $this->assertContains($expXml, $response->getBody());
  132. }
  133. public function testRestGet()
  134. {
  135. $expXml = file_get_contents($this->path . 'returnString.xml');
  136. $response = "HTTP/1.0 200 OK\r\n"
  137. . "X-powered-by: PHP/5.2.0\r\n"
  138. . "Content-type: text/xml\r\n"
  139. . "Content-length: " . strlen($expXml) . "\r\n"
  140. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  141. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  142. . "Connection: close\r\n"
  143. . "\r\n"
  144. . $expXml;
  145. $this->adapter->setResponse($response);
  146. $response = $this->rest->restGet('/rest/');
  147. $this->assertTrue($response instanceof Zend_Http_Response);
  148. $this->assertContains($expXml, $response->getBody());
  149. }
  150. public function testRestPost()
  151. {
  152. $expXml = file_get_contents($this->path . 'returnString.xml');
  153. $response = "HTTP/1.0 200 OK\r\n"
  154. . "X-powered-by: PHP/5.2.0\r\n"
  155. . "Content-type: text/xml\r\n"
  156. . "Content-length: " . strlen($expXml) . "\r\n"
  157. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  158. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  159. . "Connection: close\r\n"
  160. . "\r\n"
  161. . $expXml;
  162. $this->adapter->setResponse($response);
  163. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  164. $response = $this->rest->restPost('/rest/', $reqXml);
  165. $this->assertTrue($response instanceof Zend_Http_Response);
  166. $body = $response->getBody();
  167. $this->assertContains($expXml, $response->getBody());
  168. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  169. $this->assertContains($reqXml, $request, $request);
  170. }
  171. public function testRestPostWithArrayData()
  172. {
  173. $expXml = file_get_contents($this->path . 'returnString.xml');
  174. $response = "HTTP/1.0 200 OK\r\n"
  175. . "X-powered-by: PHP/5.2.0\r\n"
  176. . "Content-type: text/xml\r\n"
  177. . "Content-length: " . strlen($expXml) . "\r\n"
  178. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  179. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  180. . "Connection: close\r\n"
  181. . "\r\n"
  182. . $expXml;
  183. $this->adapter->setResponse($response);
  184. $response = $this->rest->restPost('/rest/', array('foo' => 'bar', 'baz' => 'bat'));
  185. $this->assertTrue($response instanceof Zend_Http_Response);
  186. $body = $response->getBody();
  187. $this->assertContains($expXml, $response->getBody());
  188. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  189. $this->assertContains('foo=bar&baz=bat', $request, $request);
  190. }
  191. public function testRestPut()
  192. {
  193. $expXml = file_get_contents($this->path . 'returnString.xml');
  194. $response = "HTTP/1.0 200 OK\r\n"
  195. . "X-powered-by: PHP/5.2.0\r\n"
  196. . "Content-type: text/xml\r\n"
  197. . "Content-length: " . strlen($expXml) . "\r\n"
  198. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  199. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  200. . "Connection: close\r\n"
  201. . "\r\n"
  202. . $expXml;
  203. $this->adapter->setResponse($response);
  204. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  205. $response = $this->rest->restPut('/rest/', $reqXml);
  206. $this->assertTrue($response instanceof Zend_Http_Response);
  207. $body = $response->getBody();
  208. $this->assertContains($expXml, $response->getBody());
  209. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  210. $this->assertContains($reqXml, $request, $request);
  211. }
  212. public function testRestDelete()
  213. {
  214. $expXml = file_get_contents($this->path . 'returnString.xml');
  215. $response = "HTTP/1.0 200 OK\r\n"
  216. . "X-powered-by: PHP/5.2.0\r\n"
  217. . "Content-type: text/xml\r\n"
  218. . "Content-length: " . strlen($expXml) . "\r\n"
  219. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  220. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  221. . "Connection: close\r\n"
  222. . "\r\n"
  223. . $expXml;
  224. $this->adapter->setResponse($response);
  225. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  226. $response = $this->rest->restDelete('/rest/', $reqXml);
  227. $this->assertTrue($response instanceof Zend_Http_Response);
  228. $body = $response->getBody();
  229. $this->assertContains($expXml, $response->getBody());
  230. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  231. $this->assertContains($reqXml, $request, $request);
  232. }
  233. public function testCallWithHttpMethod()
  234. {
  235. $expXml = file_get_contents($this->path . 'returnString.xml');
  236. $response = "HTTP/1.0 200 OK\r\n"
  237. . "X-powered-by: PHP/5.2.0\r\n"
  238. . "Content-type: text/xml\r\n"
  239. . "Content-length: " . strlen($expXml) . "\r\n"
  240. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  241. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  242. . "Connection: close\r\n"
  243. . "\r\n"
  244. . $expXml;
  245. $this->adapter->setResponse($response);
  246. $response = $this->rest->get('/rest/');
  247. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  248. $this->assertTrue($response->isSuccess());
  249. $this->assertEquals('string', $response->response());
  250. }
  251. public function testCallAsObjectMethodReturnsClient()
  252. {
  253. $expXml = file_get_contents($this->path . 'returnString.xml');
  254. $response = "HTTP/1.0 200 OK\r\n"
  255. . "X-powered-by: PHP/5.2.0\r\n"
  256. . "Content-type: text/xml\r\n"
  257. . "Content-length: " . strlen($expXml) . "\r\n"
  258. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  259. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  260. . "Connection: close\r\n"
  261. . "\r\n"
  262. . $expXml;
  263. $this->adapter->setResponse($response);
  264. $response = $this->rest->doStuff('why', 'not');
  265. $this->assertTrue($response instanceof Zend_Rest_Client);
  266. $this->assertSame($this->rest, $response);
  267. }
  268. public function testCallAsObjectMethodChainPerformsRequest()
  269. {
  270. $expXml = file_get_contents($this->path . 'returnString.xml');
  271. $response = "HTTP/1.0 200 OK\r\n"
  272. . "X-powered-by: PHP/5.2.0\r\n"
  273. . "Content-type: text/xml\r\n"
  274. . "Content-length: " . strlen($expXml) . "\r\n"
  275. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  276. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  277. . "Connection: close\r\n"
  278. . "\r\n"
  279. . $expXml;
  280. $this->adapter->setResponse($response);
  281. $response = $this->rest->doStuff('why', 'not')->get();
  282. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  283. $this->assertEquals('string', $response->response());
  284. }
  285. /**
  286. * @group ZF-3705
  287. * @group ZF-3647
  288. */
  289. public function testInvalidXmlInClientResultLeadsToException()
  290. {
  291. try {
  292. $result = new Zend_Rest_Client_Result("invalidxml");
  293. $this->fail();
  294. } catch(Zend_Rest_Client_Result_Exception $e) {
  295. }
  296. }
  297. /**
  298. * @group ZF-11281
  299. */
  300. public function testCallStatusGetterOnResponseObjectWhenServerResponseHasNoStatusXmlElement()
  301. {
  302. $expXml = file_get_contents($this->path . 'returnEmptyStatus.xml');
  303. $response = "HTTP/1.0 200 OK\r\n"
  304. . "X-powered-by: PHP/5.2.0\r\n"
  305. . "Content-type: text/xml\r\n"
  306. . "Content-length: " . strlen($expXml) . "\r\n"
  307. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  308. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  309. . "Connection: close\r\n"
  310. . "\r\n"
  311. . $expXml;
  312. $this->adapter->setResponse($response);
  313. $response = $this->rest->get('/rest/');
  314. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  315. $this->assertFalse($response->getStatus());
  316. }
  317. }