ClientTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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\Core\Configure;
  16. use Cake\Http\Client;
  17. use Cake\Http\Client\Request;
  18. use Cake\Http\Client\Response;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * HTTP client test.
  22. */
  23. class ClientTest extends TestCase
  24. {
  25. /**
  26. * Test storing config options and modifying them.
  27. *
  28. * @return void
  29. */
  30. public function testConstructConfig()
  31. {
  32. $config = [
  33. 'scheme' => 'http',
  34. 'host' => 'example.org',
  35. ];
  36. $http = new Client($config);
  37. $result = $http->config();
  38. foreach ($config as $key => $val) {
  39. $this->assertEquals($val, $result[$key]);
  40. }
  41. $result = $http->config([
  42. 'auth' => ['username' => 'mark', 'password' => 'secret']
  43. ]);
  44. $this->assertSame($result, $http);
  45. $result = $http->config();
  46. $expected = [
  47. 'scheme' => 'http',
  48. 'host' => 'example.org',
  49. 'auth' => ['username' => 'mark', 'password' => 'secret']
  50. ];
  51. foreach ($expected as $key => $val) {
  52. $this->assertEquals($val, $result[$key]);
  53. }
  54. }
  55. /**
  56. * Data provider for buildUrl() tests
  57. *
  58. * @return array
  59. */
  60. public static function urlProvider()
  61. {
  62. return [
  63. [
  64. 'http://example.com/test.html',
  65. 'http://example.com/test.html',
  66. [],
  67. null,
  68. 'Null options'
  69. ],
  70. [
  71. 'http://example.com/test.html',
  72. 'http://example.com/test.html',
  73. [],
  74. [],
  75. 'Simple string'
  76. ],
  77. [
  78. 'http://example.com/test.html',
  79. '/test.html',
  80. [],
  81. ['host' => 'example.com'],
  82. 'host name option',
  83. ],
  84. [
  85. 'https://example.com/test.html',
  86. '/test.html',
  87. [],
  88. ['host' => 'example.com', 'scheme' => 'https'],
  89. 'HTTPS',
  90. ],
  91. [
  92. 'http://example.com:8080/test.html',
  93. '/test.html',
  94. [],
  95. ['host' => 'example.com', 'port' => '8080'],
  96. 'Non standard port',
  97. ],
  98. [
  99. 'http://example.com/test.html',
  100. '/test.html',
  101. [],
  102. ['host' => 'example.com', 'port' => '80'],
  103. 'standard port, does not display'
  104. ],
  105. [
  106. 'https://example.com/test.html',
  107. '/test.html',
  108. [],
  109. ['host' => 'example.com', 'scheme' => 'https', 'port' => '443'],
  110. 'standard port, does not display'
  111. ],
  112. [
  113. 'http://example.com/test.html',
  114. 'http://example.com/test.html',
  115. [],
  116. ['host' => 'example.com', 'scheme' => 'https'],
  117. 'options do not duplicate'
  118. ],
  119. [
  120. 'http://example.com/search?q=hi+there&cat%5Bid%5D%5B0%5D=2&cat%5Bid%5D%5B1%5D=3',
  121. 'http://example.com/search',
  122. ['q' => 'hi there', 'cat' => ['id' => [2, 3]]],
  123. [],
  124. 'query string data.'
  125. ],
  126. [
  127. 'http://example.com/search?q=hi+there&id=12',
  128. 'http://example.com/search?q=hi+there',
  129. ['id' => '12'],
  130. [],
  131. 'query string data with some already on the url.'
  132. ],
  133. ];
  134. }
  135. /**
  136. * @dataProvider urlProvider
  137. */
  138. public function testBuildUrl($expected, $url, $query, $opts)
  139. {
  140. $http = new Client();
  141. $result = $http->buildUrl($url, $query, $opts);
  142. $this->assertEquals($expected, $result);
  143. }
  144. /**
  145. * test simple get request with headers & cookies.
  146. *
  147. * @return void
  148. */
  149. public function testGetSimpleWithHeadersAndCookies()
  150. {
  151. $response = new Response();
  152. $headers = [
  153. 'User-Agent' => 'Cake',
  154. 'Connection' => 'close',
  155. 'Content-Type' => 'application/x-www-form-urlencoded',
  156. ];
  157. $cookies = [
  158. 'split' => 'value'
  159. ];
  160. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  161. ->setMethods(['send'])
  162. ->getMock();
  163. $mock->expects($this->once())
  164. ->method('send')
  165. ->with($this->callback(function ($request) use ($cookies, $headers) {
  166. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  167. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  168. $this->assertEquals('http://cakephp.org/test.html', $request->getUri() . '');
  169. $this->assertEquals($cookies, $request->cookies());
  170. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('content-type'));
  171. $this->assertEquals($headers['Connection'], $request->getHeaderLine('connection'));
  172. return true;
  173. }))
  174. ->will($this->returnValue([$response]));
  175. $http = new Client(['adapter' => $mock]);
  176. $result = $http->get('http://cakephp.org/test.html', [], [
  177. 'headers' => $headers,
  178. 'cookies' => $cookies,
  179. ]);
  180. $this->assertSame($result, $response);
  181. }
  182. /**
  183. * test get request with querystring data
  184. *
  185. * @return void
  186. */
  187. public function testGetQuerystring()
  188. {
  189. $response = new Response();
  190. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  191. ->setMethods(['send'])
  192. ->getMock();
  193. $mock->expects($this->once())
  194. ->method('send')
  195. ->with($this->callback(function ($request) {
  196. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  197. $this->assertEquals(
  198. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  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. 'q' => 'hi there',
  210. 'Category' => ['id' => [2, 3]]
  211. ]);
  212. $this->assertSame($result, $response);
  213. }
  214. /**
  215. * test get request with string of query data.
  216. *
  217. * @return void
  218. */
  219. public function testGetQuerystringString()
  220. {
  221. $response = new Response();
  222. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  223. ->setMethods(['send'])
  224. ->getMock();
  225. $mock->expects($this->once())
  226. ->method('send')
  227. ->with($this->callback(function ($request) {
  228. $this->assertEquals(
  229. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  230. $request->getUri() . ''
  231. );
  232. return true;
  233. }))
  234. ->will($this->returnValue([$response]));
  235. $http = new Client([
  236. 'host' => 'cakephp.org',
  237. 'adapter' => $mock
  238. ]);
  239. $data = [
  240. 'q' => 'hi there',
  241. 'Category' => ['id' => [2, 3]]
  242. ];
  243. $result = $http->get('/search', http_build_query($data));
  244. $this->assertSame($response, $result);
  245. }
  246. /**
  247. * Test a GET with a request body. Services like
  248. * elasticsearch use this feature.
  249. *
  250. * @return void
  251. */
  252. public function testGetWithContent()
  253. {
  254. $response = new Response();
  255. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  256. ->setMethods(['send'])
  257. ->getMock();
  258. $mock->expects($this->once())
  259. ->method('send')
  260. ->with($this->callback(function ($request) {
  261. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  262. $this->assertEquals('http://cakephp.org/search', '' . $request->getUri());
  263. $this->assertEquals('some data', '' . $request->getBody());
  264. return true;
  265. }))
  266. ->will($this->returnValue([$response]));
  267. $http = new Client([
  268. 'host' => 'cakephp.org',
  269. 'adapter' => $mock
  270. ]);
  271. $result = $http->get('/search', [
  272. '_content' => 'some data'
  273. ]);
  274. $this->assertSame($result, $response);
  275. }
  276. /**
  277. * Test invalid authentication types throw exceptions.
  278. *
  279. * @expectedException \Cake\Core\Exception\Exception
  280. * @return void
  281. */
  282. public function testInvalidAuthenticationType()
  283. {
  284. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  285. ->setMethods(['send'])
  286. ->getMock();
  287. $mock->expects($this->never())
  288. ->method('send');
  289. $http = new Client([
  290. 'host' => 'cakephp.org',
  291. 'adapter' => $mock
  292. ]);
  293. $result = $http->get('/', [], [
  294. 'auth' => ['type' => 'horribly broken']
  295. ]);
  296. }
  297. /**
  298. * Test setting basic authentication with get
  299. *
  300. * @return void
  301. */
  302. public function testGetWithAuthenticationAndProxy()
  303. {
  304. $response = new Response();
  305. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  306. ->setMethods(['send'])
  307. ->getMock();
  308. $headers = [
  309. 'Authorization' => 'Basic ' . base64_encode('mark:secret'),
  310. 'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
  311. ];
  312. $mock->expects($this->once())
  313. ->method('send')
  314. ->with($this->callback(function ($request) use ($headers) {
  315. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  316. $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
  317. $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
  318. $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
  319. return true;
  320. }))
  321. ->will($this->returnValue([$response]));
  322. $http = new Client([
  323. 'host' => 'cakephp.org',
  324. 'adapter' => $mock
  325. ]);
  326. $result = $http->get('/', [], [
  327. 'auth' => ['username' => 'mark', 'password' => 'secret'],
  328. 'proxy' => ['username' => 'mark', 'password' => 'pass'],
  329. ]);
  330. $this->assertSame($result, $response);
  331. }
  332. /**
  333. * Test authentication adapter that mutates request.
  334. *
  335. * @return void
  336. */
  337. public function testAuthenticationWithMutation()
  338. {
  339. Configure::write('App.namespace', 'TestApp');
  340. $response = new Response();
  341. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  342. ->setMethods(['send'])
  343. ->getMock();
  344. $headers = [
  345. 'Authorization' => 'Bearer abc123',
  346. 'Proxy-Authorization' => 'Bearer abc123',
  347. ];
  348. $mock->expects($this->once())
  349. ->method('send')
  350. ->with($this->callback(function ($request) use ($headers) {
  351. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  352. $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
  353. $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
  354. $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
  355. return true;
  356. }))
  357. ->will($this->returnValue([$response]));
  358. $http = new Client([
  359. 'host' => 'cakephp.org',
  360. 'adapter' => $mock
  361. ]);
  362. $result = $http->get('/', [], [
  363. 'auth' => ['type' => 'TestApp\Http\CompatAuth'],
  364. 'proxy' => ['type' => 'TestApp\Http\CompatAuth'],
  365. ]);
  366. $this->assertSame($result, $response);
  367. }
  368. /**
  369. * Return a list of HTTP methods.
  370. *
  371. * @return array
  372. */
  373. public static function methodProvider()
  374. {
  375. return [
  376. [Request::METHOD_GET],
  377. [Request::METHOD_POST],
  378. [Request::METHOD_PUT],
  379. [Request::METHOD_DELETE],
  380. [Request::METHOD_PATCH],
  381. [Request::METHOD_OPTIONS],
  382. [Request::METHOD_TRACE],
  383. ];
  384. }
  385. /**
  386. * test simple POST request.
  387. *
  388. * @dataProvider methodProvider
  389. * @return void
  390. */
  391. public function testMethodsSimple($method)
  392. {
  393. $response = new Response();
  394. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  395. ->setMethods(['send'])
  396. ->getMock();
  397. $mock->expects($this->once())
  398. ->method('send')
  399. ->with($this->callback(function ($request) use ($method) {
  400. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  401. $this->assertEquals($method, $request->getMethod());
  402. $this->assertEquals('http://cakephp.org/projects/add', '' . $request->getUri());
  403. return true;
  404. }))
  405. ->will($this->returnValue([$response]));
  406. $http = new Client([
  407. 'host' => 'cakephp.org',
  408. 'adapter' => $mock
  409. ]);
  410. $result = $http->{$method}('/projects/add');
  411. $this->assertSame($result, $response);
  412. }
  413. /**
  414. * Provider for testing the type option.
  415. *
  416. * @return array
  417. */
  418. public static function typeProvider()
  419. {
  420. return [
  421. ['application/json', 'application/json'],
  422. ['json', 'application/json'],
  423. ['xml', 'application/xml'],
  424. ['application/xml', 'application/xml'],
  425. ];
  426. }
  427. /**
  428. * Test that using the 'type' option sets the correct headers
  429. *
  430. * @dataProvider typeProvider
  431. * @return void
  432. */
  433. public function testPostWithTypeKey($type, $mime)
  434. {
  435. $response = new Response();
  436. $data = 'some data';
  437. $headers = [
  438. 'Content-Type' => $mime,
  439. 'Accept' => $mime,
  440. ];
  441. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  442. ->setMethods(['send'])
  443. ->getMock();
  444. $mock->expects($this->once())
  445. ->method('send')
  446. ->with($this->callback(function ($request) use ($headers) {
  447. $this->assertEquals(Request::METHOD_POST, $request->getMethod());
  448. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('Content-Type'));
  449. $this->assertEquals($headers['Accept'], $request->getHeaderLine('Accept'));
  450. return true;
  451. }))
  452. ->will($this->returnValue([$response]));
  453. $http = new Client([
  454. 'host' => 'cakephp.org',
  455. 'adapter' => $mock
  456. ]);
  457. $http->post('/projects/add', $data, ['type' => $type]);
  458. }
  459. /**
  460. * Test that string payloads with no content type have a default content-type set.
  461. *
  462. * @return void
  463. */
  464. public function testPostWithStringDataDefaultsToFormEncoding()
  465. {
  466. $response = new Response();
  467. $data = 'some=value&more=data';
  468. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  469. ->setMethods(['send'])
  470. ->getMock();
  471. $mock->expects($this->any())
  472. ->method('send')
  473. ->with($this->callback(function ($request) use ($data) {
  474. $this->assertEquals($data, '' . $request->getBody());
  475. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  476. return true;
  477. }))
  478. ->will($this->returnValue([$response]));
  479. $http = new Client([
  480. 'host' => 'cakephp.org',
  481. 'adapter' => $mock
  482. ]);
  483. $http->post('/projects/add', $data);
  484. $http->put('/projects/add', $data);
  485. $http->delete('/projects/add', $data);
  486. }
  487. /**
  488. * Test that exceptions are raised on invalid types.
  489. *
  490. * @expectedException \Cake\Core\Exception\Exception
  491. * @return void
  492. */
  493. public function testExceptionOnUnknownType()
  494. {
  495. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  496. ->setMethods(['send'])
  497. ->getMock();
  498. $mock->expects($this->never())
  499. ->method('send');
  500. $http = new Client([
  501. 'host' => 'cakephp.org',
  502. 'adapter' => $mock
  503. ]);
  504. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  505. }
  506. /**
  507. * Test that Client stores cookies
  508. *
  509. * @return void
  510. */
  511. public function testCookieStorage()
  512. {
  513. $adapter = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  514. ->setMethods(['send'])
  515. ->getMock();
  516. $cookieJar = $this->getMockBuilder('Cake\Http\Client\CookieCollection')->getMock();
  517. $headers = [
  518. 'HTTP/1.0 200 Ok',
  519. 'Set-Cookie: first=1',
  520. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  521. ];
  522. $response = new Response($headers, '');
  523. $cookieJar->expects($this->at(0))
  524. ->method('get')
  525. ->with('http://cakephp.org/projects')
  526. ->will($this->returnValue([]));
  527. $cookieJar->expects($this->at(1))
  528. ->method('store')
  529. ->with($response);
  530. $adapter->expects($this->at(0))
  531. ->method('send')
  532. ->will($this->returnValue([$response]));
  533. $http = new Client([
  534. 'host' => 'cakephp.org',
  535. 'adapter' => $adapter,
  536. 'cookieJar' => $cookieJar
  537. ]);
  538. $http->get('/projects');
  539. }
  540. /**
  541. * test head request with querystring data
  542. *
  543. * @return void
  544. */
  545. public function testHeadQuerystring()
  546. {
  547. $response = new Response();
  548. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  549. ->setMethods(['send'])
  550. ->getMock();
  551. $mock->expects($this->once())
  552. ->method('send')
  553. ->with($this->callback(function ($request) {
  554. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  555. $this->assertEquals(Request::METHOD_HEAD, $request->getMethod());
  556. $this->assertEquals('http://cakephp.org/search?q=hi+there', '' . $request->getUri());
  557. return true;
  558. }))
  559. ->will($this->returnValue([$response]));
  560. $http = new Client([
  561. 'host' => 'cakephp.org',
  562. 'adapter' => $mock
  563. ]);
  564. $result = $http->head('/search', [
  565. 'q' => 'hi there',
  566. ]);
  567. $this->assertSame($result, $response);
  568. }
  569. }