Skip to content
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
3 changes: 2 additions & 1 deletion lib/Github/HttpClient/Listener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public function onRequestError(Event $event)

if ($response->isClientError() || $response->isServerError()) {
$remaining = (string) $response->getHeader('X-RateLimit-Remaining');
$limit = $response->getHeader('X-RateLimit-Limit');

if (null != $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getResource(), 1, 10)) {
throw new ApiLimitExceedException($this->options['api_limit']);
throw new ApiLimitExceedException($limit);
}

if (401 === $response->getStatusCode()) {
Expand Down
33 changes: 28 additions & 5 deletions test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ public function shouldFailWhenApiLimitWasExceed()
$response->expects($this->once())
->method('isClientError')
->will($this->returnValue(true));
$response->expects($this->once())
$response->expects($this->at(1))
->method('getHeader')
->with('X-RateLimit-Remaining')
->will($this->returnValue(0));
$response->expects($this->at(2))
->method('getHeader')
->with('X-RateLimit-Limit')
->will($this->returnValue(5000));

$listener = new ErrorListener(array('api_limit' => 5000));
$listener->onRequestError($this->getEventMock($response));
Expand All @@ -49,10 +53,15 @@ public function shouldNotPassWhenContentWasNotValidJson()
$response->expects($this->once())
->method('isClientError')
->will($this->returnValue(true));
$response->expects($this->once())
$response->expects($this->at(1))
->method('getHeader')
->with('X-RateLimit-Remaining')
->will($this->returnValue(5000));
$response->expects($this->at(2))
->method('getHeader')
->with('X-RateLimit-Limit')
->will($this->returnValue(5000));

$response->expects($this->once())
->method('getBody')
->will($this->returnValue('fail'));
Expand All @@ -71,10 +80,14 @@ public function shouldNotPassWhenContentWasValidJsonButStatusIsNotCovered()
$response->expects($this->once())
->method('isClientError')
->will($this->returnValue(true));
$response->expects($this->once())
$response->expects($this->at(1))
->method('getHeader')
->with('X-RateLimit-Remaining')
->will($this->returnValue(5000));
$response->expects($this->at(2))
->method('getHeader')
->with('X-RateLimit-Limit')
->will($this->returnValue(5000));
$response->expects($this->once())
->method('getBody')
->will($this->returnValue(json_encode(array('message' => 'test'))));
Expand All @@ -96,10 +109,14 @@ public function shouldNotPassWhen400IsSent()
$response->expects($this->once())
->method('isClientError')
->will($this->returnValue(true));
$response->expects($this->once())
$response->expects($this->at(1))
->method('getHeader')
->with('X-RateLimit-Remaining')
->will($this->returnValue(5000));
$response->expects($this->at(2))
->method('getHeader')
->with('X-RateLimit-Limit')
->will($this->returnValue(5000));
$response->expects($this->once())
->method('getBody')
->will($this->returnValue(json_encode(array('message' => 'test'))));
Expand Down Expand Up @@ -134,10 +151,14 @@ public function shouldNotPassWhen422IsSentWithErrorCode($errorCode)
$response->expects($this->once())
->method('isClientError')
->will($this->returnValue(true));
$response->expects($this->once())
$response->expects($this->at(1))
->method('getHeader')
->with('X-RateLimit-Remaining')
->will($this->returnValue(5000));
$response->expects($this->at(2))
->method('getHeader')
->with('X-RateLimit-Limit')
->will($this->returnValue(5000));
$response->expects($this->once())
->method('getBody')
->will($this->returnValue($content));
Expand Down Expand Up @@ -170,6 +191,8 @@ public function shouldThrowTwoFactorAuthenticationRequiredException()
return 5000;
case 'X-GitHub-OTP':
return 'required; sms';
case 'X-RateLimit-Limit':
return 5000;
}
}));
$response->expects($this->any())
Expand Down