Repository navigation
Expand file tree
/
Copy pathHandlerTest.php
More file actions
74 lines (57 loc) · 2.5 KB
/
Copy pathHandlerTest.php
File metadata and controls
74 lines (57 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Dingo\Api\Tests\Http\RateLimit;
use Dingo\Api\Http\Request;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Dingo\Api\Http\RateLimit\Handler;
use Dingo\Api\Tests\Stubs\ThrottleStub;
use Dingo\Api\Http\RateLimit\Throttle\Route;
class HandlerTest extends TestCase
{
protected $container;
protected $cache;
protected $limiter;
public function setUp()
{
$this->container = new Container;
$this->container['config'] = ['cache.default' => 'array', 'cache.stores.array' => ['driver' => 'array']];
$this->cache = new CacheManager($this->container);
$this->limiter = new Handler($this->container, $this->cache, []);
$this->limiter->setRateLimiter(function ($container, $request) {
return $request->getClientIp();
});
}
public function testSettingSpecificLimitsOnRouteUsesRouteSpecificThrottle()
{
$this->limiter->rateLimitRequest(Request::create('test', 'GET'), 100, 100);
$throttle = $this->limiter->getThrottle();
$this->assertInstanceOf(Route::class, $throttle);
$this->assertSame(100, $throttle->getLimit());
$this->assertSame(100, $throttle->getExpires());
}
public function testThrottleWithHighestAmountOfRequestsIsUsedWhenMoreThanOneMatchingThrottle()
{
$this->limiter->extend($first = new ThrottleStub(['limit' => 100, 'expires' => 200]));
$this->limiter->extend($second = new ThrottleStub(['limit' => 99, 'expires' => 400]));
$this->limiter->rateLimitRequest(Request::create('test', 'GET'));
$this->assertSame($first, $this->limiter->getThrottle());
}
public function testExceedingOfRateLimit()
{
$request = Request::create('test', 'GET');
$this->limiter->rateLimitRequest($request);
$this->assertFalse($this->limiter->exceededRateLimit());
$this->limiter->extend(new ThrottleStub(['limit' => 1, 'expires' => 200]));
$this->limiter->rateLimitRequest($request);
$this->assertFalse($this->limiter->exceededRateLimit());
$this->limiter->rateLimitRequest($request);
$this->assertTrue($this->limiter->exceededRateLimit());
}
public function testGettingTheRemainingLimit()
{
$this->limiter->extend(new ThrottleStub(['limit' => 10, 'expires' => 200]));
$this->limiter->rateLimitRequest(Request::create('test', 'GET'));
$this->assertSame(9, $this->limiter->getRemainingLimit());
}
}