Skip to content

Commit 7dff731

Browse files
Gaurav Ojhaspencergibb
authored andcommitted
Support spring.codec.max-in-memory-size configuration for CacheRequestBodyFilter
Fixes gh-2693 Fixes gh-2797
1 parent 41b212d commit 7dff731

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,9 @@ public ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFac
549549

550550
@Bean
551551
@ConditionalOnEnabledFilter
552-
public CacheRequestBodyGatewayFilterFactory cacheRequestBodyGatewayFilterFactory() {
553-
return new CacheRequestBodyGatewayFilterFactory();
552+
public CacheRequestBodyGatewayFilterFactory cacheRequestBodyGatewayFilterFactory(
553+
ServerCodecConfigurer codecConfigurer) {
554+
return new CacheRequestBodyGatewayFilterFactory(codecConfigurer.getReaders());
554555
}
555556

556557
@Bean

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public CacheRequestBodyGatewayFilterFactory() {
5151
this.messageReaders = HandlerStrategies.withDefaults().messageReaders();
5252
}
5353

54+
public CacheRequestBodyGatewayFilterFactory(List<HttpMessageReader<?>> messageReaders) {
55+
super(CacheRequestBodyGatewayFilterFactory.Config.class);
56+
this.messageReaders = messageReaders;
57+
}
58+
5459
@Override
5560
public GatewayFilter apply(CacheRequestBodyGatewayFilterFactory.Config config) {
5661
return new GatewayFilter() {

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/CacheRequestBodyGatewayFilterFactoryTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Import;
3636
import org.springframework.core.io.buffer.PooledDataBuffer;
37+
import org.springframework.http.HttpHeaders;
3738
import org.springframework.http.HttpStatus;
39+
import org.springframework.http.MediaType;
3840
import org.springframework.test.annotation.DirtiesContext;
3941
import org.springframework.util.StringUtils;
4042
import org.springframework.web.server.ServerWebExchange;
@@ -43,7 +45,7 @@
4345
import static org.assertj.core.api.Assertions.fail;
4446
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
4547

46-
@SpringBootTest(webEnvironment = RANDOM_PORT)
48+
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "spring.codec.max-in-memory-size=25")
4749
@DirtiesContext
4850
public class CacheRequestBodyGatewayFilterFactoryTests extends BaseWebClientTests {
4951

@@ -53,6 +55,8 @@ public class CacheRequestBodyGatewayFilterFactoryTests extends BaseWebClientTest
5355

5456
private static final String BODY_CACHED_EXISTS = "BODY_CACHED_EXISTS";
5557

58+
private static final String LARGE_BODY_VALUE = "here is request body which will cause payload size failure";
59+
5660
@Test
5761
public void cacheRequestBodyWorks() {
5862
testClient.post().uri("/post").header("Host", "www.cacherequestbody.org").bodyValue(BODY_VALUE).exchange()
@@ -65,6 +69,14 @@ public void cacheRequestBodyWorks() {
6569
});
6670
}
6771

72+
@Test
73+
public void cacheRequestBodyDoesntWorkForLargePayload() {
74+
testClient.post().uri("/post").header("Host", "www.cacherequestbody.org")
75+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).bodyValue(LARGE_BODY_VALUE)
76+
.exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("message")
77+
.isEqualTo("Exceeded limit on max bytes to buffer : 25");
78+
}
79+
6880
@Test
6981
public void cacheRequestBodyEmpty() {
7082
testClient.post().uri("/post").header("Host", "www.cacherequestbodyempty.org").exchange().expectStatus().isOk()

0 commit comments

Comments
 (0)