Skip to content

Commit 500edea

Browse files
committed
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
1 parent 5c0d400 commit 500edea

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* </ul>
5353
*
5454
* @author Phillip Webb
55+
* @author Andy Wilkinson
5556
* @since 1.4.0
5657
*/
5758
public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered {
@@ -172,6 +173,7 @@ public String getContentType() {
172173
@Override
173174
public void render(Map<String, ?> model, HttpServletRequest request,
174175
HttpServletResponse response) throws Exception {
176+
response.setContentType(getContentType());
175177
FileCopyUtils.copy(this.resource.getInputStream(),
176178
response.getOutputStream());
177179
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.core.env.Environment;
3838
import org.springframework.core.io.ResourceLoader;
3939
import org.springframework.http.HttpStatus;
40+
import org.springframework.http.MediaType;
4041
import org.springframework.mock.web.MockHttpServletRequest;
4142
import org.springframework.mock.web.MockHttpServletResponse;
4243
import org.springframework.web.servlet.ModelAndView;
@@ -53,6 +54,7 @@
5354
* Tests for {@link DefaultErrorViewResolver}.
5455
*
5556
* @author Phillip Webb
57+
* @author Andy Wilkinson
5658
*/
5759
public class DefaultErrorViewResolverTests {
5860

@@ -145,23 +147,29 @@ public void resolveWhenExactResourceMatchShouldReturnResource() throws Exception
145147
setResourceLocation("/exact");
146148
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
147149
HttpStatus.NOT_FOUND, this.model);
148-
assertThat(render(resolved)).isEqualTo("exact/404");
150+
MockHttpServletResponse response = render(resolved);
151+
assertThat(response.getContentAsString().trim()).isEqualTo("exact/404");
152+
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
149153
}
150154

151155
@Test
152156
public void resolveWhenSeries4xxResourceMatchShouldReturnResource() throws Exception {
153157
setResourceLocation("/4xx");
154158
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
155159
HttpStatus.NOT_FOUND, this.model);
156-
assertThat(render(resolved)).isEqualTo("4xx/4xx");
160+
MockHttpServletResponse response = render(resolved);
161+
assertThat(response.getContentAsString().trim()).isEqualTo("4xx/4xx");
162+
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
157163
}
158164

159165
@Test
160166
public void resolveWhenSeries5xxResourceMatchShouldReturnResource() throws Exception {
161167
setResourceLocation("/5xx");
162168
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
163169
HttpStatus.INTERNAL_SERVER_ERROR, this.model);
164-
assertThat(render(resolved)).isEqualTo("5xx/5xx");
170+
MockHttpServletResponse response = render(resolved);
171+
assertThat(response.getContentAsString().trim()).isEqualTo("5xx/5xx");
172+
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
165173
}
166174

167175
@Test
@@ -185,7 +193,9 @@ public void resolveWhenExactResourceMatchAndSeriesTemplateMatchShouldFavorResour
185193
any(ResourceLoader.class))).willReturn(true);
186194
ModelAndView resolved = this.resolver.resolveErrorView(this.request,
187195
HttpStatus.NOT_FOUND, this.model);
188-
assertThat(render(resolved)).isEqualTo("exact/404");
196+
MockHttpServletResponse response = render(resolved);
197+
assertThat(response.getContentAsString().trim()).isEqualTo("exact/404");
198+
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE);
189199
}
190200

191201
@Test
@@ -205,10 +215,10 @@ private void setResourceLocation(String path) {
205215
"classpath:" + packageName.replace(".", "/") + path + "/" });
206216
}
207217

208-
private String render(ModelAndView modelAndView) throws Exception {
218+
private MockHttpServletResponse render(ModelAndView modelAndView) throws Exception {
209219
MockHttpServletResponse response = new MockHttpServletResponse();
210220
modelAndView.getView().render(this.model, this.request, response);
211-
return response.getContentAsString().trim();
221+
return response;
212222
}
213223

214224
}

0 commit comments

Comments
 (0)