CurlFactoryTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. <?php
  2. // Override curl_setopt_array() to get the last set curl options
  3. namespace GuzzleHttp\Ring\Client {
  4. function curl_setopt_array($handle, array $options) {
  5. if (!empty($_SERVER['curl_test'])) {
  6. $_SERVER['_curl'] = $options;
  7. } else {
  8. unset($_SERVER['_curl']);
  9. }
  10. \curl_setopt_array($handle, $options);
  11. }
  12. }
  13. namespace GuzzleHttp\Tests\Ring\Client {
  14. use GuzzleHttp\Ring\Client\CurlFactory;
  15. use GuzzleHttp\Ring\Client\CurlMultiHandler;
  16. use GuzzleHttp\Ring\Client\MockHandler;
  17. use GuzzleHttp\Ring\Core;
  18. use GuzzleHttp\Stream\FnStream;
  19. use GuzzleHttp\Stream\NoSeekStream;
  20. use GuzzleHttp\Stream\Stream;
  21. class CurlFactoryTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public static function setUpBeforeClass()
  24. {
  25. $_SERVER['curl_test'] = true;
  26. unset($_SERVER['_curl']);
  27. }
  28. public static function tearDownAfterClass()
  29. {
  30. unset($_SERVER['_curl'], $_SERVER['curl_test']);
  31. }
  32. public function testCreatesCurlHandle()
  33. {
  34. Server::flush();
  35. Server::enqueue([[
  36. 'status' => 200,
  37. 'headers' => [
  38. 'Foo' => ['Bar'],
  39. 'Baz' => ['bam'],
  40. 'Content-Length' => [2],
  41. ],
  42. 'body' => 'hi',
  43. ]]);
  44. $stream = Stream::factory();
  45. $request = [
  46. 'http_method' => 'PUT',
  47. 'headers' => [
  48. 'host' => [Server::$url],
  49. 'Hi' => [' 123'],
  50. ],
  51. 'body' => 'testing',
  52. 'client' => ['save_to' => $stream],
  53. ];
  54. $f = new CurlFactory();
  55. $result = $f($request);
  56. $this->assertInternalType('array', $result);
  57. $this->assertCount(3, $result);
  58. $this->assertInternalType('resource', $result[0]);
  59. $this->assertInternalType('array', $result[1]);
  60. $this->assertSame($stream, $result[2]);
  61. curl_close($result[0]);
  62. $this->assertEquals('PUT', $_SERVER['_curl'][CURLOPT_CUSTOMREQUEST]);
  63. $this->assertEquals(
  64. 'http://http://127.0.0.1:8125/',
  65. $_SERVER['_curl'][CURLOPT_URL]
  66. );
  67. // Sends via post fields when the request is small enough
  68. $this->assertEquals('testing', $_SERVER['_curl'][CURLOPT_POSTFIELDS]);
  69. $this->assertEquals(0, $_SERVER['_curl'][CURLOPT_RETURNTRANSFER]);
  70. $this->assertEquals(0, $_SERVER['_curl'][CURLOPT_HEADER]);
  71. $this->assertEquals(150, $_SERVER['_curl'][CURLOPT_CONNECTTIMEOUT]);
  72. $this->assertInstanceOf('Closure', $_SERVER['_curl'][CURLOPT_HEADERFUNCTION]);
  73. if (defined('CURLOPT_PROTOCOLS')) {
  74. $this->assertEquals(
  75. CURLPROTO_HTTP | CURLPROTO_HTTPS,
  76. $_SERVER['_curl'][CURLOPT_PROTOCOLS]
  77. );
  78. }
  79. $this->assertContains('Expect:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
  80. $this->assertContains('Accept:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
  81. $this->assertContains('Content-Type:', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
  82. $this->assertContains('Hi: 123', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
  83. $this->assertContains('host: http://127.0.0.1:8125/', $_SERVER['_curl'][CURLOPT_HTTPHEADER]);
  84. }
  85. public function testSendsHeadRequests()
  86. {
  87. Server::flush();
  88. Server::enqueue([['status' => 200]]);
  89. $a = new CurlMultiHandler();
  90. $response = $a([
  91. 'http_method' => 'HEAD',
  92. 'headers' => ['host' => [Server::$host]],
  93. ]);
  94. $response->wait();
  95. $this->assertEquals(true, $_SERVER['_curl'][CURLOPT_NOBODY]);
  96. $checks = [CURLOPT_WRITEFUNCTION, CURLOPT_READFUNCTION, CURLOPT_FILE, CURLOPT_INFILE];
  97. foreach ($checks as $check) {
  98. $this->assertArrayNotHasKey($check, $_SERVER['_curl']);
  99. }
  100. $this->assertEquals('HEAD', Server::received()[0]['http_method']);
  101. }
  102. public function testCanAddCustomCurlOptions()
  103. {
  104. Server::flush();
  105. Server::enqueue([['status' => 200]]);
  106. $a = new CurlMultiHandler();
  107. $a([
  108. 'http_method' => 'GET',
  109. 'headers' => ['host' => [Server::$host]],
  110. 'client' => ['curl' => [CURLOPT_LOW_SPEED_LIMIT => 10]],
  111. ]);
  112. $this->assertEquals(10, $_SERVER['_curl'][CURLOPT_LOW_SPEED_LIMIT]);
  113. }
  114. /**
  115. * @expectedException \InvalidArgumentException
  116. * @expectedExceptionMessage SSL CA bundle not found: /does/not/exist
  117. */
  118. public function testValidatesVerify()
  119. {
  120. $f = new CurlFactory();
  121. $f([
  122. 'http_method' => 'GET',
  123. 'headers' => ['host' => ['foo.com']],
  124. 'client' => ['verify' => '/does/not/exist'],
  125. ]);
  126. }
  127. public function testCanSetVerifyToFile()
  128. {
  129. $f = new CurlFactory();
  130. $f([
  131. 'http_method' => 'GET',
  132. 'headers' => ['host' => ['foo.com']],
  133. 'client' => ['verify' => __FILE__],
  134. ]);
  135. $this->assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_CAINFO]);
  136. $this->assertEquals(2, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
  137. $this->assertEquals(true, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
  138. }
  139. public function testAddsVerifyAsTrue()
  140. {
  141. $f = new CurlFactory();
  142. $f([
  143. 'http_method' => 'GET',
  144. 'headers' => ['host' => ['foo.com']],
  145. 'client' => ['verify' => true],
  146. ]);
  147. $this->assertEquals(2, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
  148. $this->assertEquals(true, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
  149. $this->assertArrayNotHasKey(CURLOPT_CAINFO, $_SERVER['_curl']);
  150. }
  151. public function testCanDisableVerify()
  152. {
  153. $f = new CurlFactory();
  154. $f([
  155. 'http_method' => 'GET',
  156. 'headers' => ['host' => ['foo.com']],
  157. 'client' => ['verify' => false],
  158. ]);
  159. $this->assertEquals(0, $_SERVER['_curl'][CURLOPT_SSL_VERIFYHOST]);
  160. $this->assertEquals(false, $_SERVER['_curl'][CURLOPT_SSL_VERIFYPEER]);
  161. }
  162. public function testAddsProxy()
  163. {
  164. $f = new CurlFactory();
  165. $f([
  166. 'http_method' => 'GET',
  167. 'headers' => ['host' => ['foo.com']],
  168. 'client' => ['proxy' => 'http://bar.com'],
  169. ]);
  170. $this->assertEquals('http://bar.com', $_SERVER['_curl'][CURLOPT_PROXY]);
  171. }
  172. public function testAddsViaScheme()
  173. {
  174. $f = new CurlFactory();
  175. $f([
  176. 'http_method' => 'GET',
  177. 'scheme' => 'http',
  178. 'headers' => ['host' => ['foo.com']],
  179. 'client' => [
  180. 'proxy' => ['http' => 'http://bar.com', 'https' => 'https://t'],
  181. ],
  182. ]);
  183. $this->assertEquals('http://bar.com', $_SERVER['_curl'][CURLOPT_PROXY]);
  184. }
  185. /**
  186. * @expectedException \InvalidArgumentException
  187. * @expectedExceptionMessage SSL private key not found: /does/not/exist
  188. */
  189. public function testValidatesSslKey()
  190. {
  191. $f = new CurlFactory();
  192. $f([
  193. 'http_method' => 'GET',
  194. 'headers' => ['host' => ['foo.com']],
  195. 'client' => ['ssl_key' => '/does/not/exist'],
  196. ]);
  197. }
  198. public function testAddsSslKey()
  199. {
  200. $f = new CurlFactory();
  201. $f([
  202. 'http_method' => 'GET',
  203. 'headers' => ['host' => ['foo.com']],
  204. 'client' => ['ssl_key' => __FILE__],
  205. ]);
  206. $this->assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLKEY]);
  207. }
  208. public function testAddsSslKeyWithPassword()
  209. {
  210. $f = new CurlFactory();
  211. $f([
  212. 'http_method' => 'GET',
  213. 'headers' => ['host' => ['foo.com']],
  214. 'client' => ['ssl_key' => [__FILE__, 'test']],
  215. ]);
  216. $this->assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLKEY]);
  217. $this->assertEquals('test', $_SERVER['_curl'][CURLOPT_SSLKEYPASSWD]);
  218. }
  219. /**
  220. * @expectedException \InvalidArgumentException
  221. * @expectedExceptionMessage SSL certificate not found: /does/not/exist
  222. */
  223. public function testValidatesCert()
  224. {
  225. $f = new CurlFactory();
  226. $f([
  227. 'http_method' => 'GET',
  228. 'headers' => ['host' => ['foo.com']],
  229. 'client' => ['cert' => '/does/not/exist'],
  230. ]);
  231. }
  232. public function testAddsCert()
  233. {
  234. $f = new CurlFactory();
  235. $f([
  236. 'http_method' => 'GET',
  237. 'headers' => ['host' => ['foo.com']],
  238. 'client' => ['cert' => __FILE__],
  239. ]);
  240. $this->assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLCERT]);
  241. }
  242. public function testAddsCertWithPassword()
  243. {
  244. $f = new CurlFactory();
  245. $f([
  246. 'http_method' => 'GET',
  247. 'headers' => ['host' => ['foo.com']],
  248. 'client' => ['cert' => [__FILE__, 'test']],
  249. ]);
  250. $this->assertEquals(__FILE__, $_SERVER['_curl'][CURLOPT_SSLCERT]);
  251. $this->assertEquals('test', $_SERVER['_curl'][CURLOPT_SSLCERTPASSWD]);
  252. }
  253. /**
  254. * @expectedException \InvalidArgumentException
  255. * @expectedExceptionMessage progress client option must be callable
  256. */
  257. public function testValidatesProgress()
  258. {
  259. $f = new CurlFactory();
  260. $f([
  261. 'http_method' => 'GET',
  262. 'headers' => ['host' => ['foo.com']],
  263. 'client' => ['progress' => 'foo'],
  264. ]);
  265. }
  266. public function testEmitsDebugInfoToStream()
  267. {
  268. $res = fopen('php://memory', 'r+');
  269. Server::flush();
  270. Server::enqueue([['status' => 200]]);
  271. $a = new CurlMultiHandler();
  272. $response = $a([
  273. 'http_method' => 'HEAD',
  274. 'headers' => ['host' => [Server::$host]],
  275. 'client' => ['debug' => $res],
  276. ]);
  277. $response->wait();
  278. rewind($res);
  279. $output = str_replace("\r", '', stream_get_contents($res));
  280. $this->assertContains(
  281. "> HEAD / HTTP/1.1\nhost: 127.0.0.1:8125\n\n",
  282. $output
  283. );
  284. $this->assertContains("< HTTP/1.1 200", $output);
  285. fclose($res);
  286. }
  287. public function testEmitsProgressToFunction()
  288. {
  289. Server::flush();
  290. Server::enqueue([['status' => 200]]);
  291. $a = new CurlMultiHandler();
  292. $called = [];
  293. $response = $a([
  294. 'http_method' => 'HEAD',
  295. 'headers' => ['host' => [Server::$host]],
  296. 'client' => [
  297. 'progress' => function () use (&$called) {
  298. $called[] = func_get_args();
  299. },
  300. ],
  301. ]);
  302. $response->wait();
  303. $this->assertNotEmpty($called);
  304. foreach ($called as $call) {
  305. $this->assertCount(4, $call);
  306. }
  307. }
  308. private function addDecodeResponse($withEncoding = true)
  309. {
  310. $content = gzencode('test');
  311. $response = [
  312. 'status' => 200,
  313. 'reason' => 'OK',
  314. 'headers' => ['Content-Length' => [strlen($content)]],
  315. 'body' => $content,
  316. ];
  317. if ($withEncoding) {
  318. $response['headers']['Content-Encoding'] = ['gzip'];
  319. }
  320. Server::flush();
  321. Server::enqueue([$response]);
  322. return $content;
  323. }
  324. public function testDecodesGzippedResponses()
  325. {
  326. $this->addDecodeResponse();
  327. $handler = new CurlMultiHandler();
  328. $response = $handler([
  329. 'http_method' => 'GET',
  330. 'headers' => ['host' => [Server::$host]],
  331. 'client' => ['decode_content' => true],
  332. ]);
  333. $response->wait();
  334. $this->assertEquals('test', Core::body($response));
  335. $this->assertEquals('', $_SERVER['_curl'][CURLOPT_ENCODING]);
  336. $sent = Server::received()[0];
  337. $this->assertNull(Core::header($sent, 'Accept-Encoding'));
  338. }
  339. public function testDecodesGzippedResponsesWithHeader()
  340. {
  341. $this->addDecodeResponse();
  342. $handler = new CurlMultiHandler();
  343. $response = $handler([
  344. 'http_method' => 'GET',
  345. 'headers' => [
  346. 'host' => [Server::$host],
  347. 'Accept-Encoding' => ['gzip'],
  348. ],
  349. 'client' => ['decode_content' => true],
  350. ]);
  351. $response->wait();
  352. $this->assertEquals('gzip', $_SERVER['_curl'][CURLOPT_ENCODING]);
  353. $sent = Server::received()[0];
  354. $this->assertEquals('gzip', Core::header($sent, 'Accept-Encoding'));
  355. $this->assertEquals('test', Core::body($response));
  356. }
  357. public function testDoesNotForceDecode()
  358. {
  359. $content = $this->addDecodeResponse();
  360. $handler = new CurlMultiHandler();
  361. $response = $handler([
  362. 'http_method' => 'GET',
  363. 'headers' => ['host' => [Server::$host]],
  364. 'client' => ['decode_content' => false],
  365. ]);
  366. $response->wait();
  367. $sent = Server::received()[0];
  368. $this->assertNull(Core::header($sent, 'Accept-Encoding'));
  369. $this->assertEquals($content, Core::body($response));
  370. }
  371. public function testProtocolVersion()
  372. {
  373. Server::flush();
  374. Server::enqueue([['status' => 200]]);
  375. $a = new CurlMultiHandler();
  376. $a([
  377. 'http_method' => 'GET',
  378. 'headers' => ['host' => [Server::$host]],
  379. 'version' => 1.0,
  380. ]);
  381. $this->assertEquals(CURL_HTTP_VERSION_1_0, $_SERVER['_curl'][CURLOPT_HTTP_VERSION]);
  382. }
  383. /**
  384. * @expectedException \InvalidArgumentException
  385. */
  386. public function testValidatesSaveTo()
  387. {
  388. $handler = new CurlMultiHandler();
  389. $handler([
  390. 'http_method' => 'GET',
  391. 'headers' => ['host' => [Server::$host]],
  392. 'client' => ['save_to' => true],
  393. ]);
  394. }
  395. public function testSavesToStream()
  396. {
  397. $stream = fopen('php://memory', 'r+');
  398. $this->addDecodeResponse();
  399. $handler = new CurlMultiHandler();
  400. $response = $handler([
  401. 'http_method' => 'GET',
  402. 'headers' => ['host' => [Server::$host]],
  403. 'client' => [
  404. 'decode_content' => true,
  405. 'save_to' => $stream,
  406. ],
  407. ]);
  408. $response->wait();
  409. rewind($stream);
  410. $this->assertEquals('test', stream_get_contents($stream));
  411. }
  412. public function testSavesToGuzzleStream()
  413. {
  414. $stream = Stream::factory();
  415. $this->addDecodeResponse();
  416. $handler = new CurlMultiHandler();
  417. $response = $handler([
  418. 'http_method' => 'GET',
  419. 'headers' => ['host' => [Server::$host]],
  420. 'client' => [
  421. 'decode_content' => true,
  422. 'save_to' => $stream,
  423. ],
  424. ]);
  425. $response->wait();
  426. $this->assertEquals('test', (string) $stream);
  427. }
  428. public function testSavesToFileOnDisk()
  429. {
  430. $tmpfile = tempnam(sys_get_temp_dir(), 'testfile');
  431. $this->addDecodeResponse();
  432. $handler = new CurlMultiHandler();
  433. $response = $handler([
  434. 'http_method' => 'GET',
  435. 'headers' => ['host' => [Server::$host]],
  436. 'client' => [
  437. 'decode_content' => true,
  438. 'save_to' => $tmpfile,
  439. ],
  440. ]);
  441. $response->wait();
  442. $this->assertEquals('test', file_get_contents($tmpfile));
  443. unlink($tmpfile);
  444. }
  445. /**
  446. * @expectedException \InvalidArgumentException
  447. */
  448. public function testValidatesBody()
  449. {
  450. $handler = new CurlMultiHandler();
  451. $handler([
  452. 'http_method' => 'GET',
  453. 'headers' => ['host' => [Server::$host]],
  454. 'body' => false,
  455. ]);
  456. }
  457. public function testAddsLargePayloadFromStreamWithNoSizeUsingChunked()
  458. {
  459. $stream = Stream::factory('foo');
  460. $stream = FnStream::decorate($stream, [
  461. 'getSize' => function () {
  462. return null;
  463. }
  464. ]);
  465. $this->addDecodeResponse();
  466. $handler = new CurlMultiHandler();
  467. $response = $handler([
  468. 'http_method' => 'GET',
  469. 'headers' => ['host' => [Server::$host]],
  470. 'body' => $stream,
  471. ]);
  472. $response->wait();
  473. $sent = Server::received()[0];
  474. $this->assertEquals('chunked', Core::header($sent, 'Transfer-Encoding'));
  475. $this->assertNull(Core::header($sent, 'Content-Length'));
  476. $this->assertEquals('foo', $sent['body']);
  477. }
  478. public function testAddsPayloadFromIterator()
  479. {
  480. $iter = new \ArrayIterator(['f', 'o', 'o']);
  481. $this->addDecodeResponse();
  482. $handler = new CurlMultiHandler();
  483. $response = $handler([
  484. 'http_method' => 'GET',
  485. 'headers' => ['host' => [Server::$host]],
  486. 'body' => $iter,
  487. ]);
  488. $response->wait();
  489. $sent = Server::received()[0];
  490. $this->assertEquals('chunked', Core::header($sent, 'Transfer-Encoding'));
  491. $this->assertNull(Core::header($sent, 'Content-Length'));
  492. $this->assertEquals('foo', $sent['body']);
  493. }
  494. public function testAddsPayloadFromResource()
  495. {
  496. $res = fopen('php://memory', 'r+');
  497. $data = str_repeat('.', 1000000);
  498. fwrite($res, $data);
  499. rewind($res);
  500. $this->addDecodeResponse();
  501. $handler = new CurlMultiHandler();
  502. $response = $handler([
  503. 'http_method' => 'GET',
  504. 'headers' => [
  505. 'host' => [Server::$host],
  506. 'content-length' => [1000000],
  507. ],
  508. 'body' => $res,
  509. ]);
  510. $response->wait();
  511. $sent = Server::received()[0];
  512. $this->assertNull(Core::header($sent, 'Transfer-Encoding'));
  513. $this->assertEquals(1000000, Core::header($sent, 'Content-Length'));
  514. $this->assertEquals($data, $sent['body']);
  515. }
  516. public function testAddsContentLengthFromStream()
  517. {
  518. $stream = Stream::factory('foo');
  519. $this->addDecodeResponse();
  520. $handler = new CurlMultiHandler();
  521. $response = $handler([
  522. 'http_method' => 'GET',
  523. 'headers' => ['host' => [Server::$host]],
  524. 'body' => $stream,
  525. ]);
  526. $response->wait();
  527. $sent = Server::received()[0];
  528. $this->assertEquals(3, Core::header($sent, 'Content-Length'));
  529. $this->assertNull(Core::header($sent, 'Transfer-Encoding'));
  530. $this->assertEquals('foo', $sent['body']);
  531. }
  532. public function testDoesNotAddMultipleContentLengthHeaders()
  533. {
  534. $this->addDecodeResponse();
  535. $handler = new CurlMultiHandler();
  536. $response = $handler([
  537. 'http_method' => 'GET',
  538. 'headers' => [
  539. 'host' => [Server::$host],
  540. 'content-length' => [3],
  541. ],
  542. 'body' => 'foo',
  543. ]);
  544. $response->wait();
  545. $sent = Server::received()[0];
  546. $this->assertEquals(3, Core::header($sent, 'Content-Length'));
  547. $this->assertNull(Core::header($sent, 'Transfer-Encoding'));
  548. $this->assertEquals('foo', $sent['body']);
  549. }
  550. public function testSendsPostWithNoBodyOrDefaultContentType()
  551. {
  552. Server::flush();
  553. Server::enqueue([['status' => 200]]);
  554. $handler = new CurlMultiHandler();
  555. $response = $handler([
  556. 'http_method' => 'POST',
  557. 'uri' => '/',
  558. 'headers' => ['host' => [Server::$host]],
  559. ]);
  560. $response->wait();
  561. $received = Server::received()[0];
  562. $this->assertEquals('POST', $received['http_method']);
  563. $this->assertNull(Core::header($received, 'content-type'));
  564. $this->assertSame('0', Core::firstHeader($received, 'content-length'));
  565. }
  566. public function testParseProtocolVersion()
  567. {
  568. $res = CurlFactory::createResponse(
  569. function () {},
  570. [],
  571. ['curl' => ['errno' => null]],
  572. ['HTTP/1.1 200 Ok'],
  573. null
  574. );
  575. $this->assertSame('1.1', $res['version']);
  576. }
  577. public function testFailsWhenNoResponseAndNoBody()
  578. {
  579. $res = CurlFactory::createResponse(function () {}, [], [], [], null);
  580. $this->assertInstanceOf('GuzzleHttp\Ring\Exception\RingException', $res['error']);
  581. $this->assertContains(
  582. 'No response was received for a request with no body',
  583. $res['error']->getMessage()
  584. );
  585. }
  586. public function testFailsWhenCannotRewindRetry()
  587. {
  588. $res = CurlFactory::createResponse(function () {}, [
  589. 'body' => new NoSeekStream(Stream::factory('foo'))
  590. ], [], [], null);
  591. $this->assertInstanceOf('GuzzleHttp\Ring\Exception\RingException', $res['error']);
  592. $this->assertContains(
  593. 'rewind the request body failed',
  594. $res['error']->getMessage()
  595. );
  596. }
  597. public function testRetriesWhenBodyCanBeRewound()
  598. {
  599. $callHandler = $called = false;
  600. $res = CurlFactory::createResponse(function () use (&$callHandler) {
  601. $callHandler = true;
  602. return ['status' => 200];
  603. }, [
  604. 'body' => FnStream::decorate(Stream::factory('test'), [
  605. 'seek' => function () use (&$called) {
  606. $called = true;
  607. return true;
  608. }
  609. ])
  610. ], [], [], null);
  611. $this->assertTrue($callHandler);
  612. $this->assertTrue($called);
  613. $this->assertEquals('200', $res['status']);
  614. }
  615. public function testFailsWhenRetryMoreThanThreeTimes()
  616. {
  617. $call = 0;
  618. $mock = new MockHandler(function (array $request) use (&$mock, &$call) {
  619. $call++;
  620. return CurlFactory::createResponse($mock, $request, [], [], null);
  621. });
  622. $response = $mock([
  623. 'http_method' => 'GET',
  624. 'body' => 'test',
  625. ]);
  626. $this->assertEquals(3, $call);
  627. $this->assertArrayHasKey('error', $response);
  628. $this->assertContains(
  629. 'The cURL request was retried 3 times',
  630. $response['error']->getMessage()
  631. );
  632. }
  633. public function testHandles100Continue()
  634. {
  635. Server::flush();
  636. Server::enqueue([
  637. [
  638. 'status' => '200',
  639. 'reason' => 'OK',
  640. 'headers' => [
  641. 'Test' => ['Hello'],
  642. 'Content-Length' => ['4'],
  643. ],
  644. 'body' => 'test',
  645. ],
  646. ]);
  647. $request = [
  648. 'http_method' => 'PUT',
  649. 'headers' => [
  650. 'Host' => [Server::$host],
  651. 'Expect' => ['100-Continue'],
  652. ],
  653. 'body' => 'test',
  654. ];
  655. $handler = new CurlMultiHandler();
  656. $response = $handler($request)->wait();
  657. $this->assertEquals(200, $response['status']);
  658. $this->assertEquals('OK', $response['reason']);
  659. $this->assertEquals(['Hello'], $response['headers']['Test']);
  660. $this->assertEquals(['4'], $response['headers']['Content-Length']);
  661. $this->assertEquals('test', Core::body($response));
  662. }
  663. public function testCreatesConnectException()
  664. {
  665. $m = new \ReflectionMethod('GuzzleHttp\Ring\Client\CurlFactory', 'createErrorResponse');
  666. $m->setAccessible(true);
  667. $response = $m->invoke(
  668. null,
  669. function () {},
  670. [],
  671. [
  672. 'err_message' => 'foo',
  673. 'curl' => [
  674. 'errno' => CURLE_COULDNT_CONNECT,
  675. ]
  676. ]
  677. );
  678. $this->assertInstanceOf('GuzzleHttp\Ring\Exception\ConnectException', $response['error']);
  679. }
  680. public function testParsesLastResponseOnly()
  681. {
  682. $response1 = [
  683. 'status' => 301,
  684. 'headers' => [
  685. 'Content-Length' => ['0'],
  686. 'Location' => ['/foo']
  687. ]
  688. ];
  689. $response2 = [
  690. 'status' => 200,
  691. 'headers' => [
  692. 'Content-Length' => ['0'],
  693. 'Foo' => ['bar']
  694. ]
  695. ];
  696. Server::flush();
  697. Server::enqueue([$response1, $response2]);
  698. $a = new CurlMultiHandler();
  699. $response = $a([
  700. 'http_method' => 'GET',
  701. 'headers' => ['Host' => [Server::$host]],
  702. 'client' => [
  703. 'curl' => [
  704. CURLOPT_FOLLOWLOCATION => true
  705. ]
  706. ]
  707. ])->wait();
  708. $this->assertEquals(1, $response['transfer_stats']['redirect_count']);
  709. $this->assertEquals('http://127.0.0.1:8125/foo', $response['effective_url']);
  710. $this->assertEquals(['bar'], $response['headers']['Foo']);
  711. $this->assertEquals(200, $response['status']);
  712. $this->assertFalse(Core::hasHeader($response, 'Location'));
  713. }
  714. public function testMaintainsMultiHeaderOrder()
  715. {
  716. Server::flush();
  717. Server::enqueue([
  718. [
  719. 'status' => 200,
  720. 'headers' => [
  721. 'Content-Length' => ['0'],
  722. 'Foo' => ['a', 'b'],
  723. 'foo' => ['c', 'd'],
  724. ]
  725. ]
  726. ]);
  727. $a = new CurlMultiHandler();
  728. $response = $a([
  729. 'http_method' => 'GET',
  730. 'headers' => ['Host' => [Server::$host]]
  731. ])->wait();
  732. $this->assertEquals(
  733. ['a', 'b', 'c', 'd'],
  734. Core::headerLines($response, 'Foo')
  735. );
  736. }
  737. /**
  738. * @expectedException \RuntimeException
  739. * @expectedExceptionMessage Directory /path/to/does/not does not exist for save_to value of /path/to/does/not/exist.txt
  740. */
  741. public function testThrowsWhenDirNotFound()
  742. {
  743. $request = [
  744. 'http_method' => 'GET',
  745. 'headers' => ['host' => [Server::$url]],
  746. 'client' => ['save_to' => '/path/to/does/not/exist.txt'],
  747. ];
  748. $f = new CurlFactory();
  749. $f($request);
  750. }
  751. }
  752. }