Refactor solution for respones error details
See gh-1956
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.http.HttpMethod.*;
|
||||
import static org.springframework.http.HttpStatus.*;
|
||||
|
||||
public class DefaultHttpErrorDetailsExtractorTests {
|
||||
|
||||
private final DefaultHttpErrorDetailsExtractor extractor = new DefaultHttpErrorDetailsExtractor();
|
||||
|
||||
@Test
|
||||
public void shouldGetSimpleExceptionMessage() {
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", null, null, null, null);
|
||||
|
||||
assertEquals("Should get a simple message", "404 Not Found", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithoutBody() {
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", null, null, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a complete message without body", "404 Not Found after GET http://localhost:8080/my-endpoint : [no body]", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithShortAsciiBodyNoCharset() {
|
||||
String responseBody = "my short response body";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(), null, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", "404 Not Found after GET http://localhost:8080/my-endpoint : [my short response body]", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithShortAsciiBodyUtfCharset() {
|
||||
String responseBody = "my short response body";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(), UTF_8, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", "404 Not Found after GET http://localhost:8080/my-endpoint : [my short response body]", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithShortUtfBodyUtfCharset() {
|
||||
String responseBody = "my short response body \u0105\u0119";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(), UTF_8, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", "404 Not Found after GET http://localhost:8080/my-endpoint : [my short response body \u0105\u0119]", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithShortUtfBodyNoCharset() {
|
||||
String responseBody = "my short response body \u0105\u0119";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(UTF_8), null, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", "404 Not Found after GET http://localhost:8080/my-endpoint : [my short response body \u00c4\u0085\u00c4\u0099]", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithLongAsciiBodyNoCharset() {
|
||||
String responseBody = Strings.repeat("asdfg", 100);
|
||||
String expectedMessage = "404 Not Found after GET http://localhost:8080/my-endpoint : [" + Strings.repeat("asdfg", 40) + "... (500 bytes)]";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(UTF_8), null, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", expectedMessage, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithLongUtfBodyNoCharset() {
|
||||
String responseBody = Strings.repeat("asd\u0105\u0119", 100);
|
||||
String expectedMessage = "404 Not Found after GET http://localhost:8080/my-endpoint : [" + Strings.repeat("asd\u00c4\u0085\u00c4\u0099", 28) + "asd\u00c4... (700 bytes)]";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(UTF_8), null, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", expectedMessage, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetCompleteMessageWithLongUtfBodyUtfCharset() {
|
||||
String responseBody = Strings.repeat("asd\u0105\u0119", 100);
|
||||
String expectedMessage = "404 Not Found after GET http://localhost:8080/my-endpoint : [" + Strings.repeat("asd\u0105\u0119", 40) + "... (700 bytes)]";
|
||||
|
||||
String actual = extractor.getErrorDetails(NOT_FOUND.value(), "Not Found", responseBody.getBytes(UTF_8), UTF_8, URI.create("http://localhost:8080/my-endpoint"), GET);
|
||||
|
||||
assertEquals("Should get a simple message", expectedMessage, actual);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,8 +19,10 @@ package org.springframework.web.client;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -69,9 +71,28 @@ public class DefaultResponseErrorHandlerTests {
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
|
||||
handler.handleError(response))
|
||||
.satisfies(ex -> assertThat(ex.getResponseHeaders()).isSameAs(headers));
|
||||
assertThatExceptionOfType(HttpClientErrorException.class)
|
||||
.isThrownBy(() -> handler.handleError(response))
|
||||
.satisfies(ex -> assertThat(ex.getResponseHeaders()).isSameAs(headers))
|
||||
.satisfies(ex -> assertThat(ex.getMessage()).isEqualTo("404 Not Found: [Hello World]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleErrorWithLongBody() throws Exception {
|
||||
|
||||
Function<Integer, String> bodyGenerator =
|
||||
size -> Flux.just("a").repeat(size-1).reduce((s, s2) -> s + s2).block();
|
||||
|
||||
given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
|
||||
given(response.getStatusText()).willReturn("Not Found");
|
||||
given(response.getHeaders()).willReturn(new HttpHeaders());
|
||||
given(response.getBody()).willReturn(
|
||||
new ByteArrayInputStream(bodyGenerator.apply(500).getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
assertThatExceptionOfType(HttpClientErrorException.class)
|
||||
.isThrownBy(() -> handler.handleError(response))
|
||||
.satisfies(ex -> assertThat(ex.getMessage()).isEqualTo(
|
||||
"404 Not Found: [" + bodyGenerator.apply(200) + "... (500 bytes)]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,8 +105,7 @@ public class DefaultResponseErrorHandlerTests {
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
given(response.getBody()).willThrow(new IOException());
|
||||
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
|
||||
handler.handleError(response));
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() -> handler.handleError(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -254,8 +254,7 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
|
||||
template.execute(baseUrl + "/status/badrequest", HttpMethod.GET, null, null))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(ex.getMessage()).isEqualTo(
|
||||
"400 Client Error after GET http://localhost:" + port + "/status/badrequest : [no body]");
|
||||
assertThat(ex.getMessage()).isEqualTo("400 Client Error: [no body]");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user