From d963597ec076da41a238659271ada292f3540275 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 24 Oct 2017 15:51:29 +0200 Subject: [PATCH] Test with hasError for unknown status code Issue: SPR-16108 --- .../web/client/DefaultResponseErrorHandler.java | 8 +++++--- .../client/DefaultResponseErrorHandlerTests.java | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java index 9c2097f3ab..e545f978e4 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java @@ -30,12 +30,14 @@ import org.springframework.util.FileCopyUtils; * Spring's default implementation of the {@link ResponseErrorHandler} interface. * *

This error handler checks for the status code on the {@link ClientHttpResponse}: - * Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or - * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an - * error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method. + * Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} + * or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be + * an error; this behavior can be changed by overriding the {@link #hasError(HttpStatus)} + * method. Unknown status codes will be ignored by {@link #hasError(ClientHttpResponse)}. * * @author Arjen Poutsma * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 3.0 * @see RestTemplate#setErrorHandler */ diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java index 40bb6981c6..b34cd1d599 100644 --- a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java @@ -67,8 +67,8 @@ public class DefaultResponseErrorHandlerTests { handler.handleError(response); fail("expected HttpClientErrorException"); } - catch (HttpClientErrorException e) { - assertSame(headers, e.getResponseHeaders()); + catch (HttpClientErrorException ex) { + assertSame(headers, ex.getResponseHeaders()); } } @@ -109,4 +109,16 @@ public class DefaultResponseErrorHandlerTests { handler.handleError(response); } + @Test // SPR-16108 + public void hasErrorForUnknownStatusCode() throws Exception { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_PLAIN); + + given(response.getRawStatusCode()).willReturn(999); + given(response.getStatusText()).willReturn("Custom status code"); + given(response.getHeaders()).willReturn(headers); + + assertFalse(handler.hasError(response)); + } + }