ClientTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-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. /** 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-2012 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. $client = new Zend_Rest_Client('http://framework.zend.com');
  60. $httpClient = new Zend_Http_Client();
  61. $text = 'this is some plain text';
  62. $httpClient->setFileUpload('some_text.txt', 'upload', $text, 'text/plain');
  63. $client->setHttpClient($httpClient);
  64. $client->setNoReset();
  65. $client->restPost('/file');
  66. $request = $httpClient->getLastRequest();
  67. $this->assertTrue(strpos($request, $text) !== false, 'The file is not in the request');
  68. }
  69. public function testUri()
  70. {
  71. $client = new Zend_Rest_Client('http://framework.zend.com/rest/');
  72. $uri = $client->getUri();
  73. $this->assertTrue($uri instanceof Zend_Uri_Http);
  74. $this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
  75. $client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
  76. $uri = $client->getUri();
  77. $this->assertTrue($uri instanceof Zend_Uri_Http);
  78. $this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
  79. $client->setUri('http://framework.zend.com/xmlrpc/');
  80. $uri = $client->getUri();
  81. $this->assertTrue($uri instanceof Zend_Uri_Http);
  82. $this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
  83. }
  84. public function testRestGetThrowsExceptionWithNoUri()
  85. {
  86. $expXml = file_get_contents($this->path . 'returnString.xml');
  87. $response = "HTTP/1.0 200 OK\r\n"
  88. . "X-powered-by: PHP/5.2.0\r\n"
  89. . "Content-type: text/xml\r\n"
  90. . "Content-length: " . strlen($expXml) . "\r\n"
  91. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  92. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  93. . "Connection: close\r\n"
  94. . "\r\n"
  95. . $expXml;
  96. $this->adapter->setResponse($response);
  97. $rest = new Zend_Rest_Client();
  98. try {
  99. $response = $rest->restGet('/rest/');
  100. $this->fail('Should throw exception if no URI in object');
  101. } catch (Exception $e) {
  102. // success
  103. }
  104. }
  105. public function testRestFixesPathWithMissingSlashes()
  106. {
  107. $expXml = file_get_contents($this->path . 'returnString.xml');
  108. $response = "HTTP/1.0 200 OK\r\n"
  109. . "X-powered-by: PHP/5.2.0\r\n"
  110. . "Content-type: text/xml\r\n"
  111. . "Content-length: " . strlen($expXml) . "\r\n"
  112. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  113. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  114. . "Connection: close\r\n"
  115. . "\r\n"
  116. . $expXml;
  117. $this->adapter->setResponse($response);
  118. $rest = new Zend_Rest_Client('http://framework.zend.com');
  119. $response = $rest->restGet('rest');
  120. $this->assertTrue($response instanceof Zend_Http_Response);
  121. $this->assertContains($expXml, $response->getBody());
  122. }
  123. public function testRestGet()
  124. {
  125. $expXml = file_get_contents($this->path . 'returnString.xml');
  126. $response = "HTTP/1.0 200 OK\r\n"
  127. . "X-powered-by: PHP/5.2.0\r\n"
  128. . "Content-type: text/xml\r\n"
  129. . "Content-length: " . strlen($expXml) . "\r\n"
  130. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  131. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  132. . "Connection: close\r\n"
  133. . "\r\n"
  134. . $expXml;
  135. $this->adapter->setResponse($response);
  136. $response = $this->rest->restGet('/rest/');
  137. $this->assertTrue($response instanceof Zend_Http_Response);
  138. $this->assertContains($expXml, $response->getBody());
  139. }
  140. public function testRestPost()
  141. {
  142. $expXml = file_get_contents($this->path . 'returnString.xml');
  143. $response = "HTTP/1.0 200 OK\r\n"
  144. . "X-powered-by: PHP/5.2.0\r\n"
  145. . "Content-type: text/xml\r\n"
  146. . "Content-length: " . strlen($expXml) . "\r\n"
  147. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  148. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  149. . "Connection: close\r\n"
  150. . "\r\n"
  151. . $expXml;
  152. $this->adapter->setResponse($response);
  153. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  154. $response = $this->rest->restPost('/rest/', $reqXml);
  155. $this->assertTrue($response instanceof Zend_Http_Response);
  156. $body = $response->getBody();
  157. $this->assertContains($expXml, $response->getBody());
  158. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  159. $this->assertContains($reqXml, $request, $request);
  160. }
  161. public function testRestPostWithArrayData()
  162. {
  163. $expXml = file_get_contents($this->path . 'returnString.xml');
  164. $response = "HTTP/1.0 200 OK\r\n"
  165. . "X-powered-by: PHP/5.2.0\r\n"
  166. . "Content-type: text/xml\r\n"
  167. . "Content-length: " . strlen($expXml) . "\r\n"
  168. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  169. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  170. . "Connection: close\r\n"
  171. . "\r\n"
  172. . $expXml;
  173. $this->adapter->setResponse($response);
  174. $response = $this->rest->restPost('/rest/', array('foo' => 'bar', 'baz' => 'bat'));
  175. $this->assertTrue($response instanceof Zend_Http_Response);
  176. $body = $response->getBody();
  177. $this->assertContains($expXml, $response->getBody());
  178. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  179. $this->assertContains('foo=bar&baz=bat', $request, $request);
  180. }
  181. public function testRestPut()
  182. {
  183. $expXml = file_get_contents($this->path . 'returnString.xml');
  184. $response = "HTTP/1.0 200 OK\r\n"
  185. . "X-powered-by: PHP/5.2.0\r\n"
  186. . "Content-type: text/xml\r\n"
  187. . "Content-length: " . strlen($expXml) . "\r\n"
  188. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  189. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  190. . "Connection: close\r\n"
  191. . "\r\n"
  192. . $expXml;
  193. $this->adapter->setResponse($response);
  194. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  195. $response = $this->rest->restPut('/rest/', $reqXml);
  196. $this->assertTrue($response instanceof Zend_Http_Response);
  197. $body = $response->getBody();
  198. $this->assertContains($expXml, $response->getBody());
  199. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  200. $this->assertContains($reqXml, $request, $request);
  201. }
  202. public function testRestDelete()
  203. {
  204. $expXml = file_get_contents($this->path . 'returnString.xml');
  205. $response = "HTTP/1.0 200 OK\r\n"
  206. . "X-powered-by: PHP/5.2.0\r\n"
  207. . "Content-type: text/xml\r\n"
  208. . "Content-length: " . strlen($expXml) . "\r\n"
  209. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  210. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  211. . "Connection: close\r\n"
  212. . "\r\n"
  213. . $expXml;
  214. $this->adapter->setResponse($response);
  215. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  216. $response = $this->rest->restDelete('/rest/', $reqXml);
  217. $this->assertTrue($response instanceof Zend_Http_Response);
  218. $body = $response->getBody();
  219. $this->assertContains($expXml, $response->getBody());
  220. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  221. $this->assertContains($reqXml, $request, $request);
  222. }
  223. public function testCallWithHttpMethod()
  224. {
  225. $expXml = file_get_contents($this->path . 'returnString.xml');
  226. $response = "HTTP/1.0 200 OK\r\n"
  227. . "X-powered-by: PHP/5.2.0\r\n"
  228. . "Content-type: text/xml\r\n"
  229. . "Content-length: " . strlen($expXml) . "\r\n"
  230. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  231. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  232. . "Connection: close\r\n"
  233. . "\r\n"
  234. . $expXml;
  235. $this->adapter->setResponse($response);
  236. $response = $this->rest->get('/rest/');
  237. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  238. $this->assertTrue($response->isSuccess());
  239. $this->assertEquals('string', $response->response());
  240. }
  241. public function testCallAsObjectMethodReturnsClient()
  242. {
  243. $expXml = file_get_contents($this->path . 'returnString.xml');
  244. $response = "HTTP/1.0 200 OK\r\n"
  245. . "X-powered-by: PHP/5.2.0\r\n"
  246. . "Content-type: text/xml\r\n"
  247. . "Content-length: " . strlen($expXml) . "\r\n"
  248. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  249. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  250. . "Connection: close\r\n"
  251. . "\r\n"
  252. . $expXml;
  253. $this->adapter->setResponse($response);
  254. $response = $this->rest->doStuff('why', 'not');
  255. $this->assertTrue($response instanceof Zend_Rest_Client);
  256. $this->assertSame($this->rest, $response);
  257. }
  258. public function testCallAsObjectMethodChainPerformsRequest()
  259. {
  260. $expXml = file_get_contents($this->path . 'returnString.xml');
  261. $response = "HTTP/1.0 200 OK\r\n"
  262. . "X-powered-by: PHP/5.2.0\r\n"
  263. . "Content-type: text/xml\r\n"
  264. . "Content-length: " . strlen($expXml) . "\r\n"
  265. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  266. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  267. . "Connection: close\r\n"
  268. . "\r\n"
  269. . $expXml;
  270. $this->adapter->setResponse($response);
  271. $response = $this->rest->doStuff('why', 'not')->get();
  272. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  273. $this->assertEquals('string', $response->response());
  274. }
  275. /**
  276. * @group ZF-3705
  277. * @group ZF-3647
  278. */
  279. public function testInvalidXmlInClientResultLeadsToException()
  280. {
  281. try {
  282. $result = new Zend_Rest_Client_Result("invalidxml");
  283. $this->fail();
  284. } catch(Zend_Rest_Client_Result_Exception $e) {
  285. }
  286. }
  287. /**
  288. * @group ZF-11281
  289. */
  290. public function testCallStatusGetterOnResponseObjectWhenServerResponseHasNoStatusXmlElement()
  291. {
  292. $expXml = file_get_contents($this->path . 'returnEmptyStatus.xml');
  293. $response = "HTTP/1.0 200 OK\r\n"
  294. . "X-powered-by: PHP/5.2.0\r\n"
  295. . "Content-type: text/xml\r\n"
  296. . "Content-length: " . strlen($expXml) . "\r\n"
  297. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  298. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  299. . "Connection: close\r\n"
  300. . "\r\n"
  301. . $expXml;
  302. $this->adapter->setResponse($response);
  303. $response = $this->rest->get('/rest/');
  304. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  305. $this->assertFalse($response->getStatus());
  306. }
  307. }