CurlTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.7.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Client\Adapter;
  16. use Cake\Http\Client\Adapter\Curl;
  17. use Cake\Http\Client\Exception\NetworkException;
  18. use Cake\Http\Client\Request;
  19. use Cake\Http\Client\Response;
  20. use Cake\TestSuite\TestCase;
  21. use Composer\CaBundle\CaBundle;
  22. /**
  23. * HTTP curl adapter test.
  24. */
  25. class CurlTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Http\Client\Adapter\Curl
  29. */
  30. protected $curl;
  31. /**
  32. * @var string
  33. */
  34. protected $caFile;
  35. /**
  36. * @return void
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $this->skipIf(!function_exists('curl_init'), 'Skipping as ext/curl is not installed.');
  42. $this->curl = new Curl();
  43. $this->caFile = CaBundle::getBundledCaBundlePath();
  44. }
  45. /**
  46. * Test the send method
  47. *
  48. * @return void
  49. */
  50. public function testSendLive()
  51. {
  52. $request = new Request('http://localhost', 'GET', [
  53. 'User-Agent' => 'CakePHP TestSuite',
  54. 'Cookie' => 'testing=value',
  55. ]);
  56. try {
  57. $responses = $this->curl->send($request, []);
  58. } catch (NetworkException $e) {
  59. $this->markTestSkipped('Could not connect to localhost, skipping');
  60. }
  61. $this->assertCount(1, $responses);
  62. /** @var \Cake\Http\Response $response */
  63. $response = $responses[0];
  64. $this->assertInstanceOf(Response::class, $response);
  65. $this->assertNotEmpty($response->getHeaders());
  66. }
  67. /**
  68. * Test the send method
  69. *
  70. * @return void
  71. */
  72. public function testSendLiveResponseCheck()
  73. {
  74. $request = new Request('https://api.cakephp.org/3.0/', 'GET', [
  75. 'User-Agent' => 'CakePHP TestSuite',
  76. ]);
  77. try {
  78. $responses = $this->curl->send($request, []);
  79. } catch (NetworkException $e) {
  80. $this->markTestSkipped('Could not connect to api.cakephp.org, skipping');
  81. }
  82. $this->assertCount(1, $responses);
  83. /** @var \Cake\Http\Response $response */
  84. $response = $responses[0];
  85. $this->assertInstanceOf(Response::class, $response);
  86. $this->assertTrue($response->hasHeader('Date'));
  87. $this->assertTrue($response->hasHeader('Content-type'));
  88. $this->assertStringContainsString('<html', $response->getBody()->getContents());
  89. }
  90. /**
  91. * Test converting client options into curl ones.
  92. *
  93. * @return void
  94. */
  95. public function testBuildOptionsGet()
  96. {
  97. $options = [
  98. 'timeout' => 5,
  99. ];
  100. $request = new Request(
  101. 'http://localhost/things',
  102. 'GET',
  103. ['Cookie' => 'testing=value']
  104. );
  105. $result = $this->curl->buildOptions($request, $options);
  106. $expected = [
  107. CURLOPT_URL => 'http://localhost/things',
  108. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  109. CURLOPT_RETURNTRANSFER => true,
  110. CURLOPT_HEADER => true,
  111. CURLOPT_HTTPHEADER => [
  112. 'Cookie: testing=value',
  113. 'Connection: close',
  114. 'User-Agent: CakePHP',
  115. ],
  116. CURLOPT_HTTPGET => true,
  117. CURLOPT_TIMEOUT => 5,
  118. CURLOPT_CAINFO => $this->caFile,
  119. ];
  120. $this->assertSame($expected, $result);
  121. }
  122. /**
  123. * Test converting client options into curl ones.
  124. *
  125. * @return void
  126. */
  127. public function testBuildOptionsGetWithBody()
  128. {
  129. $options = [
  130. 'timeout' => 5,
  131. ];
  132. $request = new Request(
  133. 'http://localhost/things',
  134. 'GET',
  135. ['Cookie' => 'testing=value'],
  136. '{"some":"body"}'
  137. );
  138. $result = $this->curl->buildOptions($request, $options);
  139. $expected = [
  140. CURLOPT_URL => 'http://localhost/things',
  141. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  142. CURLOPT_RETURNTRANSFER => true,
  143. CURLOPT_HEADER => true,
  144. CURLOPT_HTTPHEADER => [
  145. 'Cookie: testing=value',
  146. 'Connection: close',
  147. 'User-Agent: CakePHP',
  148. ],
  149. CURLOPT_HTTPGET => true,
  150. CURLOPT_POSTFIELDS => '{"some":"body"}',
  151. CURLOPT_CUSTOMREQUEST => 'get',
  152. CURLOPT_TIMEOUT => 5,
  153. CURLOPT_CAINFO => $this->caFile,
  154. ];
  155. $this->assertSame($expected, $result);
  156. }
  157. /**
  158. * Test converting client options into curl ones.
  159. *
  160. * @return void
  161. */
  162. public function testBuildOptionsPost()
  163. {
  164. $options = [];
  165. $request = new Request(
  166. 'http://localhost/things',
  167. 'POST',
  168. ['Cookie' => 'testing=value'],
  169. ['name' => 'cakephp', 'yes' => 1]
  170. );
  171. $result = $this->curl->buildOptions($request, $options);
  172. $expected = [
  173. CURLOPT_URL => 'http://localhost/things',
  174. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  175. CURLOPT_RETURNTRANSFER => true,
  176. CURLOPT_HEADER => true,
  177. CURLOPT_HTTPHEADER => [
  178. 'Cookie: testing=value',
  179. 'Connection: close',
  180. 'User-Agent: CakePHP',
  181. 'Content-Type: application/x-www-form-urlencoded',
  182. ],
  183. CURLOPT_POST => true,
  184. CURLOPT_POSTFIELDS => 'name=cakephp&yes=1',
  185. CURLOPT_CAINFO => $this->caFile,
  186. ];
  187. $this->assertSame($expected, $result);
  188. }
  189. /**
  190. * Test converting client options into curl ones.
  191. *
  192. * @return void
  193. */
  194. public function testBuildOptionsPut()
  195. {
  196. $options = [];
  197. $request = new Request(
  198. 'http://localhost/things',
  199. 'PUT',
  200. ['Cookie' => 'testing=value']
  201. );
  202. $result = $this->curl->buildOptions($request, $options);
  203. $expected = [
  204. CURLOPT_URL => 'http://localhost/things',
  205. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  206. CURLOPT_RETURNTRANSFER => true,
  207. CURLOPT_HEADER => true,
  208. CURLOPT_HTTPHEADER => [
  209. 'Cookie: testing=value',
  210. 'Connection: close',
  211. 'User-Agent: CakePHP',
  212. ],
  213. CURLOPT_POST => true,
  214. CURLOPT_CUSTOMREQUEST => 'PUT',
  215. CURLOPT_CAINFO => $this->caFile,
  216. ];
  217. $this->assertSame($expected, $result);
  218. }
  219. /**
  220. * Test converting client options into curl ones.
  221. *
  222. * @return void
  223. */
  224. public function testBuildOptionsJsonPost()
  225. {
  226. $options = [];
  227. $content = json_encode(['a' => 1, 'b' => 2]);
  228. $request = new Request(
  229. 'http://localhost/things',
  230. 'POST',
  231. ['Content-type' => 'application/json'],
  232. $content
  233. );
  234. $result = $this->curl->buildOptions($request, $options);
  235. $expected = [
  236. CURLOPT_URL => 'http://localhost/things',
  237. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  238. CURLOPT_RETURNTRANSFER => true,
  239. CURLOPT_HEADER => true,
  240. CURLOPT_HTTPHEADER => [
  241. 'Content-type: application/json',
  242. 'Connection: close',
  243. 'User-Agent: CakePHP',
  244. ],
  245. CURLOPT_POST => true,
  246. CURLOPT_POSTFIELDS => $content,
  247. CURLOPT_CAINFO => $this->caFile,
  248. ];
  249. $this->assertSame($expected, $result);
  250. }
  251. /**
  252. * Test converting client options into curl ones.
  253. *
  254. * @return void
  255. */
  256. public function testBuildOptionsSsl()
  257. {
  258. $options = [
  259. 'ssl_verify_host' => true,
  260. 'ssl_verify_peer' => true,
  261. 'ssl_verify_peer_name' => true,
  262. // These options do nothing in curl.
  263. 'ssl_verify_depth' => 9000,
  264. 'ssl_allow_self_signed' => false,
  265. ];
  266. $request = new Request('http://localhost/things', 'GET');
  267. $result = $this->curl->buildOptions($request, $options);
  268. $expected = [
  269. CURLOPT_URL => 'http://localhost/things',
  270. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  271. CURLOPT_RETURNTRANSFER => true,
  272. CURLOPT_HEADER => true,
  273. CURLOPT_HTTPHEADER => [
  274. 'Connection: close',
  275. 'User-Agent: CakePHP',
  276. ],
  277. CURLOPT_HTTPGET => true,
  278. CURLOPT_SSL_VERIFYPEER => true,
  279. CURLOPT_SSL_VERIFYHOST => 2,
  280. CURLOPT_CAINFO => $this->caFile,
  281. ];
  282. $this->assertSame($expected, $result);
  283. }
  284. /**
  285. * Test converting client options into curl ones.
  286. *
  287. * @return void
  288. */
  289. public function testBuildOptionsProxy()
  290. {
  291. $options = [
  292. 'proxy' => [
  293. 'proxy' => '127.0.0.1:8080',
  294. 'username' => 'frodo',
  295. 'password' => 'one_ring',
  296. ],
  297. ];
  298. $request = new Request('http://localhost/things', 'GET');
  299. $result = $this->curl->buildOptions($request, $options);
  300. $expected = [
  301. CURLOPT_URL => 'http://localhost/things',
  302. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  303. CURLOPT_RETURNTRANSFER => true,
  304. CURLOPT_HEADER => true,
  305. CURLOPT_HTTPHEADER => [
  306. 'Connection: close',
  307. 'User-Agent: CakePHP',
  308. ],
  309. CURLOPT_HTTPGET => true,
  310. CURLOPT_CAINFO => $this->caFile,
  311. CURLOPT_PROXY => '127.0.0.1:8080',
  312. CURLOPT_PROXYUSERPWD => 'frodo:one_ring',
  313. ];
  314. $this->assertSame($expected, $result);
  315. }
  316. /**
  317. * Test converting client options into curl ones.
  318. *
  319. * @return void
  320. */
  321. public function testBuildOptionsHead()
  322. {
  323. $options = [];
  324. $request = new Request(
  325. 'http://localhost/things',
  326. 'HEAD',
  327. ['Cookie' => 'testing=value']
  328. );
  329. $result = $this->curl->buildOptions($request, $options);
  330. $expected = [
  331. CURLOPT_URL => 'http://localhost/things',
  332. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  333. CURLOPT_RETURNTRANSFER => true,
  334. CURLOPT_HEADER => true,
  335. CURLOPT_HTTPHEADER => [
  336. 'Cookie: testing=value',
  337. 'Connection: close',
  338. 'User-Agent: CakePHP',
  339. ],
  340. CURLOPT_NOBODY => true,
  341. CURLOPT_CAINFO => $this->caFile,
  342. ];
  343. $this->assertSame($expected, $result);
  344. }
  345. /**
  346. * Test converting client options into curl ones.
  347. *
  348. * @return void
  349. */
  350. public function testBuildOptionsCurlOptions()
  351. {
  352. $options = [
  353. 'curl' => [
  354. CURLOPT_USERAGENT => 'Super-secret',
  355. ],
  356. ];
  357. $request = new Request('http://localhost/things', 'GET');
  358. $request = $request->withProtocolVersion('1.0');
  359. $result = $this->curl->buildOptions($request, $options);
  360. $expected = [
  361. CURLOPT_URL => 'http://localhost/things',
  362. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
  363. CURLOPT_RETURNTRANSFER => true,
  364. CURLOPT_HEADER => true,
  365. CURLOPT_HTTPHEADER => [
  366. 'Connection: close',
  367. 'User-Agent: CakePHP',
  368. ],
  369. CURLOPT_HTTPGET => true,
  370. CURLOPT_CAINFO => $this->caFile,
  371. CURLOPT_USERAGENT => 'Super-secret',
  372. ];
  373. $this->assertSame($expected, $result);
  374. }
  375. /**
  376. * Test that an exception is raised when timed out.
  377. *
  378. * @return void
  379. */
  380. public function testNetworkException()
  381. {
  382. $this->expectException(NetworkException::class);
  383. $this->expectExceptionMessageMatches('/(Could not resolve|Resolving timed out)/');
  384. $request = new Request('http://dummy/?sleep');
  385. $options = [
  386. 'timeout' => 2,
  387. ];
  388. $this->curl->send($request, $options);
  389. }
  390. /**
  391. * Test converting client options into curl ones.
  392. *
  393. * @return void
  394. */
  395. public function testBuildOptionsProtocolVersion()
  396. {
  397. $this->skipIf(!defined('CURL_HTTP_VERSION_2TLS'), 'Requires libcurl 7.42');
  398. $options = [];
  399. $request = new Request('http://localhost/things', 'GET');
  400. $request = $request->withProtocolVersion('2');
  401. $result = $this->curl->buildOptions($request, $options);
  402. $this->assertSame(CURL_HTTP_VERSION_2TLS, $result[CURLOPT_HTTP_VERSION]);
  403. }
  404. }