From c5b56369b8da4819827af7b159cad5c92cfde7b1 Mon Sep 17 00:00:00 2001 From: Marc Giffing Date: Wed, 28 Feb 2024 15:57:07 +0100 Subject: [PATCH] #71 add post-execute-condition minor refactoring add empty response body test --- README.adoc | 1 + .../filter/servlet/EmptyHttpResponseTest.java | 44 +++++++++++++++++++ .../filter/servlet/GreadyRefillSpeedTest.java | 2 +- .../servlet/IntervalRefillSpeedTest.java | 2 +- .../tests/filter/servlet/MockMvcHelper.java | 13 +++++- .../servlet/PostExecuteConditionTest.java | 2 +- .../filter/servlet/ServletTestSuite.java | 3 +- 7 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/EmptyHttpResponseTest.java diff --git a/README.adoc b/README.adoc index b5e37612..1c1fae5c 100644 --- a/README.adoc +++ b/README.adoc @@ -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 diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/EmptyHttpResponseTest.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/EmptyHttpResponseTest.java new file mode 100644 index 00000000..2d38a2dc --- /dev/null +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/EmptyHttpResponseTest.java @@ -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); + } + + +} diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/GreadyRefillSpeedTest.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/GreadyRefillSpeedTest.java index b80edc64..cb018561 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/GreadyRefillSpeedTest.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/GreadyRefillSpeedTest.java @@ -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); } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/IntervalRefillSpeedTest.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/IntervalRefillSpeedTest.java index 697e273c..050fe8af 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/IntervalRefillSpeedTest.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/IntervalRefillSpeedTest.java @@ -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); } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/MockMvcHelper.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/MockMvcHelper.java index 6908eaf8..3611c4d0 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/MockMvcHelper.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/MockMvcHelper.java @@ -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)) @@ -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()); + } } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/PostExecuteConditionTest.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/PostExecuteConditionTest.java index 62614968..cf19c728 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/PostExecuteConditionTest.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/PostExecuteConditionTest.java @@ -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); } diff --git a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/ServletTestSuite.java b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/ServletTestSuite.java index 10ee0737..23ad59a6 100644 --- a/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/ServletTestSuite.java +++ b/examples/general-tests/src/main/java/com/giffing/bucket4j/spring/boot/starter/general/tests/filter/servlet/ServletTestSuite.java @@ -8,7 +8,8 @@ ServletRateLimitTest.class, GreadyRefillSpeedTest.class, IntervalRefillSpeedTest.class, - PostExecuteConditionTest.class + PostExecuteConditionTest.class, + EmptyHttpResponseTest.class }) public class ServletTestSuite { }