Search in sources :

Example 96 with FilterConfig

use of javax.servlet.FilterConfig in project OpenAM by OpenRock.

the class CORSFilterTest method shouldConfigureWithBothMethodsAndOriginsNoErrors.

@Test
public void shouldConfigureWithBothMethodsAndOriginsNoErrors() {
    //given
    FilterConfig config = mock(FilterConfig.class);
    given(config.getInitParameter(CORSConstants.ORIGINS_KEY)).willReturn("www.google.com");
    given(config.getInitParameter(CORSConstants.METHODS_KEY)).willReturn("GET,POST");
    boolean success = true;
    //when
    try {
        testFilter.init(config);
    } catch (ServletException e) {
        success = false;
    }
    //then
    assertTrue(success);
}
Also used : ServletException(javax.servlet.ServletException) FilterConfig(javax.servlet.FilterConfig) Test(org.testng.annotations.Test)

Example 97 with FilterConfig

use of javax.servlet.FilterConfig in project OpenAM by OpenRock.

the class CORSFilterTest method shoudlThrowExceptionWhenNoMethodsInConfig.

@Test(expectedExceptions = ServletException.class)
public void shoudlThrowExceptionWhenNoMethodsInConfig() throws ServletException {
    //given
    FilterConfig config = mock(FilterConfig.class);
    given(config.getInitParameter(CORSConstants.ORIGINS_KEY)).willReturn("www.google.com");
    //when
    testFilter.init(config);
//then
}
Also used : FilterConfig(javax.servlet.FilterConfig) Test(org.testng.annotations.Test)

Example 98 with FilterConfig

use of javax.servlet.FilterConfig in project roboguice by roboguice.

the class ContinuingRequestIntegrationTest method testRequestContinuesInOtherThread.

public final void testRequestContinuesInOtherThread() throws ServletException, IOException, InterruptedException {
    executor = Executors.newSingleThreadExecutor();
    injector = Guice.createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            serve("/*").with(ContinuingServlet.class);
            bind(ExecutorService.class).toInstance(executor);
        }
    });
    FilterConfig filterConfig = createMock(FilterConfig.class);
    expect(filterConfig.getServletContext()).andReturn(createMock(ServletContext.class));
    GuiceFilter guiceFilter = injector.getInstance(GuiceFilter.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    expect(request.getRequestURI()).andReturn("/");
    expect(request.getContextPath()).andReturn("").anyTimes();
    expect(request.getMethod()).andReturn("GET");
    expect(request.getCookies()).andReturn(new Cookie[0]);
    FilterChain filterChain = createMock(FilterChain.class);
    expect(request.getParameter(PARAM_NAME)).andReturn(PARAM_VALUE);
    replay(request, filterConfig, filterChain);
    guiceFilter.init(filterConfig);
    guiceFilter.doFilter(request, null, filterChain);
    // join.
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertEquals(PARAM_VALUE, injector.getInstance(OffRequestCallable.class).value);
    verify(request, filterConfig, filterChain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) AbstractExecutorService(java.util.concurrent.AbstractExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig)

Example 99 with FilterConfig

use of javax.servlet.FilterConfig in project roboguice by roboguice.

the class FilterDefinition method init.

public void init(final ServletContext servletContext, Injector injector, Set<Filter> initializedSoFar) throws ServletException {
    // This absolutely must be a singleton, and so is only initialized once.
    if (!Scopes.isSingleton(injector.getBinding(filterKey))) {
        throw new ServletException("Filters must be bound as singletons. " + filterKey + " was not bound in singleton scope.");
    }
    Filter filter = injector.getInstance(filterKey);
    this.filter.set(filter);
    // in the filter chain.
    if (initializedSoFar.contains(filter)) {
        return;
    }
    //initialize our filter with the configured context params and servlet context
    filter.init(new FilterConfig() {

        public String getFilterName() {
            return filterKey.toString();
        }

        public ServletContext getServletContext() {
            return servletContext;
        }

        public String getInitParameter(String s) {
            return initParams.get(s);
        }

        public Enumeration getInitParameterNames() {
            return Iterators.asEnumeration(initParams.keySet().iterator());
        }
    });
    initializedSoFar.add(filter);
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) Filter(javax.servlet.Filter) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig)

Example 100 with FilterConfig

use of javax.servlet.FilterConfig in project roboguice by roboguice.

the class FilterPipelineTest method testDispatchThruGuiceFilter.

public final void testDispatchThruGuiceFilter() throws ServletException, IOException {
    //create mocks
    FilterConfig filterConfig = createMock(FilterConfig.class);
    ServletContext servletContext = createMock(ServletContext.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    FilterChain proceedingFilterChain = createMock(FilterChain.class);
    //begin mock script ***
    expect(filterConfig.getServletContext()).andReturn(servletContext).once();
    expect(request.getRequestURI()).andReturn("/public/login.jsp").anyTimes();
    expect(request.getContextPath()).andReturn("").anyTimes();
    //at the end, proceed down webapp's normal filter chain
    proceedingFilterChain.doFilter(isA(HttpServletRequest.class), (ServletResponse) isNull());
    expectLastCall().once();
    //run mock script ***
    replay(filterConfig, servletContext, request, proceedingFilterChain);
    final GuiceFilter webFilter = new GuiceFilter();
    webFilter.init(filterConfig);
    webFilter.doFilter(request, null, proceedingFilterChain);
    webFilter.destroy();
    //assert expectations
    verify(filterConfig, servletContext, request, proceedingFilterChain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig)

Aggregations

FilterConfig (javax.servlet.FilterConfig)118 Test (org.junit.Test)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)64 FilterChain (javax.servlet.FilterChain)63 HttpServletResponse (javax.servlet.http.HttpServletResponse)50 ServletContext (javax.servlet.ServletContext)28 HashMap (java.util.HashMap)21 ServletException (javax.servlet.ServletException)21 Vector (java.util.Vector)17 Properties (java.util.Properties)15 Filter (javax.servlet.Filter)14 ServletResponse (javax.servlet.ServletResponse)14 ServletRequest (javax.servlet.ServletRequest)12 IOException (java.io.IOException)10 SignerSecretProvider (org.apache.hadoop.security.authentication.util.SignerSecretProvider)10 HttpCookie (java.net.HttpCookie)9 Cookie (javax.servlet.http.Cookie)9 Signer (org.apache.hadoop.security.authentication.util.Signer)9 Enumeration (java.util.Enumeration)8 CrossOriginFilter (org.apache.hadoop.security.http.CrossOriginFilter)8