Skip to content

Commit db28fa4

Browse files
committed
Add test coverage for RateLimitReachedException handling
See #69
1 parent b978adb commit db28fa4

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/ExpoPushConnector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ protected function resolveLimits(): array
3434
Limit::allow(6)
3535
->everySeconds(1)
3636
->sleep()
37-
->name('expo-push-limit'),
37+
->setPrefix('expo')
38+
->name('push-limit'),
3839
];
3940
}
4041

tests/Unit/ExpoPushConnectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function rate_limit_allows_six_requests_per_second_and_sleeps_when_exceed
6565
{
6666
$connector = new ExpoPushConnector();
6767

68-
$limit = $this->findLimit($connector, 'expo-push-limit');
68+
$limit = $this->findLimit($connector, 'push-limit');
6969

7070
$this->assertSame(6, $limit->getAllow());
7171
$this->assertSame(1, $limit->getReleaseInSeconds());
@@ -95,7 +95,7 @@ private function findLimit(ExpoPushConnector $connector, string $name): Limit
9595
{
9696
try {
9797
foreach ($connector->getLimits() as $limit) {
98-
if (str_ends_with($limit->getName(), ":{$name}")) {
98+
if (str_ends_with($limit->getName(), ":$name")) {
9999
return $limit;
100100
}
101101
}

tests/Unit/Request/RequestExceptionHandlerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Saloon\Exceptions\Request\FatalRequestException;
1414
use Saloon\Exceptions\Request\RequestException;
1515
use Saloon\Http\Response;
16+
use Saloon\RateLimitPlugin\Exceptions\RateLimitReachedException;
17+
use Saloon\RateLimitPlugin\Limit;
1618

1719
class RequestExceptionHandlerTest extends TestCase
1820
{
@@ -43,6 +45,24 @@ public function invoke_with_fatal_request_exception_adds_push_error(): void
4345
$this->assertEquals(199, $error->endIndex);
4446
}
4547

48+
#[Test]
49+
public function invoke_with_rate_limit_reached_exception_adds_push_error(): void
50+
{
51+
$limit = Limit::allow(100)->setPrefix('expo')->name('push-limit');
52+
$rateLimitReachedException = new RateLimitReachedException($limit);
53+
54+
$handler = $this->handler;
55+
$handler($rateLimitReachedException, 2);
56+
57+
$this->assertCount(1, $this->errors);
58+
59+
$error = $this->errors->get(0);
60+
$this->assertEquals(PushErrorCode::TooManyRequests, $error->code);
61+
$this->assertEquals('Request Rate Limit Reached (Name: expo:push-limit)', $error->message);
62+
$this->assertEquals(200, $error->startIndex);
63+
$this->assertEquals(299, $error->endIndex);
64+
}
65+
4666
#[Test]
4767
public function invoke_with_request_exception_adds_push_error(): void
4868
{

0 commit comments

Comments
 (0)