Search in sources :

Example 21 with FilterConfig

use of javax.servlet.FilterConfig in project hbase by apache.

the class TestStaticUserWebFilter method testFilter.

@Test
public void testFilter() throws Exception {
    FilterConfig config = mockConfig("myuser");
    StaticUserFilter suf = new StaticUserFilter();
    suf.init(config);
    ArgumentCaptor<HttpServletRequestWrapper> wrapperArg = ArgumentCaptor.forClass(HttpServletRequestWrapper.class);
    FilterChain chain = mock(FilterChain.class);
    suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), chain);
    Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject());
    HttpServletRequestWrapper wrapper = wrapperArg.getValue();
    assertEquals("myuser", wrapper.getUserPrincipal().getName());
    assertEquals("myuser", wrapper.getRemoteUser());
    suf.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) FilterChain(javax.servlet.FilterChain) StaticUserFilter(org.apache.hadoop.hbase.http.lib.StaticUserWebFilter.StaticUserFilter) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 22 with FilterConfig

use of javax.servlet.FilterConfig in project jetty.project by eclipse.

the class ProxyServletTest method testResponseHeadersAreNotRemoved.

@Test
public void testResponseHeadersAreNotRemoved() throws Exception {
    startServer(new EmptyHttpServlet());
    startProxy();
    proxyContext.stop();
    final String headerName = "X-Test";
    final String headerValue = "test-value";
    proxyContext.addFilter(new FilterHolder(new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            ((HttpServletResponse) response).addHeader(headerName, headerValue);
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }
    }), "/*", EnumSet.of(DispatcherType.REQUEST));
    proxyContext.start();
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(headerValue, response.getHeaders().get(headerName));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 23 with FilterConfig

use of javax.servlet.FilterConfig in project sonarqube by SonarSource.

the class MasterServletFilterTest method should_init_and_destroy_filters.

@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, singletonList(filter));
    assertThat(master.getFilters()).containsOnly(filter);
    verify(filter).init(config);
    master.destroy();
    verify(filter).destroy();
}
Also used : ServletFilter(org.sonar.api.web.ServletFilter) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 24 with FilterConfig

use of javax.servlet.FilterConfig in project sonarqube by SonarSource.

the class MasterServletFilterTest method should_propagate_initialization_failure.

@Test
public void should_propagate_initialization_failure() throws Exception {
    expectedException.expect(IllegalStateException.class);
    expectedException.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, singletonList(filter));
}
Also used : ServletFilter(org.sonar.api.web.ServletFilter) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 25 with FilterConfig

use of javax.servlet.FilterConfig in project sonarqube by SonarSource.

the class MasterServletFilterTest method filters_should_be_optional.

@Test
public void filters_should_be_optional() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    MasterServletFilter filters = new MasterServletFilter();
    filters.init(config, Collections.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);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Aggregations

FilterConfig (javax.servlet.FilterConfig)117 Test (org.junit.Test)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)63 FilterChain (javax.servlet.FilterChain)62 HttpServletResponse (javax.servlet.http.HttpServletResponse)50 ServletContext (javax.servlet.ServletContext)28 HashMap (java.util.HashMap)21 ServletException (javax.servlet.ServletException)20 Vector (java.util.Vector)17 Properties (java.util.Properties)15 Filter (javax.servlet.Filter)13 ServletResponse (javax.servlet.ServletResponse)13 ServletRequest (javax.servlet.ServletRequest)11 SignerSecretProvider (org.apache.hadoop.security.authentication.util.SignerSecretProvider)10 IOException (java.io.IOException)9 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