DefaultResponseErrorHandler detects non-standard error code as well
Issue: SPR-17439
This commit is contained in:
@@ -28,7 +28,6 @@ import org.springframework.http.client.ClientHttpResponse;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.http.HttpStatus.*;
|
||||
|
||||
/**
|
||||
@@ -66,7 +65,7 @@ public class DefaultResponseErrorHandlerHttpStatusTests {
|
||||
public HttpStatus httpStatus;
|
||||
|
||||
@Parameterized.Parameter(1)
|
||||
public Class expectedExceptionClass;
|
||||
public Class<? extends Throwable> expectedExceptionClass;
|
||||
|
||||
private final DefaultResponseErrorHandler handler = new DefaultResponseErrorHandler();
|
||||
|
||||
@@ -74,7 +73,13 @@ public class DefaultResponseErrorHandlerHttpStatusTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void handleErrorIOException() throws Exception {
|
||||
public void hasErrorTrue() throws Exception {
|
||||
given(this.response.getRawStatusCode()).willReturn(this.httpStatus.value());
|
||||
assertTrue(this.handler.hasError(this.response));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleErrorException() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
@@ -91,10 +96,4 @@ public class DefaultResponseErrorHandlerHttpStatusTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasErrorTrue() throws Exception {
|
||||
given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
|
||||
assertTrue(handler.hasError(this.response));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class DefaultResponseErrorHandlerTests {
|
||||
given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
|
||||
given(response.getStatusText()).willReturn("Not Found");
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("Hello World".getBytes("UTF-8")));
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
try {
|
||||
handler.handleError(response);
|
||||
@@ -101,18 +101,6 @@ public class DefaultResponseErrorHandlerTests {
|
||||
handler.handleError(response);
|
||||
}
|
||||
|
||||
@Test(expected = UnknownHttpStatusCodeException.class) // SPR-9406
|
||||
public void unknownStatusCode() 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);
|
||||
|
||||
handler.handleError(response);
|
||||
}
|
||||
|
||||
@Test // SPR-16108
|
||||
public void hasErrorForUnknownStatusCode() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
@@ -125,6 +113,66 @@ public class DefaultResponseErrorHandlerTests {
|
||||
assertFalse(handler.hasError(response));
|
||||
}
|
||||
|
||||
@Test(expected = UnknownHttpStatusCodeException.class) // SPR-9406
|
||||
public void handleErrorUnknownStatusCode() 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);
|
||||
|
||||
handler.handleError(response);
|
||||
}
|
||||
|
||||
@Test // SPR-17461
|
||||
public void hasErrorForCustomClientError() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
given(response.getRawStatusCode()).willReturn(499);
|
||||
given(response.getStatusText()).willReturn("Custom status code");
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
|
||||
assertTrue(handler.hasError(response));
|
||||
}
|
||||
|
||||
@Test(expected = UnknownHttpStatusCodeException.class)
|
||||
public void handleErrorForCustomClientError() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
given(response.getRawStatusCode()).willReturn(499);
|
||||
given(response.getStatusText()).willReturn("Custom status code");
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
|
||||
handler.handleError(response);
|
||||
}
|
||||
|
||||
@Test // SPR-17461
|
||||
public void hasErrorForCustomServerError() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
given(response.getRawStatusCode()).willReturn(599);
|
||||
given(response.getStatusText()).willReturn("Custom status code");
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
|
||||
assertTrue(handler.hasError(response));
|
||||
}
|
||||
|
||||
@Test(expected = UnknownHttpStatusCodeException.class)
|
||||
public void handleErrorForCustomServerError() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
given(response.getRawStatusCode()).willReturn(599);
|
||||
given(response.getStatusText()).willReturn("Custom status code");
|
||||
given(response.getHeaders()).willReturn(headers);
|
||||
|
||||
handler.handleError(response);
|
||||
}
|
||||
|
||||
@Test // SPR-16604
|
||||
public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
Reference in New Issue
Block a user