@Test public void addInterceptorsWithCustomPathMatcher() { PathMatcher pathMatcher = Mockito.mock(PathMatcher.class); registry.addInterceptor(interceptor1).addPathPatterns("/path1/**").pathMatcher(pathMatcher); MappedInterceptor mappedInterceptor = (MappedInterceptor) registry.getInterceptors().get(0); assertSame(pathMatcher, mappedInterceptor.getPathMatcher()); }
@Test public void addInterceptorsWithUrlPatterns() { registry .addInterceptor(interceptor1) .addPathPatterns("/path1/**") .excludePathPatterns("/path1/secret"); registry.addInterceptor(interceptor2).addPathPatterns("/path2"); assertEquals(Arrays.asList(interceptor1), getInterceptorsForPath("/path1")); assertEquals(Arrays.asList(interceptor2), getInterceptorsForPath("/path2")); assertEquals(Collections.emptyList(), getInterceptorsForPath("/path1/secret")); }
@Test public void addInterceptor() { registry.addInterceptor(interceptor1); List<HandlerInterceptor> interceptors = getInterceptorsForPath(null); assertEquals(Arrays.asList(interceptor1), interceptors); }
@Test public void addWebRequestInterceptor() throws Exception { registry.addWebRequestInterceptor(webRequestInterceptor1); List<HandlerInterceptor> interceptors = getInterceptorsForPath(null); assertEquals(1, interceptors.size()); verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1); }
@Test public void addWebRequestInterceptorsWithUrlPatterns() throws Exception { registry.addWebRequestInterceptor(webRequestInterceptor1).addPathPatterns("/path1"); registry.addWebRequestInterceptor(webRequestInterceptor2).addPathPatterns("/path2"); List<HandlerInterceptor> interceptors = getInterceptorsForPath("/path1"); assertEquals(1, interceptors.size()); verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor1); interceptors = getInterceptorsForPath("/path2"); assertEquals(1, interceptors.size()); verifyAdaptedInterceptor(interceptors.get(0), webRequestInterceptor2); }
private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) { PathMatcher pathMatcher = new AntPathMatcher(); List<HandlerInterceptor> result = new ArrayList<HandlerInterceptor>(); for (Object i : registry.getInterceptors()) { if (i instanceof MappedInterceptor) { MappedInterceptor mappedInterceptor = (MappedInterceptor) i; if (mappedInterceptor.matches(lookupPath, pathMatcher)) { result.add(mappedInterceptor.getInterceptor()); } } else if (i instanceof HandlerInterceptor) { result.add((HandlerInterceptor) i); } else { fail("Unexpected interceptor type: " + i.getClass().getName()); } } return result; }