|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Logic\NetifydCatalogRepository; |
| 4 | +use Illuminate\Support\Facades\Config; |
| 5 | +use Mockery\MockInterface; |
| 6 | + |
| 7 | +use function Pest\Laravel\get; |
| 8 | +use function Pest\Laravel\partialMock; |
| 9 | + |
| 10 | +it('returns 503 with a Retry-After header once the netifyd rate limit is exceeded', function () { |
| 11 | + Config::set('netifyd.rate-limit', 2); |
| 12 | + |
| 13 | + partialMock(NetifydCatalogRepository::class, function (MockInterface $mock) { |
| 14 | + $mock->allows('applicationsCatalog')->andReturn([]); |
| 15 | + }); |
| 16 | + |
| 17 | + get('/api/netifyd/applications/catalog')->assertOk(); |
| 18 | + get('/api/netifyd/applications/catalog')->assertOk(); |
| 19 | + |
| 20 | + get('/api/netifyd/applications/catalog') |
| 21 | + ->assertServiceUnavailable() |
| 22 | + ->assertHeader('Retry-After'); |
| 23 | +}); |
| 24 | + |
| 25 | +it('returns a randomized Retry-After header between 300 and 900 seconds', function () { |
| 26 | + Config::set('netifyd.rate-limit', 1); |
| 27 | + |
| 28 | + partialMock(NetifydCatalogRepository::class, function (MockInterface $mock) { |
| 29 | + $mock->allows('applicationsCatalog')->andReturn([]); |
| 30 | + }); |
| 31 | + |
| 32 | + get('/api/netifyd/applications/catalog')->assertOk(); |
| 33 | + |
| 34 | + $response = get('/api/netifyd/applications/catalog')->assertServiceUnavailable(); |
| 35 | + |
| 36 | + expect((int) $response->headers->get('Retry-After')) |
| 37 | + ->toBeGreaterThanOrEqual(300) |
| 38 | + ->toBeLessThanOrEqual(900); |
| 39 | +}); |
| 40 | + |
| 41 | +it('rate limits globally across clients, not per ip', function () { |
| 42 | + Config::set('netifyd.rate-limit', 1); |
| 43 | + |
| 44 | + partialMock(NetifydCatalogRepository::class, function (MockInterface $mock) { |
| 45 | + $mock->allows('applicationsCatalog')->andReturn([]); |
| 46 | + }); |
| 47 | + |
| 48 | + get('/api/netifyd/applications/catalog', ['X-Forwarded-For' => '203.0.113.10'])->assertOk(); |
| 49 | + get('/api/netifyd/applications/catalog', ['X-Forwarded-For' => '198.51.100.20']) |
| 50 | + ->assertServiceUnavailable(); |
| 51 | +}); |
0 commit comments