ClientTest.php 15 KB

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