ServiceTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\net\http;
  9. use lithium\net\http\Media;
  10. use lithium\net\http\Service;
  11. class ServiceTest extends \lithium\test\Unit {
  12. public $request = null;
  13. protected $_testConfig = array(
  14. 'classes' => array('response' => 'lithium\net\http\Response'),
  15. 'socket' => 'lithium\tests\mocks\net\http\MockSocket',
  16. 'host' => 'localhost',
  17. 'port' => 80,
  18. 'timeout' => 2
  19. );
  20. public function setUp() {
  21. Media::reset();
  22. }
  23. public function testAllMethodsNoConnection() {
  24. $http = new Service(array('init' => false));
  25. $this->assertFalse($http->get());
  26. $this->assertFalse($http->post());
  27. $this->assertFalse($http->put());
  28. $this->assertFalse($http->delete());
  29. }
  30. public function testRequestPath() {
  31. $http = new Service(array('host' => 'localhost') + $this->_testConfig);
  32. $result = $http->get();
  33. $expected = '/';
  34. $result = $http->last->request->path;
  35. $this->assertEqual($expected, $result);
  36. $http = new Service(array('host' => 'localhost/base/path/') + $this->_testConfig);
  37. $result = $http->get();
  38. $expected = '/base/path/';
  39. $result = $http->last->request->path;
  40. $this->assertEqual($expected, $result);
  41. $http = new Service(array('host' => 'localhost/base/path') + $this->_testConfig);
  42. $result = $http->get('/somewhere');
  43. $expected = '/base/path/somewhere';
  44. $result = $http->last->request->path;
  45. $this->assertEqual($expected, $result);
  46. $http = new Service(array('host' => 'localhost/base/path/') + $this->_testConfig);
  47. $result = $http->get('/somewhere');
  48. $expected = '/base/path/somewhere';
  49. $result = $http->last->request->path;
  50. $this->assertEqual($expected, $result);
  51. }
  52. public function testReturnHandlers() {
  53. $http = new Service($this->_testConfig);
  54. $result = $http->get(null, null, array('return' => 'headers'));
  55. $this->assertEqual('localhost:80', $result['Host']);
  56. $result = $http->get(null, null, array('return' => 'response'));
  57. $this->assertEqual($result, $http->last->response);
  58. $result = $http->get(null, null, array('return' => 'body'));
  59. $this->assertEqual($result, $http->last->response->body());
  60. }
  61. public function testHead() {
  62. $http = new Service($this->_testConfig);
  63. $result = $http->head();
  64. $this->assertEqual('localhost:80', $result['Host']);
  65. $this->assertEqual('HTTP/1.1', $http->last->response->protocol);
  66. $this->assertEqual('200', $http->last->response->status['code']);
  67. $this->assertEqual('OK', $http->last->response->status['message']);
  68. $this->assertEqual(null, $http->last->response->type());
  69. $this->assertEqual('UTF-8', $http->last->response->encoding);
  70. $this->assertEqual('', $http->last->response->body());
  71. }
  72. public function testHeadPath() {
  73. $http = new Service($this->_testConfig);
  74. $expected = '/somewhere';
  75. $result = $http->head('/somewhere');
  76. $this->assertEqual($expected, $http->last->request->path);
  77. }
  78. public function testHeadQueryString() {
  79. $http = new Service($this->_testConfig);
  80. $expected = array('foo' => 'bar');
  81. $result = $http->head('/', $expected);
  82. $this->assertEqual($expected, $http->last->request->query);
  83. }
  84. public function testGet() {
  85. $http = new Service($this->_testConfig);
  86. $this->assertEqual('', $http->get());
  87. $this->assertEqual('HTTP/1.1', $http->last->response->protocol);
  88. $this->assertEqual('200', $http->last->response->status['code']);
  89. $this->assertEqual('OK', $http->last->response->status['message']);
  90. $this->assertEqual(null, $http->last->response->type());
  91. $this->assertEqual('UTF-8', $http->last->response->encoding);
  92. }
  93. public function testGetPath() {
  94. $http = new Service($this->_testConfig);
  95. $this->assertEqual('', $http->get('search.json'));
  96. $this->assertEqual('HTTP/1.1', $http->last->response->protocol);
  97. $this->assertEqual('200', $http->last->response->status['code']);
  98. $this->assertEqual('OK', $http->last->response->status['message']);
  99. $this->assertEqual(null, $http->last->response->type());
  100. $this->assertEqual('UTF-8', $http->last->response->encoding);
  101. }
  102. public function testPost() {
  103. $http = new Service($this->_testConfig);
  104. $http->post('update.xml', array('status' => 'cool'));
  105. $expected = join("\r\n", array(
  106. 'POST /update.xml HTTP/1.1',
  107. 'Host: localhost:80',
  108. 'Connection: Close',
  109. 'User-Agent: Mozilla/5.0',
  110. 'Content-Type: application/x-www-form-urlencoded',
  111. 'Content-Length: 11',
  112. '', 'status=cool'
  113. ));
  114. $result = (string) $http->last->request;
  115. $this->assertEqual($expected, $result);
  116. $expected = join("\r\n", array(
  117. 'HTTP/1.1 200 OK',
  118. 'Host: localhost:80',
  119. 'Connection: Close',
  120. 'User-Agent: Mozilla/5.0',
  121. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  122. 'Content-Length: 11',
  123. '', 'status=cool'
  124. ));
  125. $result = (string) $http->last->response;
  126. $this->assertEqual($expected, $result);
  127. }
  128. public function testPut() {
  129. $http = new Service($this->_testConfig);
  130. $http->put('update.xml', array('status' => 'cool'));
  131. $expected = join("\r\n", array(
  132. 'PUT /update.xml HTTP/1.1',
  133. 'Host: localhost:80',
  134. 'Connection: Close',
  135. 'User-Agent: Mozilla/5.0',
  136. 'Content-Type: application/x-www-form-urlencoded',
  137. 'Content-Length: 11',
  138. '', 'status=cool'
  139. ));
  140. $result = (string) $http->last->request;
  141. $this->assertEqual($expected, $result);
  142. $expected = join("\r\n", array(
  143. 'HTTP/1.1 200 OK',
  144. 'Host: localhost:80',
  145. 'Connection: Close',
  146. 'User-Agent: Mozilla/5.0',
  147. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  148. 'Content-Length: 11',
  149. '', 'status=cool'
  150. ));
  151. $result = (string) $http->last->response;
  152. $this->assertEqual($expected, $result);
  153. }
  154. public function testDelete() {
  155. $http = new Service($this->_testConfig);
  156. $http->delete('posts/1');
  157. $expected = join("\r\n", array(
  158. 'DELETE /posts/1 HTTP/1.1',
  159. 'Host: localhost:80',
  160. 'Connection: Close',
  161. 'User-Agent: Mozilla/5.0',
  162. '', ''
  163. ));
  164. $result = (string) $http->last->request;
  165. $this->assertEqual($expected, $result);
  166. $expected = join("\r\n", array(
  167. 'HTTP/1.1 200 OK',
  168. 'Host: localhost:80',
  169. 'Connection: Close',
  170. 'User-Agent: Mozilla/5.0',
  171. '', ''
  172. ));
  173. $result = (string) $http->last->response;
  174. $this->assertEqual($expected, $result);
  175. }
  176. public function testJsonPost() {
  177. $http = new Service($this->_testConfig);
  178. $data = array('status' => array('cool', 'awesome'));
  179. $http->post('update.xml', $data, array('type' => 'json'));
  180. $expected = join("\r\n", array(
  181. 'POST /update.xml HTTP/1.1',
  182. 'Host: localhost:80',
  183. 'Connection: Close',
  184. 'User-Agent: Mozilla/5.0',
  185. 'Content-Type: application/json',
  186. 'Content-Length: 29',
  187. '', '{"status":["cool","awesome"]}'
  188. ));
  189. $result = (string) $http->last->request;
  190. $this->assertEqual($expected, $result);
  191. $expected = join("\r\n", array(
  192. 'HTTP/1.1 200 OK',
  193. 'Host: localhost:80',
  194. 'Connection: Close',
  195. 'User-Agent: Mozilla/5.0',
  196. 'Content-Type: application/json;charset=UTF-8',
  197. 'Content-Length: 29',
  198. '', '{"status":["cool","awesome"]}'
  199. ));
  200. $result = (string) $http->last->response;
  201. $this->assertEqual($expected, $result);
  202. }
  203. public function testConnection() {
  204. $http = new Service($this->_testConfig);
  205. $connection = $http->connection;
  206. $this->assertEqual('lithium\tests\mocks\net\http\MockSocket', get_class($connection));
  207. $http->connection->open(array('scheme' => 'https'));
  208. $config = $http->connection->config();
  209. $this->assertEqual('https', $config['scheme']);
  210. }
  211. public function testSendConfiguringConnection() {
  212. $http = new Service($this->_testConfig);
  213. $result = $http->send('get', 'some-path/stuff', array(), array('someKey' => 'someValue'));
  214. $config = $http->connection->config();
  215. $this->assertEqual('someValue', $config['someKey']);
  216. }
  217. public function testPatchMethod() {
  218. $http = new Service($this->_testConfig);
  219. $response = $http->patch(
  220. 'some-path/stuff',
  221. array('someData' => 'someValue'),
  222. array('return' => 'response')
  223. );
  224. $result = $http->last->request;
  225. $this->assertEqual('PATCH', $result->method);
  226. $this->assertEqual('lithium\net\http\Response', get_class($response));
  227. $this->assertEqual('someData=someValue', $result->body());
  228. }
  229. public function testPatchWithJson() {
  230. $http = new Service($this->_testConfig);
  231. $response = $http->patch(
  232. 'some-path/stuff',
  233. array('someData' => 'someValue'),
  234. array('return' => 'response', 'type' => 'json')
  235. );
  236. $result = $http->last->request;
  237. $this->assertEqual('{"someData":"someValue"}', $result->body());
  238. $this->assertEqual('application/json', $result->headers['Content-Type']);
  239. }
  240. public function testMagicMethod() {
  241. $http = new Service($this->_testConfig);
  242. $response = $http->magic('some-path/stuff');
  243. $expected = "http://localhost:80/some-path/stuff";
  244. $result = $http->last->request;
  245. $this->assertEqual($expected, $result->to('url'));
  246. $this->assertEqual('MAGIC', $result->method);
  247. }
  248. public function testDigestAuth() {
  249. $this->_testConfig += array('auth' => 'digest', 'username' => 'gwoo', 'password' => 'li3');
  250. $http = new Service($this->_testConfig);
  251. $response = $http->get('/http_auth/', array(), array('return' => 'response'));
  252. $this->assertEqual('success', $response->body());
  253. }
  254. public function testRespondsTo() {
  255. $query = new Service();
  256. $this->assertTrue($query->respondsTo('foobarbaz'));
  257. $this->assertFalse($query->respondsTo(0));
  258. }
  259. }
  260. ?>