Polish "Include URL and HTTP method in DefaultResponseErrorHandler"
See gh-28958
This commit is contained in:
@@ -29,6 +29,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -80,19 +81,38 @@ class DefaultResponseErrorHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleErrorWithUrlAndMethod() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
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)));
|
||||
|
||||
void handleErrorWithUrlAndMethod() throws Exception {
|
||||
setupClientHttpResponse(HttpStatus.NOT_FOUND, "Hello World");
|
||||
assertThatExceptionOfType(HttpClientErrorException.class)
|
||||
.isThrownBy(() -> handler.handleError(URI.create("https://example.com"), HttpMethod.GET, response))
|
||||
.withMessage("404 Not Found after GET https://example.com : \"Hello World\"")
|
||||
.satisfies(ex -> assertThat(ex.getResponseHeaders()).isSameAs(headers));
|
||||
.withMessage("404 Not Found on GET request for \"https://example.com\": \"Hello World\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleErrorWithUrlAndQueryParameters() throws Exception {
|
||||
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\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleErrorWithUrlAndNoBody() throws Exception {
|
||||
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]");
|
||||
}
|
||||
|
||||
private void setupClientHttpResponse(HttpStatus status, @Nullable String textBody) throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
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.getHeaders()).willReturn(headers);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -241,13 +241,16 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
|
||||
void notFound(ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
setUpClient(clientHttpRequestFactory);
|
||||
|
||||
String url = baseUrl + "/status/notfound";
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
|
||||
template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null))
|
||||
template.execute(url, HttpMethod.GET, null, null))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
assertThat(ex.getStatusText()).isNotNull();
|
||||
assertThat(ex.getResponseBodyAsString()).isNotNull();
|
||||
assertThat(ex.getMessage()).isEqualTo("404 Client Error after GET http://localhost:" + port + "/status/notfound : [no body]");
|
||||
assertThat(ex.getMessage()).containsSubsequence("404", "on GET request for \"" + url + "\": [no body]");
|
||||
assumeFalse(clientHttpRequestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
|
||||
assertThat(ex.getMessage()).isEqualTo("404 Client Error on GET request for \"" + url + "\": [no body]");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -255,12 +258,14 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
|
||||
void badRequest(ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
setUpClient(clientHttpRequestFactory);
|
||||
|
||||
String url = baseUrl + "/status/badrequest";
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
|
||||
template.execute(baseUrl + "/status/badrequest", HttpMethod.GET, null, null))
|
||||
template.execute(url, HttpMethod.GET, null, null))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat(ex.getMessage()).containsSubsequence("400", "on GET request for \""+url+ "\": [no body]");
|
||||
assumeFalse(clientHttpRequestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
|
||||
assertThat(ex.getMessage()).isEqualTo("400 Client Error after GET http://localhost:" + port + "/status/badrequest : [no body]");
|
||||
assertThat(ex.getMessage()).isEqualTo("400 Client Error on GET request for \""+url+ "\": [no body]");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -268,13 +273,16 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
|
||||
void serverError(ClientHttpRequestFactory clientHttpRequestFactory) {
|
||||
setUpClient(clientHttpRequestFactory);
|
||||
|
||||
String url = baseUrl + "/status/server";
|
||||
assertThatExceptionOfType(HttpServerErrorException.class).isThrownBy(() ->
|
||||
template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null))
|
||||
template.execute(url, HttpMethod.GET, null, null))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
assertThat(ex.getStatusText()).isNotNull();
|
||||
assertThat(ex.getResponseBodyAsString()).isNotNull();
|
||||
assertThat(ex.getMessage()).isEqualTo("500 Server Error after GET http://localhost:" + port + "/status/server : [no body]");
|
||||
assertThat(ex.getMessage()).containsSubsequence("500", "on GET request for \"" + url + "\": [no body]");
|
||||
assumeFalse(clientHttpRequestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
|
||||
assertThat(ex.getMessage()).isEqualTo("500 Server Error on GET request for \"" + url + "\": [no body]");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user