From 1175aab028d16007aa4e392c5ecb3e5f2775247c Mon Sep 17 00:00:00 2001 From: Orkun Date: Thu, 29 Aug 2024 13:10:26 +0300 Subject: [PATCH] chore: add test case for retry-after header --- src/Api/FingerprintApi.php | 16 ++++++++-------- template/api.mustache | 4 ++-- test/FingerprintApiTest.php | 8 +++++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Api/FingerprintApi.php b/src/Api/FingerprintApi.php index a5c40379..7d13b875 100644 --- a/src/Api/FingerprintApi.php +++ b/src/Api/FingerprintApi.php @@ -159,7 +159,7 @@ public function deleteVisitorData(string $visitor_id): array if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -243,7 +243,7 @@ function ($e) { if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -335,7 +335,7 @@ public function getEvent(string $request_id): array if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -414,7 +414,7 @@ function ($e) { if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -511,7 +511,7 @@ public function getVisits(string $visitor_id, ?string $request_id = null, ?strin if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -595,7 +595,7 @@ function ($e) { if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -693,7 +693,7 @@ public function updateEvent(EventUpdateRequest $body, string $request_id): array if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -778,7 +778,7 @@ function ($e) { if (429 === $e->getCode()) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int) $response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } diff --git a/template/api.mustache b/template/api.mustache index 39c03724..790ac62e 100644 --- a/template/api.mustache +++ b/template/api.mustache @@ -142,7 +142,7 @@ use \GuzzleHttp\Exception\GuzzleException; if ($e->getCode() === 429) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int)$response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } @@ -218,7 +218,7 @@ use \GuzzleHttp\Exception\GuzzleException; if ($e->getCode() === 429) { $e->setRetryAfter(1); if ($response->hasHeader('retry-after')) { - $e->setRetryAfter((int)$response->getHeader('retry-after')); + $e->setRetryAfter((int) $response->getHeader('retry-after')[0]); } } diff --git a/test/FingerprintApiTest.php b/test/FingerprintApiTest.php index b13e9f17..e58ec861 100644 --- a/test/FingerprintApiTest.php +++ b/test/FingerprintApiTest.php @@ -68,6 +68,9 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri { $mock_name = ""; $status = 200; + $headers = [ + 'Content-Type' => 'application/json', + ]; if ($method === "GET" || $method === "DELETE") { switch ($mockId) { @@ -111,6 +114,7 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri case self::MOCK_VISITOR_ID_429_ERROR: $mock_name = 'get_visits_429_too_many_requests_error.json'; $status = 429; + $headers['retry-after'] = 30; break; case self::MOCK_VISITOR_ID_400_ERROR: $mock_name = "400_error_incorrect_visitor_id.json"; @@ -157,7 +161,7 @@ protected function getMockResponse(string $mockId, string $method = "GET", ?stri $contents = json_encode($visits_mock_data); } - return new Response($status, [], $contents); + return new Response($status, $headers, $contents); } public function testGetEvent() @@ -389,6 +393,7 @@ public function testGetVisits429Error() $this->fingerprint_api->getVisits(self::MOCK_VISITOR_ID_429_ERROR); } catch (ApiException $e) { $this->assertEquals(TooManyRequestsResponse::class, get_class($e->getErrorDetails())); + $this->assertEquals(30, $e->getRetryAfter()); throw $e; } } @@ -492,6 +497,7 @@ public function testDeleteVisitorData429Error() $this->fingerprint_api->deleteVisitorData(self::MOCK_VISITOR_ID_429_ERROR); } catch (ApiException $e) { $this->assertEquals(ErrorCommon429Response::class, get_class($e->getErrorDetails())); + $this->assertEquals(30, $e->getRetryAfter()); throw $e; } }