/** * Add interceptors mapped to a set of path patterns. */ public StandaloneMockMvcBuilder addMappedInterceptors( @Nullable String[] pathPatterns, HandlerInterceptor... interceptors) { for (HandlerInterceptor interceptor : interceptors) { this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor)); } return this; }
/** * Build the underlying interceptor. If URL patterns are provided, the returned * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. */ protected Object getInterceptor() { if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { return this.interceptor; } String[] include = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }
/** * Since in the admin we have multiple handler mappings, we need to register a {@link MappedInterceptor} * to apply to all of them. Modifying just tine {@link InterceptorRegistry} will apply those * interceptors to _only_ the default RequestMappingHandlerMapping. */ @Bean public MappedInterceptor blJsFieldNameCompatibilityInterceptor() { return new MappedInterceptor(null, new JSFieldNameCompatibilityInterceptor()); }
@Bean public MappedInterceptor blLocaleChangeInterceptor() { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("blLocaleCode"); return new MappedInterceptor(null, localeInterceptor); }
/** * Build the underlying interceptor. If URL patterns are provided, the returned * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. */ protected Object getInterceptor() { if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { return this.interceptor; } String[] include = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }
@Test public void includePatternWithMatrixVariables() { MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo*/*" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo;q=1/bar;s=2", pathMatcher)); }
@Test public void noPatterns() { MappedInterceptor mappedInterceptor = new MappedInterceptor(null, null, this.interceptor); assertTrue(mappedInterceptor.matches("/foo", pathMatcher)); }
@Test public void orderedInterceptors() throws Exception { HandlerInterceptor i1 = Mockito.mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor1 = new MappedInterceptor(new String[]{"/**"}, i1); HandlerInterceptor i2 = Mockito.mock(HandlerInterceptor.class); HandlerInterceptor i3 = Mockito.mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor3 = new MappedInterceptor(new String[]{"/**"}, i3); HandlerInterceptor i4 = Mockito.mock(HandlerInterceptor.class); this.handlerMapping.setInterceptors(mappedInterceptor1, i2, mappedInterceptor3, i4); this.handlerMapping.setApplicationContext(this.context); HandlerExecutionChain chain = this.handlerMapping.getHandlerExecutionChain(new SimpleHandler(), this.request); Assert.assertThat(chain.getInterceptors(), Matchers.arrayContaining( mappedInterceptor1.getInterceptor(), i2, mappedInterceptor3.getInterceptor(), i4)); }
@Test public void excludePattern() { MappedInterceptor mappedInterceptor = new MappedInterceptor(null, new String[] { "/admin/**" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo", pathMatcher)); assertFalse(mappedInterceptor.matches("/admin/foo", pathMatcher)); }
@Test public void includePattern() { MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo/*" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo/bar", pathMatcher)); assertFalse(mappedInterceptor.matches("/bar/foo", pathMatcher)); }
@Test public void includeAndExcludePatterns() { MappedInterceptor mappedInterceptor = new MappedInterceptor( new String[] { "/**" }, new String[] { "/admin/**" }, this.interceptor); assertTrue(mappedInterceptor.matches("/foo", pathMatcher)); assertFalse(mappedInterceptor.matches("/admin/foo", pathMatcher)); }
@Test public void customPathMatcher() { MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/foo/[0-9]*" }, this.interceptor); mappedInterceptor.setPathMatcher(new TestPathMatcher()); assertTrue(mappedInterceptor.matches("/foo/123", pathMatcher)); assertFalse(mappedInterceptor.matches("/foo/bar", pathMatcher)); }
@Test public void preHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.preHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null); then(interceptor).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any()); }
@Test public void postHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.postHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(ModelAndView.class)); then(interceptor).should().postHandle(any(), any(), any(), any()); }
@Test public void afterCompletion() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.afterCompletion(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(Exception.class)); then(interceptor).should().afterCompletion(any(), any(), any(), any()); }
@Test public void getHandlerMappedInterceptors() throws Exception { String path = "/foo"; HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {}; MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor); TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping(); mapping.registerHandler(new TestController()); mapping.setInterceptors(new Object[] { mappedInterceptor }); mapping.setApplicationContext(new StaticWebApplicationContext()); HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path)); assertNotNull(chain); assertNotNull(chain.getInterceptors()); assertSame(interceptor, chain.getInterceptors()[0]); chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid")); assertNull(chain); }
/** * Add interceptors mapped to a set of path patterns. */ public StandaloneMockMvcBuilder addMappedInterceptors(String[] pathPatterns, HandlerInterceptor... interceptors) { for (HandlerInterceptor interceptor : interceptors) { this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor)); } return this; }
/** * Add interceptors mapped to a set of path patterns. */ public StandaloneMockMvcBuilder addMappedInterceptors(String[] pathPatterns, HandlerInterceptor... interceptors) { for (HandlerInterceptor interceptor : interceptors) { this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor)); } return this; }
/** * Add interceptors mapped to a set of path patterns. */ public StandaloneMockMvcBuilder addMappedInterceptors( @Nullable String[] pathPatterns, HandlerInterceptor... interceptors) { for (HandlerInterceptor interceptor : interceptors) { this.mappedInterceptors.add(new MappedInterceptor(pathPatterns, interceptor)); } return this; }
/** * Build the underlying interceptor. If URL patterns are provided, the returned * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. */ protected Object getInterceptor() { if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { return this.interceptor; } String[] include = StringUtils.toStringArray(this.includePatterns); String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { mappedInterceptor.setPathMatcher(this.pathMatcher); } return mappedInterceptor; }