Search in sources :

Example 1 with FilterChain

use of javax.servlet.FilterChain 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 2 with FilterChain

use of javax.servlet.FilterChain in project zeppelin by apache.

the class CorsFilterTest method InvalidCorsFilterTest.

@Test
@SuppressWarnings("rawtypes")
public void InvalidCorsFilterTest() throws IOException, ServletException {
    CorsFilter filter = new CorsFilter();
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    FilterChain mockedFilterChain = mock(FilterChain.class);
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    when(mockRequest.getHeader("Origin")).thenReturn("http://evillocalhost:8080");
    when(mockRequest.getMethod()).thenReturn("Empty");
    when(mockRequest.getServerName()).thenReturn("evillocalhost");
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            headers[count] = invocationOnMock.getArguments()[1].toString();
            count++;
            return null;
        }
    }).when(mockResponse).addHeader(anyString(), anyString());
    filter.doFilter(mockRequest, mockResponse, mockedFilterChain);
    Assert.assertTrue(headers[0].equals(""));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 3 with FilterChain

use of javax.servlet.FilterChain in project hadoop by apache.

the class TestCrossOriginFilter method testDisallowedOrigin.

@Test
public void testDisallowedOrigin() throws ServletException, IOException {
    // Setup the configuration settings of the server
    Map<String, String> conf = new HashMap<String, String>();
    conf.put(CrossOriginFilter.ALLOWED_ORIGINS, "example.com");
    FilterConfig filterConfig = new FilterConfigTest(conf);
    // Origin is not specified for same origin requests
    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ORIGIN)).thenReturn("example.org");
    // Objects to verify interactions based on request
    HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
    FilterChain mockChain = Mockito.mock(FilterChain.class);
    // Object under test
    CrossOriginFilter filter = new CrossOriginFilter();
    filter.init(filterConfig);
    filter.doFilter(mockReq, mockRes, mockChain);
    Mockito.verifyZeroInteractions(mockRes);
    Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) CrossOriginFilter(org.apache.hadoop.security.http.CrossOriginFilter) Test(org.junit.Test)

Example 4 with FilterChain

use of javax.servlet.FilterChain in project hadoop by apache.

the class TestCrossOriginFilter method testCrossOriginFilter.

@Test
public void testCrossOriginFilter() throws ServletException, IOException {
    // Setup the configuration settings of the server
    Map<String, String> conf = new HashMap<String, String>();
    conf.put(CrossOriginFilter.ALLOWED_ORIGINS, "example.com");
    FilterConfig filterConfig = new FilterConfigTest(conf);
    // Origin is not specified for same origin requests
    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ORIGIN)).thenReturn("example.com");
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD)).thenReturn("GET");
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ACCESS_CONTROL_REQUEST_HEADERS)).thenReturn("X-Requested-With");
    // Objects to verify interactions based on request
    HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
    FilterChain mockChain = Mockito.mock(FilterChain.class);
    // Object under test
    CrossOriginFilter filter = new CrossOriginFilter();
    filter.init(filterConfig);
    filter.doFilter(mockReq, mockRes, mockChain);
    Mockito.verify(mockRes).setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN, "example.com");
    Mockito.verify(mockRes).setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString());
    Mockito.verify(mockRes).setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_METHODS, filter.getAllowedMethodsHeader());
    Mockito.verify(mockRes).setHeader(CrossOriginFilter.ACCESS_CONTROL_ALLOW_HEADERS, filter.getAllowedHeadersHeader());
    Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) CrossOriginFilter(org.apache.hadoop.security.http.CrossOriginFilter) Test(org.junit.Test)

Example 5 with FilterChain

use of javax.servlet.FilterChain in project hadoop by apache.

the class TestCrossOriginFilter method testDisallowedMethod.

@Test
public void testDisallowedMethod() throws ServletException, IOException {
    // Setup the configuration settings of the server
    Map<String, String> conf = new HashMap<String, String>();
    conf.put(CrossOriginFilter.ALLOWED_ORIGINS, "example.com");
    FilterConfig filterConfig = new FilterConfigTest(conf);
    // Origin is not specified for same origin requests
    HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ORIGIN)).thenReturn("example.com");
    Mockito.when(mockReq.getHeader(CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD)).thenReturn("DISALLOWED_METHOD");
    // Objects to verify interactions based on request
    HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
    FilterChain mockChain = Mockito.mock(FilterChain.class);
    // Object under test
    CrossOriginFilter filter = new CrossOriginFilter();
    filter.init(filterConfig);
    filter.doFilter(mockReq, mockRes, mockChain);
    Mockito.verifyZeroInteractions(mockRes);
    Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) CrossOriginFilter(org.apache.hadoop.security.http.CrossOriginFilter) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19