ClientTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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-2009 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. require_once dirname(__FILE__)."/../../TestHelper.php";
  23. /** Zend_Rest_Client */
  24. require_once 'Zend/Rest/Client.php';
  25. /** Zend_Http_Client_Adapter_Test */
  26. require_once 'Zend/Http/Client/Adapter/Test.php';
  27. /** PHPUnit Test Case */
  28. require_once 'PHPUnit/Framework/TestCase.php';
  29. /**
  30. * Test cases for Zend_Rest_Client
  31. *
  32. * @category Zend
  33. * @package Zend_Rest
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Rest
  38. * @group Zend_Rest_Client
  39. */
  40. class Zend_Rest_ClientTest extends PHPUnit_Framework_TestCase
  41. {
  42. public function setUp()
  43. {
  44. $this->path = dirname(__FILE__) . '/responses/';
  45. $this->adapter = new Zend_Http_Client_Adapter_Test();
  46. $client = new Zend_Http_Client(null, array(
  47. 'adapter' => $this->adapter
  48. ));
  49. Zend_Rest_Client::setHttpClient($client);
  50. $this->rest = new Zend_Rest_Client('http://framework.zend.com/');
  51. }
  52. public function testUri()
  53. {
  54. $client = new Zend_Rest_Client('http://framework.zend.com/rest/');
  55. $uri = $client->getUri();
  56. $this->assertTrue($uri instanceof Zend_Uri_Http);
  57. $this->assertEquals('http://framework.zend.com/rest/', $uri->getUri());
  58. $client->setUri(Zend_Uri::factory('http://framework.zend.com/soap/'));
  59. $uri = $client->getUri();
  60. $this->assertTrue($uri instanceof Zend_Uri_Http);
  61. $this->assertEquals('http://framework.zend.com/soap/', $uri->getUri());
  62. $client->setUri('http://framework.zend.com/xmlrpc/');
  63. $uri = $client->getUri();
  64. $this->assertTrue($uri instanceof Zend_Uri_Http);
  65. $this->assertEquals('http://framework.zend.com/xmlrpc/', $uri->getUri());
  66. }
  67. public function testRestGetThrowsExceptionWithNoUri()
  68. {
  69. $expXml = file_get_contents($this->path . 'returnString.xml');
  70. $response = "HTTP/1.0 200 OK\r\n"
  71. . "X-powered-by: PHP/5.2.0\r\n"
  72. . "Content-type: text/xml\r\n"
  73. . "Content-length: " . strlen($expXml) . "\r\n"
  74. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  75. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  76. . "Connection: close\r\n"
  77. . "\r\n"
  78. . $expXml;
  79. $this->adapter->setResponse($response);
  80. $rest = new Zend_Rest_Client();
  81. try {
  82. $response = $rest->restGet('/rest/');
  83. $this->fail('Should throw exception if no URI in object');
  84. } catch (Exception $e) {
  85. // success
  86. }
  87. }
  88. public function testRestFixesPathWithMissingSlashes()
  89. {
  90. $expXml = file_get_contents($this->path . 'returnString.xml');
  91. $response = "HTTP/1.0 200 OK\r\n"
  92. . "X-powered-by: PHP/5.2.0\r\n"
  93. . "Content-type: text/xml\r\n"
  94. . "Content-length: " . strlen($expXml) . "\r\n"
  95. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  96. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  97. . "Connection: close\r\n"
  98. . "\r\n"
  99. . $expXml;
  100. $this->adapter->setResponse($response);
  101. $rest = new Zend_Rest_Client('http://framework.zend.com');
  102. $response = $rest->restGet('rest');
  103. $this->assertTrue($response instanceof Zend_Http_Response);
  104. $this->assertContains($expXml, $response->getBody());
  105. }
  106. public function testRestGet()
  107. {
  108. $expXml = file_get_contents($this->path . 'returnString.xml');
  109. $response = "HTTP/1.0 200 OK\r\n"
  110. . "X-powered-by: PHP/5.2.0\r\n"
  111. . "Content-type: text/xml\r\n"
  112. . "Content-length: " . strlen($expXml) . "\r\n"
  113. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  114. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  115. . "Connection: close\r\n"
  116. . "\r\n"
  117. . $expXml;
  118. $this->adapter->setResponse($response);
  119. $response = $this->rest->restGet('/rest/');
  120. $this->assertTrue($response instanceof Zend_Http_Response);
  121. $this->assertContains($expXml, $response->getBody());
  122. }
  123. public function testRestPost()
  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. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  137. $response = $this->rest->restPost('/rest/', $reqXml);
  138. $this->assertTrue($response instanceof Zend_Http_Response);
  139. $body = $response->getBody();
  140. $this->assertContains($expXml, $response->getBody());
  141. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  142. $this->assertContains($reqXml, $request, $request);
  143. }
  144. public function testRestPostWithArrayData()
  145. {
  146. $expXml = file_get_contents($this->path . 'returnString.xml');
  147. $response = "HTTP/1.0 200 OK\r\n"
  148. . "X-powered-by: PHP/5.2.0\r\n"
  149. . "Content-type: text/xml\r\n"
  150. . "Content-length: " . strlen($expXml) . "\r\n"
  151. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  152. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  153. . "Connection: close\r\n"
  154. . "\r\n"
  155. . $expXml;
  156. $this->adapter->setResponse($response);
  157. $response = $this->rest->restPost('/rest/', array('foo' => 'bar', 'baz' => 'bat'));
  158. $this->assertTrue($response instanceof Zend_Http_Response);
  159. $body = $response->getBody();
  160. $this->assertContains($expXml, $response->getBody());
  161. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  162. $this->assertContains('foo=bar&baz=bat', $request, $request);
  163. }
  164. public function testRestPut()
  165. {
  166. $expXml = file_get_contents($this->path . 'returnString.xml');
  167. $response = "HTTP/1.0 200 OK\r\n"
  168. . "X-powered-by: PHP/5.2.0\r\n"
  169. . "Content-type: text/xml\r\n"
  170. . "Content-length: " . strlen($expXml) . "\r\n"
  171. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  172. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  173. . "Connection: close\r\n"
  174. . "\r\n"
  175. . $expXml;
  176. $this->adapter->setResponse($response);
  177. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  178. $response = $this->rest->restPut('/rest/', $reqXml);
  179. $this->assertTrue($response instanceof Zend_Http_Response);
  180. $body = $response->getBody();
  181. $this->assertContains($expXml, $response->getBody());
  182. $request = Zend_Rest_Client::getHttpClient()->getLastRequest();
  183. $this->assertContains($reqXml, $request, $request);
  184. }
  185. public function testRestDelete()
  186. {
  187. $expXml = file_get_contents($this->path . 'returnString.xml');
  188. $response = "HTTP/1.0 200 OK\r\n"
  189. . "X-powered-by: PHP/5.2.0\r\n"
  190. . "Content-type: text/xml\r\n"
  191. . "Content-length: " . strlen($expXml) . "\r\n"
  192. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  193. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  194. . "Connection: close\r\n"
  195. . "\r\n"
  196. . $expXml;
  197. $this->adapter->setResponse($response);
  198. $reqXml = file_get_contents($this->path . 'returnInt.xml');
  199. $response = $this->rest->restDelete('/rest/');
  200. $this->assertTrue($response instanceof Zend_Http_Response);
  201. $body = $response->getBody();
  202. $this->assertContains($expXml, $response->getBody());
  203. }
  204. public function testCallWithHttpMethod()
  205. {
  206. $expXml = file_get_contents($this->path . 'returnString.xml');
  207. $response = "HTTP/1.0 200 OK\r\n"
  208. . "X-powered-by: PHP/5.2.0\r\n"
  209. . "Content-type: text/xml\r\n"
  210. . "Content-length: " . strlen($expXml) . "\r\n"
  211. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  212. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  213. . "Connection: close\r\n"
  214. . "\r\n"
  215. . $expXml;
  216. $this->adapter->setResponse($response);
  217. $response = $this->rest->get('/rest/');
  218. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  219. $this->assertTrue($response->isSuccess());
  220. $this->assertEquals('string', $response->response());
  221. }
  222. public function testCallAsObjectMethodReturnsClient()
  223. {
  224. $expXml = file_get_contents($this->path . 'returnString.xml');
  225. $response = "HTTP/1.0 200 OK\r\n"
  226. . "X-powered-by: PHP/5.2.0\r\n"
  227. . "Content-type: text/xml\r\n"
  228. . "Content-length: " . strlen($expXml) . "\r\n"
  229. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  230. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  231. . "Connection: close\r\n"
  232. . "\r\n"
  233. . $expXml;
  234. $this->adapter->setResponse($response);
  235. $response = $this->rest->doStuff('why', 'not');
  236. $this->assertTrue($response instanceof Zend_Rest_Client);
  237. $this->assertSame($this->rest, $response);
  238. }
  239. public function testCallAsObjectMethodChainPerformsRequest()
  240. {
  241. $expXml = file_get_contents($this->path . 'returnString.xml');
  242. $response = "HTTP/1.0 200 OK\r\n"
  243. . "X-powered-by: PHP/5.2.0\r\n"
  244. . "Content-type: text/xml\r\n"
  245. . "Content-length: " . strlen($expXml) . "\r\n"
  246. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  247. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  248. . "Connection: close\r\n"
  249. . "\r\n"
  250. . $expXml;
  251. $this->adapter->setResponse($response);
  252. $response = $this->rest->doStuff('why', 'not')->get();
  253. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  254. $this->assertEquals('string', $response->response());
  255. }
  256. /**
  257. * @group ZF-3705
  258. * @group ZF-3647
  259. */
  260. public function testInvalidXmlInClientResultLeadsToException()
  261. {
  262. try {
  263. $result = new Zend_Rest_Client_Result("invalidxml");
  264. $this->fail();
  265. } catch(Zend_Rest_Client_Result_Exception $e) {
  266. }
  267. }
  268. }