Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpClient] Fix tracing requests made after calling withOptions()
  • Loading branch information
nicolas-grekas committed Dec 16, 2021
commit 06b25c713b6e76360b51f95b04e4a531fcb158c0
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,18 @@ public function testStopwatchDestruct()
$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());
}
}
7 changes: 4 additions & 3 deletions src/Symfony/Component/HttpClient/TraceableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
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();
}

/**
Expand Down Expand Up @@ -84,7 +85,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa

public function getTracedRequests(): array
{
return $this->tracedRequests;
return $this->tracedRequests->getArrayCopy();
}

public function reset()
Expand All @@ -93,7 +94,7 @@ public function reset()
$this->client->reset();
}

$this->tracedRequests = [];
$this->tracedRequests->exchangeArray([]);
}

/**
Expand Down