From 1a650c10a18bc389bfcd1f45976031a19ea110de Mon Sep 17 00:00:00 2001 From: Stephan Vock Date: Tue, 29 Dec 2020 11:48:05 +0000 Subject: [PATCH] ExceptionThrower: adjust rate limit detection --- .../Plugin/GithubExceptionThrower.php | 2 +- .../Plugin/GithubExceptionThrowerTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php index 70373e00ea1..21e6dd3434f 100644 --- a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php +++ b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php @@ -37,7 +37,7 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla // If error: $remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining'); - if (null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) { + if ((429 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) { $limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit'); $reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset'); diff --git a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php index 9c1de98d59d..b367fe15779 100644 --- a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php +++ b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php @@ -177,6 +177,25 @@ public static function responseProvider() ), 'exception' => new \Github\Exception\RuntimeException('Field "xxxx" doesn\'t exist on type "Issue", Field "dummy" doesn\'t exist on type "PullRequest"'), ], + 'Grapql requires authentication' => [ + 'response' => new Response( + 401, + [ + 'content-type' => 'application/json', + 'X-RateLimit-Limit' => 0, + 'X-RateLimit-Remaining' => 0, + 'X-RateLimit-Reset' => 1609245810, + 'X-RateLimit-Used' => 0, + ], + json_encode( + [ + 'message' => 'This endpoint requires you to be authenticated.', + 'documentation_url' => 'https://docs.github.com/v3/#authentication', + ] + ) + ), + 'exception' => new \Github\Exception\RuntimeException('This endpoint requires you to be authenticated.', 401), + ], ]; } }