ClientTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. ];
  299. }
  300. /**
  301. * test simple POST request.
  302. *
  303. * @dataProvider methodProvider
  304. * @return void
  305. */
  306. public function testMethodsSimple($method)
  307. {
  308. $response = new Response();
  309. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  310. $mock->expects($this->once())
  311. ->method('send')
  312. ->with($this->logicalAnd(
  313. $this->isInstanceOf('Cake\Network\Http\Request'),
  314. $this->attributeEqualTo('_method', $method),
  315. $this->attributeEqualTo('_url', 'http://cakephp.org/projects/add')
  316. ))
  317. ->will($this->returnValue([$response]));
  318. $http = new Client([
  319. 'host' => 'cakephp.org',
  320. 'adapter' => $mock
  321. ]);
  322. $result = $http->{$method}('/projects/add');
  323. $this->assertSame($result, $response);
  324. }
  325. /**
  326. * Provider for testing the type option.
  327. *
  328. * @return array
  329. */
  330. public static function typeProvider()
  331. {
  332. return [
  333. ['application/json', 'application/json'],
  334. ['json', 'application/json'],
  335. ['xml', 'application/xml'],
  336. ['application/xml', 'application/xml'],
  337. ];
  338. }
  339. /**
  340. * Test that using the 'type' option sets the correct headers
  341. *
  342. * @dataProvider typeProvider
  343. * @return void
  344. */
  345. public function testPostWithTypeKey($type, $mime)
  346. {
  347. $response = new Response();
  348. $data = 'some data';
  349. $headers = [
  350. 'Connection' => 'close',
  351. 'User-Agent' => 'CakePHP',
  352. 'Content-Type' => $mime,
  353. 'Accept' => $mime,
  354. ];
  355. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  356. $mock->expects($this->once())
  357. ->method('send')
  358. ->with($this->logicalAnd(
  359. $this->attributeEqualTo('_method', Request::METHOD_POST),
  360. $this->attributeEqualTo('_body', $data),
  361. $this->attributeEqualTo('_headers', $headers)
  362. ))
  363. ->will($this->returnValue([$response]));
  364. $http = new Client([
  365. 'host' => 'cakephp.org',
  366. 'adapter' => $mock
  367. ]);
  368. $http->post('/projects/add', $data, ['type' => $type]);
  369. }
  370. /**
  371. * Test that exceptions are raised on invalid types.
  372. *
  373. * @expectedException \Cake\Core\Exception\Exception
  374. * @return void
  375. */
  376. public function testExceptionOnUnknownType()
  377. {
  378. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  379. $mock->expects($this->never())
  380. ->method('send');
  381. $http = new Client([
  382. 'host' => 'cakephp.org',
  383. 'adapter' => $mock
  384. ]);
  385. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  386. }
  387. /**
  388. * Test that Client stores cookies
  389. *
  390. * @return void
  391. */
  392. public function testCookieStorage()
  393. {
  394. $adapter = $this->getMock(
  395. 'Cake\Network\Http\Adapter\Stream',
  396. ['send']
  397. );
  398. $cookieJar = $this->getMock('Cake\Network\Http\CookieCollection');
  399. $headers = [
  400. 'HTTP/1.0 200 Ok',
  401. 'Set-Cookie: first=1',
  402. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  403. ];
  404. $response = new Response($headers, '');
  405. $cookieJar->expects($this->at(0))
  406. ->method('get')
  407. ->with('http://cakephp.org/projects')
  408. ->will($this->returnValue([]));
  409. $cookieJar->expects($this->at(1))
  410. ->method('store')
  411. ->with($response);
  412. $adapter->expects($this->at(0))
  413. ->method('send')
  414. ->will($this->returnValue([$response]));
  415. $http = new Client([
  416. 'host' => 'cakephp.org',
  417. 'adapter' => $adapter,
  418. 'cookieJar' => $cookieJar
  419. ]);
  420. $http->get('/projects');
  421. }
  422. /**
  423. * test head request with querystring data
  424. *
  425. * @return void
  426. */
  427. public function testHeadQuerystring()
  428. {
  429. $response = new Response();
  430. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  431. $mock->expects($this->once())
  432. ->method('send')
  433. ->with($this->logicalAnd(
  434. $this->isInstanceOf('Cake\Network\Http\Request'),
  435. $this->attributeEqualTo('_method', Request::METHOD_HEAD),
  436. $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there')
  437. ))
  438. ->will($this->returnValue([$response]));
  439. $http = new Client([
  440. 'host' => 'cakephp.org',
  441. 'adapter' => $mock
  442. ]);
  443. $result = $http->head('/search', [
  444. 'q' => 'hi there',
  445. ]);
  446. $this->assertSame($result, $response);
  447. }
  448. }