ClientTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http;
  15. use Cake\Http\Client;
  16. use Cake\Http\Client\Request;
  17. use Cake\Http\Client\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 ($expected 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/x-www-form-urlencoded',
  155. ];
  156. $cookies = [
  157. 'split' => 'value'
  158. ];
  159. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  160. ->setMethods(['send'])
  161. ->getMock();
  162. $mock->expects($this->once())
  163. ->method('send')
  164. ->with($this->callback(function ($request) use ($cookies, $headers) {
  165. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  166. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  167. $this->assertEquals('http://cakephp.org/test.html', $request->getUri() . '');
  168. $this->assertEquals($cookies, $request->cookies());
  169. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('content-type'));
  170. $this->assertEquals($headers['Connection'], $request->getHeaderLine('connection'));
  171. return true;
  172. }))
  173. ->will($this->returnValue([$response]));
  174. $http = new Client(['adapter' => $mock]);
  175. $result = $http->get('http://cakephp.org/test.html', [], [
  176. 'headers' => $headers,
  177. 'cookies' => $cookies,
  178. ]);
  179. $this->assertSame($result, $response);
  180. }
  181. /**
  182. * test get request with no data
  183. *
  184. * @return void
  185. */
  186. public function testGetNoData()
  187. {
  188. $response = new Response();
  189. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  190. ->setMethods(['send'])
  191. ->getMock();
  192. $mock->expects($this->once())
  193. ->method('send')
  194. ->with($this->callback(function ($request) {
  195. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  196. $this->assertEmpty($request->getHeaderLine('Content-Type'), 'Should have no content-type set');
  197. $this->assertEquals(
  198. 'http://cakephp.org/search',
  199. $request->getUri() . ''
  200. );
  201. return true;
  202. }))
  203. ->will($this->returnValue([$response]));
  204. $http = new Client([
  205. 'host' => 'cakephp.org',
  206. 'adapter' => $mock
  207. ]);
  208. $result = $http->get('/search');
  209. $this->assertSame($result, $response);
  210. }
  211. /**
  212. * test get request with querystring data
  213. *
  214. * @return void
  215. */
  216. public function testGetQuerystring()
  217. {
  218. $response = new Response();
  219. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  220. ->setMethods(['send'])
  221. ->getMock();
  222. $mock->expects($this->once())
  223. ->method('send')
  224. ->with($this->callback(function ($request) {
  225. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  226. $this->assertEquals(
  227. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  228. $request->getUri() . ''
  229. );
  230. return true;
  231. }))
  232. ->will($this->returnValue([$response]));
  233. $http = new Client([
  234. 'host' => 'cakephp.org',
  235. 'adapter' => $mock
  236. ]);
  237. $result = $http->get('/search', [
  238. 'q' => 'hi there',
  239. 'Category' => ['id' => [2, 3]]
  240. ]);
  241. $this->assertSame($result, $response);
  242. }
  243. /**
  244. * test get request with string of query data.
  245. *
  246. * @return void
  247. */
  248. public function testGetQuerystringString()
  249. {
  250. $response = new Response();
  251. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  252. ->setMethods(['send'])
  253. ->getMock();
  254. $mock->expects($this->once())
  255. ->method('send')
  256. ->with($this->callback(function ($request) {
  257. $this->assertEquals(
  258. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  259. $request->getUri() . ''
  260. );
  261. return true;
  262. }))
  263. ->will($this->returnValue([$response]));
  264. $http = new Client([
  265. 'host' => 'cakephp.org',
  266. 'adapter' => $mock
  267. ]);
  268. $data = [
  269. 'q' => 'hi there',
  270. 'Category' => ['id' => [2, 3]]
  271. ];
  272. $result = $http->get('/search', http_build_query($data));
  273. $this->assertSame($response, $result);
  274. }
  275. /**
  276. * Test a GET with a request body. Services like
  277. * elasticsearch use this feature.
  278. *
  279. * @return void
  280. */
  281. public function testGetWithContent()
  282. {
  283. $response = new Response();
  284. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  285. ->setMethods(['send'])
  286. ->getMock();
  287. $mock->expects($this->once())
  288. ->method('send')
  289. ->with($this->callback(function ($request) {
  290. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  291. $this->assertEquals('http://cakephp.org/search', '' . $request->getUri());
  292. $this->assertEquals('some data', '' . $request->getBody());
  293. return true;
  294. }))
  295. ->will($this->returnValue([$response]));
  296. $http = new Client([
  297. 'host' => 'cakephp.org',
  298. 'adapter' => $mock
  299. ]);
  300. $result = $http->get('/search', [
  301. '_content' => 'some data'
  302. ]);
  303. $this->assertSame($result, $response);
  304. }
  305. /**
  306. * Test invalid authentication types throw exceptions.
  307. *
  308. * @expectedException \Cake\Core\Exception\Exception
  309. * @return void
  310. */
  311. public function testInvalidAuthenticationType()
  312. {
  313. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  314. ->setMethods(['send'])
  315. ->getMock();
  316. $mock->expects($this->never())
  317. ->method('send');
  318. $http = new Client([
  319. 'host' => 'cakephp.org',
  320. 'adapter' => $mock
  321. ]);
  322. $result = $http->get('/', [], [
  323. 'auth' => ['type' => 'horribly broken']
  324. ]);
  325. }
  326. /**
  327. * Test setting basic authentication with get
  328. *
  329. * @return void
  330. */
  331. public function testGetWithAuthenticationAndProxy()
  332. {
  333. $response = new Response();
  334. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  335. ->setMethods(['send'])
  336. ->getMock();
  337. $headers = [
  338. 'Authorization' => 'Basic ' . base64_encode('mark:secret'),
  339. 'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
  340. ];
  341. $mock->expects($this->once())
  342. ->method('send')
  343. ->with($this->callback(function ($request) use ($headers) {
  344. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  345. $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
  346. $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
  347. $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
  348. return true;
  349. }))
  350. ->will($this->returnValue([$response]));
  351. $http = new Client([
  352. 'host' => 'cakephp.org',
  353. 'adapter' => $mock
  354. ]);
  355. $result = $http->get('/', [], [
  356. 'auth' => ['username' => 'mark', 'password' => 'secret'],
  357. 'proxy' => ['username' => 'mark', 'password' => 'pass'],
  358. ]);
  359. $this->assertSame($result, $response);
  360. }
  361. /**
  362. * Test authentication adapter that mutates request.
  363. *
  364. * @return void
  365. */
  366. public function testAuthenticationWithMutation()
  367. {
  368. static::setAppNamespace();
  369. $response = new Response();
  370. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  371. ->setMethods(['send'])
  372. ->getMock();
  373. $headers = [
  374. 'Authorization' => 'Bearer abc123',
  375. 'Proxy-Authorization' => 'Bearer abc123',
  376. ];
  377. $mock->expects($this->once())
  378. ->method('send')
  379. ->with($this->callback(function ($request) use ($headers) {
  380. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  381. $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
  382. $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
  383. $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
  384. return true;
  385. }))
  386. ->will($this->returnValue([$response]));
  387. $http = new Client([
  388. 'host' => 'cakephp.org',
  389. 'adapter' => $mock
  390. ]);
  391. $result = $http->get('/', [], [
  392. 'auth' => ['type' => 'TestApp\Http\CompatAuth'],
  393. 'proxy' => ['type' => 'TestApp\Http\CompatAuth'],
  394. ]);
  395. $this->assertSame($result, $response);
  396. }
  397. /**
  398. * Return a list of HTTP methods.
  399. *
  400. * @return array
  401. */
  402. public static function methodProvider()
  403. {
  404. return [
  405. [Request::METHOD_GET],
  406. [Request::METHOD_POST],
  407. [Request::METHOD_PUT],
  408. [Request::METHOD_DELETE],
  409. [Request::METHOD_PATCH],
  410. [Request::METHOD_OPTIONS],
  411. [Request::METHOD_TRACE],
  412. ];
  413. }
  414. /**
  415. * test simple POST request.
  416. *
  417. * @dataProvider methodProvider
  418. * @return void
  419. */
  420. public function testMethodsSimple($method)
  421. {
  422. $response = new Response();
  423. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  424. ->setMethods(['send'])
  425. ->getMock();
  426. $mock->expects($this->once())
  427. ->method('send')
  428. ->with($this->callback(function ($request) use ($method) {
  429. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  430. $this->assertEquals($method, $request->getMethod());
  431. $this->assertEquals('http://cakephp.org/projects/add', '' . $request->getUri());
  432. return true;
  433. }))
  434. ->will($this->returnValue([$response]));
  435. $http = new Client([
  436. 'host' => 'cakephp.org',
  437. 'adapter' => $mock
  438. ]);
  439. $result = $http->{$method}('/projects/add');
  440. $this->assertSame($result, $response);
  441. }
  442. /**
  443. * Provider for testing the type option.
  444. *
  445. * @return array
  446. */
  447. public static function typeProvider()
  448. {
  449. return [
  450. ['application/json', 'application/json'],
  451. ['json', 'application/json'],
  452. ['xml', 'application/xml'],
  453. ['application/xml', 'application/xml'],
  454. ];
  455. }
  456. /**
  457. * Test that using the 'type' option sets the correct headers
  458. *
  459. * @dataProvider typeProvider
  460. * @return void
  461. */
  462. public function testPostWithTypeKey($type, $mime)
  463. {
  464. $response = new Response();
  465. $data = 'some data';
  466. $headers = [
  467. 'Content-Type' => $mime,
  468. 'Accept' => $mime,
  469. ];
  470. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  471. ->setMethods(['send'])
  472. ->getMock();
  473. $mock->expects($this->once())
  474. ->method('send')
  475. ->with($this->callback(function ($request) use ($headers) {
  476. $this->assertEquals(Request::METHOD_POST, $request->getMethod());
  477. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('Content-Type'));
  478. $this->assertEquals($headers['Accept'], $request->getHeaderLine('Accept'));
  479. return true;
  480. }))
  481. ->will($this->returnValue([$response]));
  482. $http = new Client([
  483. 'host' => 'cakephp.org',
  484. 'adapter' => $mock
  485. ]);
  486. $http->post('/projects/add', $data, ['type' => $type]);
  487. }
  488. /**
  489. * Test that string payloads with no content type have a default content-type set.
  490. *
  491. * @return void
  492. */
  493. public function testPostWithStringDataDefaultsToFormEncoding()
  494. {
  495. $response = new Response();
  496. $data = 'some=value&more=data';
  497. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  498. ->setMethods(['send'])
  499. ->getMock();
  500. $mock->expects($this->any())
  501. ->method('send')
  502. ->with($this->callback(function ($request) use ($data) {
  503. $this->assertEquals($data, '' . $request->getBody());
  504. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  505. return true;
  506. }))
  507. ->will($this->returnValue([$response]));
  508. $http = new Client([
  509. 'host' => 'cakephp.org',
  510. 'adapter' => $mock
  511. ]);
  512. $http->post('/projects/add', $data);
  513. $http->put('/projects/add', $data);
  514. $http->delete('/projects/add', $data);
  515. }
  516. /**
  517. * Test that exceptions are raised on invalid types.
  518. *
  519. * @expectedException \Cake\Core\Exception\Exception
  520. * @return void
  521. */
  522. public function testExceptionOnUnknownType()
  523. {
  524. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  525. ->setMethods(['send'])
  526. ->getMock();
  527. $mock->expects($this->never())
  528. ->method('send');
  529. $http = new Client([
  530. 'host' => 'cakephp.org',
  531. 'adapter' => $mock
  532. ]);
  533. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  534. }
  535. /**
  536. * Test that Client stores cookies
  537. *
  538. * @return void
  539. */
  540. public function testCookieStorage()
  541. {
  542. $adapter = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  543. ->setMethods(['send'])
  544. ->getMock();
  545. $cookieJar = $this->getMockBuilder('Cake\Http\Client\CookieCollection')->getMock();
  546. $headers = [
  547. 'HTTP/1.0 200 Ok',
  548. 'Set-Cookie: first=1',
  549. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  550. ];
  551. $response = new Response($headers, '');
  552. $cookieJar->expects($this->at(0))
  553. ->method('get')
  554. ->with('http://cakephp.org/projects')
  555. ->will($this->returnValue([]));
  556. $cookieJar->expects($this->at(1))
  557. ->method('store')
  558. ->with($response);
  559. $adapter->expects($this->at(0))
  560. ->method('send')
  561. ->will($this->returnValue([$response]));
  562. $http = new Client([
  563. 'host' => 'cakephp.org',
  564. 'adapter' => $adapter,
  565. 'cookieJar' => $cookieJar
  566. ]);
  567. $http->get('/projects');
  568. }
  569. /**
  570. * test head request with querystring data
  571. *
  572. * @return void
  573. */
  574. public function testHeadQuerystring()
  575. {
  576. $response = new Response();
  577. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  578. ->setMethods(['send'])
  579. ->getMock();
  580. $mock->expects($this->once())
  581. ->method('send')
  582. ->with($this->callback(function ($request) {
  583. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  584. $this->assertEquals(Request::METHOD_HEAD, $request->getMethod());
  585. $this->assertEquals('http://cakephp.org/search?q=hi+there', '' . $request->getUri());
  586. return true;
  587. }))
  588. ->will($this->returnValue([$response]));
  589. $http = new Client([
  590. 'host' => 'cakephp.org',
  591. 'adapter' => $mock
  592. ]);
  593. $result = $http->head('/search', [
  594. 'q' => 'hi there',
  595. ]);
  596. $this->assertSame($result, $response);
  597. }
  598. }