|
19 | 19 | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
20 | 20 | use Symfony\Contracts\HttpClient\HttpClientInterface;
|
21 | 21 | use Symfony\Contracts\HttpClient\ResponseInterface;
|
| 22 | +use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
22 | 23 |
|
23 | 24 | class AsyncDecoratorTraitTest extends NativeHttpClientTest
|
24 | 25 | {
|
25 |
| - protected function getHttpClient(string $testCase, \Closure $chunkFilter = null): HttpClientInterface |
| 26 | + protected function getHttpClient(string $testCase, \Closure
8000
span> $chunkFilter = null, HttpClientInterface $decoratedClient = null): HttpClientInterface |
26 | 27 | {
|
27 | 28 | if ('testHandleIsRemovedOnException' === $testCase) {
|
28 | 29 | $this->markTestSkipped("AsyncDecoratorTrait doesn't cache handles");
|
29 | 30 | }
|
30 | 31 |
|
31 | 32 | $chunkFilter = $chunkFilter ?? static function (ChunkInterface $chunk, AsyncContext $context) { yield $chunk; };
|
32 | 33 |
|
33 |
| - return new class(parent::getHttpClient($testCase), $chunkFilter) implements HttpClientInterface { |
| 34 | + return new class($decoratedClient ?? parent::getHttpClient($testCase), $chunkFilter) implements HttpClientInterface { |
34 | 35 | use AsyncDecoratorTrait;
|
35 | 36 |
|
36 | 37 | private $chunkFilter;
|
@@ -303,4 +304,30 @@ public function testMultipleYieldInInitializer()
|
303 | 304 | $this->assertSame(404, $response->getStatusCode());
|
304 | 305 | $this->assertStringContainsString('injectedFoo', $response->getContent(false));
|
305 | 306 | }
|
| 307 | + |
| 308 | + pub
8000
lic function testConsumingDecoratedClient() |
| 309 | + { |
| 310 | + $client = $this->getHttpClient(__FUNCTION__, null, new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface { |
| 311 | + use AsyncDecoratorTrait; |
| 312 | + |
| 313 | + public function request(string $method, string $url, array $options = []): ResponseInterface |
| 314 | + { |
| 315 | + $response = $this->client->request($method, $url, $options); |
| 316 | + $response->getStatusCode(); // should be avoided and breaks compatibility with AsyncDecoratorTrait |
| 317 | + |
| 318 | + return $response; |
| 319 | + } |
| 320 | + |
| 321 | + public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 322 | + { |
| 323 | + return $this->client->stream($responses, $timeout); |
| 324 | + } |
| 325 | + }); |
| 326 | + |
| 327 | + $response = $client->request('GET', 'http://localhost:8057/'); |
| 328 | + |
| 329 | + $this->expectException(\LogicException::class); |
| 330 | + $this->expectExceptionMessage('Instance of "Symfony\Component\HttpClient\Response\NativeResponse" is already consumed and cannot be managed by "Symfony\Component\HttpClient\Response\AsyncResponse". A decorated client should not call any of the response\'s methods in its "request()" method.'); |
| 331 | + $response->getStatusCode(); |
| 332 | + } |
306 | 333 | }
|
0 commit comments