Skip to content

Commit

Permalink
#71 add post-execute-condition
Browse files Browse the repository at this point in the history
minor refactoring
add empty response body test
  • Loading branch information
MarcGiffing committed Feb 28, 2024
1 parent efae5f4 commit c5b5636
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ bucket4j.filters[0].strategy=first # [first, all] if multiple rate limits config
bucket4j.filters[0].rate-limits[0].cache-key=getRemoteAddr() # defines the cache key. It will be evaluated with the Spring Expression Language
bucket4j.filters[0].rate-limits[0].num-tokens=1 # The number of tokens to consume
bucket4j.filters[0].rate-limits[0].execute-condition=1==1 # an optional SpEl expression to decide to execute the rate limit or not
bucket4j.filters[1].rate-limits[0].post-execute-condition= # an optional SpEl expression to decide if the token consumption should only estimated for the incoming request and the returning response used to check if the token must be consumed: getStatus() eq 401
bucket4j.filters[0].rate-limits[0].execute-predicates[0]=PATH=/hello,/world # On the HTTP Path as a list
bucket4j.filters[0].rate-limits[0].execute-predicates[1]=METHOD=GET,POST # On the HTTP Method
bucket4j.filters[0].rate-limits[0].execute-predicates[2]=QUERY=HELLO # Checks for the existence of a Query Parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.giffing.bucket4j.spring.boot.starter.general.tests.filter.servlet;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Collections;
import java.util.stream.IntStream;

import static com.giffing.bucket4j.spring.boot.starter.general.tests.filter.servlet.MockMvcHelper.*;


@SpringBootTest(properties = {
"bucket4j.filters[0].cache-name=buckets",
"bucket4j.filters[0].url=^(/hello).*",
"bucket4j.filters[0].http-response-body=null",
"bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=5",
"bucket4j.filters[0].rate-limits[0].bandwidths[0].time=10",
"bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=seconds",
"bucket4j.filters[0].rate-limits[0].bandwidths[0].refill-speed=interval",
})
@AutoConfigureMockMvc
@DirtiesContext
public class EmptyHttpResponseTest {

@Autowired
private MockMvc mockMvc;

@Test
void helloTest() throws Exception {
String url = "/hello";
IntStream.rangeClosed(1, 5)
.boxed()
.sorted(Collections.reverseOrder())
.forEach(counter -> webRequestWithStatus(mockMvc, url, HttpStatus.OK, counter - 1));
blockedWebRequestDueToRateLimitWithEmptyBody(mockMvc, url);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void helloTest() throws Exception {
IntStream.rangeClosed(1, 5)
.boxed()
.sorted(Collections.reverseOrder())
.forEach(counter -> webRequestWithStatus(mockMvc, url, counter - 1, HttpStatus.OK));
.forEach(counter -> webRequestWithStatus(mockMvc, url, HttpStatus.OK, counter - 1));
blockedWebRequestDueToRateLimit(mockMvc, url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void helloTest() throws Exception {
IntStream.rangeClosed(1, 5)
.boxed()
.sorted(Collections.reverseOrder())
.forEach(counter -> webRequestWithStatus(mockMvc, url, counter - 1, HttpStatus.OK));
.forEach(counter -> webRequestWithStatus(mockMvc, url, HttpStatus.OK, counter - 1));
blockedWebRequestDueToRateLimit(mockMvc, url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

public class MockMvcHelper {

public static void webRequestWithStatus(MockMvc mockMvc, String url, Integer remainingTries, HttpStatus httpStatus) {
public static void webRequestWithStatus(
MockMvc mockMvc,
String url,
HttpStatus httpStatus,
Integer remainingTries) {
try {
mockMvc
.perform(get(url))
Expand All @@ -29,4 +33,11 @@ public static void blockedWebRequestDueToRateLimit(MockMvc mockMvc, String url)
.andExpect(status().is(HttpStatus.TOO_MANY_REQUESTS.value()))
.andExpect(content().string(containsString("{ \"message\": \"Too many requests!\" }")));
}

public static void blockedWebRequestDueToRateLimitWithEmptyBody(MockMvc mockMvc, String url) throws Exception {
mockMvc
.perform(get(url))
.andExpect(status().is(HttpStatus.TOO_MANY_REQUESTS.value()))
.andExpect(jsonPath("$").doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void assert_rate_limit_when_unauthorized() throws Exception {
IntStream.rangeClosed(1, 5)
.boxed()
.sorted(Collections.reverseOrder())
.forEach(counter -> webRequestWithStatus(mockMvc, url, counter, HttpStatus.UNAUTHORIZED));
.forEach(counter -> webRequestWithStatus(mockMvc, url, HttpStatus.UNAUTHORIZED, counter));

blockedWebRequestDueToRateLimit(mockMvc, url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
ServletRateLimitTest.class,
GreadyRefillSpeedTest.class,
IntervalRefillSpeedTest.class,
PostExecuteConditionTest.class
PostExecuteConditionTest.class,
EmptyHttpResponseTest.class
})
public class ServletTestSuite {
}

0 comments on commit c5b5636

Please sign in to comment.