|
|
@ -92,13 +92,23 @@ public class BasicErrorControllerMockMvcTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testErrorWithResponseStatus() throws Exception {
|
|
|
|
public void testErrorWithNotFoundResponseStatus() throws Exception {
|
|
|
|
MvcResult result = this.mockMvc.perform(get("/bang")).andExpect(status().isNotFound()).andReturn();
|
|
|
|
MvcResult result = this.mockMvc.perform(get("/bang")).andExpect(status().isNotFound()).andReturn();
|
|
|
|
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error")).andReturn();
|
|
|
|
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error")).andReturn();
|
|
|
|
String content = response.getResponse().getContentAsString();
|
|
|
|
String content = response.getResponse().getContentAsString();
|
|
|
|
assertThat(content).contains("Expected!");
|
|
|
|
assertThat(content).contains("Expected!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void testErrorWithNoContentResponseStatus() throws Exception {
|
|
|
|
|
|
|
|
MvcResult result = this.mockMvc.perform(get("/noContent").accept("some/thing"))
|
|
|
|
|
|
|
|
.andExpect(status().isNoContent()).andReturn();
|
|
|
|
|
|
|
|
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error"))
|
|
|
|
|
|
|
|
.andExpect(status().isNoContent()).andReturn();
|
|
|
|
|
|
|
|
String content = response.getResponse().getContentAsString();
|
|
|
|
|
|
|
|
assertThat(content).isEmpty();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testBindingExceptionForMachineClient() throws Exception {
|
|
|
|
public void testBindingExceptionForMachineClient() throws Exception {
|
|
|
|
// In a real server the response is carried over into the error dispatcher, but
|
|
|
|
// In a real server the response is carried over into the error dispatcher, but
|
|
|
@ -174,6 +184,11 @@ public class BasicErrorControllerMockMvcTests {
|
|
|
|
throw error;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/noContent")
|
|
|
|
|
|
|
|
public void noContent() throws Exception {
|
|
|
|
|
|
|
|
throw new NoContentException("Expected!");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -187,6 +202,15 @@ public class BasicErrorControllerMockMvcTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
|
|
|
|
|
|
|
private static class NoContentException extends RuntimeException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NoContentException(String string) {
|
|
|
|
|
|
|
|
super(string);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class ErrorDispatcher implements RequestBuilder {
|
|
|
|
private class ErrorDispatcher implements RequestBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
private MvcResult result;
|
|
|
|
private MvcResult result;
|
|
|
@ -203,6 +227,7 @@ public class BasicErrorControllerMockMvcTests {
|
|
|
|
MockHttpServletRequest request = this.result.getRequest();
|
|
|
|
MockHttpServletRequest request = this.result.getRequest();
|
|
|
|
request.setDispatcherType(DispatcherType.ERROR);
|
|
|
|
request.setDispatcherType(DispatcherType.ERROR);
|
|
|
|
request.setRequestURI(this.path);
|
|
|
|
request.setRequestURI(this.path);
|
|
|
|
|
|
|
|
request.setAttribute("javax.servlet.error.status_code", this.result.getResponse().getStatus());
|
|
|
|
return request;
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|