ClientTest.php 13 KB

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