|
11 | 11 | use Nyholm\Psr7\Request;
|
12 | 12 | use PHPUnit\Framework\TestCase;
|
13 | 13 | use Symfony\Bridge\PhpUnit\ClockMock;
|
| 14 | +use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException; |
14 | 15 | use Symfony\Component\RateLimiter\RateLimit;
|
15 | 16 | use Symfony\Component\RateLimiter\RateLimiterFactory;
|
16 | 17 | use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
|
@@ -56,4 +57,51 @@ public function testThrottle(): void
|
56 | 57 | $this->client->sendRequest(new Request('GET', ''));
|
57 | 58 | $this->assertEqualsWithDelta($timeAfterThrottle, time(), 1);
|
58 | 59 | }
|
| 60 | + |
| 61 | + public function testTokens(): void |
| 62 | + { |
| 63 | + $this->client = new PluginClient($this->mockClient, [ |
| 64 | + new ThrottlePlugin( |
| 65 | + (new RateLimiterFactory( |
| 66 | + ['id' => 'foo', 'policy' => 'fixed_window', 'limit' => 2, 'interval' => '3 seconds'], |
| 67 | + new InMemoryStorage(), |
| 68 | + ))->create(), |
| 69 | + 2, |
| 70 | + ), |
| 71 | + ]); |
| 72 | + |
| 73 | + $time = time(); |
| 74 | + $this->client->sendRequest(new Request('GET', '')); |
| 75 | + $this->client->sendRequest(new Request('GET', '')); |
| 76 | + $this->assertEqualsWithDelta($time, ($timeAfterThrottle = time()) - 3, 1); |
| 77 | + |
| 78 | + $this->client->sendRequest(new Request('GET', '')); |
| 79 | + $this->assertEqualsWithDelta($timeAfterThrottle, time(), 1); |
| 80 | + } |
| 81 | + |
| 82 | + public function testMaxTime(): void |
| 83 | + { |
| 84 | + $this->client = new PluginClient($this->mockClient, [ |
| 85 | + new ThrottlePlugin( |
| 86 | + $rateLimit = (new RateLimiterFactory( |
| 87 | + ['id' => 'foo', 'policy' => 'fixed_window', 'limit' => 2, 'interval' => '3 seconds'], |
| 88 | + new InMemoryStorage(), |
| 89 | + ))->create(), |
| 90 | + 1, |
| 91 | + 1, |
| 92 | + ), |
| 93 | + ]); |
| 94 | + |
| 95 | + $this->expectException(MaxWaitDurationExceededException::class); |
| 96 | + $this->expectExceptionMessage('The rate limiter wait time ("3" seconds) is longer than the provided maximum time ("1" seconds).'); |
| 97 | + |
| 98 | + $time = time(); |
| 99 | + $this->client->sendRequest(new Request('GET', '')); |
| 100 | + $this->client->sendRequest(new Request('GET', '')); |
| 101 | + $this->client->sendRequest(new Request('GET', '')); |
| 102 | + $this->assertEqualsWithDelta($time, ($timeAfterThrottle = time()) - 3, 1); |
| 103 | + |
| 104 | + $this->client->sendRequest(new Request('GET', '')); |
| 105 | + $this->assertEqualsWithDelta($timeAfterThrottle, time(), 1); |
| 106 | + } |
59 | 107 | }
|
0 commit comments