|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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.servlet.error; |
| 18 | + |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 23 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 24 | +import org.springframework.boot.test.rule.OutputCapture; |
| 25 | +import org.springframework.boot.web.servlet.error.ErrorAttributes; |
| 26 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 27 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 28 | +import org.springframework.web.context.request.WebRequest; |
| 29 | +import org.springframework.web.servlet.View; |
| 30 | +import org.springframework.web.servlet.handler.DispatcherServletWebRequest; |
| 31 | + |
| 32 | +import static org.hamcrest.Matchers.allOf; |
| 33 | +import static org.hamcrest.Matchers.containsString; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link ErrorMvcAutoConfiguration}. |
| 37 | + * |
| 38 | + * @author Brian Clozel |
| 39 | + */ |
| 40 | +public class ErrorMvcAutoConfigurationTests { |
| 41 | + |
| 42 | + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() |
| 43 | + .withConfiguration(AutoConfigurations.of(ErrorMvcAutoConfiguration.class)); |
| 44 | + |
| 45 | + @Rule |
| 46 | + public OutputCapture outputCapture = new OutputCapture(); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testDefaultViewWithResponseAlreadyCommitted() { |
| 50 | + this.contextRunner.run((context) -> { |
| 51 | + View errorView = context.getBean("error", View.class); |
| 52 | + ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); |
| 53 | + DispatcherServletWebRequest webRequest = |
| 54 | + createCommittedWebRequest(new IllegalStateException("Exception message")); |
| 55 | + errorView.render(errorAttributes.getErrorAttributes(webRequest, true), |
| 56 | + webRequest.getRequest(), webRequest.getResponse()); |
| 57 | + this.outputCapture.expect(allOf( |
| 58 | + containsString("Cannot render error page for request [/path]" + |
| 59 | + " and exception [Exception message]] as the response has already been committed."), |
| 60 | + containsString("As a result, the response may have the wrong status code."))); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + protected DispatcherServletWebRequest createCommittedWebRequest(Exception e) { |
| 65 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path"); |
| 66 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 67 | + DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, response); |
| 68 | + webRequest.setAttribute("javax.servlet.error.exception", e, WebRequest.SCOPE_REQUEST); |
| 69 | + webRequest.setAttribute("javax.servlet.error.request_uri", "/path", WebRequest.SCOPE_REQUEST); |
| 70 | + response.setCommitted(true); |
| 71 | + response.setOutputStreamAccessAllowed(false); |
| 72 | + response.setWriterAccessAllowed(false); |
| 73 | + return webRequest; |
| 74 | + } |
| 75 | +} |
0 commit comments