|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.web; |
| 18 | + |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import org.springframework.boot.test.rule.OutputCapture; |
| 23 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 24 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 25 | +import org.springframework.mock.web.MockServletContext; |
| 26 | +import org.springframework.web.context.request.RequestAttributes; |
| 27 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 28 | +import org.springframework.web.servlet.View; |
| 29 | +import org.springframework.web.servlet.handler.DispatcherServletWebRequest; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link ErrorMvcAutoConfiguration}. |
| 35 | + * |
| 36 | + * @author Brian Clozel |
| 37 | + * @author Phillip Webb |
| 38 | + */ |
| 39 | +public class ErrorMvcAutoConfigurationTests { |
| 40 | + |
| 41 | + @Rule |
| 42 | + public OutputCapture outputCapture = new OutputCapture(); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void renderContainsViewWithExceptionDetails() throws Exception { |
| 46 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
| 47 | + try { |
| 48 | + context.setServletContext(new MockServletContext()); |
| 49 | + context.register(ServerProperties.class, ErrorMvcAutoConfiguration.class); |
| 50 | + context.refresh(); |
| 51 | + View errorView = context.getBean("error", View.class); |
| 52 | + ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); |
| 53 | + DispatcherServletWebRequest webRequest = createWebRequest( |
| 54 | + new IllegalStateException("Exception message"), false); |
| 55 | + errorView.render(errorAttributes.getErrorAttributes(webRequest, true), |
| 56 | + webRequest.getRequest(), webRequest.getResponse()); |
| 57 | + assertThat(((MockHttpServletResponse) webRequest.getResponse()) |
| 58 | + .getContentAsString()).contains("<div>Exception message</div>"); |
| 59 | + } |
| 60 | + finally { |
| 61 | + context.close(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private DispatcherServletWebRequest createWebRequest(Exception ex, |
| 66 | + boolean committed) { |
| 67 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); |
| 68 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 69 | + DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, |
| 70 | + response); |
| 71 | + webRequest.setAttribute("javax.servlet.error.exception", ex, |
| 72 | + RequestAttributes.SCOPE_REQUEST); |
| 73 | + webRequest.setAttribute("javax.servlet.error.request_uri", "/path", |
| 74 | + RequestAttributes.SCOPE_REQUEST); |
| 75 | + response.setCommitted(committed); |
| 76 | + response.setOutputStreamAccessAllowed(!committed); |
| 77 | + response.setWriterAccessAllowed(!committed); |
| 78 | + return webRequest; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments