Browse Source

Merge branch '2.1.x'

Closes gh-18343
pull/18347/head
Andy Wilkinson 6 years ago
parent
commit
a354657ace
  1. 5
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java
  2. 27
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerMockMvcTests.java

5
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java

@ -94,8 +94,11 @@ public class BasicErrorController extends AbstractErrorController { @@ -94,8 +94,11 @@ public class BasicErrorController extends AbstractErrorController {
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
if (status == HttpStatus.NO_CONTENT) {
return new ResponseEntity<Map<String, Object>>(status);
}
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
return new ResponseEntity<>(body, status);
}

27
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerMockMvcTests.java

@ -90,13 +90,23 @@ class BasicErrorControllerMockMvcTests { @@ -90,13 +90,23 @@ class BasicErrorControllerMockMvcTests {
}
@Test
void testErrorWithResponseStatus() throws Exception {
void testErrorWithNotFoundResponseStatus() throws Exception {
MvcResult result = this.mockMvc.perform(get("/bang")).andExpect(status().isNotFound()).andReturn();
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error")).andReturn();
String content = response.getResponse().getContentAsString();
assertThat(content).contains("Expected!");
}
@Test
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
void testBindingExceptionForMachineClient() throws Exception {
// In a real server the response is carried over into the error dispatcher, but
@ -168,6 +178,11 @@ class BasicErrorControllerMockMvcTests { @@ -168,6 +178,11 @@ class BasicErrorControllerMockMvcTests {
throw error;
}
@RequestMapping("/noContent")
void noContent() throws Exception {
throw new NoContentException("Expected!");
}
public String getFoo() {
return "foo";
}
@ -185,6 +200,15 @@ class BasicErrorControllerMockMvcTests { @@ -185,6 +200,15 @@ class BasicErrorControllerMockMvcTests {
}
@ResponseStatus(HttpStatus.NO_CONTENT)
private static class NoContentException extends RuntimeException {
NoContentException(String string) {
super(string);
}
}
private class ErrorDispatcher implements RequestBuilder {
private MvcResult result;
@ -201,6 +225,7 @@ class BasicErrorControllerMockMvcTests { @@ -201,6 +225,7 @@ class BasicErrorControllerMockMvcTests {
MockHttpServletRequest request = this.result.getRequest();
request.setDispatcherType(DispatcherType.ERROR);
request.setRequestURI(this.path);
request.setAttribute("javax.servlet.error.status_code", this.result.getResponse().getStatus());
return request;
}

Loading…
Cancel
Save