HttpTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\data\source;
  9. use lithium\data\source\Http;
  10. use lithium\data\model\Query;
  11. class HttpTest extends \lithium\test\Unit {
  12. protected $_model = 'lithium\tests\mocks\data\source\MockHttpModel';
  13. protected $_testConfig = array(
  14. 'classes' => array('response' => 'lithium\net\http\Response'),
  15. 'persistent' => false,
  16. 'scheme' => 'tcp',
  17. 'host' => 'localhost',
  18. 'login' => 'root',
  19. 'password' => '',
  20. 'port' => 80,
  21. 'timeout' => 2,
  22. 'socket' => 'lithium\tests\mocks\data\source\http\adapter\MockSocket'
  23. );
  24. protected $_connectionConfig = array(
  25. 'methods' => array(
  26. 'something' => array('method' => 'get'),
  27. 'do' => array('method' => 'post')
  28. )
  29. );
  30. public function setUp() {
  31. $model = $this->_model;
  32. $model::$connection = new Http($this->_connectionConfig);
  33. }
  34. public function testAllMethodsNoConnection() {
  35. $http = new Http(array('socket' => false));
  36. $this->assertTrue($http->connect());
  37. $this->assertTrue($http->disconnect());
  38. $this->assertFalse($http->get());
  39. $this->assertFalse($http->post());
  40. $this->assertFalse($http->put());
  41. }
  42. public function testConnect() {
  43. $http = new Http();
  44. $result = $http->connect();
  45. $this->assertTrue($result);
  46. }
  47. public function testDisconnect() {
  48. $http = new Http($this->_testConfig);
  49. $result = $http->connect();
  50. $this->assertTrue($result);
  51. $result = $http->disconnect();
  52. $this->assertTrue($result);
  53. }
  54. public function testSources() {
  55. $http = new Http($this->_testConfig);
  56. $result = $http->sources();
  57. }
  58. public function testDescribe() {
  59. $http = new Http($this->_testConfig);
  60. $result = $http->describe(null, array());
  61. }
  62. public function testCreate() {
  63. $http = new Http($this->_testConfig);
  64. $result = $http->create(null);
  65. $expected = join("\r\n", array(
  66. 'POST / HTTP/1.1',
  67. 'Host: localhost:80',
  68. 'Connection: Close',
  69. 'User-Agent: Mozilla/5.0',
  70. 'Content-Type: application/x-www-form-urlencoded',
  71. '', ''
  72. ));
  73. $result = (string) $http->last->request;
  74. $this->assertEqual($expected, $result);
  75. }
  76. public function testRead() {
  77. $http = new Http($this->_testConfig);
  78. $result = $http->read(null);
  79. $expected = join("\r\n", array(
  80. 'GET / HTTP/1.1',
  81. 'Host: localhost:80',
  82. 'Connection: Close',
  83. 'User-Agent: Mozilla/5.0',
  84. '', ''
  85. ));
  86. $result = (string) $http->last->request;
  87. $this->assertEqual($expected, $result);
  88. }
  89. public function testUpdate() {
  90. $http = new Http($this->_testConfig);
  91. $result = $http->update(null);
  92. $expected = join("\r\n", array(
  93. 'PUT / HTTP/1.1',
  94. 'Host: localhost:80',
  95. 'Connection: Close',
  96. 'User-Agent: Mozilla/5.0',
  97. 'Content-Type: application/x-www-form-urlencoded',
  98. '', ''
  99. ));
  100. $result = (string) $http->last->request;
  101. $this->assertEqual($expected, $result);
  102. }
  103. public function testDelete() {
  104. $http = new Http($this->_testConfig);
  105. $result = $http->delete(null);
  106. $expected = join("\r\n", array(
  107. 'DELETE / HTTP/1.1',
  108. 'Host: localhost:80',
  109. 'Connection: Close',
  110. 'User-Agent: Mozilla/5.0',
  111. '', ''
  112. ));
  113. $result = (string) $http->last->request;
  114. $this->assertEqual($expected, $result);
  115. }
  116. public function testCreateWithModel() {
  117. $model = $this->_model;
  118. $model::config(array('meta' => array('key' => 'id')));
  119. $http = new Http($this->_testConfig);
  120. $query = new Query(compact('model') + array('data' => array('title' => 'Test Title')));
  121. $result = $http->create($query);
  122. $expected = join("\r\n", array(
  123. 'POST /posts HTTP/1.1',
  124. 'Host: localhost:80',
  125. 'Connection: Close',
  126. 'User-Agent: Mozilla/5.0',
  127. 'Content-Type: application/x-www-form-urlencoded',
  128. 'Content-Length: 16',
  129. '', 'title=Test+Title'
  130. ));
  131. $result = (string) $http->last->request;
  132. $this->assertEqual($expected, $result);
  133. }
  134. public function testReadWithModel() {
  135. $http = new Http($this->_testConfig);
  136. $query = new Query(array('model' => $this->_model));
  137. $result = $http->read($query);
  138. $expected = join("\r\n", array(
  139. 'GET /posts HTTP/1.1',
  140. 'Host: localhost:80',
  141. 'Connection: Close',
  142. 'User-Agent: Mozilla/5.0',
  143. '', ''
  144. ));
  145. $result = (string) $http->last->request;
  146. $this->assertEqual($expected, $result);
  147. }
  148. public function testReadWithModelConditions() {
  149. $http = new Http($this->_testConfig);
  150. $query = new Query(array(
  151. 'model' => $this->_model,
  152. 'conditions' => array('page' => 2)
  153. ));
  154. $result = $http->read($query);
  155. $expected = join("\r\n", array(
  156. 'GET /posts?page=2 HTTP/1.1',
  157. 'Host: localhost:80',
  158. 'Connection: Close',
  159. 'User-Agent: Mozilla/5.0',
  160. '', ''
  161. ));
  162. $result = (string) $http->last->request;
  163. $this->assertEqual($expected, $result);
  164. }
  165. public function testUpdateWithModel() {
  166. $http = new Http($this->_testConfig);
  167. $query = new Query(array(
  168. 'model' => $this->_model,
  169. 'data' => array('id' => '1', 'title' => 'Test Title')
  170. ));
  171. $result = $http->update($query);
  172. $expected = join("\r\n", array(
  173. 'PUT /posts/1 HTTP/1.1',
  174. 'Host: localhost:80',
  175. 'Connection: Close',
  176. 'User-Agent: Mozilla/5.0',
  177. 'Content-Type: application/x-www-form-urlencoded',
  178. 'Content-Length: 16',
  179. '', 'title=Test+Title'
  180. ));
  181. $result = (string) $http->last->request;
  182. $this->assertEqual($expected, $result);
  183. }
  184. public function testDeleteWithModel() {
  185. $http = new Http($this->_testConfig);
  186. $query = new Query(array('model' => $this->_model, 'data' => array('id' => '1')));
  187. $result = $http->delete($query);
  188. $expected = join("\r\n", array(
  189. 'DELETE /posts/1 HTTP/1.1',
  190. 'Host: localhost:80',
  191. 'Connection: Close',
  192. 'User-Agent: Mozilla/5.0',
  193. '', ''
  194. ));
  195. $result = (string) $http->last->request;
  196. $this->assertEqual($expected, $result);
  197. }
  198. public function testCustomActionWithoutMethod() {
  199. $http = new Http($this->_testConfig);
  200. $result = $http->something();
  201. $expected = join("\r\n", array(
  202. 'GET /something HTTP/1.1',
  203. 'Host: localhost:80',
  204. 'Connection: Close',
  205. 'User-Agent: Mozilla/5.0',
  206. '', ''
  207. ));
  208. $result = (string) $http->last->request;
  209. $this->assertEqual($expected, $result);
  210. }
  211. public function testCustomGetMethod() {
  212. $config = $this->_testConfig + array('methods' => array(
  213. 'something' => array('method' => 'get', 'path' => '/something')
  214. ));
  215. $http = new Http($config);
  216. $result = $http->something();
  217. $expected = join("\r\n", array(
  218. 'GET /something HTTP/1.1',
  219. 'Host: localhost:80',
  220. 'Connection: Close',
  221. 'User-Agent: Mozilla/5.0',
  222. '', ''
  223. ));
  224. $result = (string) $http->last->request;
  225. $this->assertEqual($expected, $result);
  226. }
  227. public function testCustomGetMethodWithModel() {
  228. $config = $this->_testConfig + array('methods' => array(
  229. 'something' => array('method' => 'get', 'path' => '/something')
  230. ));
  231. $http = new Http($config);
  232. $query = new Query(array('model' => $this->_model));
  233. $result = $http->something($query);
  234. $expected = join("\r\n", array(
  235. 'GET /something HTTP/1.1',
  236. 'Host: localhost:80',
  237. 'Connection: Close',
  238. 'User-Agent: Mozilla/5.0',
  239. '', ''
  240. ));
  241. $result = (string) $http->last->request;
  242. $this->assertEqual($expected, $result);
  243. }
  244. public function testCustomPostMethod() {
  245. $config = $this->_testConfig + array('methods' => array(
  246. 'do' => array('method' => 'post', 'path' => '/do')
  247. ));
  248. $http = new Http($config);
  249. $result = $http->do(array('title' => 'sup'));
  250. $expected = join("\r\n", array(
  251. 'POST /do HTTP/1.1',
  252. 'Host: localhost:80',
  253. 'Connection: Close',
  254. 'User-Agent: Mozilla/5.0',
  255. 'Content-Type: application/x-www-form-urlencoded',
  256. 'Content-Length: 9',
  257. '', 'title=sup'
  258. ));
  259. $result = (string) $http->last->request;
  260. $this->assertEqual($expected, $result);
  261. }
  262. public function testCustomPostMethodWithModel() {
  263. $config = $this->_testConfig + array('methods' => array(
  264. 'do' => array('method' => 'post', 'path' => '/do')
  265. ));
  266. $http = new Http($config);
  267. $query = new Query(array('model' => $this->_model, 'data' => array('title' => 'sup')));
  268. $result = $http->do($query);
  269. $expected = join("\r\n", array(
  270. 'POST /do HTTP/1.1',
  271. 'Host: localhost:80',
  272. 'Connection: Close',
  273. 'User-Agent: Mozilla/5.0',
  274. 'Content-Type: application/x-www-form-urlencoded',
  275. 'Content-Length: 9',
  276. '', 'title=sup'
  277. ));
  278. $result = (string) $http->last->request;
  279. $this->assertEqual($expected, $result);
  280. }
  281. public function testSendWithQueryObject() {
  282. $http = new Http($this->_testConfig);
  283. $query = new Query(array(
  284. 'model' => $this->_model,
  285. 'data' => array('title' => 'sup'),
  286. 'method' => 'post',
  287. 'path' => '/some/resource/path'
  288. ));
  289. $result = $http->send($query);
  290. $expected = join("\r\n", array(
  291. 'POST /some/resource/path HTTP/1.1',
  292. 'Host: localhost:80',
  293. 'Connection: Close',
  294. 'User-Agent: Mozilla/5.0',
  295. 'Content-Type: application/x-www-form-urlencoded',
  296. 'Content-Length: 9',
  297. '', 'title=sup'
  298. ));
  299. $result = (string) $http->last->request;
  300. $this->assertEqual($expected, $result);
  301. }
  302. public function testRespondsTo() {
  303. $http = new Http();
  304. $this->assertFalse($http->respondsTo('refactor'));
  305. $this->assertTrue($http->respondsTo('create'));
  306. $this->assertTrue($http->respondsTo('read'));
  307. }
  308. public function testRespondsToParentCall() {
  309. $http = new Http();
  310. $this->assertTrue($http->respondsTo('applyFilter'));
  311. $this->assertFalse($http->respondsTo('fooBarBaz'));
  312. }
  313. }
  314. ?>