Allows RewriteFunction to be applied when no request body.
Fixes gh-1309
This commit is contained in:
@@ -71,8 +71,10 @@ public class ModifyRequestBodyGatewayFilterFactory extends
|
||||
|
||||
// TODO: flux or mono
|
||||
Mono<?> modifiedBody = serverRequest.bodyToMono(inClass)
|
||||
// .log("modify_request_mono", Level.INFO)
|
||||
.flatMap(o -> config.rewriteFunction.apply(exchange, o));
|
||||
.flatMap(originalBody -> config.getRewriteFunction()
|
||||
.apply(exchange, originalBody))
|
||||
.switchIfEmpty(Mono.defer(() -> (Mono) config.getRewriteFunction()
|
||||
.apply(exchange, null)));
|
||||
|
||||
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody,
|
||||
config.getOutClass());
|
||||
|
||||
@@ -56,6 +56,17 @@ public class ModifyRequestBodyGatewayFilterFactoryTests extends BaseWebClientTes
|
||||
.isEqualTo("modifyrequest");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upstreamRequestBodyIsEmpty() {
|
||||
testClient.post().uri("/post").header("Host", "www.modifyrequestbodyempty.org")
|
||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||
.exchange().expectStatus().isEqualTo(HttpStatus.OK).expectBody()
|
||||
.jsonPath("headers.Content-Type")
|
||||
.isEqualTo(MediaType.APPLICATION_JSON_VALUE).jsonPath("data")
|
||||
.isEqualTo("modifyrequest");
|
||||
}
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootConfiguration
|
||||
@Import(DefaultTestConfig.class)
|
||||
@@ -74,6 +85,17 @@ public class ModifyRequestBodyGatewayFilterFactoryTests extends BaseWebClientTes
|
||||
return Mono.just("modifyrequest");
|
||||
}))
|
||||
.uri(uri))
|
||||
.route("test_modify_request_body_empty",
|
||||
r -> r.order(-1).host("**.modifyrequestbodyempty.org")
|
||||
.filters(f -> f.modifyRequestBody(String.class, String.class,
|
||||
MediaType.APPLICATION_JSON_VALUE,
|
||||
(serverWebExchange, body) -> {
|
||||
if (body == null) {
|
||||
return Mono.just("modifyrequest");
|
||||
}
|
||||
return Mono.just(body.toUpperCase());
|
||||
}))
|
||||
.uri(uri))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user