Handle error responses in RestClientAdapter

Closes gh-35375
This commit is contained in:
rstoyanchev
2025-08-29 16:02:14 +03:00
parent f22d1eab23
commit 441b14b0c1
2 changed files with 39 additions and 13 deletions
@@ -56,6 +56,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.client.ApiVersionInserter;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
@@ -69,6 +70,7 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Integration tests for {@link HttpServiceProxyFactory} with {@link RestClient}
@@ -335,6 +337,15 @@ class RestClientAdapterTests {
assertThat(StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8)).isEqualTo("Hello Spring 2!");
}
@Test // gh-35375
void getInputStreamWithError() {
prepareResponse(builder -> builder.code(400).body("rejected"));
assertThatThrownBy(() -> initService().getInputStream())
.isExactlyInstanceOf(HttpClientErrorException.BadRequest.class)
.hasMessage("400 Client Error: \"rejected\"");
}
@Test
void postOutputStream() throws Exception {
prepareResponse(builder ->