16
16
17
17
package org .springframework .web .servlet .handler ;
18
18
19
+ import java .io .IOException ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
23
24
25
+ import jakarta .servlet .Filter ;
26
+ import jakarta .servlet .ServletException ;
27
+ import jakarta .servlet .http .HttpServlet ;
24
28
import jakarta .servlet .http .HttpServletRequest ;
29
+ import jakarta .servlet .http .HttpServletResponse ;
25
30
import org .junit .jupiter .api .Test ;
26
31
import org .junit .jupiter .params .ParameterizedTest ;
27
32
import org .junit .jupiter .params .provider .ValueSource ;
44
49
import org .springframework .web .servlet .function .ServerResponse ;
45
50
import org .springframework .web .servlet .function .support .RouterFunctionMapping ;
46
51
import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerMapping ;
52
+ import org .springframework .web .testfixture .servlet .MockFilterChain ;
47
53
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
54
+ import org .springframework .web .testfixture .servlet .MockHttpServletResponse ;
48
55
import org .springframework .web .util .ServletRequestPathUtils ;
49
56
import org .springframework .web .util .pattern .PathPattern ;
50
57
import org .springframework .web .util .pattern .PathPatternParser ;
@@ -137,7 +144,7 @@ void getMatchable(boolean usePathPatterns) throws Exception {
137
144
@ Test
138
145
void getMatchableWhereHandlerMappingDoesNotImplementMatchableInterface () {
139
146
StaticWebApplicationContext cxt = new StaticWebApplicationContext ();
140
- cxt .registerSingleton ("mapping" , TestHandlerMapping .class );
147
+ cxt .registerBean ("mapping" , HandlerMapping .class , () -> request -> new HandlerExecutionChain ( new Object ()) );
141
148
cxt .refresh ();
142
149
143
150
MockHttpServletRequest request = new MockHttpServletRequest ();
@@ -193,6 +200,69 @@ void getCorsConfigurationActual() {
193
200
assertThat (corsConfig .getAllowedMethods ()).isEqualTo (Collections .singletonList ("POST" ));
194
201
}
195
202
203
+ @ Test
204
+ void cacheFilter () throws Exception {
205
+ testCacheFilter (new MockHttpServletRequest ());
206
+ }
207
+
208
+ @ Test
209
+ void cacheFilterRestoresPreviousValues () throws Exception {
210
+ TestMatchableHandlerMapping previousMapping = new TestMatchableHandlerMapping ();
211
+ CorsConfiguration previousCorsConfig = new CorsConfiguration ();
212
+
213
+ MockHttpServletRequest request = new MockHttpServletRequest ();
214
+ request .setAttribute (HandlerMappingIntrospector .MAPPING_ATTRIBUTE , previousMapping );
215
+ request .setAttribute (HandlerMappingIntrospector .CORS_CONFIG_ATTRIBUTE , previousCorsConfig );
216
+
217
+ testCacheFilter (request );
218
+
219
+ assertThat (previousMapping .getInvocationCount ()).isEqualTo (0 );
220
+ assertThat (request .getAttribute (HandlerMappingIntrospector .MAPPING_ATTRIBUTE )).isSameAs (previousMapping );
221
+ assertThat (request .getAttribute (HandlerMappingIntrospector .CORS_CONFIG_ATTRIBUTE )).isSameAs (previousCorsConfig );
222
+ }
223
+
224
+ private void testCacheFilter (MockHttpServletRequest request ) throws IOException , ServletException {
225
+ TestMatchableHandlerMapping mapping = new TestMatchableHandlerMapping ();
226
+ StaticWebApplicationContext context = new StaticWebApplicationContext ();
227
+ context .registerBean (TestMatchableHandlerMapping .class , () -> mapping );
228
+ context .refresh ();
229
+
230
+ HandlerMappingIntrospector introspector = initIntrospector (context );
231
+ MockHttpServletResponse response = new MockHttpServletResponse ();
232
+
233
+ Filter filter = (req , res , chain ) -> {
234
+ try {
235
+ for (int i = 0 ; i < 10 ; i ++) {
236
+ introspector .getMatchableHandlerMapping ((HttpServletRequest ) req );
237
+ introspector .getCorsConfiguration ((HttpServletRequest ) req );
238
+ }
239
+ }
240
+ catch (Exception ex ) {
241
+ throw new IllegalStateException (ex );
242
+ }
243
+ chain .doFilter (req , res );
244
+ };
245
+
246
+ HttpServlet servlet = new HttpServlet () {
247
+
248
+ @ Override
249
+ protected void service (HttpServletRequest req , HttpServletResponse res ) {
250
+ try {
251
+ res .getWriter ().print ("Success" );
252
+ }
253
+ catch (Exception ex ) {
254
+ throw new IllegalStateException (ex );
255
+ }
256
+ }
257
+ };
258
+
259
+ new MockFilterChain (servlet , introspector .createCacheFilter (), filter )
260
+ .doFilter (request , response );
261
+
262
+ assertThat (response .getContentAsString ()).isEqualTo ("Success" );
263
+ assertThat (mapping .getInvocationCount ()).isEqualTo (1 );
264
+ }
265
+
196
266
private HandlerMappingIntrospector initIntrospector (WebApplicationContext context ) {
197
267
HandlerMappingIntrospector introspector = new HandlerMappingIntrospector ();
198
268
introspector .setApplicationContext (context );
@@ -201,15 +271,6 @@ private HandlerMappingIntrospector initIntrospector(WebApplicationContext contex
201
271
}
202
272
203
273
204
- private static class TestHandlerMapping implements HandlerMapping {
205
-
206
- @ Override
207
- public HandlerExecutionChain getHandler (HttpServletRequest request ) {
208
- return new HandlerExecutionChain (new Object ());
209
- }
210
- }
211
-
212
-
213
274
@ Configuration
214
275
static class TestConfig {
215
276
@@ -248,6 +309,7 @@ void handle() {
248
309
}
249
310
}
250
311
312
+
251
313
private static class TestPathPatternParser extends PathPatternParser {
252
314
253
315
private final List <String > parsedPatterns = new ArrayList <>();
@@ -264,4 +326,25 @@ public PathPattern parse(String pathPattern) throws PatternParseException {
264
326
}
265
327
}
266
328
329
+
330
+ private static class TestMatchableHandlerMapping implements MatchableHandlerMapping {
331
+
332
+ private int invocationCount ;
333
+
334
+ public int getInvocationCount () {
335
+ return this .invocationCount ;
336
+ }
337
+
338
+ @ Override
339
+ public HandlerExecutionChain getHandler (HttpServletRequest request ) {
340
+ this .invocationCount ++;
341
+ return new HandlerExecutionChain (new Object ());
342
+ }
343
+
344
+ @ Override
345
+ public RequestMatchResult match (HttpServletRequest request , String pattern ) {
346
+ throw new UnsupportedOperationException ();
347
+ }
348
+ }
349
+
267
350
}
0 commit comments