Skip to content

Commit d963597

Browse files
committed
Test with hasError for unknown status code
Issue: SPR-16108
1 parent 9cfa929 commit d963597

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
3131
*
3232
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}:
33-
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
34-
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an
35-
* error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method.
33+
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}
34+
* or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be
35+
* an error; this behavior can be changed by overriding the {@link #hasError(HttpStatus)}
36+
* method. Unknown status codes will be ignored by {@link #hasError(ClientHttpResponse)}.
3637
*
3738
* @author Arjen Poutsma
3839
* @author Rossen Stoyanchev
40+
* @author Juergen Hoeller
3941
* @since 3.0
4042
* @see RestTemplate#setErrorHandler
4143
*/

spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void handleError() throws Exception {
6767
handler.handleError(response);
6868
fail("expected HttpClientErrorException");
6969
}
70-
catch (HttpClientErrorException e) {
71-
assertSame(headers, e.getResponseHeaders());
70+
catch (HttpClientErrorException ex) {
71+
assertSame(headers, ex.getResponseHeaders());
7272
}
7373
}
7474

@@ -109,4 +109,16 @@ public void unknownStatusCode() throws Exception {
109109
handler.handleError(response);
110110
}
111111

112+
@Test // SPR-16108
113+
public void hasErrorForUnknownStatusCode() throws Exception {
114+
HttpHeaders headers = new HttpHeaders();
115+
headers.setContentType(MediaType.TEXT_PLAIN);
116+
117+
given(response.getRawStatusCode()).willReturn(999);
118+
given(response.getStatusText()).willReturn("Custom status code");
119+
given(response.getHeaders()).willReturn(headers);
120+
121+
assertFalse(handler.hasError(response));
122+
}
123+
112124
}

0 commit comments

Comments
 (0)