ClientTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http;
  15. use Cake\Network\Http\Client;
  16. use Cake\Network\Http\Request;
  17. use Cake\Network\Http\Response;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * HTTP client test.
  21. */
  22. class ClientTest extends TestCase {
  23. /**
  24. * Test storing config options and modifying them.
  25. *
  26. * @return void
  27. */
  28. public function testConstructConfig() {
  29. $config = [
  30. 'scheme' => 'http',
  31. 'host' => 'example.org',
  32. ];
  33. $http = new Client($config);
  34. $result = $http->config();
  35. foreach ($config as $key => $val) {
  36. $this->assertEquals($val, $result[$key]);
  37. }
  38. $result = $http->config([
  39. 'auth' => ['username' => 'mark', 'password' => 'secret']
  40. ]);
  41. $this->assertSame($result, $http);
  42. $result = $http->config();
  43. $expected = [
  44. 'scheme' => 'http',
  45. 'host' => 'example.org',
  46. 'auth' => ['username' => 'mark', 'password' => 'secret']
  47. ];
  48. foreach ($config as $key => $val) {
  49. $this->assertEquals($val, $result[$key]);
  50. }
  51. }
  52. /**
  53. * Data provider for buildUrl() tests
  54. *
  55. * @return array
  56. */
  57. public static function urlProvider() {
  58. return [
  59. [
  60. 'http://example.com/test.html',
  61. 'http://example.com/test.html',
  62. [],
  63. null,
  64. 'Null options'
  65. ],
  66. [
  67. 'http://example.com/test.html',
  68. 'http://example.com/test.html',
  69. [],
  70. [],
  71. 'Simple string'
  72. ],
  73. [
  74. 'http://example.com/test.html',
  75. '/test.html',
  76. [],
  77. ['host' => 'example.com'],
  78. 'host name option',
  79. ],
  80. [
  81. 'https://example.com/test.html',
  82. '/test.html',
  83. [],
  84. ['host' => 'example.com', 'scheme' => 'https'],
  85. 'HTTPS',
  86. ],
  87. [
  88. 'http://example.com:8080/test.html',
  89. '/test.html',
  90. [],
  91. ['host' => 'example.com', 'port' => '8080'],
  92. 'Non standard port',
  93. ],
  94. [
  95. 'http://example.com/test.html',
  96. '/test.html',
  97. [],
  98. ['host' => 'example.com', 'port' => '80'],
  99. 'standard port, does not display'
  100. ],
  101. [
  102. 'https://example.com/test.html',
  103. '/test.html',
  104. [],
  105. ['host' => 'example.com', 'scheme' => 'https', 'port' => '443'],
  106. 'standard port, does not display'
  107. ],
  108. [
  109. 'http://example.com/test.html',
  110. 'http://example.com/test.html',
  111. [],
  112. ['host' => 'example.com', 'scheme' => 'https'],
  113. 'options do not duplicate'
  114. ],
  115. [
  116. 'http://example.com/search?q=hi+there&cat%5Bid%5D%5B0%5D=2&cat%5Bid%5D%5B1%5D=3',
  117. 'http://example.com/search',
  118. ['q' => 'hi there', 'cat' => ['id' => [2, 3]]],
  119. [],
  120. 'query string data.'
  121. ],
  122. [
  123. 'http://example.com/search?q=hi+there&id=12',
  124. 'http://example.com/search?q=hi+there',
  125. ['id' => '12'],
  126. [],
  127. 'query string data with some already on the url.'
  128. ],
  129. ];
  130. }
  131. /**
  132. * @dataProvider urlProvider
  133. */
  134. public function testBuildUrl($expected, $url, $query, $opts) {
  135. $http = new Client();
  136. $result = $http->buildUrl($url, $query, $opts);
  137. $this->assertEquals($expected, $result);
  138. }
  139. /**
  140. * test simple get request with headers & cookies.
  141. *
  142. * @return void
  143. */
  144. public function testGetSimpleWithHeadersAndCookies() {
  145. $response = new Response();
  146. $headers = [
  147. 'User-Agent' => 'Cake',
  148. 'Connection' => 'close',
  149. 'Content-Type' => 'application/json',
  150. ];
  151. $cookies = [
  152. 'split' => 'value'
  153. ];
  154. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  155. $mock->expects($this->once())
  156. ->method('send')
  157. ->with($this->logicalAnd(
  158. $this->isInstanceOf('Cake\Network\Http\Request'),
  159. $this->attributeEqualTo('_method', Request::METHOD_GET),
  160. $this->attributeEqualTo('_url', 'http://cakephp.org/test.html'),
  161. $this->attributeEqualTo('_headers', $headers),
  162. $this->attributeEqualTo('_cookies', $cookies)
  163. ))
  164. ->will($this->returnValue([$response]));
  165. $http = new Client(['adapter' => $mock]);
  166. $result = $http->get('http://cakephp.org/test.html', [], [
  167. 'headers' => $headers,
  168. 'cookies' => $cookies,
  169. ]);
  170. $this->assertSame($result, $response);
  171. }
  172. /**
  173. * test get request with querystring data
  174. *
  175. * @return void
  176. */
  177. public function testGetQuerystring() {
  178. $response = new Response();
  179. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  180. $mock->expects($this->once())
  181. ->method('send')
  182. ->with($this->logicalAnd(
  183. $this->isInstanceOf('Cake\Network\Http\Request'),
  184. $this->attributeEqualTo('_method', Request::METHOD_GET),
  185. $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
  186. ))
  187. ->will($this->returnValue([$response]));
  188. $http = new Client([
  189. 'host' => 'cakephp.org',
  190. 'adapter' => $mock
  191. ]);
  192. $result = $http->get('/search', [
  193. 'q' => 'hi there',
  194. 'Category' => ['id' => [2, 3]]
  195. ]);
  196. $this->assertSame($result, $response);
  197. }
  198. /**
  199. * Test a GET with a request body. Services like
  200. * elasticsearch use this feature.
  201. *
  202. * @return void
  203. */
  204. public function testGetWithContent() {
  205. $response = new Response();
  206. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  207. $mock->expects($this->once())
  208. ->method('send')
  209. ->with($this->logicalAnd(
  210. $this->isInstanceOf('Cake\Network\Http\Request'),
  211. $this->attributeEqualTo('_method', Request::METHOD_GET),
  212. $this->attributeEqualTo('_url', 'http://cakephp.org/search'),
  213. $this->attributeEqualTo('_body', 'some data')
  214. ))
  215. ->will($this->returnValue([$response]));
  216. $http = new Client([
  217. 'host' => 'cakephp.org',
  218. 'adapter' => $mock
  219. ]);
  220. $result = $http->get('/search', [
  221. '_content' => 'some data'
  222. ]);
  223. $this->assertSame($result, $response);
  224. }
  225. /**
  226. * Test invalid authentication types throw exceptions.
  227. *
  228. * @expectedException \Cake\Error\Exception
  229. * @return void
  230. */
  231. public function testInvalidAuthenticationType() {
  232. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  233. $mock->expects($this->never())
  234. ->method('send');
  235. $http = new Client([
  236. 'host' => 'cakephp.org',
  237. 'adapter' => $mock
  238. ]);
  239. $result = $http->get('/', [], [
  240. 'auth' => ['type' => 'horribly broken']
  241. ]);
  242. }
  243. /**
  244. * Test setting basic authentication with get
  245. *
  246. * @return void
  247. */
  248. public function testGetWithAuthenticationAndProxy() {
  249. $response = new Response();
  250. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  251. $headers = [
  252. 'Connection' => 'close',
  253. 'User-Agent' => 'CakePHP',
  254. 'Authorization' => 'Basic ' . base64_encode('mark:secret'),
  255. 'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
  256. ];
  257. $mock->expects($this->once())
  258. ->method('send')
  259. ->with($this->logicalAnd(
  260. $this->isInstanceOf('Cake\Network\Http\Request'),
  261. $this->attributeEqualTo('_method', Request::METHOD_GET),
  262. $this->attributeEqualTo('_url', 'http://cakephp.org/'),
  263. $this->attributeEqualTo('_headers', $headers)
  264. ))
  265. ->will($this->returnValue([$response]));
  266. $http = new Client([
  267. 'host' => 'cakephp.org',
  268. 'adapter' => $mock
  269. ]);
  270. $result = $http->get('/', [], [
  271. 'auth' => ['username' => 'mark', 'password' => 'secret'],
  272. 'proxy' => ['username' => 'mark', 'password' => 'pass'],
  273. ]);
  274. $this->assertSame($result, $response);
  275. }
  276. /**
  277. * Return a list of HTTP methods.
  278. *
  279. * @return array
  280. */
  281. public static function methodProvider() {
  282. return [
  283. [Request::METHOD_GET],
  284. [Request::METHOD_POST],
  285. [Request::METHOD_PUT],
  286. [Request::METHOD_DELETE],
  287. [Request::METHOD_PATCH],
  288. ];
  289. }
  290. /**
  291. * test simple POST request.
  292. *
  293. * @dataProvider methodProvider
  294. * @return void
  295. */
  296. public function testMethodsSimple($method) {
  297. $response = new Response();
  298. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  299. $mock->expects($this->once())
  300. ->method('send')
  301. ->with($this->logicalAnd(
  302. $this->isInstanceOf('Cake\Network\Http\Request'),
  303. $this->attributeEqualTo('_method', $method),
  304. $this->attributeEqualTo('_url', 'http://cakephp.org/projects/add')
  305. ))
  306. ->will($this->returnValue([$response]));
  307. $http = new Client([
  308. 'host' => 'cakephp.org',
  309. 'adapter' => $mock
  310. ]);
  311. $result = $http->{$method}('/projects/add');
  312. $this->assertSame($result, $response);
  313. }
  314. /**
  315. * Provider for testing the type option.
  316. *
  317. * @return array
  318. */
  319. public static function typeProvider() {
  320. return [
  321. ['application/json', 'application/json'],
  322. ['json', 'application/json'],
  323. ['xml', 'application/xml'],
  324. ['application/xml', 'application/xml'],
  325. ];
  326. }
  327. /**
  328. * Test that using the 'type' option sets the correct headers
  329. *
  330. * @dataProvider typeProvider
  331. * @return void
  332. */
  333. public function testPostWithTypeKey($type, $mime) {
  334. $response = new Response();
  335. $data = 'some data';
  336. $headers = [
  337. 'Connection' => 'close',
  338. 'User-Agent' => 'CakePHP',
  339. 'Content-Type' => $mime,
  340. 'Accept' => $mime,
  341. ];
  342. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  343. $mock->expects($this->once())
  344. ->method('send')
  345. ->with($this->logicalAnd(
  346. $this->attributeEqualTo('_method', Request::METHOD_POST),
  347. $this->attributeEqualTo('_body', $data),
  348. $this->attributeEqualTo('_headers', $headers)
  349. ))
  350. ->will($this->returnValue([$response]));
  351. $http = new Client([
  352. 'host' => 'cakephp.org',
  353. 'adapter' => $mock
  354. ]);
  355. $http->post('/projects/add', $data, ['type' => $type]);
  356. }
  357. /**
  358. * Test that exceptions are raised on invalid types.
  359. *
  360. * @expectedException \Cake\Error\Exception
  361. * @return void
  362. */
  363. public function testExceptionOnUnknownType() {
  364. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  365. $mock->expects($this->never())
  366. ->method('send');
  367. $http = new Client([
  368. 'host' => 'cakephp.org',
  369. 'adapter' => $mock
  370. ]);
  371. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  372. }
  373. /**
  374. * Test that Client stores cookies
  375. *
  376. * @return void
  377. */
  378. public function testCookieStorage() {
  379. $adapter = $this->getMock(
  380. 'Cake\Network\Http\Adapter\Stream',
  381. ['send']
  382. );
  383. $cookieJar = $this->getMock('Cake\Network\Http\CookieCollection');
  384. $headers = [
  385. 'HTTP/1.0 200 Ok',
  386. 'Set-Cookie: first=1',
  387. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  388. ];
  389. $response = new Response($headers, '');
  390. $cookieJar->expects($this->at(0))
  391. ->method('get')
  392. ->with('http://cakephp.org/projects')
  393. ->will($this->returnValue([]));
  394. $cookieJar->expects($this->at(1))
  395. ->method('store')
  396. ->with($response);
  397. $adapter->expects($this->at(0))
  398. ->method('send')
  399. ->will($this->returnValue([$response]));
  400. $http = new Client([
  401. 'host' => 'cakephp.org',
  402. 'adapter' => $adapter,
  403. 'cookieJar' => $cookieJar
  404. ]);
  405. $http->get('/projects');
  406. }
  407. /**
  408. * test head request with querystring data
  409. *
  410. * @return void
  411. */
  412. public function testHeadQuerystring() {
  413. $response = new Response();
  414. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  415. $mock->expects($this->once())
  416. ->method('send')
  417. ->with($this->logicalAnd(
  418. $this->isInstanceOf('Cake\Network\Http\Request'),
  419. $this->attributeEqualTo('_method', Request::METHOD_HEAD),
  420. $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there')
  421. ))
  422. ->will($this->returnValue([$response]));
  423. $http = new Client([
  424. 'host' => 'cakephp.org',
  425. 'adapter' => $mock
  426. ]);
  427. $result = $http->head('/search', [
  428. 'q' => 'hi there',
  429. ]);
  430. $this->assertSame($result, $response);
  431. }
  432. }