Polishing in DefaultResponseErrorHandler

See gh-34231
This commit is contained in:
rstoyanchev
2024-11-28 17:03:39 +00:00
committed by Brian Clozel
parent 53b3b934fc
commit cdddf09c20
4 changed files with 127 additions and 131 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.web.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
@@ -32,6 +31,7 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.StreamUtils;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
@@ -72,7 +72,7 @@ class DefaultResponseErrorHandlerTests {
given(response.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(response.getStatusText()).willReturn("Not Found");
given(response.getHeaders()).willReturn(headers);
given(response.getBody()).willReturn(new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)));
given(response.getBody()).willReturn(new ByteArrayInputStream("Hello World".getBytes(UTF_8)));
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> handler.handleError(response))
@@ -90,18 +90,20 @@ class DefaultResponseErrorHandlerTests {
@Test
void handleErrorWithUrlAndQueryParameters() throws Exception {
String url = "https://example.com/resource";
setupClientHttpResponse(HttpStatus.NOT_FOUND, "Hello World");
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("https://example.com/resource?access_token=123"), HttpMethod.GET, response))
.withMessage("404 Not Found on GET request for \"https://example.com/resource\": \"Hello World\"");
.isThrownBy(() -> handler.handleError(URI.create(url + "?access_token=123"), HttpMethod.GET, response))
.withMessage("404 Not Found on GET request for \"" + url + "\": \"Hello World\"");
}
@Test
void handleErrorWithUrlAndNoBody() throws Exception {
String url = "https://example.com";
setupClientHttpResponse(HttpStatus.NOT_FOUND, null);
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> handler.handleError(URI.create("https://example.com"), HttpMethod.GET, response))
.withMessage("404 Not Found on GET request for \"https://example.com\": [no body]");
.isThrownBy(() -> handler.handleError(URI.create(url), HttpMethod.GET, response))
.withMessage("404 Not Found on GET request for \"" + url + "\": [no body]");
}
private void setupClientHttpResponse(HttpStatus status, @Nullable String textBody) throws Exception {
@@ -110,7 +112,7 @@ class DefaultResponseErrorHandlerTests {
given(response.getStatusText()).willReturn(status.getReasonPhrase());
if (textBody != null) {
headers.setContentType(MediaType.TEXT_PLAIN);
given(response.getBody()).willReturn(new ByteArrayInputStream(textBody.getBytes(StandardCharsets.UTF_8)));
given(response.getBody()).willReturn(new ByteArrayInputStream(textBody.getBytes(UTF_8)));
}
given(response.getHeaders()).willReturn(headers);
}
@@ -187,7 +189,7 @@ class DefaultResponseErrorHandlerTests {
headers.setContentType(MediaType.TEXT_PLAIN);
String responseBody = "Hello World";
TestByteArrayInputStream body = new TestByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8));
TestByteArrayInputStream body = new TestByteArrayInputStream(responseBody.getBytes(UTF_8));
given(response.getStatusCode()).willReturn(statusCode);
given(response.getStatusText()).willReturn(statusText);
@@ -227,7 +229,7 @@ class DefaultResponseErrorHandlerTests {
headers.setContentType(MediaType.TEXT_PLAIN);
String responseBody = "Hello World";
TestByteArrayInputStream body = new TestByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8));
TestByteArrayInputStream body = new TestByteArrayInputStream(responseBody.getBytes(UTF_8));
given(response.getStatusCode()).willReturn(statusCode);
given(response.getStatusText()).willReturn(statusText);
@@ -250,7 +252,7 @@ class DefaultResponseErrorHandlerTests {
public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8));
TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes(UTF_8));
given(response.getStatusCode()).willReturn(HttpStatusCode.valueOf(999));
given(response.getStatusText()).willReturn("Custom status code");
@@ -259,7 +261,7 @@ class DefaultResponseErrorHandlerTests {
assertThat(handler.hasError(response)).isFalse();
assertThat(body.isClosed()).isFalse();
assertThat(StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8)).isEqualTo("Hello World");
assertThat(StreamUtils.copyToString(response.getBody(), UTF_8)).isEqualTo("Hello World");
}

