PageRenderTime 26ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Zend/Rest/ClientTest.php

https://github.com/stefanklug/Zend-Framework-1.x-Mirror
PHP | 323 lines | 244 code | 38 blank | 41 comment | 0 complexity | 824618abdbd45f1c1eef56198acb42d9 MD5 | raw file
  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: ClientTest.php 23966 2011-05-03 14:30:07Z ralph $
  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/');
  197. $this->assertTrue($response instanceof Zend_Http_Response);
  198. $body = $response->getBody();
  199. $this->assertContains($expXml, $response->getBody());
  200. }
  201. public function testCallWithHttpMethod()
  202. {
  203. $expXml = file_get_contents($this->path . 'returnString.xml');
  204. $response = "HTTP/1.0 200 OK\r\n"
  205. . "X-powered-by: PHP/5.2.0\r\n"
  206. . "Content-type: text/xml\r\n"
  207. . "Content-length: " . strlen($expXml) . "\r\n"
  208. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  209. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  210. . "Connection: close\r\n"
  211. . "\r\n"
  212. . $expXml;
  213. $this->adapter->setResponse($response);
  214. $response = $this->rest->get('/rest/');
  215. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  216. $this->assertTrue($response->isSuccess());
  217. $this->assertEquals('string', $response->response());
  218. }
  219. public function testCallAsObjectMethodReturnsClient()
  220. {
  221. $expXml = file_get_contents($this->path . 'returnString.xml');
  222. $response = "HTTP/1.0 200 OK\r\n"
  223. . "X-powered-by: PHP/5.2.0\r\n"
  224. . "Content-type: text/xml\r\n"
  225. . "Content-length: " . strlen($expXml) . "\r\n"
  226. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  227. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  228. . "Connection: close\r\n"
  229. . "\r\n"
  230. . $expXml;
  231. $this->adapter->setResponse($response);
  232. $response = $this->rest->doStuff('why', 'not');
  233. $this->assertTrue($response instanceof Zend_Rest_Client);
  234. $this->assertSame($this->rest, $response);
  235. }
  236. public function testCallAsObjectMethodChainPerformsRequest()
  237. {
  238. $expXml = file_get_contents($this->path . 'returnString.xml');
  239. $response = "HTTP/1.0 200 OK\r\n"
  240. . "X-powered-by: PHP/5.2.0\r\n"
  241. . "Content-type: text/xml\r\n"
  242. . "Content-length: " . strlen($expXml) . "\r\n"
  243. . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n"
  244. . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n"
  245. . "Connection: close\r\n"
  246. . "\r\n"
  247. . $expXml;
  248. $this->adapter->setResponse($response);
  249. $response = $this->rest->doStuff('why', 'not')->get();
  250. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  251. $this->assertEquals('string', $response->response());
  252. }
  253. /**
  254. * @group ZF-3705
  255. * @group ZF-3647
  256. */
  257. public function testInvalidXmlInClientResultLeadsToException()
  258. {
  259. try {
  260. $result = new Zend_Rest_Client_Result("invalidxml");
  261. $this->fail();
  262. } catch(Zend_Rest_Client_Result_Exception $e) {
  263. }
  264. }
  265. /**
  266. * @group ZF-11281
  267. */
  268. public function testCallStatusGetterOnResponseObjectWhenServerResponseHasNoStatusXmlElement()
  269. {
  270. $expXml = file_get_contents($this->path . 'returnEmptyStatus.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->get('/rest/');
  282. $this->assertTrue($response instanceof Zend_Rest_Client_Result);
  283. $this->assertFalse($response->getStatus());
  284. }
  285. }