Skip to content

Commit eeba392

Browse files
Josh Salwayclaude
andcommitted
test: add tests for ThrottleRequestsWithRedis key prefix
Verifies that the key prefix is prepended to Redis keys and that setKeyPrefix() returns the middleware instance for fluent chaining. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0acaae0 commit eeba392

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

tests/Integration/Http/ThrottleRequestsWithRedisTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,37 @@ public function testItReturnsConfiguredResponseWhenUsingAfterLimit(): void
9292
$this->get('/')->assertTooManyRequests()->assertContent('ah ah ah');
9393
});
9494
}
95+
96+
public function testKeyPrefixIsPrependedToRedisKeys()
97+
{
98+
$this->ifRedisAvailable(function () {
99+
Route::get('/', function () {
100+
return 'yes';
101+
})->middleware(ThrottleRequestsWithRedis::class.':2,1');
102+
103+
// Set a key prefix on the middleware
104+
$this->app->resolving(ThrottleRequestsWithRedis::class, function ($middleware) {
105+
$middleware->setKeyPrefix('throttle:');
106+
});
107+
108+
$response = $this->withoutExceptionHandling()->get('/');
109+
$this->assertSame('yes', $response->getContent());
110+
111+
// Verify the middleware works correctly with the prefix
112+
$response = $this->withoutExceptionHandling()->get('/');
113+
$this->assertSame('yes', $response->getContent());
114+
$this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));
115+
});
116+
}
117+
118+
public function testSetKeyPrefixReturnsSelf()
119+
{
120+
$limiter = $this->app->make(\Illuminate\Cache\RateLimiter::class);
121+
$redis = $this->app->make(\Illuminate\Contracts\Redis\Factory::class);
122+
$middleware = new ThrottleRequestsWithRedis($limiter, $redis);
123+
124+
$result = $middleware->setKeyPrefix('throttle:');
125+
126+
$this->assertSame($middleware, $result);
127+
}
95128
}

0 commit comments

Comments
 (0)