-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: - change response status code - additional response headers
- Loading branch information
1 parent
5caf0de
commit 6c43898
Showing
5 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...fing/bucket4j/spring/boot/starter/general/tests/filter/servlet/AddResponseHeaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...4j/spring/boot/starter/general/tests/filter/servlet/ChangeResponseHttpStatusCodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters