mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
RestTestClient correctly exposes the response body
Closes gh-35385
This commit is contained in:
+6
-6
@@ -286,33 +286,33 @@ class DefaultRestTestClient implements RestTestClient {
|
||||
|
||||
@Override
|
||||
public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) {
|
||||
B body = this.exchangeResult.getBody(bodyType);
|
||||
B body = this.exchangeResult.getClientResponse().bodyTo(bodyType);
|
||||
EntityExchangeResult<B> result = new EntityExchangeResult<>(this.exchangeResult, body);
|
||||
return new DefaultBodySpec<>(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType) {
|
||||
B body = this.exchangeResult.getBody(bodyType);
|
||||
B body = this.exchangeResult.getClientResponse().bodyTo(bodyType);
|
||||
EntityExchangeResult<B> result = initExchangeResult(body);
|
||||
return new DefaultBodySpec<>(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyContentSpec expectBody() {
|
||||
byte[] body = this.exchangeResult.getBody(byte[].class);
|
||||
byte[] body = this.exchangeResult.getClientResponse().bodyTo(byte[].class);
|
||||
EntityExchangeResult<byte[]> result = initExchangeResult(body);
|
||||
return new DefaultBodyContentSpec(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> EntityExchangeResult<T> returnResult(Class<T> elementClass) {
|
||||
return initExchangeResult(this.exchangeResult.getBody(elementClass));
|
||||
return initExchangeResult(this.exchangeResult.getClientResponse().bodyTo(elementClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> EntityExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementTypeRef) {
|
||||
return initExchangeResult(this.exchangeResult.getBody(elementTypeRef));
|
||||
return initExchangeResult(this.exchangeResult.getClientResponse().bodyTo(elementTypeRef));
|
||||
}
|
||||
|
||||
private <B> EntityExchangeResult<B> initExchangeResult(@Nullable B body) {
|
||||
@@ -412,7 +412,7 @@ class DefaultRestTestClient implements RestTestClient {
|
||||
@Override
|
||||
public EntityExchangeResult<Void> isEmpty() {
|
||||
this.result.assertWithDiagnostics(() ->
|
||||
AssertionErrors.assertTrue("Expected empty body", this.result.getBody(byte[].class) == null));
|
||||
AssertionErrors.assertTrue("Expected empty body", this.result.getResponseBody() == null));
|
||||
return new EntityExchangeResult<>(this.result, null);
|
||||
}
|
||||
|
||||
|
||||
+5
-9
@@ -29,7 +29,6 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
@@ -160,14 +159,11 @@ public class ExchangeResult {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getBody(Class<T> bodyType) {
|
||||
return this.clientResponse.bodyTo(bodyType);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T getBody(ParameterizedTypeReference<T> bodyType) {
|
||||
return this.clientResponse.bodyTo(bodyType);
|
||||
/**
|
||||
* Provide access to the response. For internal use to decode the body.
|
||||
*/
|
||||
ConvertibleClientHttpResponse getClientResponse() {
|
||||
return this.clientResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user