Skip to content

[HttpClient] minor fixes in RetryableHttpClient #38418

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
Oct 5, 2020
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/Response/AsyncResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ final class AsyncResponse implements ResponseInterface, StreamableInterface
private $passthru;

/**
* @param callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
* @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
*/
public function __construct(HttpClientInterface $client, string $method, string $url, array $options, callable $passthru)
public function __construct(HttpClientInterface $client, string $method, string $url, array $options, callable $passthru = null)
{
$this->client = $client;
$this->shouldBuffer = $options['buffer'] ?? true;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(HttpClientInterface $client, RetryDeciderInterface $
public function request(string $method, string $url, array $options = []): ResponseInterface
{
if ($this->maxRetries <= 0) {
return $this->client->request($method, $url, $options);
return new AsyncResponse($this->client, $method, $url, $options);
}

$retryCount = 0;
Expand Down Expand Up @@ -97,7 +97,7 @@ public function request(string $method, string $url, array $options = []): Respo
if (!$chunk->isLast()) {
return;
}
$shouldRetry = $this->decider->shouldRetry($method, $url, $options, $statusCode, $headers, $content, null);
$shouldRetry = $this->decider->shouldRetry($method, $url, $options, $statusCode, $headers, $content);
if (null === $shouldRetry) {
throw new \LogicException(sprintf('The "%s::shouldRetry" method must not return null when called with a body.', \get_class($this->decider)));
}
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/RetryableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,24 @@ public function shouldRetry(string $requestMethod, string $requestUrl, array $re
$this->expectExceptionMessageMatches('/must not return null when called with a body/');
$response->getHeaders();
}

public function testStreamNoRetry()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
]),
new HttpStatusCodeDecider([500]),
new ExponentialBackOff(0),
0
);

$response = $client->request('GET', 'http://example.com/foo-bar');

foreach ($client->stream($response) as $chunk) {
if ($chunk->isFirst()) {
self::assertSame(500, $response->getStatusCode());
}
}
}
}