44import dev .aikido .agent_api .context .Context ;
55import dev .aikido .agent_api .context .ContextObject ;
66import dev .aikido .agent_api .context .User ;
7+ import dev .aikido .agent_api .storage .RateLimiterStore ;
78import dev .aikido .agent_api .storage .ServiceConfigStore ;
9+ import dev .aikido .agent_api .storage .statistics .StatisticsStore ;
810import org .junit .jupiter .api .AfterEach ;
911import org .junit .jupiter .api .BeforeAll ;
1012import org .junit .jupiter .api .Test ;
@@ -38,12 +40,16 @@ public SampleContextObject() {
3840 public static void clean () {
3941 Context .set (null );
4042 ServiceConfigStore .updateFromAPIResponse (emptyAPIResponse );
43+ StatisticsStore .clear ();
44+ RateLimiterStore .clear ();
4145 };
4246
4347 @ AfterEach
4448 public void tearDown () throws SQLException {
4549 Context .set (null );
4650 ServiceConfigStore .updateFromAPIResponse (emptyAPIResponse );
51+ StatisticsStore .clear ();
52+ RateLimiterStore .clear ();
4753 }
4854
4955 @ Test
@@ -135,7 +141,7 @@ public void testEndpointsExistButNoMatch() throws SQLException {
135141 public void testEndpointsExistWithMatch () throws SQLException {
136142 Context .set (null );
137143 setEmptyConfigWithEndpointList (List .of (
138- new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , false )
144+ new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , false )
139145 ));
140146
141147 // Test with match & rate-limiting disabled :
@@ -144,14 +150,40 @@ public void testEndpointsExistWithMatch() throws SQLException {
144150
145151 Context .set (null );
146152 setEmptyConfigWithEndpointList (List .of (
147- new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , true )
153+ new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , true )
148154 ));
149155
150156 // Test with match & rate-limiting enabled :
151157 var res2 = ShouldBlockRequest .shouldBlockRequest ();
152158 assertFalse (res2 .block ());
153159 }
154160
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 ());
175+ assertEquals (0 , StatisticsStore .getStatsRecord ().requests ().rateLimited ());
176+
177+ var res3 = ShouldBlockRequest .shouldBlockRequest ();
178+ var res4 = ShouldBlockRequest .shouldBlockRequest ();
179+ assertTrue (res3 .block ());
180+ assertTrue (res4 .block ());
181+ assertEquals ("ip" , res3 .data ().trigger ());
182+ assertEquals ("192.168.1.1" , res3 .data ().ip ());
183+ assertEquals ("ratelimited" , res3 .data ().type ());
184+ assertEquals (2 , StatisticsStore .getStatsRecord ().requests ().rateLimited ());
185+ }
186+
155187 @ Test
156188 public void testThreadClientInvalid () throws SQLException {
157189 Context .set (new SampleContextObject ());
0 commit comments