Commit 9372d154 authored by Dave Syer's avatar Dave Syer

Explicitly set status on raw unwrapped response

The ErrorPageFilter wasn't setting the response status in the case that
there was an error page mapped to the current request (i.e. for all
autoconfigured apps). N.B. this only affects non-embedded apps.

Fixes gh-1320
parent 6c5f8f95
...@@ -56,7 +56,7 @@ import org.springframework.web.filter.OncePerRequestFilter; ...@@ -56,7 +56,7 @@ import org.springframework.web.filter.OncePerRequestFilter;
@Component @Component
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements
Filter, NonEmbeddedServletContainerFactory { Filter, NonEmbeddedServletContainerFactory {
private static Log logger = LogFactory.getLog(ErrorPageFilter.class); private static Log logger = LogFactory.getLog(ErrorPageFilter.class);
...@@ -129,6 +129,7 @@ Filter, NonEmbeddedServletContainerFactory { ...@@ -129,6 +129,7 @@ Filter, NonEmbeddedServletContainerFactory {
response.sendError(status, message); response.sendError(status, message);
return; return;
} }
response.setStatus(status);
setErrorAttributes(request, status, message); setErrorAttributes(request, status, message);
request.getRequestDispatcher(errorPath).forward(request, response); request.getRequestDispatcher(errorPath).forward(request, response);
} }
......
...@@ -63,6 +63,28 @@ public class ErrorPageFilterTests { ...@@ -63,6 +63,28 @@ public class ErrorPageFilterTests {
assertTrue(this.response.isCommitted()); assertTrue(this.response.isCommitted());
} }
@Test
public void unauthorizedWithErrorPath() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(401, "UNAUTHORIZED");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request));
HttpServletResponseWrapper wrapper = (HttpServletResponseWrapper) this.chain
.getResponse();
assertThat(wrapper.getResponse(), equalTo((ServletResponse) this.response));
assertTrue(this.response.isCommitted());
assertThat(wrapper.getStatus(), equalTo(401));
// The real response has to be 401 as well...
assertThat(this.response.getStatus(), equalTo(401));
}
@Test @Test
public void responseCommitted() throws Exception { public void responseCommitted() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error")); this.filter.addErrorPages(new ErrorPage("/error"));
......
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