|
12 | 12 | namespace Symfony\Component\HttpClient\Tests;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\HttpClient\AsyncDecoratorTrait;
|
| 15 | +use Symfony\Component\HttpClient\DecoratorTrait; |
15 | 16 | use Symfony\Component\HttpClient\Response\AsyncContext;
|
16 | 17 | use Symfony\Component\HttpClient\Response\AsyncResponse;
|
17 | 18 | use Symfony\Contracts\HttpClient\ChunkInterface;
|
|
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 $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,25 @@ public function testMultipleYieldInInitializer()
|
303 | 304 | $this->assertSame(404, $response->getStatusCode());
|
304 | 305 | $this->assertStringContainsString('injectedFoo', $response->getContent(false));
|
305 | 306 | }
|
| 307 | + |
| 308 | + public function testConsumingDecoratedClient() |
| 309 | + { |
| 310 | + $client = $this->getHttpClient(__FUNCTION__, null, new class(parent::getHttpClient(__FUNCTION__)) implements HttpClientInterface { |
| 311 | + use DecoratorTrait; |
| 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 | + |
| 322 | + $response = $client->request('GET', 'http://localhost:8057/'); |
| 323 | + |
| 324 | + $this->expectException(\LogicException::class); |
| 325 | + $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.'); |
| 326 | + $response->getStatusCode(); |
| 327 | + } |
306 | 328 | }
|
0 commit comments