razonyang /
yii2-rate-limiter
| 1 | <?php |
||||
| 2 | namespace RazonYang\Yii2\RateLimiter\Tests\Unit; |
||||
| 3 | |||||
| 4 | use Codeception\Test\Unit; |
||||
| 5 | use RazonYang\Yii2\RateLimiter\BaseRateLimiter; |
||||
| 6 | use RazonYang\Yii2\RateLimiter\Tests\TestRateLimiter; |
||||
| 7 | use RazonYang\Yii2\RateLimiter\Tests\TestUser; |
||||
| 8 | use yii\base\Action; |
||||
| 9 | use yii\web\Controller; |
||||
| 10 | use Yii; |
||||
| 11 | use yii\web\TooManyRequestsHttpException; |
||||
| 12 | |||||
| 13 | class RateLimiterTest extends Unit |
||||
| 14 | { |
||||
| 15 | private function createRateLimiter(int $capacity = 10, $rate = 1): TestRateLimiter |
||||
| 16 | { |
||||
| 17 | return new TestRateLimiter(['capacity' => $capacity, 'rate' => $rate]); |
||||
| 18 | } |
||||
| 19 | |||||
| 20 | public function testGetLogger(): void |
||||
| 21 | { |
||||
| 22 | $limiter = $this->createRateLimiter(); |
||||
| 23 | $property = new \ReflectionProperty(BaseRateLimiter::class, 'logger'); |
||||
| 24 | $property->setAccessible(true); |
||||
| 25 | |||||
| 26 | $method = new \ReflectionMethod(TestRateLimiter::class, 'getLogger'); |
||||
| 27 | $method->setAccessible(true); |
||||
| 28 | $this->assertSame($property->getValue($limiter), $method->invoke($limiter)); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | public function testGetName(): void |
||||
| 32 | { |
||||
| 33 | $limiter = $this->createRateLimiter(); |
||||
| 34 | $action = $this->createAction('test'); |
||||
| 35 | $request = Yii::$app->getRequest(); |
||||
| 36 | $user = Yii::$app->getUser(); |
||||
| 37 | |||||
| 38 | $this->assertSame($request->getUserIP() . ':' . $action->getUniqueId(), $limiter->getName($user, $request, $action)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 39 | |||||
| 40 | $identity = new TestUser(); |
||||
| 41 | $identity->id = 'foo'; |
||||
| 42 | $user->login($identity); |
||||
| 43 | $this->assertSame('foo:' . $action->getUniqueId(), $limiter->getName($user, $request, $action)); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | public function testNameCallback(): void |
||||
| 47 | { |
||||
| 48 | $name = uniqid(); |
||||
| 49 | $callback = function () use ($name) { |
||||
| 50 | return $name; |
||||
| 51 | }; |
||||
| 52 | $limiter = new TestRateLimiter([ |
||||
| 53 | 'nameCallback' => $callback, |
||||
| 54 | ]); |
||||
| 55 | $action = $this->createAction('test'); |
||||
| 56 | $this->assertSame($name, $limiter->getName(Yii::$app->getUser(), Yii::$app->getRequest(), $action)); |
||||
|
0 ignored issues
–
show
It seems like
Yii::app->getRequest() can also be of type yii\console\Request; however, parameter $request of RazonYang\Yii2\RateLimit...eRateLimiter::getName() does only seem to accept yii\web\Request, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 57 | } |
||||
| 58 | |||||
| 59 | public function testBeforeAction(): void |
||||
| 60 | { |
||||
| 61 | $limiter = $this->createRateLimiter(1, 10); |
||||
| 62 | $action = $this->createAction('test'); |
||||
| 63 | $this->assertTrue($limiter->beforeAction($action)); |
||||
| 64 | $headers = $limiter->response->getHeaders(); |
||||
| 65 | $this->assertTrue($headers->has('X-Rate-Limit-Limit')); |
||||
| 66 | $this->assertTrue($headers->has('X-Rate-Limit-Remaining')); |
||||
| 67 | $this->assertTrue($headers->has('X-Rate-Limit-Reset')); |
||||
| 68 | |||||
| 69 | $this->expectException(TooManyRequestsHttpException::class); |
||||
| 70 | $limiter->beforeAction($action); |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | public function createAction(string $id): Action |
||||
| 74 | { |
||||
| 75 | return new Action($id, new class('test', Yii::$app) extends Controller { |
||||
| 76 | }); |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 |