Skip to content

Commit

Permalink
bug #36214 [HttpClient] Fix TraceableHttpClient::stream that not work…
Browse files Browse the repository at this point in the history
…s (l-vo)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[HttpClient] Fix TraceableHttpClient::stream that not works

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

The stream method when using the TraceableHttpClient seems not work.

I'm not sure to really understand what the previous implementation should do but this is an attempt to fix it.

Commits
-------

575f040 [HttpClient] Fix TraceableHttpClient::stream that not works
  • Loading branch information
fabpot committed Mar 27, 2020
2 parents 4dabd00 + 575f040 commit e0de6cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php
Expand Up @@ -13,9 +13,11 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;

class TraceableHttpClientTest extends TestCase
{
Expand Down Expand Up @@ -80,4 +82,18 @@ public function testItResetsTraces()
$sut->reset();
$this->assertCount(0, $sut->getTracedRequests());
}

public function testStream()
{
TestHttpServer::start();

$sut = new TraceableHttpClient(new NativeHttpClient());
$chunked = $sut->request('GET', 'http://localhost:8057/chunked');
$chunks = [];
foreach ($sut->stream($chunked) as $response) {
$chunks[] = $response->getContent();
}
$this->assertGreaterThan(1, \count($chunks));
$this->assertSame('Symfony is awesome!', implode('', $chunks));
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpClient/TraceableHttpClient.php
Expand Up @@ -67,18 +67,18 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof TraceableResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

return $this->client->stream(\Closure::bind(static function () use ($responses) {
foreach ($responses as $k => $r) {
if (!$r instanceof TraceableResponse) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
}

yield $k => $r->response;
}
}, null, TraceableResponse::class), $timeout);
}, null, TraceableResponse::class)(), $timeout);
}

public function getTracedRequests(): array
Expand Down

0 comments on commit e0de6cc

Please sign in to comment.