RestTestClient correctly exposes the response body

Closes gh-35385
This commit is contained in:
rstoyanchev
2025-08-26 18:21:56 +03:00
parent 55c315eb12
commit d8804c798b
3 changed files with 26 additions and 15 deletions
@@ -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);
}
@@ -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;
}
/**