From cb02b0e776337d3667db8b915d4c7ac8adeb5931 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 19 Aug 2020 17:33:27 +0100 Subject: [PATCH] WebTestClient releases body on returnResult(Void.class) The original behavior was to ignore the body which came with odd warnings in the Javadoc and potential leaks that could be reported from tests causing unnecessary concern. This change causes the body to be released and effectively still ignores it but minus the potential leaks. See gh-19647 --- .../reactive/server/DefaultWebTestClient.java | 9 +++++- .../web/reactive/server/WebTestClient.java | 28 ++++++------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index c9dce22f2a..56a41deaa1 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -378,7 +378,14 @@ class DefaultWebTestClient implements WebTestClient { @Override public FluxExchangeResult returnResult(Class elementClass) { - Flux body = this.response.bodyToFlux(elementClass); + Flux body; + if (elementClass.equals(Void.class)) { + this.response.releaseBody().block(); + body = Flux.empty(); + } + else { + body = this.response.bodyToFlux(elementClass); + } return new FluxExchangeResult<>(this.exchangeResult, body); } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java index 6b55d52075..ed9b1ecde7 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java @@ -172,7 +172,7 @@ public interface WebTestClient { // Static factory methods /** - * Use this server setup to test one `@Controller` at a time. + * Use this server setup to test one {@code @Controller} at a time. * This option loads the default configuration of * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}. * There are builder methods to customize the Java config. The resulting @@ -229,8 +229,8 @@ public interface WebTestClient { } /** - * This server setup option allows you to connect to a running server via - * Reactor Netty. + * This server setup option allows you to connect to a live server through + * a Reactor Netty client connector. *

 	 * WebTestClient client = WebTestClient.bindToServer()
 	 *         .baseUrl("http://localhost:8080")
@@ -244,11 +244,6 @@ public interface WebTestClient {
 
 	/**
 	 * A variant of {@link #bindToServer()} with a pre-configured connector.
-	 * 

-	 * WebTestClient client = WebTestClient.bindToServer()
-	 *         .baseUrl("http://localhost:8080")
-	 *         .build();
-	 * 
* @return chained API to customize client config * @since 5.0.2 */ @@ -802,18 +797,13 @@ public interface WebTestClient { BodyContentSpec expectBody(); /** - * Exit the chained API and consume the response body externally. This - * is useful for testing infinite streams (e.g. SSE) where you need to - * to assert decoded objects as they come and then cancel at some point - * when test objectives are met. Consider using {@code StepVerifier} - * from {@literal "reactor-test"} to assert the {@code Flux} stream - * of decoded objects. + * Exit the chained flow in order to consume the response body + * externally, e.g. via {@link reactor.test.StepVerifier}. * - *

Note: Do not use this option for cases where there - * is no content (e.g. 204, 4xx) or you're not interested in the content. - * For such cases you can use {@code expectBody().isEmpty()} or - * {@code expectBody(Void.class)} which ensures that resources are - * released regardless of whether the response has content or not. + *

Note that when {@code Void.class} is passed in, the response body + * is consumed and released. If no content is expected, then consider + * using {@code .expectBody().isEmpty()} instead which asserts that + * there is no content. */ FluxExchangeResult returnResult(Class elementClass);