|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Cache;
|
4 | 4 |
|
| 5 | +use Illuminate\Cache\ArrayStore; |
5 | 6 | use Illuminate\Cache\RateLimiter;
|
6 | 7 | use Illuminate\Cache\RateLimiting\Limit;
|
| 8 | +use Illuminate\Cache\Repository; |
7 | 9 | use Illuminate\Contracts\Cache\Repository as Cache;
|
8 | 10 | use PHPUnit\Framework\Attributes\DataProvider;
|
9 | 11 | use PHPUnit\Framework\TestCase;
|
@@ -38,6 +40,33 @@ public function testRegisterNamedRateLimiter(mixed $name, string $expected): voi
|
38 | 40 |
|
39 | 41 | $this->assertNotNull($limiterClosure);
|
40 | 42 | }
|
| 43 | + |
| 44 | + public function testShouldUseOriginKeyAsPrefixWhenMultipleLimiterWithSameKey() |
| 45 | + { |
| 46 | + $rateLimiter = new RateLimiter(new Repository(new ArrayStore)); |
| 47 | + |
| 48 | + $rateLimiter->for('user_limiter', fn (string $userId) => [ |
| 49 | + Limit::perSecond(3)->by($userId), |
| 50 | + Limit::perMinute(5)->by($userId), |
| 51 | + ]); |
| 52 | + |
| 53 | + $userId1 = '123'; |
| 54 | + $userId2 = '456'; |
| 55 | + |
| 56 | + $limiterForUser1 = $rateLimiter->limiter('user_limiter')($userId1); |
| 57 | + $limiterForUser2 = $rateLimiter->limiter('user_limiter')($userId2); |
| 58 | + |
| 59 | + for ($i = 0; $i < 3; $i++) { |
| 60 | + $this->assertFalse($rateLimiter->tooManyAttempts($limiterForUser1[0]->key, $limiterForUser1[0]->maxAttempts)); |
| 61 | + $this->assertFalse($rateLimiter->tooManyAttempts($limiterForUser2[0]->key, $limiterForUser2[0]->maxAttempts)); |
| 62 | + |
| 63 | + $rateLimiter->hit($limiterForUser1[0]->key, $limiterForUser1[0]->decaySeconds); |
| 64 | + $rateLimiter->hit($limiterForUser2[0]->key, $limiterForUser2[0]->decaySeconds); |
| 65 | + } |
| 66 | + |
| 67 | + $this->assertNotSame($limiterForUser1[0]->key, $limiterForUser2[0]->key); |
| 68 | + $this->assertNotSame($limiterForUser1[1]->key, $limiterForUser2[1]->key); |
| 69 | + } |
41 | 70 | }
|
42 | 71 |
|
43 | 72 | enum BackedEnumNamedRateLimiter: string
|
|
0 commit comments