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
This commit is contained in:
Rossen Stoyanchev 2020-08-19 17:33:27 +01:00
parent dd7369df48
commit cb02b0e776
2 changed files with 17 additions and 20 deletions

View File

@ -378,7 +378,14 @@ class DefaultWebTestClient implements WebTestClient {
@Override
public <T> FluxExchangeResult<T> returnResult(Class<T> elementClass) {
Flux<T> body = this.response.bodyToFlux(elementClass);
Flux<T> 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);
}

View File

@ -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.
* <p><pre class="code">
* 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.
* <p><pre class="code">
* WebTestClient client = WebTestClient.bindToServer()
* .baseUrl("http://localhost:8080")
* .build();
* </pre>
* @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<T>} stream
* of decoded objects.
* Exit the chained flow in order to consume the response body
* externally, e.g. via {@link reactor.test.StepVerifier}.
*
* <p><strong>Note:</strong> 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.
* <p>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.
*/
<T> FluxExchangeResult<T> returnResult(Class<T> elementClass);