Skip to content

Commit 63125f0

Browse files
committed
Add test case to ShouldBlockRequestTest
1 parent 8c3f024 commit 63125f0

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

agent_api/src/test/java/ShouldBlockRequestTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dev.aikido.agent_api.context.ContextObject;
66
import dev.aikido.agent_api.context.User;
77
import dev.aikido.agent_api.storage.ServiceConfigStore;
8+
import dev.aikido.agent_api.storage.statistics.StatisticsStore;
89
import org.junit.jupiter.api.AfterEach;
910
import org.junit.jupiter.api.BeforeAll;
1011
import org.junit.jupiter.api.Test;
@@ -38,6 +39,7 @@ public SampleContextObject() {
3839
public static void clean() {
3940
Context.set(null);
4041
ServiceConfigStore.updateFromAPIResponse(emptyAPIResponse);
42+
StatisticsStore.clear();
4143
};
4244

4345
@AfterEach
@@ -133,7 +135,7 @@ public void testEndpointsExistButNoMatch() throws SQLException {
133135

134136
@Test
135137
public void testEndpointsExistWithMatch() throws SQLException {
136-
Context.set(null);
138+
Context.set(new SampleContextObject());
137139
setEmptyConfigWithEndpointList(List.of(
138140
new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, false)
139141
));
@@ -142,14 +144,23 @@ public void testEndpointsExistWithMatch() throws SQLException {
142144
var res1 = ShouldBlockRequest.shouldBlockRequest();
143145
assertFalse(res1.block());
144146

145-
Context.set(null);
146147
setEmptyConfigWithEndpointList(List.of(
147148
new Endpoint("GET", "/api/*", 1, 1000, Collections.emptyList(), false, false, true)
148149
));
149150

150151
// Test with match & rate-limiting enabled :
151152
var res2 = ShouldBlockRequest.shouldBlockRequest();
152153
assertFalse(res2.block());
154+
assertEquals(0, StatisticsStore.getStatsRecord().requests().rateLimited());
155+
156+
var res3 = ShouldBlockRequest.shouldBlockRequest();
157+
var res4 = ShouldBlockRequest.shouldBlockRequest();
158+
assertTrue(res3.block());
159+
assertTrue(res4.block());
160+
assertEquals("ip", res3.data().trigger());
161+
assertEquals("192.168.1.1", res3.data().ip());
162+
assertEquals("ratelimited", res3.data().type());
163+
assertEquals(2, StatisticsStore.getStatsRecord().requests().rateLimited());
153164
}
154165

155166
@Test

0 commit comments

Comments
 (0)