Skip to content

A free open source IT asset/license management system

Added test

snipe406e8c58749f
diff --git a/tests/Feature/ApiRateLimitTest.php b/tests/Feature/ApiRateLimitTest.php
new file mode 100644
index 0000000000..1239c00727
--- /dev/null
+++ b/tests/Feature/ApiRateLimitTest.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Tests\Feature;
+
+use App\Models\User;
+use Tests\TestCase;
+
+class ApiRateLimitTest extends TestCase
+{
+
+ public function testRateLimit()
+ {
+ config(['app.api_throttle_per_minute' => 10]);
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.users.me'))
+ ->assertOk()
+ ->assertHeader('X-Ratelimit-Limit', config('app.api_throttle_per_minute'))
+ ->assertHeader('X-Ratelimit-Remaining', 9);
+ }
+
+ public function testRateLimitDecreasesRemaining()
+ {
+ config(['app.api_throttle_per_minute' => 5]);
+ $expected_remaining = (config('app.api_throttle_per_minute') - 1);
+ $admin = User::factory()->create();
+
+ for ($x = 0; $x < 5; $x++) {
+
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.users.me'))
+ ->assertOk()
+ ->assertHeader('X-Ratelimit-Remaining', $expected_remaining--);
+
+ }
+
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.users.me'))
+ ->assertStatus(429)
+ ->assertHeader('Retry-After', 60);
+ }
+
+}