@Test public void should_propagate_initialization_failure() throws Exception { thrown.expect(IllegalStateException.class); thrown.expectMessage("foo"); ServletFilter filter = mock(ServletFilter.class); doThrow(new IllegalStateException("foo")).when(filter).init(any(FilterConfig.class)); FilterConfig config = mock(FilterConfig.class); MasterServletFilter filters = new MasterServletFilter(); filters.init(config, Arrays.asList(filter)); }
@Test public void filters_should_be_optional() throws Exception { FilterConfig config = mock(FilterConfig.class); MasterServletFilter filters = new MasterServletFilter(); filters.init(config, Collections.<ServletFilter>emptyList()); ServletRequest request = mock(HttpServletRequest.class); ServletResponse response = mock(HttpServletResponse.class); FilterChain chain = mock(FilterChain.class); filters.doFilter(request, response, chain); verify(chain).doFilter(request, response); }
@Test public void should_init_and_destroy_filters() throws Exception { ServletFilter filter = mock(ServletFilter.class); FilterConfig config = mock(FilterConfig.class); MasterServletFilter master = new MasterServletFilter(); master.init(config, Arrays.asList(filter)); assertThat(master.getFilters()).containsOnly(filter); verify(filter).init(config); master.destroy(); verify(filter).destroy(); }
@Test public void should_keep_filter_ordering() throws Exception { TrueFilter filter1 = new TrueFilter(); TrueFilter filter2 = new TrueFilter(); MasterServletFilter filters = new MasterServletFilter(); filters.init(mock(FilterConfig.class), Arrays.<ServletFilter>asList(filter1, filter2)); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getRequestURI()).thenReturn("/foo/bar"); when(request.getContextPath()).thenReturn(""); ServletResponse response = mock(HttpServletResponse.class); FilterChain chain = mock(FilterChain.class); filters.doFilter(request, response, chain); assertThat(filter1.count).isEqualTo(1); assertThat(filter2.count).isEqualTo(2); }