use of javax.servlet.Filter in project sling by apache.
the class LogTracerTest method noTurboFilterRegisteredUnlessTracingRequested.
@Test
public void noTurboFilterRegisteredUnlessTracingRequested() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
activateTracer();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
assertNull(context.getService(TurboFilter.class));
}
};
Filter filter = getFilter(false);
filter.doFilter(request, response, chain);
}
use of javax.servlet.Filter in project ddf by codice.
the class DelegateServletFilterTest method createMockContext.
private BundleContext createMockContext(boolean includeFilters) throws InvalidSyntaxException, IOException, ServletException {
BundleContext context = mock(BundleContext.class);
List<Filter> mockFilters = mockFilters(includeFilters);
List<ServiceReference<Filter>> referenceList = new ArrayList<ServiceReference<Filter>>();
for (Filter curFilter : mockFilters) {
ServiceReference<Filter> mockRef = mock(ServiceReference.class);
when(context.getService(mockRef)).thenReturn(curFilter);
referenceList.add(mockRef);
}
when(context.getServiceReferences(Filter.class, null)).thenReturn(referenceList);
return context;
}
use of javax.servlet.Filter in project ddf by codice.
the class ProxyFilterChainTest method testDoFilter.
/**
* Tests that all of the filters are properly called.
*
* @throws ServletException
* @throws IOException
*/
@Test
public void testDoFilter() throws IOException, ServletException {
FilterChain initialChain = mock(FilterChain.class);
ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
Filter filter1 = createMockFilter("filter1");
Filter filter2 = createMockFilter("filter2");
Filter filter3 = createMockFilter("filter3");
ServletRequest request = mock(ServletRequest.class);
ServletResponse response = mock(ServletResponse.class);
proxyChain.addFilter(filter1);
proxyChain.addFilters(Arrays.asList(filter2, filter3));
proxyChain.doFilter(request, response);
// Verify that all of the filters were called once.
verify(filter1).doFilter(request, response, proxyChain);
verify(filter2).doFilter(request, response, proxyChain);
verify(filter3).doFilter(request, response, proxyChain);
// the initial chain should have also been called once (at the end).
verify(initialChain).doFilter(request, response);
}
use of javax.servlet.Filter in project ddf by codice.
the class ProxyFilterChainTest method testAddFilterAfterDo.
/**
* Tests that an exception is thrown if a new filter is attempted to be
* added after the filter has been run.
*
* @throws IOException
* @throws ServletException
*/
@Test(expected = IllegalStateException.class)
public void testAddFilterAfterDo() throws IOException, ServletException {
FilterChain initialChain = mock(FilterChain.class);
ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
Filter filter1 = mock(Filter.class);
proxyChain.doFilter(mock(ServletRequest.class), mock(ServletResponse.class));
proxyChain.addFilter(filter1);
}
use of javax.servlet.Filter in project ddf by codice.
the class ProxyFilterChainTest method createMockFilter.
private Filter createMockFilter(final String name) throws IOException, ServletException {
Filter mockFilter = mock(Filter.class);
Mockito.when(mockFilter.toString()).thenReturn(name);
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
LOGGER.debug("{} was called.", name);
((FilterChain) args[2]).doFilter(((ServletRequest) args[0]), ((ServletResponse) args[1]));
return null;
}
}).when(mockFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class));
return mockFilter;
}
Aggregations