Skip to content

Commit

Permalink
#71 add post-execute-condition
Browse files Browse the repository at this point in the history
tests:
- change response status code
- additional response headers
  • Loading branch information
MarcGiffing committed Feb 28, 2024
1 parent 5caf0de commit 6c43898
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.webRequestWithStatus;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@SpringBootTest(properties = {
"bucket4j.filters[0].cache-name=buckets",
"bucket4j.filters[0].url=.*",
"bucket4j.filters[0].http-response-headers.hello=world",
"bucket4j.filters[0].http-response-headers.abc=cba",
"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",
})
@AutoConfigureMockMvc
@DirtiesContext
public class AddResponseHeaderTest {

@Autowired
private MockMvc mockMvc;

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

mockMvc
.perform(get(url))
.andExpect(status().is(HttpStatus.TOO_MANY_REQUESTS.value()))
.andExpect(header().exists("X-Rate-Limit-Retry-After-Seconds"))
.andExpect(header().string("hello", "world"))
.andExpect(header().string("abc", "cba"));
}


}
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.blockedWebRequestWithStatus;
import static com.giffing.bucket4j.spring.boot.starter.general.tests.filter.servlet.MockMvcHelper.webRequestWithStatus;


@SpringBootTest(properties = {
"bucket4j.filters[0].cache-name=buckets",
"bucket4j.filters[0].url=.*",
"bucket4j.filters[0].http-status-code=NOT_FOUND",
"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",
})
@AutoConfigureMockMvc
@DirtiesContext
public class ChangeResponseHttpStatusCodeTest {

@Autowired
private MockMvc mockMvc;

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import java.util.Collections;
import java.util.stream.IntStream;

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


@SpringBootTest(properties = {
Expand All @@ -31,7 +32,7 @@ public class EmptyHttpResponseTest {
private MockMvc mockMvc;

@Test
void helloTest() throws Exception {
void assert_empty_response() throws Exception {
String url = "/hello";
IntStream.rangeClosed(1, 5)
.boxed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public static void webRequestWithStatus(
}

public static void blockedWebRequestDueToRateLimit(MockMvc mockMvc, String url) throws Exception {
blockedWebRequestWithStatus(mockMvc, url, HttpStatus.TOO_MANY_REQUESTS);
}
public static void blockedWebRequestWithStatus(MockMvc mockMvc, String url, HttpStatus httpStatus) throws Exception {
mockMvc
.perform(get(url))
.andExpect(status().is(HttpStatus.TOO_MANY_REQUESTS.value()))
.andExpect(status().is(httpStatus.value()))
.andExpect(content().string(containsString("{ \"message\": \"Too many requests!\" }")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
GreadyRefillSpeedTest.class,
IntervalRefillSpeedTest.class,
PostExecuteConditionTest.class,
EmptyHttpResponseTest.class
EmptyHttpResponseTest.class,
ChangeResponseHttpStatusCodeTest.class,
AddResponseHeaderTest.class
})
public class ServletTestSuite {
}

0 comments on commit 6c43898

Please sign in to comment.