Skip to content

Commit ce541be

Browse files
committed
Align BasicErrorController’s HTML response status with non-HTML status
Previously, BasicErrorController would return the response status set in the javax.servlet.error.status_code request attribute when serving JSON but would also return a 200 OK response when serving HTML. This didn’t cause much trouble when a person was browsing, but proved problematic for machine clients that request text/html and care about the response status. For example, the success handler would be driven for an XHR request even though the response was really an error. This commit updates BasicErrorController to set the response status for text/html responses to match the status that it would use in an application/json response. Closes spring-projectsgh-4694
1 parent b36fe2c commit ce541be

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
2223

2324
import org.springframework.beans.factory.annotation.Value;
2425
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
@@ -61,7 +62,9 @@ public String getErrorPath() {
6162
}
6263

6364
@RequestMapping(value = "${error.path:/error}", produces = "text/html")
64-
public ModelAndView errorHtml(HttpServletRequest request) {
65+
public ModelAndView errorHtml(HttpServletRequest request,
66+
HttpServletResponse response) {
67+
response.setStatus(getStatus(request).value());
6568
return new ModelAndView("error", getErrorAttributes(request, false));
6669
}
6770

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void errorPageAvailableWithParentContext() throws Exception {
8585
.run("--server.port=0"));
8686
MvcResult response = this.mockMvc
8787
.perform(get("/error").accept(MediaType.TEXT_HTML))
88-
.andExpect(status().isOk()).andReturn();
88+
.andExpect(status().is5xxServerError()).andReturn();
8989
String content = response.getResponse().getContentAsString();
9090
assertTrue("Wrong content: " + content, content.contains("status=999"));
9191
}
@@ -96,7 +96,7 @@ public void errorPageAvailableWithMvcIncluded() throws Exception {
9696
WebMvcIncludedConfiguration.class).run("--server.port=0"));
9797
MvcResult response = this.mockMvc
9898
.perform(get("/error").accept(MediaType.TEXT_HTML))
99-
.andExpect(status().isOk()).andReturn();
99+
.andExpect(status().is5xxServerError()).andReturn();
100100
String content = response.getResponse().getContentAsString();
101101
assertTrue("Wrong content: " + content, content.contains("status=999"));
102102
}
@@ -116,7 +116,7 @@ public void errorControllerWithAop() throws Exception {
116116
WithAopConfiguration.class).run("--server.port=0"));
117117
MvcResult response = this.mockMvc
118118
.perform(get("/error").accept(MediaType.TEXT_HTML))
119-
.andExpect(status().isOk()).andReturn();
119+
.andExpect(status().is5xxServerError()).andReturn();
120120
String content = response.getResponse().getContentAsString();
121121
assertTrue("Wrong content: " + content, content.contains("status=999"));
122122
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void testBindingExceptionForMachineClient() throws Exception {
121121
public void testDirectAccessForBrowserClient() throws Exception {
122122
MvcResult response = this.mockMvc
123123
.perform(get("/error").accept(MediaType.TEXT_HTML))
124-
.andExpect(status().isOk()).andReturn();
124+
.andExpect(status().is5xxServerError()).andReturn();
125125
String content = response.getResponse().getContentAsString();
126126
assertTrue("Wrong content: " + content, content.contains("ERROR_BEAN"));
127127
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorViewIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setup() {
6969
public void testErrorForBrowserClient() throws Exception {
7070
MvcResult response = this.mockMvc
7171
.perform(get("/error").accept(MediaType.TEXT_HTML))
72-
.andExpect(status().isOk()).andReturn();
72+
.andExpect(status().is5xxServerError()).andReturn();
7373
String content = response.getResponse().getContentAsString();
7474
assertTrue("Wrong content: " + content, content.contains("<html>"));
7575
assertTrue("Wrong content: " + content, content.contains("999"));
@@ -83,7 +83,7 @@ public void testErrorWithEscape() throws Exception {
8383
new RuntimeException(
8484
"<script>alert('Hello World')</script>"))
8585
.accept(MediaType.TEXT_HTML))
86-
.andExpect(status().isOk()).andReturn();
86+
.andExpect(status().is5xxServerError()).andReturn();
8787
String content = response.getResponse().getContentAsString();
8888
assertTrue("Wrong content: " + content, content.contains("&lt;script&gt;"));
8989
assertTrue("Wrong content: " + content, content.contains("Hello World"));

spring-boot-samples/spring-boot-sample-actuator-ui/src/test/java/sample/actuator/ui/SampleActuatorUiApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testError() throws Exception {
8989
ResponseEntity<String> entity = new TestRestTemplate().exchange(
9090
"http://localhost:" + this.port + "/error", HttpMethod.GET,
9191
new HttpEntity<Void>(headers), String.class);
92-
assertEquals(HttpStatus.OK, entity.getStatusCode());
92+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
9393
assertTrue("Wrong body:\n" + entity.getBody(),
9494
entity.getBody().contains("<html>"));
9595
assertTrue("Wrong body:\n" + entity.getBody(),

0 commit comments

Comments
 (0)