|
4 | 4 | import dev.aikido.agent_api.context.Context; |
5 | 5 | import dev.aikido.agent_api.context.ContextObject; |
6 | 6 | import dev.aikido.agent_api.context.User; |
| 7 | +import dev.aikido.agent_api.storage.RateLimiterStore; |
7 | 8 | import dev.aikido.agent_api.storage.ServiceConfigStore; |
8 | 9 | import dev.aikido.agent_api.storage.statistics.StatisticsStore; |
9 | 10 | import org.junit.jupiter.api.AfterEach; |
@@ -40,12 +41,15 @@ public static void clean() { |
40 | 41 | Context.set(null); |
41 | 42 | ServiceConfigStore.updateFromAPIResponse(emptyAPIResponse); |
42 | 43 | StatisticsStore.clear(); |
| 44 | + RateLimiterStore.clear(); |
43 | 45 | }; |
44 | 46 |
|
45 | 47 | @AfterEach |
46 | 48 | public void tearDown() throws SQLException { |
47 | 49 | Context.set(null); |
48 | 50 | ServiceConfigStore.updateFromAPIResponse(emptyAPIResponse); |
| 51 | + StatisticsStore.clear(); |
| 52 | + RateLimiterStore.clear(); |
49 | 53 | } |
50 | 54 |
|
51 | 55 | @Test |
@@ -135,22 +139,39 @@ public void testEndpointsExistButNoMatch() throws SQLException { |
135 | 139 |
|
136 | 140 | @Test |
137 | 141 | public void testEndpointsExistWithMatch() throws SQLException { |
138 | | - Context.set(new SampleContextObject()); |
| 142 | + Context.set(null); |
139 | 143 | setEmptyConfigWithEndpointList(List.of( |
140 | | - new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, false) |
| 144 | + new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, false) |
141 | 145 | )); |
142 | 146 |
|
143 | 147 | // Test with match & rate-limiting disabled : |
144 | 148 | var res1 = ShouldBlockRequest.shouldBlockRequest(); |
145 | 149 | assertFalse(res1.block()); |
146 | 150 |
|
| 151 | + Context.set(null); |
147 | 152 | setEmptyConfigWithEndpointList(List.of( |
148 | | - new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, true) |
| 153 | + new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, true) |
149 | 154 | )); |
150 | 155 |
|
151 | 156 | // Test with match & rate-limiting enabled : |
152 | 157 | var res2 = ShouldBlockRequest.shouldBlockRequest(); |
153 | 158 | assertFalse(res2.block()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testEndpointsExistAndGetsRateLimited() throws SQLException { |
| 163 | + setEmptyConfigWithEndpointList(List.of( |
| 164 | + new Endpoint("GET", "/api/*", 2, 1000, Collections.emptyList(), false, false, true) |
| 165 | + )); |
| 166 | + |
| 167 | + // Test with match |
| 168 | + Context.set(new SampleContextObject()); |
| 169 | + var res1 = ShouldBlockRequest.shouldBlockRequest(); |
| 170 | + assertFalse(res1.block()); |
| 171 | + |
| 172 | + // Test with match |
| 173 | + var res2 = ShouldBlockRequest.shouldBlockRequest(); |
| 174 | + assertFalse(res2.block()); |
154 | 175 | assertEquals(0, StatisticsStore.getStatsRecord().requests().rateLimited()); |
155 | 176 |
|
156 | 177 | var res3 = ShouldBlockRequest.shouldBlockRequest(); |
|
0 commit comments