Merge branch '1.5.x'

pull/12762/merge
Phillip Webb 7 years ago
commit d49a1024bd

@ -69,7 +69,7 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
@ -304,10 +304,8 @@ public class ErrorMvcAutoConfiguration {
} }
private EvaluationContext getContext(Map<String, ?> map) { private EvaluationContext getContext(Map<String, ?> map) {
StandardEvaluationContext context = new StandardEvaluationContext(); return SimpleEvaluationContext.forPropertyAccessors(new MapAccessor())
context.addPropertyAccessor(new MapAccessor()); .withRootObject(map).build();
context.setRootObject(map);
return context;
} }
@Override @Override

@ -45,12 +45,26 @@ public class ErrorMvcAutoConfigurationTests {
public OutputCapture outputCapture = new OutputCapture(); public OutputCapture outputCapture = new OutputCapture();
@Test @Test
public void testDefaultViewWithResponseAlreadyCommitted() { public void renderContainsViewWithExceptionDetails() throws Exception {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class); View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createCommittedWebRequest( DispatcherServletWebRequest webRequest = createWebRequest(
new IllegalStateException("Exception message")); new IllegalStateException("Exception message"), false);
errorView.render(errorAttributes.getErrorAttributes(webRequest, true),
webRequest.getRequest(), webRequest.getResponse());
assertThat(((MockHttpServletResponse) webRequest.getResponse())
.getContentAsString()).contains("<div>Exception message</div>");
});
}
@Test
public void renderWhenAlreadyCommittedLogsMessage() {
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(
new IllegalStateException("Exception message"), true);
errorView.render(errorAttributes.getErrorAttributes(webRequest, true), errorView.render(errorAttributes.getErrorAttributes(webRequest, true),
webRequest.getRequest(), webRequest.getResponse()); webRequest.getRequest(), webRequest.getResponse());
assertThat(this.outputCapture.toString()) assertThat(this.outputCapture.toString())
@ -61,7 +75,8 @@ public class ErrorMvcAutoConfigurationTests {
}); });
} }
protected DispatcherServletWebRequest createCommittedWebRequest(Exception ex) { private DispatcherServletWebRequest createWebRequest(Exception ex,
boolean committed) {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request,
@ -70,9 +85,9 @@ public class ErrorMvcAutoConfigurationTests {
RequestAttributes.SCOPE_REQUEST); RequestAttributes.SCOPE_REQUEST);
webRequest.setAttribute("javax.servlet.error.request_uri", "/path", webRequest.setAttribute("javax.servlet.error.request_uri", "/path",
RequestAttributes.SCOPE_REQUEST); RequestAttributes.SCOPE_REQUEST);
response.setCommitted(true); response.setCommitted(committed);
response.setOutputStreamAccessAllowed(false); response.setOutputStreamAccessAllowed(!committed);
response.setWriterAccessAllowed(false); response.setWriterAccessAllowed(!committed);
return webRequest; return webRequest;
} }

Loading…
Cancel
Save