Skip to content

Commit

Permalink
chore: add test case for retry-after header
Browse files Browse the repository at this point in the history
  • Loading branch information
Orkuncakilkaya committed Aug 29, 2024
1 parent ad187f3 commit 1175aab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/Api/FingerprintApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}
}

Expand Down
8 changes: 7 additions & 1 deletion test/FingerprintApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 1175aab

Please sign in to comment.