bug #44671 [HttpClient] Fix tracing requests made after calling withOptions() (nicolas-grekas)

This PR was merged into the 5.3 branch.

Discussion
----------

[HttpClient] Fix tracing requests made after calling withOptions()

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44312
| License       | MIT
| Doc PR        | -

Commits
-------

06b25c713b [HttpClient] Fix tracing requests made after calling withOptions()
This commit is contained in:
Nicolas Grekas 2021-12-16 22:49:51 +01:00
commit f190e9e816
2 changed files with 18 additions and 3 deletions

View File

@ -218,4 +218,18 @@ class TraceableHttpClientTest extends TestCase
$this->assertCount(1, $events['GET http://localhost:8057']->getPeriods());
$this->assertGreaterThan(0.0, $events['GET http://localhost:8057']->getDuration());
}
public function testWithOptions()
{
$sut = new TraceableHttpClient(new NativeHttpClient());
$sut2 = $sut->withOptions(['base_uri' => 'http://localhost:8057']);
$response = $sut2->request('GET', '/');
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('http://localhost:8057/', $response->getInfo('url'));
$this->assertCount(1, $sut->getTracedRequests());
}
}

View File

@ -27,13 +27,14 @@ use Symfony\Contracts\Service\ResetInterface;
final class TraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface
{
private $client;
private $tracedRequests = [];
private $stopwatch;
private $tracedRequests;
public function __construct(HttpClientInterface $client, Stopwatch $stopwatch = null)
{
$this->client = $client;
$this->stopwatch = $stopwatch;
$this->tracedRequests = new \ArrayObject();
}
/**
@ -84,7 +85,7 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
public function getTracedRequests(): array
{
return $this->tracedRequests;
return $this->tracedRequests->getArrayCopy();
}
public function reset()
@ -93,7 +94,7 @@ final class TraceableHttpClient implements HttpClientInterface, ResetInterface,
$this->client->reset();
}
$this->tracedRequests = [];
$this->tracedRequests->exchangeArray([]);
}
/**