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 .routes .RoutesStore ;
10+ import dev .aikido .agent_api .storage .statistics .StatisticsStore ;
811import org .junit .jupiter .api .AfterEach ;
912import org .junit .jupiter .api .BeforeAll ;
1013import org .junit .jupiter .api .Test ;
@@ -38,12 +41,18 @@ public SampleContextObject() {
3841 public static void clean () {
3942 Context .set (null );
4043 ServiceConfigStore .updateFromAPIResponse (emptyAPIResponse );
44+ StatisticsStore .clear ();
45+ RoutesStore .clear ();
46+ RateLimiterStore .clear ();
4147 };
4248
4349 @ AfterEach
4450 public void tearDown () throws SQLException {
4551 Context .set (null );
4652 ServiceConfigStore .updateFromAPIResponse (emptyAPIResponse );
53+ StatisticsStore .clear ();
54+ RoutesStore .clear ();
55+ RateLimiterStore .clear ();
4756 }
4857
4958 @ Test
@@ -59,6 +68,7 @@ public void testNoContext() throws SQLException {
5968 // Test with thread cache not set :
6069 var res2 = ShouldBlockRequest .shouldBlockRequest ();
6170 assertFalse (res2 .block ());
71+ assertEquals (0 , StatisticsStore .getStatsRecord ().requests ().rateLimited ());
6272 }
6373
6474 @ Test
@@ -112,7 +122,8 @@ public void testUserSet() throws SQLException {
112122
113123 @ Test
114124 public void testEndpointsExistButNoMatch () throws SQLException {
115- Context .set (null );
125+ ContextObject ctx = new SampleContextObject ();
126+ Context .set (ctx );
116127 setEmptyConfigWithEndpointList (List .of (
117128 new Endpoint ("POST" , "/api2/*" , 1 , 1000 , Collections .emptyList (), false , false , false )
118129 ));
@@ -121,7 +132,6 @@ public void testEndpointsExistButNoMatch() throws SQLException {
121132 var res1 = ShouldBlockRequest .shouldBlockRequest ();
122133 assertFalse (res1 .block ());
123134
124- Context .set (null );
125135 setEmptyConfigWithEndpointList (List .of (
126136 new Endpoint ("POST" , "/api2/*" , 1 , 1000 , Collections .emptyList (), false , false , true )
127137 ));
@@ -133,7 +143,8 @@ public void testEndpointsExistButNoMatch() throws SQLException {
133143
134144 @ Test
135145 public void testEndpointsExistWithMatch () throws SQLException {
136- Context .set (null );
146+ ContextObject ctx = new SampleContextObject ();
147+ Context .set (ctx );
137148 setEmptyConfigWithEndpointList (List .of (
138149 new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , false )
139150 ));
@@ -142,14 +153,26 @@ public void testEndpointsExistWithMatch() throws SQLException {
142153 var res1 = ShouldBlockRequest .shouldBlockRequest ();
143154 assertFalse (res1 .block ());
144155
145- Context .set (null );
146156 setEmptyConfigWithEndpointList (List .of (
147157 new Endpoint ("GET" , "/api/*" , 1 , 1000 , Collections .emptyList (), false , false , true )
148158 ));
149159
150160 // Test with match & rate-limiting enabled :
151161 var res2 = ShouldBlockRequest .shouldBlockRequest ();
152162 assertFalse (res2 .block ());
163+ assertEquals (0 , StatisticsStore .getStatsRecord ().requests ().rateLimited ());
164+
165+
166+ var res3 = ShouldBlockRequest .shouldBlockRequest ();
167+ var res4 = ShouldBlockRequest .shouldBlockRequest ();
168+ assertTrue (res3 .block ());
169+ assertTrue (res4 .block ());
170+ assertEquals ("ip" , res3 .data ().trigger ());
171+ assertEquals ("192.168.1.1" , res3 .data ().ip ());
172+ assertEquals ("ratelimited" , res3 .data ().type ());
173+ assertEquals (2 , StatisticsStore .getStatsRecord ().requests ().rateLimited ());
174+ assertEquals (2 , RoutesStore .getRoutesAsList ()[0 ].getRateLimitCount ());
175+
153176 }
154177
155178 @ Test
0 commit comments