Commit 500edeac authored by Andy Wilkinson's avatar Andy Wilkinson

Ensure that static error pages have a Content-Type header

The getContentType() accessor method on View appears to only be used
for content negotiation. For the Content-Type header to be included in
the response the view needs to set it explicitly when render is called.

Closes gh-5918
parent 5c0d400c
......@@ -52,6 +52,7 @@ import org.springframework.web.servlet.View;
* </ul>
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.4.0
*/
public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {
......@@ -172,6 +173,7 @@ public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {
@Override
public void render(Map<String, ?> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType(getContentType());
FileCopyUtils.copy(this.resource.getInputStream(),
response.getOutputStream());
}
......
......@@ -37,6 +37,7 @@ import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
......@@ -53,6 +54,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* Tests for {@link DefaultErrorViewResolver}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class DefaultErrorViewResolverTests {
......@@ -145,7 +147,9 @@ public class DefaultErrorViewResolverTests {
setResourceLocation("/exact");
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
HttpStatus.NOT_FOUND, this.model);
assertThat(render(resolved)).isEqualTo("exact/404");
MockHttpServletResponse response = render(resolved);
assertThat(response.getContentAsString().trim()).isEqualTo("exact/404");
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
}
@Test
......@@ -153,7 +157,9 @@ public class DefaultErrorViewResolverTests {
setResourceLocation("/4xx");
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
HttpStatus.NOT_FOUND, this.model);
assertThat(render(resolved)).isEqualTo("4xx/4xx");
MockHttpServletResponse response = render(resolved);
assertThat(response.getContentAsString().trim()).isEqualTo("4xx/4xx");
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
}
@Test
......@@ -161,7 +167,9 @@ public class DefaultErrorViewResolverTests {
setResourceLocation("/5xx");
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
HttpStatus.INTERNAL_SERVER_ERROR, this.model);
assertThat(render(resolved)).isEqualTo("5xx/5xx");
MockHttpServletResponse response = render(resolved);
assertThat(response.getContentAsString().trim()).isEqualTo("5xx/5xx");
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
}
@Test
......@@ -185,7 +193,9 @@ public class DefaultErrorViewResolverTests {
any(ResourceLoader.class))).willReturn(true);
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
HttpStatus.NOT_FOUND, this.model);
assertThat(render(resolved)).isEqualTo("exact/404");
MockHttpServletResponse response = render(resolved);
assertThat(response.getContentAsString().trim()).isEqualTo("exact/404");
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
}
@Test
......@@ -205,10 +215,10 @@ public class DefaultErrorViewResolverTests {
"classpath:" + packageName.replace(".", "/") + path + "/" });
}
private String render(ModelAndView modelAndView) throws Exception {
private MockHttpServletResponse render(ModelAndView modelAndView) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
modelAndView.getView().render(this.model, this.request, response);
return response.getContentAsString().trim();
return response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment