From 6b59814cbc5ed995c8c786c99349e2ed5f7cf1cc Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 4 Apr 2018 15:20:16 -0700 Subject: [PATCH] Switch error views to use SimpleEvaluationContext Update `ErrorMvcAutoConfiguration` to use `SimpleEvaluationContext` rather than `StandardEvaluationContext`. Fixes gh-12507 --- .../web/ErrorMvcAutoConfiguration.java | 8 +- .../web/ErrorMvcAutoConfigurationTests.java | 81 +++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java index 4ca95579be..ced7474966 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java @@ -59,7 +59,7 @@ import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; 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.web.servlet.DispatcherServlet; import org.springframework.web.servlet.View; @@ -276,10 +276,8 @@ public class ErrorMvcAutoConfiguration { } private EvaluationContext getContext(Map map) { - StandardEvaluationContext context = new StandardEvaluationContext(); - context.addPropertyAccessor(new MapAccessor()); - context.setRootObject(map); - return context; + return SimpleEvaluationContext.forPropertyAccessors(new MapAccessor()) + .withRootObject(map).build(); } @Override diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfigurationTests.java new file mode 100644 index 0000000000..ff3d914953 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfigurationTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.web; + +import org.junit.Rule; +import org.junit.Test; + +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletContext; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.handler.DispatcherServletWebRequest; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ErrorMvcAutoConfiguration}. + * + * @author Brian Clozel + * @author Phillip Webb + */ +public class ErrorMvcAutoConfigurationTests { + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Test + public void renderContainsViewWithExceptionDetails() throws Exception { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + try { + context.setServletContext(new MockServletContext()); + context.register(ServerProperties.class, ErrorMvcAutoConfiguration.class); + context.refresh(); + View errorView = context.getBean("error", View.class); + ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); + DispatcherServletWebRequest webRequest = createWebRequest( + new IllegalStateException("Exception message"), false); + errorView.render(errorAttributes.getErrorAttributes(webRequest, true), + webRequest.getRequest(), webRequest.getResponse()); + assertThat(((MockHttpServletResponse) webRequest.getResponse()) + .getContentAsString()).contains("
Exception message
"); + } + finally { + context.close(); + } + } + + private DispatcherServletWebRequest createWebRequest(Exception ex, + boolean committed) { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); + MockHttpServletResponse response = new MockHttpServletResponse(); + DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, + response); + webRequest.setAttribute("javax.servlet.error.exception", ex, + RequestAttributes.SCOPE_REQUEST); + webRequest.setAttribute("javax.servlet.error.request_uri", "/path", + RequestAttributes.SCOPE_REQUEST); + response.setCommitted(committed); + response.setOutputStreamAccessAllowed(!committed); + response.setWriterAccessAllowed(!committed); + return webRequest; + } + +}