View File

@@ -19,6 +19,8 @@ package org.springframework.web.client;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -36,8 +38,11 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Unit tests for {@link ExtractingResponseErrorHandler}.
*
* @author Arjen Poutsma
*/
@SuppressWarnings("ALL")
class ExtractingResponseErrorHandlerTests {
private ExtractingResponseErrorHandler errorHandler;
@@ -48,13 +53,10 @@ class ExtractingResponseErrorHandlerTests {
@BeforeEach
void setup() {
HttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter();
this.errorHandler = new ExtractingResponseErrorHandler(
Collections.singletonList(converter));
this.errorHandler = new ExtractingResponseErrorHandler(List.of(converter));
this.errorHandler.setStatusMapping(
Collections.singletonMap(HttpStatus.I_AM_A_TEAPOT, MyRestClientException.class));
this.errorHandler.setSeriesMapping(Collections
.singletonMap(HttpStatus.Series.SERVER_ERROR, MyRestClientException.class));
this.errorHandler.setStatusMapping(Map.of(HttpStatus.I_AM_A_TEAPOT, MyRestClientException.class));
this.errorHandler.setSeriesMapping(Map.of(HttpStatus.Series.SERVER_ERROR, MyRestClientException.class));
}
@@ -72,8 +74,7 @@ class ExtractingResponseErrorHandlerTests {
@Test
void hasErrorOverride() throws Exception {
this.errorHandler.setSeriesMapping(Collections
.singletonMap(HttpStatus.Series.CLIENT_ERROR, null));
this.errorHandler.setSeriesMapping(Collections.singletonMap(HttpStatus.Series.CLIENT_ERROR, null));
given(this.response.getStatusCode()).willReturn(HttpStatus.I_AM_A_TEAPOT);
assertThat(this.errorHandler.hasError(this.response)).isTrue();
@@ -96,9 +97,9 @@ class ExtractingResponseErrorHandlerTests {
responseHeaders.setContentLength(body.length);
given(this.response.getBody()).willReturn(new ByteArrayInputStream(body));
assertThatExceptionOfType(MyRestClientException.class).isThrownBy(() ->
this.errorHandler.handleError(this.response))
.satisfies(ex -> assertThat(ex.getFoo()).isEqualTo("bar"));
assertThatExceptionOfType(MyRestClientException.class)
.isThrownBy(() -> this.errorHandler.handleError(this.response))
.satisfies(ex -> assertThat(ex.getFoo()).isEqualTo("bar"));
}
@Test
@@ -112,9 +113,9 @@ class ExtractingResponseErrorHandlerTests {
responseHeaders.setContentLength(body.length);
given(this.response.getBody()).willReturn(new ByteArrayInputStream(body));
assertThatExceptionOfType(MyRestClientException.class).isThrownBy(() ->
this.errorHandler.handleError(this.response))
.satisfies(ex -> assertThat(ex.getFoo()).isEqualTo("bar"));
assertThatExceptionOfType(MyRestClientException.class)
.isThrownBy(() -> this.errorHandler.handleError(this.response))
.satisfies(ex -> assertThat(ex.getFoo()).isEqualTo("bar"));
}
@Test
@@ -128,18 +129,17 @@ class ExtractingResponseErrorHandlerTests {
responseHeaders.setContentLength(body.length);
given(this.response.getBody()).willReturn(new ByteArrayInputStream(body));
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
this.errorHandler.handleError(this.response))
.satisfies(ex -> {
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(ex.getResponseBodyAsByteArray()).isEqualTo(body);
});
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> this.errorHandler.handleError(this.response))
.satisfies(ex -> {
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(ex.getResponseBodyAsByteArray()).isEqualTo(body);
});
}
@Test
void handleNoMatchOverride() throws Exception {
this.errorHandler.setSeriesMapping(Collections
.singletonMap(HttpStatus.Series.CLIENT_ERROR, null));
this.errorHandler.setSeriesMapping(Collections.singletonMap(HttpStatus.Series.CLIENT_ERROR, null));
given(this.response.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
HttpHeaders responseHeaders = new HttpHeaders();