|
70 | 70 | import static org.assertj.core.api.Assertions.assertThat;
|
71 | 71 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
72 | 72 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 73 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
73 | 74 | import static org.mockito.ArgumentMatchers.anyString;
|
74 | 75 | import static org.mockito.Mockito.never;
|
75 | 76 | import static org.mockito.Mockito.spy;
|
76 | 77 | import static org.mockito.Mockito.verify;
|
77 | 78 |
|
78 | 79 | /**
|
| 80 | + * Tests for {@link DispatcherServlet}. |
| 81 | + * |
79 | 82 | * @author Rod Johnson
|
80 | 83 | * @author Juergen Hoeller
|
81 | 84 | * @author Rob Harrop
|
@@ -887,6 +890,39 @@ public void webDavMethod() throws Exception {
|
887 | 890 | assertThat(response.getContentAsString()).isEqualTo("body");
|
888 | 891 | }
|
889 | 892 |
|
| 893 | + @Test |
| 894 | + public void shouldResetResponseIfNotCommitted() throws Exception { |
| 895 | + StaticWebApplicationContext context = new StaticWebApplicationContext(); |
| 896 | + context.setServletContext(getServletContext()); |
| 897 | + context.registerSingleton("/error", ErrorController.class); |
| 898 | + DispatcherServlet servlet = new DispatcherServlet(context); |
| 899 | + servlet.init(servletConfig); |
| 900 | + |
| 901 | + MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error"); |
| 902 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 903 | + assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class) |
| 904 | + .hasCauseInstanceOf(IllegalArgumentException.class); |
| 905 | + assertThat(response.getContentAsByteArray()).isEmpty(); |
| 906 | + assertThat(response.getStatus()).isEqualTo(200); |
| 907 | + } |
| 908 | + |
| 909 | + @Test |
| 910 | + public void shouldAttemptToResetResponseIfCommitted() throws Exception { |
| 911 | + StaticWebApplicationContext context = new StaticWebApplicationContext(); |
| 912 | + context.setServletContext(getServletContext()); |
| 913 | + context.registerSingleton("/error", ErrorController.class); |
| 914 | + DispatcherServlet servlet = new DispatcherServlet(context); |
| 915 | + servlet.init(servletConfig); |
| 916 | + |
| 917 | + MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error"); |
| 918 | + request.setAttribute("commit", true); |
| 919 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 920 | + assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class) |
| 921 | + .hasCauseInstanceOf(IllegalArgumentException.class); |
| 922 | + assertThat(response.getContentAsByteArray()).isNotEmpty(); |
| 923 | + assertThat(response.getStatus()).isEqualTo(400); |
| 924 | + } |
| 925 | + |
890 | 926 |
|
891 | 927 | public static class ControllerFromParent implements Controller {
|
892 | 928 |
|
@@ -934,4 +970,16 @@ public SimpleUrlHandlerMapping handlerMapping() {
|
934 | 970 | }
|
935 | 971 | }
|
936 | 972 |
|
| 973 | + private static class ErrorController implements Controller { |
| 974 | + @Override |
| 975 | + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 976 | + response.setStatus(400); |
| 977 | + if (request.getAttribute("commit") != null) { |
| 978 | + response.flushBuffer(); |
| 979 | + } |
| 980 | + response.getWriter().write("error"); |
| 981 | + throw new IllegalArgumentException("test error"); |
| 982 | + } |
| 983 | + } |
| 984 | + |
937 | 985 | }
|
0 commit comments