From 5fee7248046a2dc2f97b5ff5614d92a6f99a0740 Mon Sep 17 00:00:00 2001 From: martkop26 <112486711+martkop26@users.noreply.github.com> Date: Tue, 30 Aug 2022 15:50:25 +0200 Subject: [PATCH] [HttpClient] Fix computing retry delay when using RetryableHttpClient --- RetryableHttpClient.php | 2 +- Tests/RetryableHttpClientTest.php | 38 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/RetryableHttpClient.php b/RetryableHttpClient.php index 4df466f..9a5b503 100644 --- a/RetryableHttpClient.php +++ b/RetryableHttpClient.php @@ -138,7 +138,7 @@ private function getDelayFromHeader(array $headers): ?int { if (null !== $after = $headers['retry-after'][0] ?? null) { if (is_numeric($after)) { - return (int) $after * 1000; + return (int) ($after * 1000); } if (false !== $time = strtotime($after)) { diff --git a/Tests/RetryableHttpClientTest.php b/Tests/RetryableHttpClientTest.php index 6bd9a1f..21e63ba 100644 --- a/Tests/RetryableHttpClientTest.php +++ b/Tests/RetryableHttpClientTest.php @@ -187,4 +187,42 @@ public function testCancelOnTimeout() $response->cancel(); } } + + public function testRetryWithDelay() + { + $retryAfter = '0.46'; + + $client = new RetryableHttpClient( + new MockHttpClient([ + new MockResponse('', [ + 'http_code' => 503, + 'response_headers' => [ + 'retry-after' => $retryAfter, + ], + ]), + new MockResponse('', [ + 'http_code' => 200, + ]), + ]), + new GenericRetryStrategy(), + 1, + $logger = new class() extends TestLogger { + public array $context = []; + + public function log($level, $message, array $context = []): void + { + $this->context = $context; + parent::log($level, $message, $context); + } + }, + ); + + $client->request('GET', 'http://example.com/foo-bar')->getContent(); + + $delay = $logger->context['delay'] ?? null; + + $this->assertArrayHasKey('delay', $logger->context); + $this->assertNotNull($delay); + $this->assertSame((int) ($retryAfter * 1000), $delay); + } }