Skip to content

Commit 1c08236

Browse files
committed
Merge pull request #276 from KnpLabs/feature/limit-from-header
Use actual api limit from header
2 parents 3c36776 + a275448 commit 1c08236

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/Github/HttpClient/Listener/ErrorListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ public function onRequestError(Event $event)
4040

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

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

4849
if (401 === $response->getStatusCode()) {

test/Github/Tests/HttpClient/Listener/ErrorListenerTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ public function shouldFailWhenApiLimitWasExceed()
3030
$response->expects($this->once())
3131
->method('isClientError')
3232
->will($this->returnValue(true));
33-
$response->expects($this->once())
33+
$response->expects($this->at(1))
3434
->method('getHeader')
3535
->with('X-RateLimit-Remaining')
3636
->will($this->returnValue(0));
37+
$response->expects($this->at(2))
38+
->method('getHeader')
39+
->with('X-RateLimit-Limit')
40+
->will($this->returnValue(5000));
3741

3842
$listener = new ErrorListener(array('api_limit' => 5000));
3943
$listener->onRequestError($this->getEventMock($response));
@@ -49,10 +53,15 @@ public function shouldNotPassWhenContentWasNotValidJson()
4953
$response->expects($this->once())
5054
->method('isClientError')
5155
->will($this->returnValue(true));
52-
$response->expects($this->once())
56+
$response->expects($this->at(1))
5357
->method('getHeader')
5458
->with('X-RateLimit-Remaining')
5559
->will($this->returnValue(5000));
60+
$response->expects($this->at(2))
61+
->method('getHeader')
62+
->with('X-RateLimit-Limit')
63+
->will($this->returnValue(5000));
64+
5665
$response->expects($this->once())
5766
->method('getBody')
5867
->will($this->returnValue('fail'));
@@ -71,10 +80,14 @@ public function shouldNotPassWhenContentWasValidJsonButStatusIsNotCovered()
7180
$response->expects($this->once())
7281
->method('isClientError')
7382
->will($this->returnValue(true));
74-
$response->expects($this->once())
83+
$response->expects($this->at(1))
7584
->method('getHeader')
7685
->with('X-RateLimit-Remaining')
7786
->will($this->returnValue(5000));
87+
$response->expects($this->at(2))
88+
->method('getHeader')
89+
->with('X-RateLimit-Limit')
90+
->will($this->returnValue(5000));
7891
$response->expects($this->once())
7992
->method('getBody')
8093
->will($this->returnValue(json_encode(array('message' => 'test'))));
@@ -96,10 +109,14 @@ public function shouldNotPassWhen400IsSent()
96109
$response->expects($this->once())
97110
->method('isClientError')
98111
->will($this->returnValue(true));
99-
$response->expects($this->once())
112+
$response->expects($this->at(1))
100113
->method('getHeader')
101114
->with('X-RateLimit-Remaining')
102115
->will($this->returnValue(5000));
116+
$response->expects($this->at(2))
117+
->method('getHeader')
118+
->with('X-RateLimit-Limit')
119+
->will($this->returnValue(5000));
103120
$response->expects($this->once())
104121
->method('getBody')
105122
->will($this->returnValue(json_encode(array('message' => 'test'))));
@@ -134,10 +151,14 @@ public function shouldNotPassWhen422IsSentWithErrorCode($errorCode)
134151
$response->expects($this->once())
135152
->method('isClientError')
136153
->will($this->returnValue(true));
137-
$response->expects($this->once())
154+
$response->expects($this->at(1))
138155
->method('getHeader')
139156
->with('X-RateLimit-Remaining')
140157
->will($this->returnValue(5000));
158+
$response->expects($this->at(2))
159+
->method('getHeader')
160+
->with('X-RateLimit-Limit')
161+
->will($this->returnValue(5000));
141162
$response->expects($this->once())
142163
->method('getBody')
143164
->will($this->returnValue($content));
@@ -170,6 +191,8 @@ public function shouldThrowTwoFactorAuthenticationRequiredException()
170191
return 5000;
171192
case 'X-GitHub-OTP':
172193
return 'required; sms';
194+
case 'X-RateLimit-Limit':
195+
return 5000;
173196
}
174197
}));
175198
$response->expects($this->any())

0 commit comments

Comments
 (0)