1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .web .util ;
18
18
19
+ import java .io .IOException ;
20
+
21
+ import jakarta .servlet .Filter ;
22
+ import jakarta .servlet .RequestDispatcher ;
23
+ import jakarta .servlet .ServletException ;
24
+ import jakarta .servlet .http .HttpServletMapping ;
19
25
import jakarta .servlet .http .MappingMatch ;
20
26
import org .junit .jupiter .api .Test ;
21
27
@@ -88,6 +94,35 @@ void modifyPathContextWithContextPathEndingWithSlash() {
88
94
.withMessage ("Invalid contextPath '/persons/': must start with '/' and not end with '/'" );
89
95
}
90
96
97
+ @ Test
98
+ void filterParsesAndCachesThenCleansUp () throws IOException , ServletException {
99
+ MockHttpServletRequest request = createRequest ("/servlet/request" , "" , "/servlet" , "/request" );
100
+ Filter filter = ServletRequestPathUtils .getParsedRequestPathCacheFilter ();
101
+ filter .doFilter (request , null , (req , res ) -> {
102
+ RequestPath currentPath = ServletRequestPathUtils .getParsedRequestPath (request );
103
+ assertThat (currentPath .pathWithinApplication ().value ()).isEqualTo ("/request" );
104
+ });
105
+ assertThat (ServletRequestPathUtils .hasParsedRequestPath (request )).isFalse ();
106
+ }
107
+
108
+ @ Test
109
+ void filterParsesCachesAndThenRestores () throws IOException , ServletException {
110
+ MockHttpServletRequest request = createRequest ("/servlet/request" , "" , "/servlet" , "/request" );
111
+ RequestPath requestPath = ServletRequestPathUtils .parseAndCache (request );
112
+
113
+ HttpServletMapping mapping = new MockHttpServletMapping ("/include" , "" , "myServlet" , MappingMatch .PATH );
114
+ request .setAttribute (RequestDispatcher .INCLUDE_MAPPING , mapping );
115
+ request .setAttribute (WebUtils .INCLUDE_SERVLET_PATH_ATTRIBUTE , "/servlet" );
116
+ request .setAttribute (WebUtils .INCLUDE_REQUEST_URI_ATTRIBUTE , "/servlet/include" );
117
+
118
+ Filter filter = ServletRequestPathUtils .getParsedRequestPathCacheFilter ();
119
+ filter .doFilter (request , null , (req , res ) -> {
120
+ RequestPath currentPath = ServletRequestPathUtils .getParsedRequestPath (request );
121
+ assertThat (currentPath .pathWithinApplication ().value ()).isEqualTo ("/include" );
122
+ });
123
+ assertThat (requestPath ).isEqualTo (ServletRequestPathUtils .getParsedRequestPath (request ));
124
+ }
125
+
91
126
private void testParseAndCache (
92
127
String requestUri , String contextPath , String servletPath , String pathWithinApplication ) {
93
128
@@ -100,13 +135,19 @@ private void testParseAndCache(
100
135
private static RequestPath createRequestPath (
101
136
String requestUri , String contextPath , String servletPath , String pathWithinApplication ) {
102
137
138
+ MockHttpServletRequest request = createRequest (requestUri , contextPath , servletPath , pathWithinApplication );
139
+ return ServletRequestPathUtils .parseAndCache (request );
140
+ }
141
+
142
+ private static MockHttpServletRequest createRequest (
143
+ String requestUri , String contextPath , String servletPath , String pathWithinApplication ) {
144
+
103
145
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , requestUri );
104
146
request .setContextPath (contextPath );
105
147
request .setServletPath (servletPath );
106
148
request .setHttpServletMapping (new MockHttpServletMapping (
107
149
pathWithinApplication , contextPath , "myServlet" , MappingMatch .PATH ));
108
-
109
- return ServletRequestPathUtils .parseAndCache (request );
150
+ return request ;
110
151
}
111
152
112
153
}
0 commit comments