Skip to content

Commit a354657

Browse files
committed
Merge branch '2.1.x'
Closes gh-18343
2 parents 4ab53dc + c613418 commit a354657

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse re
9494

9595
@RequestMapping
9696
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
97-
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
9897
HttpStatus status = getStatus(request);
98+
if (status == HttpStatus.NO_CONTENT) {
99+
return new ResponseEntity<Map<String, Object>>(status);
100+
}
101+
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
99102
return new ResponseEntity<>(body, status);
100103
}
101104

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,23 @@ void testDirectAccessForMachineClient() throws Exception {
9090
}
9191

9292
@Test
93-
void testErrorWithResponseStatus() throws Exception {
93+
void testErrorWithNotFoundResponseStatus() throws Exception {
9494
MvcResult result = this.mockMvc.perform(get("/bang")).andExpect(status().isNotFound()).andReturn();
9595
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error")).andReturn();
9696
String content = response.getResponse().getContentAsString();
9797
assertThat(content).contains("Expected!");
9898
}
9999

100+
@Test
101+
void testErrorWithNoContentResponseStatus() throws Exception {
102+
MvcResult result = this.mockMvc.perform(get("/noContent").accept("some/thing"))
103+
.andExpect(status().isNoContent()).andReturn();
104+
MvcResult response = this.mockMvc.perform(new ErrorDispatcher(result, "/error"))
105+
.andExpect(status().isNoContent()).andReturn();
106+
String content = response.getResponse().getContentAsString();
107+
assertThat(content).isEmpty();
108+
}
109+
100110
@Test
101111
void testBindingExceptionForMachineClient() throws Exception {
102112
// In a real server the response is carried over into the error dispatcher, but
@@ -168,6 +178,11 @@ String bind() throws Exception {
168178
throw error;
169179
}
170180

181+
@RequestMapping("/noContent")
182+
void noContent() throws Exception {
183+
throw new NoContentException("Expected!");
184+
}
185+
171186
public String getFoo() {
172187
return "foo";
173188
}
@@ -185,6 +200,15 @@ static class NotFoundException extends RuntimeException {
185200

186201
}
187202

203+
@ResponseStatus(HttpStatus.NO_CONTENT)
204+
private static class NoContentException extends RuntimeException {
205+
206+
NoContentException(String string) {
207+
super(string);
208+
}
209+
210+
}
211+
188212
private class ErrorDispatcher implements RequestBuilder {
189213

190214
private MvcResult result;
@@ -201,6 +225,7 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
201225
MockHttpServletRequest request = this.result.getRequest();
202226
request.setDispatcherType(DispatcherType.ERROR);
203227
request.setRequestURI(this.path);
228+
request.setAttribute("javax.servlet.error.status_code", this.result.getResponse().getStatus());
204229
return request;
205230
}
206231

0 commit comments

Comments
 (0)