HttpClientTestCase.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Contracts\HttpClient\Test;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. /**
  17. * A reference test suite for HttpClientInterface implementations.
  18. *
  19. * @experimental in 1.1
  20. */
  21. abstract class HttpClientTestCase extends TestCase
  22. {
  23. private static $server;
  24. public static function setUpBeforeClass(): void
  25. {
  26. TestHttpServer::start();
  27. }
  28. abstract protected function getHttpClient(string $testCase): HttpClientInterface;
  29. public function testGetRequest()
  30. {
  31. $client = $this->getHttpClient(__FUNCTION__);
  32. $response = $client->request('GET', 'http://localhost:8057', [
  33. 'headers' => ['Foo' => 'baR'],
  34. 'user_data' => $data = new \stdClass(),
  35. ]);
  36. $this->assertSame([], $response->getInfo('response_headers'));
  37. $this->assertSame($data, $response->getInfo()['user_data']);
  38. $this->assertSame(200, $response->getStatusCode());
  39. $info = $response->getInfo();
  40. $this->assertNull($info['error']);
  41. $this->assertSame(0, $info['redirect_count']);
  42. $this->assertSame('HTTP/1.1 200 OK', $info['response_headers'][0]);
  43. $this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
  44. $this->assertSame('http://localhost:8057/', $info['url']);
  45. $headers = $response->getHeaders();
  46. $this->assertSame('localhost:8057', $headers['host'][0]);
  47. $this->assertSame(['application/json'], $headers['content-type']);
  48. $body = json_decode($response->getContent(), true);
  49. $this->assertSame($body, $response->toArray());
  50. $this->assertSame('HTTP/1.1', $body['SERVER_PROTOCOL']);
  51. $this->assertSame('/', $body['REQUEST_URI']);
  52. $this->assertSame('GET', $body['REQUEST_METHOD']);
  53. $this->assertSame('localhost:8057', $body['HTTP_HOST']);
  54. $this->assertSame('baR', $body['HTTP_FOO']);
  55. $response = $client->request('GET', 'http://localhost:8057/length-broken');
  56. $this->expectException(TransportExceptionInterface::class);
  57. $response->getContent();
  58. }
  59. public function testHeadRequest()
  60. {
  61. $client = $this->getHttpClient(__FUNCTION__);
  62. $response = $client->request('HEAD', 'http://localhost:8057/head', [
  63. 'headers' => ['Foo' => 'baR'],
  64. 'user_data' => $data = new \stdClass(),
  65. 'buffer' => false,
  66. ]);
  67. $this->assertSame([], $response->getInfo('response_headers'));
  68. $this->assertSame(200, $response->getStatusCode());
  69. $info = $response->getInfo();
  70. $this->assertSame('HTTP/1.1 200 OK', $info['response_headers'][0]);
  71. $this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
  72. $headers = $response->getHeaders();
  73. $this->assertSame('localhost:8057', $headers['host'][0]);
  74. $this->assertSame(['application/json'], $headers['content-type']);
  75. $this->assertTrue(0 < $headers['content-length'][0]);
  76. $this->assertSame('', $response->getContent());
  77. }
  78. public function testNonBufferedGetRequest()
  79. {
  80. $client = $this->getHttpClient(__FUNCTION__);
  81. $response = $client->request('GET', 'http://localhost:8057', [
  82. 'buffer' => false,
  83. 'headers' => ['Foo' => 'baR'],
  84. ]);
  85. $body = $response->toArray();
  86. $this->assertSame('baR', $body['HTTP_FOO']);
  87. $this->expectException(TransportExceptionInterface::class);
  88. $response->getContent();
  89. }
  90. public function testBufferSink()
  91. {
  92. $sink = fopen('php://temp', 'w+');
  93. $client = $this->getHttpClient(__FUNCTION__);
  94. $response = $client->request('GET', 'http://localhost:8057', [
  95. 'buffer' => $sink,
  96. 'headers' => ['Foo' => 'baR'],
  97. ]);
  98. $body = $response->toArray();
  99. $this->assertSame('baR', $body['HTTP_FOO']);
  100. rewind($sink);
  101. $sink = stream_get_contents($sink);
  102. $this->assertSame($sink, $response->getContent());
  103. }
  104. public function testConditionalBuffering()
  105. {
  106. $client = $this->getHttpClient(__FUNCTION__);
  107. $response = $client->request('GET', 'http://localhost:8057');
  108. $firstContent = $response->getContent();
  109. $secondContent = $response->getContent();
  110. $this->assertSame($firstContent, $secondContent);
  111. $response = $client->request('GET', 'http://localhost:8057', ['buffer' => function () { return false; }]);
  112. $response->getContent();
  113. $this->expectException(TransportExceptionInterface::class);
  114. $response->getContent();
  115. }
  116. public function testReentrantBufferCallback()
  117. {
  118. $client = $this->getHttpClient(__FUNCTION__);
  119. $response = $client->request('GET', 'http://localhost:8057', ['buffer' => function () use (&$response) {
  120. $response->cancel();
  121. return true;
  122. }]);
  123. $this->assertSame(200, $response->getStatusCode());
  124. $this->expectException(TransportExceptionInterface::class);
  125. $response->getContent();
  126. }
  127. public function testThrowingBufferCallback()
  128. {
  129. $client = $this->getHttpClient(__FUNCTION__);
  130. $response = $client->request('GET', 'http://localhost:8057', ['buffer' => function () {
  131. throw new \Exception('Boo');
  132. }]);
  133. $this->assertSame(200, $response->getStatusCode());
  134. $this->expectException(TransportExceptionInterface::class);
  135. $this->expectExceptionMessage('Boo');
  136. $response->getContent();
  137. }
  138. public function testUnsupportedOption()
  139. {
  140. $client = $this->getHttpClient(__FUNCTION__);
  141. $this->expectException(\InvalidArgumentException::class);
  142. $client->request('GET', 'http://localhost:8057', [
  143. 'capture_peer_cert' => 1.0,
  144. ]);
  145. }
  146. public function testHttpVersion()
  147. {
  148. $client = $this->getHttpClient(__FUNCTION__);
  149. $response = $client->request('GET', 'http://localhost:8057', [
  150. 'http_version' => 1.0,
  151. ]);
  152. $this->assertSame(200, $response->getStatusCode());
  153. $this->assertSame('HTTP/1.0 200 OK', $response->getInfo('response_headers')[0]);
  154. $body = $response->toArray();
  155. $this->assertSame('HTTP/1.0', $body['SERVER_PROTOCOL']);
  156. $this->assertSame('GET', $body['REQUEST_METHOD']);
  157. $this->assertSame('/', $body['REQUEST_URI']);
  158. }
  159. public function testChunkedEncoding()
  160. {
  161. $client = $this->getHttpClient(__FUNCTION__);
  162. $response = $client->request('GET', 'http://localhost:8057/chunked');
  163. $this->assertSame(['chunked'], $response->getHeaders()['transfer-encoding']);
  164. $this->assertSame('Symfony is awesome!', $response->getContent());
  165. $response = $client->request('GET', 'http://localhost:8057/chunked-broken');
  166. $this->expectException(TransportExceptionInterface::class);
  167. $response->getContent();
  168. }
  169. public function testClientError()
  170. {
  171. $client = $this->getHttpClient(__FUNCTION__);
  172. $response = $client->request('GET', 'http://localhost:8057/404');
  173. $client->stream($response)->valid();
  174. $this->assertSame(404, $response->getInfo('http_code'));
  175. try {
  176. $response->getHeaders();
  177. $this->fail(ClientExceptionInterface::class.' expected');
  178. } catch (ClientExceptionInterface $e) {
  179. }
  180. try {
  181. $response->getContent();
  182. $this->fail(ClientExceptionInterface::class.' expected');
  183. } catch (ClientExceptionInterface $e) {
  184. }
  185. $this->assertSame(404, $response->getStatusCode());
  186. $this->assertSame(['application/json'], $response->getHeaders(false)['content-type']);
  187. $this->assertNotEmpty($response->getContent(false));
  188. $response = $client->request('GET', 'http://localhost:8057/404');
  189. try {
  190. foreach ($client->stream($response) as $chunk) {
  191. $this->assertTrue($chunk->isFirst());
  192. }
  193. $this->fail(ClientExceptionInterface::class.' expected');
  194. } catch (ClientExceptionInterface $e) {
  195. }
  196. }
  197. public function testIgnoreErrors()
  198. {
  199. $client = $this->getHttpClient(__FUNCTION__);
  200. $response = $client->request('GET', 'http://localhost:8057/404');
  201. $this->assertSame(404, $response->getStatusCode());
  202. }
  203. public function testDnsError()
  204. {
  205. $client = $this->getHttpClient(__FUNCTION__);
  206. $response = $client->request('GET', 'http://localhost:8057/301/bad-tld');
  207. try {
  208. $response->getStatusCode();
  209. $this->fail(TransportExceptionInterface::class.' expected');
  210. } catch (TransportExceptionInterface $e) {
  211. $this->addToAssertionCount(1);
  212. }
  213. try {
  214. $response->getStatusCode();
  215. $this->fail(TransportExceptionInterface::class.' still expected');
  216. } catch (TransportExceptionInterface $e) {
  217. $this->addToAssertionCount(1);
  218. }
  219. $response = $client->request('GET', 'http://localhost:8057/301/bad-tld');
  220. try {
  221. foreach ($client->stream($response) as $r => $chunk) {
  222. }
  223. $this->fail(TransportExceptionInterface::class.' expected');
  224. } catch (TransportExceptionInterface $e) {
  225. $this->addToAssertionCount(1);
  226. }
  227. $this->assertSame($response, $r);
  228. $this->assertNotNull($chunk->getError());
  229. $this->expectException(TransportExceptionInterface::class);
  230. foreach ($client->stream($response) as $chunk) {
  231. }
  232. }
  233. public function testInlineAuth()
  234. {
  235. $client = $this->getHttpClient(__FUNCTION__);
  236. $response = $client->request('GET', 'http://foo:bar%3Dbar@localhost:8057');
  237. $body = $response->toArray();
  238. $this->assertSame('foo', $body['PHP_AUTH_USER']);
  239. $this->assertSame('bar=bar', $body['PHP_AUTH_PW']);
  240. }
  241. public function testBadRequestBody()
  242. {
  243. $client = $this->getHttpClient(__FUNCTION__);
  244. $this->expectException(TransportExceptionInterface::class);
  245. $response = $client->request('POST', 'http://localhost:8057/', [
  246. 'body' => function () { yield []; },
  247. ]);
  248. $response->getStatusCode();
  249. }
  250. public function test304()
  251. {
  252. $client = $this->getHttpClient(__FUNCTION__);
  253. $response = $client->request('GET', 'http://localhost:8057/304', [
  254. 'headers' => ['If-Match' => '"abc"'],
  255. 'buffer' => false,
  256. ]);
  257. $this->assertSame(304, $response->getStatusCode());
  258. $this->assertSame('', $response->getContent(false));
  259. }
  260. public function testRedirects()
  261. {
  262. $client = $this->getHttpClient(__FUNCTION__);
  263. $response = $client->request('POST', 'http://localhost:8057/301', [
  264. 'auth_basic' => 'foo:bar',
  265. 'body' => function () {
  266. yield 'foo=bar';
  267. },
  268. ]);
  269. $body = $response->toArray();
  270. $this->assertSame('GET', $body['REQUEST_METHOD']);
  271. $this->assertSame('Basic Zm9vOmJhcg==', $body['HTTP_AUTHORIZATION']);
  272. $this->assertSame('http://localhost:8057/', $response->getInfo('url'));
  273. $this->assertSame(2, $response->getInfo('redirect_count'));
  274. $this->assertNull($response->getInfo('redirect_url'));
  275. $expected = [
  276. 'HTTP/1.1 301 Moved Permanently',
  277. 'Location: http://127.0.0.1:8057/302',
  278. 'Content-Type: application/json',
  279. 'HTTP/1.1 302 Found',
  280. 'Location: http://localhost:8057/',
  281. 'Content-Type: application/json',
  282. 'HTTP/1.1 200 OK',
  283. 'Content-Type: application/json',
  284. ];
  285. $filteredHeaders = array_values(array_filter($response->getInfo('response_headers'), function ($h) {
  286. return \in_array(substr($h, 0, 4), ['HTTP', 'Loca', 'Cont'], true) && 'Content-Encoding: gzip' !== $h;
  287. }));
  288. $this->assertSame($expected, $filteredHeaders);
  289. }
  290. public function testInvalidRedirect()
  291. {
  292. $client = $this->getHttpClient(__FUNCTION__);
  293. $response = $client->request('GET', 'http://localhost:8057/301/invalid');
  294. $this->assertSame(301, $response->getStatusCode());
  295. $this->assertSame(['//?foo=bar'], $response->getHeaders(false)['location']);
  296. $this->assertSame(0, $response->getInfo('redirect_count'));
  297. $this->assertNull($response->getInfo('redirect_url'));
  298. $this->expectException(RedirectionExceptionInterface::class);
  299. $response->getHeaders();
  300. }
  301. public function testRelativeRedirects()
  302. {
  303. $client = $this->getHttpClient(__FUNCTION__);
  304. $response = $client->request('GET', 'http://localhost:8057/302/relative');
  305. $body = $response->toArray();
  306. $this->assertSame('/', $body['REQUEST_URI']);
  307. $this->assertNull($response->getInfo('redirect_url'));
  308. $response = $client->request('GET', 'http://localhost:8057/302/relative', [
  309. 'max_redirects' => 0,
  310. ]);
  311. $this->assertSame(302, $response->getStatusCode());
  312. $this->assertSame('http://localhost:8057/', $response->getInfo('redirect_url'));
  313. }
  314. public function testRedirect307()
  315. {
  316. $client = $this->getHttpClient(__FUNCTION__);
  317. $response = $client->request('POST', 'http://localhost:8057/307', [
  318. 'body' => function () {
  319. yield 'foo=bar';
  320. },
  321. 'max_redirects' => 0,
  322. ]);
  323. $this->assertSame(307, $response->getStatusCode());
  324. $response = $client->request('POST', 'http://localhost:8057/307', [
  325. 'body' => 'foo=bar',
  326. ]);
  327. $body = $response->toArray();
  328. $this->assertSame(['foo' => 'bar', 'REQUEST_METHOD' => 'POST'], $body);
  329. }
  330. public function testMaxRedirects()
  331. {
  332. $client = $this->getHttpClient(__FUNCTION__);
  333. $response = $client->request('GET', 'http://localhost:8057/301', [
  334. 'max_redirects' => 1,
  335. 'auth_basic' => 'foo:bar',
  336. ]);
  337. try {
  338. $response->getHeaders();
  339. $this->fail(RedirectionExceptionInterface::class.' expected');
  340. } catch (RedirectionExceptionInterface $e) {
  341. }
  342. $this->assertSame(302, $response->getStatusCode());
  343. $this->assertSame(1, $response->getInfo('redirect_count'));
  344. $this->assertSame('http://localhost:8057/', $response->getInfo('redirect_url'));
  345. $expected = [
  346. 'HTTP/1.1 301 Moved Permanently',
  347. 'Location: http://127.0.0.1:8057/302',
  348. 'Content-Type: application/json',
  349. 'HTTP/1.1 302 Found',
  350. 'Location: http://localhost:8057/',
  351. 'Content-Type: application/json',
  352. ];
  353. $filteredHeaders = array_values(array_filter($response->getInfo('response_headers'), function ($h) {
  354. return \in_array(substr($h, 0, 4), ['HTTP', 'Loca', 'Cont'], true);
  355. }));
  356. $this->assertSame($expected, $filteredHeaders);
  357. }
  358. public function testStream()
  359. {
  360. $client = $this->getHttpClient(__FUNCTION__);
  361. $response = $client->request('GET', 'http://localhost:8057');
  362. $chunks = $client->stream($response);
  363. $result = [];
  364. foreach ($chunks as $r => $chunk) {
  365. if ($chunk->isTimeout()) {
  366. $result[] = 't';
  367. } elseif ($chunk->isLast()) {
  368. $result[] = 'l';
  369. } elseif ($chunk->isFirst()) {
  370. $result[] = 'f';
  371. }
  372. }
  373. $this->assertSame($response, $r);
  374. $this->assertSame(['f', 'l'], $result);
  375. $chunk = null;
  376. $i = 0;
  377. foreach ($client->stream($response) as $chunk) {
  378. ++$i;
  379. }
  380. $this->assertSame(1, $i);
  381. $this->assertTrue($chunk->isLast());
  382. }
  383. public function testAddToStream()
  384. {
  385. $client = $this->getHttpClient(__FUNCTION__);
  386. $r1 = $client->request('GET', 'http://localhost:8057');
  387. $completed = [];
  388. $pool = [$r1];
  389. while ($pool) {
  390. $chunks = $client->stream($pool);
  391. $pool = [];
  392. foreach ($chunks as $r => $chunk) {
  393. if (!$chunk->isLast()) {
  394. continue;
  395. }
  396. if ($r1 === $r) {
  397. $r2 = $client->request('GET', 'http://localhost:8057');
  398. $pool[] = $r2;
  399. }
  400. $completed[] = $r;
  401. }
  402. }
  403. $this->assertSame([$r1, $r2], $completed);
  404. }
  405. public function testCompleteTypeError()
  406. {
  407. $client = $this->getHttpClient(__FUNCTION__);
  408. $this->expectException(\TypeError::class);
  409. $client->stream(123);
  410. }
  411. public function testOnProgress()
  412. {
  413. $client = $this->getHttpClient(__FUNCTION__);
  414. $response = $client->request('POST', 'http://localhost:8057/post', [
  415. 'headers' => ['Content-Length' => 14],
  416. 'body' => 'foo=0123456789',
  417. 'on_progress' => function (...$state) use (&$steps) { $steps[] = $state; },
  418. ]);
  419. $body = $response->toArray();
  420. $this->assertSame(['foo' => '0123456789', 'REQUEST_METHOD' => 'POST'], $body);
  421. $this->assertSame([0, 0], \array_slice($steps[0], 0, 2));
  422. $lastStep = \array_slice($steps, -1)[0];
  423. $this->assertSame([57, 57], \array_slice($lastStep, 0, 2));
  424. $this->assertSame('http://localhost:8057/post', $steps[0][2]['url']);
  425. }
  426. public function testPostJson()
  427. {
  428. $client = $this->getHttpClient(__FUNCTION__);
  429. $response = $client->request('POST', 'http://localhost:8057/post', [
  430. 'json' => ['foo' => 'bar'],
  431. ]);
  432. $body = $response->toArray();
  433. $this->assertStringContainsString('json', $body['content-type']);
  434. unset($body['content-type']);
  435. $this->assertSame(['foo' => 'bar', 'REQUEST_METHOD' => 'POST'], $body);
  436. }
  437. public function testPostArray()
  438. {
  439. $client = $this->getHttpClient(__FUNCTION__);
  440. $response = $client->request('POST', 'http://localhost:8057/post', [
  441. 'body' => ['foo' => 'bar'],
  442. ]);
  443. $this->assertSame(['foo' => 'bar', 'REQUEST_METHOD' => 'POST'], $response->toArray());
  444. }
  445. public function testPostResource()
  446. {
  447. $client = $this->getHttpClient(__FUNCTION__);
  448. $h = fopen('php://temp', 'w+');
  449. fwrite($h, 'foo=0123456789');
  450. rewind($h);
  451. $response = $client->request('POST', 'http://localhost:8057/post', [
  452. 'body' => $h,
  453. ]);
  454. $body = $response->toArray();
  455. $this->assertSame(['foo' => '0123456789', 'REQUEST_METHOD' => 'POST'], $body);
  456. }
  457. public function testPostCallback()
  458. {
  459. $client = $this->getHttpClient(__FUNCTION__);
  460. $response = $client->request('POST', 'http://localhost:8057/post', [
  461. 'body' => function () {
  462. yield 'foo';
  463. yield '';
  464. yield '=';
  465. yield '0123456789';
  466. },
  467. ]);
  468. $this->assertSame(['foo' => '0123456789', 'REQUEST_METHOD' => 'POST'], $response->toArray());
  469. }
  470. public function testCancel()
  471. {
  472. $client = $this->getHttpClient(__FUNCTION__);
  473. $response = $client->request('GET', 'http://localhost:8057/timeout-header');
  474. $response->cancel();
  475. $this->expectException(TransportExceptionInterface::class);
  476. $response->getHeaders();
  477. }
  478. public function testInfoOnCanceledResponse()
  479. {
  480. $client = $this->getHttpClient(__FUNCTION__);
  481. $response = $client->request('GET', 'http://localhost:8057/timeout-header');
  482. $this->assertFalse($response->getInfo('canceled'));
  483. $response->cancel();
  484. $this->assertTrue($response->getInfo('canceled'));
  485. }
  486. public function testCancelInStream()
  487. {
  488. $client = $this->getHttpClient(__FUNCTION__);
  489. $response = $client->request('GET', 'http://localhost:8057/404');
  490. foreach ($client->stream($response) as $chunk) {
  491. $response->cancel();
  492. }
  493. $this->expectException(TransportExceptionInterface::class);
  494. foreach ($client->stream($response) as $chunk) {
  495. }
  496. }
  497. public function testOnProgressCancel()
  498. {
  499. $client = $this->getHttpClient(__FUNCTION__);
  500. $response = $client->request('GET', 'http://localhost:8057/timeout-body', [
  501. 'on_progress' => function ($dlNow) {
  502. if (0 < $dlNow) {
  503. throw new \Exception('Aborting the request');
  504. }
  505. },
  506. ]);
  507. try {
  508. foreach ($client->stream([$response]) as $chunk) {
  509. }
  510. $this->fail(ClientExceptionInterface::class.' expected');
  511. } catch (TransportExceptionInterface $e) {
  512. $this->assertSame('Aborting the request', $e->getPrevious()->getMessage());
  513. }
  514. $this->assertNotNull($response->getInfo('error'));
  515. $this->expectException(TransportExceptionInterface::class);
  516. $response->getContent();
  517. }
  518. public function testOnProgressError()
  519. {
  520. $client = $this->getHttpClient(__FUNCTION__);
  521. $response = $client->request('GET', 'http://localhost:8057/timeout-body', [
  522. 'on_progress' => function ($dlNow) {
  523. if (0 < $dlNow) {
  524. throw new \Error('BUG');
  525. }
  526. },
  527. ]);
  528. try {
  529. foreach ($client->stream([$response]) as $chunk) {
  530. }
  531. $this->fail('Error expected');
  532. } catch (\Error $e) {
  533. $this->assertSame('BUG', $e->getMessage());
  534. }
  535. $this->assertNotNull($response->getInfo('error'));
  536. $this->expectException(TransportExceptionInterface::class);
  537. $response->getContent();
  538. }
  539. public function testResolve()
  540. {
  541. $client = $this->getHttpClient(__FUNCTION__);
  542. $response = $client->request('GET', 'http://symfony.com:8057/', [
  543. 'resolve' => ['symfony.com' => '127.0.0.1'],
  544. ]);
  545. $this->assertSame(200, $response->getStatusCode());
  546. $this->assertSame(200, $client->request('GET', 'http://symfony.com:8057/')->getStatusCode());
  547. $response = null;
  548. $this->expectException(TransportExceptionInterface::class);
  549. $client->request('GET', 'http://symfony.com:8057/', ['timeout' => 1]);
  550. }
  551. public function testNotATimeout()
  552. {
  553. $client = $this->getHttpClient(__FUNCTION__);
  554. $response = $client->request('GET', 'http://localhost:8057/timeout-header', [
  555. 'timeout' => 0.9,
  556. ]);
  557. sleep(1);
  558. $this->assertSame(200, $response->getStatusCode());
  559. }
  560. public function testTimeoutOnAccess()
  561. {
  562. $client = $this->getHttpClient(__FUNCTION__);
  563. $response = $client->request('GET', 'http://localhost:8057/timeout-header', [
  564. 'timeout' => 0.1,
  565. ]);
  566. $this->expectException(TransportExceptionInterface::class);
  567. $response->getHeaders();
  568. }
  569. public function testTimeoutOnStream()
  570. {
  571. usleep(300000); // wait for the previous test to release the server
  572. $client = $this->getHttpClient(__FUNCTION__);
  573. $response = $client->request('GET', 'http://localhost:8057/timeout-body');
  574. $this->assertSame(200, $response->getStatusCode());
  575. $chunks = $client->stream([$response], 0.2);
  576. $result = [];
  577. foreach ($chunks as $r => $chunk) {
  578. if ($chunk->isTimeout()) {
  579. $result[] = 't';
  580. } else {
  581. $result[] = $chunk->getContent();
  582. }
  583. }
  584. $this->assertSame(['<1>', 't'], $result);
  585. $chunks = $client->stream([$response]);
  586. foreach ($chunks as $r => $chunk) {
  587. $this->assertSame('<2>', $chunk->getContent());
  588. $this->assertSame('<1><2>', $r->getContent());
  589. return;
  590. }
  591. $this->fail('The response should have completed');
  592. }
  593. public function testUncheckedTimeoutThrows()
  594. {
  595. $client = $this->getHttpClient(__FUNCTION__);
  596. $response = $client->request('GET', 'http://localhost:8057/timeout-body');
  597. $chunks = $client->stream([$response], 0.1);
  598. $this->expectException(TransportExceptionInterface::class);
  599. foreach ($chunks as $r => $chunk) {
  600. }
  601. }
  602. public function testDestruct()
  603. {
  604. $client = $this->getHttpClient(__FUNCTION__);
  605. $start = microtime(true);
  606. $client->request('GET', 'http://localhost:8057/timeout-long');
  607. $client = null;
  608. $duration = microtime(true) - $start;
  609. $this->assertGreaterThan(1, $duration);
  610. $this->assertLessThan(4, $duration);
  611. }
  612. public function testProxy()
  613. {
  614. $client = $this->getHttpClient(__FUNCTION__);
  615. $response = $client->request('GET', 'http://localhost:8057/', [
  616. 'proxy' => 'http://localhost:8057',
  617. ]);
  618. $body = $response->toArray();
  619. $this->assertSame('localhost:8057', $body['HTTP_HOST']);
  620. $this->assertRegexp('#^http://(localhost|127\.0\.0\.1):8057/$#', $body['REQUEST_URI']);
  621. $response = $client->request('GET', 'http://localhost:8057/', [
  622. 'proxy' => 'http://foo:b%3Dar@localhost:8057',
  623. ]);
  624. $body = $response->toArray();
  625. $this->assertSame('Basic Zm9vOmI9YXI=', $body['HTTP_PROXY_AUTHORIZATION']);
  626. }
  627. public function testNoProxy()
  628. {
  629. putenv('no_proxy='.$_SERVER['no_proxy'] = 'example.com, localhost');
  630. try {
  631. $client = $this->getHttpClient(__FUNCTION__);
  632. $response = $client->request('GET', 'http://localhost:8057/', [
  633. 'proxy' => 'http://localhost:8057',
  634. ]);
  635. $body = $response->toArray();
  636. $this->assertSame('HTTP/1.1', $body['SERVER_PROTOCOL']);
  637. $this->assertSame('/', $body['REQUEST_URI']);
  638. $this->assertSame('GET', $body['REQUEST_METHOD']);
  639. } finally {
  640. putenv('no_proxy');
  641. unset($_SERVER['no_proxy']);
  642. }
  643. }
  644. /**
  645. * @requires extension zlib
  646. */
  647. public function testAutoEncodingRequest()
  648. {
  649. $client = $this->getHttpClient(__FUNCTION__);
  650. $response = $client->request('GET', 'http://localhost:8057');
  651. $this->assertSame(200, $response->getStatusCode());
  652. $headers = $response->getHeaders();
  653. $this->assertSame(['Accept-Encoding'], $headers['vary']);
  654. $this->assertStringContainsString('gzip', $headers['content-encoding'][0]);
  655. $body = $response->toArray();
  656. $this->assertStringContainsString('gzip', $body['HTTP_ACCEPT_ENCODING']);
  657. }
  658. public function testBaseUri()
  659. {
  660. $client = $this->getHttpClient(__FUNCTION__);
  661. $response = $client->request('GET', '../404', [
  662. 'base_uri' => 'http://localhost:8057/abc/',
  663. ]);
  664. $this->assertSame(404, $response->getStatusCode());
  665. $this->assertSame(['application/json'], $response->getHeaders(false)['content-type']);
  666. }
  667. public function testQuery()
  668. {
  669. $client = $this->getHttpClient(__FUNCTION__);
  670. $response = $client->request('GET', 'http://localhost:8057/?a=a', [
  671. 'query' => ['b' => 'b'],
  672. ]);
  673. $body = $response->toArray();
  674. $this->assertSame('GET', $body['REQUEST_METHOD']);
  675. $this->assertSame('/?a=a&b=b', $body['REQUEST_URI']);
  676. }
  677. public function testInformationalResponse()
  678. {
  679. $client = $this->getHttpClient(__FUNCTION__);
  680. $response = $client->request('GET', 'http://localhost:8057/103');
  681. $this->assertSame('Here the body', $response->getContent());
  682. $this->assertSame(200, $response->getStatusCode());
  683. }
  684. public function testInformationalResponseStream()
  685. {
  686. $client = $this->getHttpClient(__FUNCTION__);
  687. $response = $client->request('GET', 'http://localhost:8057/103');
  688. $chunks = [];
  689. foreach ($client->stream($response) as $chunk) {
  690. $chunks[] = $chunk;
  691. }
  692. $this->assertSame(103, $chunks[0]->getInformationalStatus()[0]);
  693. $this->assertSame(['</style.css>; rel=preload; as=style', '</script.js>; rel=preload; as=script'], $chunks[0]->getInformationalStatus()[1]['link']);
  694. $this->assertTrue($chunks[1]->isFirst());
  695. $this->assertSame('Here the body', $chunks[2]->getContent());
  696. $this->assertTrue($chunks[3]->isLast());
  697. $this->assertNull($chunks[3]->getInformationalStatus());
  698. $this->assertSame(['date', 'content-length'], array_keys($response->getHeaders()));
  699. $this->assertContains('Link: </style.css>; rel=preload; as=style', $response->getInfo('response_headers'));
  700. }
  701. /**
  702. * @requires extension zlib
  703. */
  704. public function testUserlandEncodingRequest()
  705. {
  706. $client = $this->getHttpClient(__FUNCTION__);
  707. $response = $client->request('GET', 'http://localhost:8057', [
  708. 'headers' => ['Accept-Encoding' => 'gzip'],
  709. ]);
  710. $headers = $response->getHeaders();
  711. $this->assertSame(['Accept-Encoding'], $headers['vary']);
  712. $this->assertStringContainsString('gzip', $headers['content-encoding'][0]);
  713. $body = $response->getContent();
  714. $this->assertSame("\x1F", $body[0]);
  715. $body = json_decode(gzdecode($body), true);
  716. $this->assertSame('gzip', $body['HTTP_ACCEPT_ENCODING']);
  717. }
  718. /**
  719. * @requires extension zlib
  720. */
  721. public function testGzipBroken()
  722. {
  723. $client = $this->getHttpClient(__FUNCTION__);
  724. $response = $client->request('GET', 'http://localhost:8057/gzip-broken');
  725. $this->expectException(TransportExceptionInterface::class);
  726. $response->getContent();
  727. }
  728. public function testMaxDuration()
  729. {
  730. $client = $this->getHttpClient(__FUNCTION__);
  731. $response = $client->request('GET', 'http://localhost:8057/max-duration', [
  732. 'max_duration' => 0.1,
  733. ]);
  734. $start = microtime(true);
  735. try {
  736. $response->getContent();
  737. } catch (TransportExceptionInterface $e) {
  738. $this->addToAssertionCount(1);
  739. }
  740. $duration = microtime(true) - $start;
  741. $this->assertLessThan(10, $duration);
  742. }
  743. }