ClientTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 get request with string of query data.
  206. *
  207. * @return void
  208. */
  209. public function testGetQuerystringString()
  210. {
  211. $response = new Response();
  212. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  213. $mock->expects($this->once())
  214. ->method('send')
  215. ->with($this->logicalAnd(
  216. $this->isInstanceOf('Cake\Network\Http\Request'),
  217. $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
  218. ))
  219. ->will($this->returnValue([$response]));
  220. $http = new Client([
  221. 'host' => 'cakephp.org',
  222. 'adapter' => $mock
  223. ]);
  224. $data = [
  225. 'q' => 'hi there',
  226. 'Category' => ['id' => [2, 3]]
  227. ];
  228. $result = $http->get('/search', http_build_query($data));
  229. $this->assertSame($response, $result);
  230. }
  231. /**
  232. * Test a GET with a request body. Services like
  233. * elasticsearch use this feature.
  234. *
  235. * @return void
  236. */
  237. public function testGetWithContent()
  238. {
  239. $response = new Response();
  240. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  241. $mock->expects($this->once())
  242. ->method('send')
  243. ->with($this->logicalAnd(
  244. $this->isInstanceOf('Cake\Network\Http\Request'),
  245. $this->attributeEqualTo('_method', Request::METHOD_GET),
  246. $this->attributeEqualTo('_url', 'http://cakephp.org/search'),
  247. $this->attributeEqualTo('_body', 'some data')
  248. ))
  249. ->will($this->returnValue([$response]));
  250. $http = new Client([
  251. 'host' => 'cakephp.org',
  252. 'adapter' => $mock
  253. ]);
  254. $result = $http->get('/search', [
  255. '_content' => 'some data'
  256. ]);
  257. $this->assertSame($result, $response);
  258. }
  259. /**
  260. * Test invalid authentication types throw exceptions.
  261. *
  262. * @expectedException \Cake\Core\Exception\Exception
  263. * @return void
  264. */
  265. public function testInvalidAuthenticationType()
  266. {
  267. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  268. $mock->expects($this->never())
  269. ->method('send');
  270. $http = new Client([
  271. 'host' => 'cakephp.org',
  272. 'adapter' => $mock
  273. ]);
  274. $result = $http->get('/', [], [
  275. 'auth' => ['type' => 'horribly broken']
  276. ]);
  277. }
  278. /**
  279. * Test setting basic authentication with get
  280. *
  281. * @return void
  282. */
  283. public function testGetWithAuthenticationAndProxy()
  284. {
  285. $response = new Response();
  286. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  287. $headers = [
  288. 'Connection' => 'close',
  289. 'User-Agent' => 'CakePHP',
  290. 'Authorization' => 'Basic ' . base64_encode('mark:secret'),
  291. 'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
  292. ];
  293. $mock->expects($this->once())
  294. ->method('send')
  295. ->with($this->logicalAnd(
  296. $this->isInstanceOf('Cake\Network\Http\Request'),
  297. $this->attributeEqualTo('_method', Request::METHOD_GET),
  298. $this->attributeEqualTo('_url', 'http://cakephp.org/'),
  299. $this->attributeEqualTo('_headers', $headers)
  300. ))
  301. ->will($this->returnValue([$response]));
  302. $http = new Client([
  303. 'host' => 'cakephp.org',
  304. 'adapter' => $mock
  305. ]);
  306. $result = $http->get('/', [], [
  307. 'auth' => ['username' => 'mark', 'password' => 'secret'],
  308. 'proxy' => ['username' => 'mark', 'password' => 'pass'],
  309. ]);
  310. $this->assertSame($result, $response);
  311. }
  312. /**
  313. * Return a list of HTTP methods.
  314. *
  315. * @return array
  316. */
  317. public static function methodProvider()
  318. {
  319. return [
  320. [Request::METHOD_GET],
  321. [Request::METHOD_POST],
  322. [Request::METHOD_PUT],
  323. [Request::METHOD_DELETE],
  324. [Request::METHOD_PATCH],
  325. [Request::METHOD_OPTIONS],
  326. [Request::METHOD_TRACE],
  327. ];
  328. }
  329. /**
  330. * test simple POST request.
  331. *
  332. * @dataProvider methodProvider
  333. * @return void
  334. */
  335. public function testMethodsSimple($method)
  336. {
  337. $response = new Response();
  338. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  339. $mock->expects($this->once())
  340. ->method('send')
  341. ->with($this->logicalAnd(
  342. $this->isInstanceOf('Cake\Network\Http\Request'),
  343. $this->attributeEqualTo('_method', $method),
  344. $this->attributeEqualTo('_url', 'http://cakephp.org/projects/add')
  345. ))
  346. ->will($this->returnValue([$response]));
  347. $http = new Client([
  348. 'host' => 'cakephp.org',
  349. 'adapter' => $mock
  350. ]);
  351. $result = $http->{$method}('/projects/add');
  352. $this->assertSame($result, $response);
  353. }
  354. /**
  355. * Provider for testing the type option.
  356. *
  357. * @return array
  358. */
  359. public static function typeProvider()
  360. {
  361. return [
  362. ['application/json', 'application/json'],
  363. ['json', 'application/json'],
  364. ['xml', 'application/xml'],
  365. ['application/xml', 'application/xml'],
  366. ];
  367. }
  368. /**
  369. * Test that using the 'type' option sets the correct headers
  370. *
  371. * @dataProvider typeProvider
  372. * @return void
  373. */
  374. public function testPostWithTypeKey($type, $mime)
  375. {
  376. $response = new Response();
  377. $data = 'some data';
  378. $headers = [
  379. 'Connection' => 'close',
  380. 'User-Agent' => 'CakePHP',
  381. 'Content-Type' => $mime,
  382. 'Accept' => $mime,
  383. ];
  384. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  385. $mock->expects($this->once())
  386. ->method('send')
  387. ->with($this->logicalAnd(
  388. $this->attributeEqualTo('_method', Request::METHOD_POST),
  389. $this->attributeEqualTo('_body', $data),
  390. $this->attributeEqualTo('_headers', $headers)
  391. ))
  392. ->will($this->returnValue([$response]));
  393. $http = new Client([
  394. 'host' => 'cakephp.org',
  395. 'adapter' => $mock
  396. ]);
  397. $http->post('/projects/add', $data, ['type' => $type]);
  398. }
  399. /**
  400. * Test that string payloads with no content type have a default content-type set.
  401. *
  402. * @return void
  403. */
  404. public function testPostWithStringDataDefaultsToFormEncoding()
  405. {
  406. $response = new Response();
  407. $data = 'some=value&more=data';
  408. $headers = [
  409. 'Connection' => 'close',
  410. 'User-Agent' => 'CakePHP',
  411. 'Content-Type' => 'application/x-www-form-urlencoded',
  412. ];
  413. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  414. $mock->expects($this->any())
  415. ->method('send')
  416. ->with($this->logicalAnd(
  417. $this->attributeEqualTo('_body', $data),
  418. $this->attributeEqualTo('_headers', $headers)
  419. ))
  420. ->will($this->returnValue([$response]));
  421. $http = new Client([
  422. 'host' => 'cakephp.org',
  423. 'adapter' => $mock
  424. ]);
  425. $http->post('/projects/add', $data);
  426. $http->put('/projects/add', $data);
  427. $http->delete('/projects/add', $data);
  428. }
  429. /**
  430. * Test that exceptions are raised on invalid types.
  431. *
  432. * @expectedException \Cake\Core\Exception\Exception
  433. * @return void
  434. */
  435. public function testExceptionOnUnknownType()
  436. {
  437. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  438. $mock->expects($this->never())
  439. ->method('send');
  440. $http = new Client([
  441. 'host' => 'cakephp.org',
  442. 'adapter' => $mock
  443. ]);
  444. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  445. }
  446. /**
  447. * Test that Client stores cookies
  448. *
  449. * @return void
  450. */
  451. public function testCookieStorage()
  452. {
  453. $adapter = $this->getMock(
  454. 'Cake\Network\Http\Adapter\Stream',
  455. ['send']
  456. );
  457. $cookieJar = $this->getMock('Cake\Network\Http\CookieCollection');
  458. $headers = [
  459. 'HTTP/1.0 200 Ok',
  460. 'Set-Cookie: first=1',
  461. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  462. ];
  463. $response = new Response($headers, '');
  464. $cookieJar->expects($this->at(0))
  465. ->method('get')
  466. ->with('http://cakephp.org/projects')
  467. ->will($this->returnValue([]));
  468. $cookieJar->expects($this->at(1))
  469. ->method('store')
  470. ->with($response);
  471. $adapter->expects($this->at(0))
  472. ->method('send')
  473. ->will($this->returnValue([$response]));
  474. $http = new Client([
  475. 'host' => 'cakephp.org',
  476. 'adapter' => $adapter,
  477. 'cookieJar' => $cookieJar
  478. ]);
  479. $http->get('/projects');
  480. }
  481. /**
  482. * test head request with querystring data
  483. *
  484. * @return void
  485. */
  486. public function testHeadQuerystring()
  487. {
  488. $response = new Response();
  489. $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
  490. $mock->expects($this->once())
  491. ->method('send')
  492. ->with($this->logicalAnd(
  493. $this->isInstanceOf('Cake\Network\Http\Request'),
  494. $this->attributeEqualTo('_method', Request::METHOD_HEAD),
  495. $this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there')
  496. ))
  497. ->will($this->returnValue([$response]));
  498. $http = new Client([
  499. 'host' => 'cakephp.org',
  500. 'adapter' => $mock
  501. ]);
  502. $result = $http->head('/search', [
  503. 'q' => 'hi there',
  504. ]);
  505. $this->assertSame($result, $response);
  506. }
  507. }