|
17 | 17 | package org.springframework.web.servlet;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.util.Collections; |
20 | 21 | import java.util.Locale;
|
| 22 | +import java.util.Map; |
21 | 23 |
|
| 24 | +import javax.servlet.DispatcherType; |
22 | 25 | import javax.servlet.Servlet;
|
23 | 26 | import javax.servlet.ServletConfig;
|
24 | 27 | import javax.servlet.ServletContext;
|
|
35 | 38 | import org.springframework.beans.testfixture.beans.TestBean;
|
36 | 39 | import org.springframework.context.ApplicationContextInitializer;
|
37 | 40 | import org.springframework.context.ConfigurableApplicationContext;
|
| 41 | +import org.springframework.context.annotation.Bean; |
38 | 42 | import org.springframework.core.env.ConfigurableEnvironment;
|
39 | 43 | import org.springframework.http.HttpHeaders;
|
| 44 | +import org.springframework.http.server.RequestPath; |
| 45 | +import org.springframework.web.HttpRequestHandler; |
40 | 46 | import org.springframework.web.context.ConfigurableWebApplicationContext;
|
41 | 47 | import org.springframework.web.context.ConfigurableWebEnvironment;
|
42 | 48 | import org.springframework.web.context.ContextLoader;
|
43 | 49 | import org.springframework.web.context.ServletConfigAwareBean;
|
44 | 50 | import org.springframework.web.context.ServletContextAwareBean;
|
45 | 51 | import org.springframework.web.context.WebApplicationContext;
|
| 52 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
46 | 53 | import org.springframework.web.context.support.StandardServletEnvironment;
|
47 | 54 | import org.springframework.web.context.support.StaticWebApplicationContext;
|
48 | 55 | import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
|
56 | 63 | import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
57 | 64 | import org.springframework.web.testfixture.servlet.MockServletConfig;
|
58 | 65 | import org.springframework.web.testfixture.servlet.MockServletContext;
|
| 66 | +import org.springframework.web.util.ServletRequestPathUtils; |
59 | 67 | import org.springframework.web.util.WebUtils;
|
| 68 | +import org.springframework.web.util.pattern.PathPatternParser; |
60 | 69 |
|
61 | 70 | import static org.assertj.core.api.Assertions.assertThat;
|
62 | 71 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -700,6 +709,27 @@ public void withNoViewAndSamePath() throws Exception {
|
700 | 709 | complexDispatcherServlet.service(request, response));
|
701 | 710 | }
|
702 | 711 |
|
| 712 | + @Test // gh-26318 |
| 713 | + public void parsedRequestPathIsRestoredOnForward() throws Exception { |
| 714 | + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
| 715 | + context.register(PathPatternParserConfig.class); |
| 716 | + DispatcherServlet servlet = new DispatcherServlet(context); |
| 717 | + servlet.init(servletConfig); |
| 718 | + |
| 719 | + RequestPath previousRequestPath = RequestPath.parse("/", null); |
| 720 | + |
| 721 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test"); |
| 722 | + request.setDispatcherType(DispatcherType.FORWARD); |
| 723 | + request.setAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE, previousRequestPath); |
| 724 | + |
| 725 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 726 | + servlet.service(request, response); |
| 727 | + |
| 728 | + assertThat(response.getStatus()).isEqualTo(200); |
| 729 | + assertThat(response.getContentAsString()).isEqualTo("test-body"); |
| 730 | + assertThat(request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE)).isSameAs(previousRequestPath); |
| 731 | + } |
| 732 | + |
703 | 733 | @Test
|
704 | 734 | public void dispatcherServletRefresh() throws ServletException {
|
705 | 735 | MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
|
@@ -867,4 +897,22 @@ public void initialize(ConfigurableWebApplicationContext applicationContext) {
|
867 | 897 | }
|
868 | 898 | }
|
869 | 899 |
|
| 900 | + |
| 901 | + private static class PathPatternParserConfig { |
| 902 | + |
| 903 | + @Bean |
| 904 | + public SimpleUrlHandlerMapping handlerMapping() { |
| 905 | + Map<String, Object> urlMap = Collections.singletonMap("/test", |
| 906 | + (HttpRequestHandler) (request, response) -> { |
| 907 | + response.setStatus(200); |
| 908 | + response.getWriter().print("test-body"); |
| 909 | + }); |
| 910 | + |
| 911 | + SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); |
| 912 | + mapping.setPatternParser(new PathPatternParser()); |
| 913 | + mapping.setUrlMap(urlMap); |
| 914 | + return mapping; |
| 915 | + } |
| 916 | + } |
| 917 | + |
870 | 918 | }
|
0 commit comments