1818
1919import java .io .IOException ;
2020
21+ import jakarta .servlet .DispatcherType ;
2122import jakarta .servlet .ServletException ;
2223import jakarta .servlet .http .HttpServlet ;
2324import jakarta .servlet .http .HttpServletRequest ;
@@ -127,20 +128,20 @@ private static void testNoUrlHandling(String pattern, String contextPath, String
127128 assertThat (response .isCommitted ()).isFalse ();
128129 }
129130
130- @ Test
131+ @ Test // gh-35538
131132 void shouldNotFilterErrorAndAsyncDispatches () {
132133 UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/**" ).wrapRequest ().build ();
133134
134135 assertThat (filter .shouldNotFilterAsyncDispatch ())
135- .as ("Should not filter async dispatch: wrapped request is reused" )
136+ .as ("Should not filter ASYNC dispatch as wrapped request is reused" )
136137 .isTrue ();
137138
138139 assertThat (filter .shouldNotFilterErrorDispatch ())
139- .as ("Should not filter error dispatch: it's a different path" )
140+ .as ("Should not filter ERROR dispatch as it's an internal, fixed path" )
140141 .isTrue ();
141142 }
142143
143- @ Test
144+ @ Test // gh-35538
144145 void shouldNotCacheParsedPath () throws Exception {
145146 UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/*" ).wrapRequest ().build ();
146147
@@ -155,8 +156,8 @@ void shouldNotCacheParsedPath() throws Exception {
155156 .isFalse ();
156157 }
157158
158- @ Test
159- void shouldReplaceCachedPath () throws Exception {
159+ @ Test // gh-35538
160+ void shouldClearPreviouslyCachedPath () throws Exception {
160161 UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/path/*" ).wrapRequest ().build ();
161162
162163 MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/path/123/" );
@@ -165,16 +166,38 @@ void shouldReplaceCachedPath() throws Exception {
165166 ServletRequestPathUtils .parseAndCache (request );
166167 assertThat (ServletRequestPathUtils .getParsedRequestPath (request ).value ()).isEqualTo ("/path/123/" );
167168
168- PathSavingServlet servlet = new PathSavingServlet ();
169+ PathServlet servlet = new PathServlet ();
169170 MockFilterChain chain = new MockFilterChain (servlet );
170171 filter .doFilterInternal (request , new MockHttpServletResponse (), chain );
171172
172- assertThat (servlet .getParsedPath ()).isEqualTo ("/path/123" );
173- assertThat (ServletRequestPathUtils .getParsedRequestPath (request ).value ()).isEqualTo ("/path/123/" );
173+ assertThat (servlet .getParsedPath ()).isNull ();
174+ }
175+
176+ @ Test // gh-35509
177+ void shouldRespectForwardedPath () throws Exception {
178+ UrlHandlerFilter filter = UrlHandlerFilter .trailingSlashHandler ("/requestURI/*" ).wrapRequest ().build ();
179+
180+ String requestURI = "/requestURI/123/" ;
181+ MockHttpServletRequest originalRequest = new MockHttpServletRequest ("GET" , requestURI );
182+ originalRequest .setServletPath (requestURI );
183+
184+ MockFilterChain chain = new MockFilterChain ();
185+ filter .doFilterInternal (originalRequest , new MockHttpServletResponse (), chain );
186+
187+ HttpServletRequest wrapped = (HttpServletRequest ) chain .getRequest ();
188+ assertThat (wrapped ).isNotNull ().isNotSameAs (originalRequest );
189+ assertThat (wrapped .getRequestURI ()).isEqualTo ("/requestURI/123" );
190+
191+ // Change dispatcher type of underlying requests
192+ originalRequest .setDispatcherType (DispatcherType .FORWARD );
193+ assertThat (wrapped .getRequestURI ())
194+ .as ("Should delegate to underlying request for the requestURI on FORWARD" )
195+ .isEqualTo (requestURI );
174196 }
175197
176198
177- private static class PathSavingServlet extends HttpServlet {
199+ @ SuppressWarnings ("serial" )
200+ private static class PathServlet extends HttpServlet {
178201
179202 private String parsedPath ;
180203
@@ -183,8 +206,9 @@ public String getParsedPath() {
183206 }
184207
185208 @ Override
186- protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
187- this .parsedPath = ServletRequestPathUtils .getParsedRequestPath (request ).value ();
209+ protected void doGet (HttpServletRequest request , HttpServletResponse response ) {
210+ this .parsedPath = (ServletRequestPathUtils .hasParsedRequestPath (request ) ?
211+ ServletRequestPathUtils .getParsedRequestPath (request ).value () : null );
188212 }
189213 }
190214
0 commit comments