/tests/cases/data/source/HttpTest.php

https://github.com/mackstar/lithium · PHP · 380 lines · 323 code · 51 blank · 6 comment · 0 complexity · 0d9585b232fc82ccbb36b74b806e29e1 MD5 · raw file

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