Search in sources :

Example 1 with FilterConfig

use of jakarta.servlet.FilterConfig in project OpenGrok by OpenGrok.

the class CookieFilterTest method doTest.

@Test
public void doTest() throws IOException, ServletException {
    CookieFilter filter = new CookieFilter();
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = new DummyHttpServletResponse();
    String[] headerValues = new String[] { "foo=bar", "Tessier=Ashpool" };
    for (String value : headerValues) {
        response.addHeader(HttpHeaders.SET_COOKIE, value);
    }
    FilterChain chain = mock(FilterChain.class);
    FilterConfig filterConfig = spy(FilterConfig.class);
    Map<String, String> m = new HashMap<>();
    m.put("Bonnie", "Clyde");
    m.put("Porgy", "Bess");
    m.put("Empty", "");
    doReturn(Collections.enumeration(m.keySet())).when(filterConfig).getInitParameterNames();
    for (String key : m.keySet()) {
        doReturn(m.get(key)).when(filterConfig).getInitParameter(key);
    }
    filter.init(filterConfig);
    filter.doFilter(request, response, chain);
    // Mimic what the filter should be doing.
    Set<String> expectedHeaderValues = new HashSet<>();
    for (String headerValue : headerValues) {
        String suffix = m.keySet().stream().map(s -> m.get(s).isEmpty() ? s : s + "=" + m.get(s)).collect(Collectors.joining("; "));
        expectedHeaderValues.add(headerValue + "; " + suffix);
    }
    assertEquals(expectedHeaderValues, new HashSet<>(response.getHeaders(HttpHeaders.SET_COOKIE)));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) ServletException(jakarta.servlet.ServletException) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) ServletOutputStream(jakarta.servlet.ServletOutputStream) HashSet(java.util.HashSet) FilterConfig(jakarta.servlet.FilterConfig) Locale(java.util.Locale) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.doReturn(org.mockito.Mockito.doReturn) PrintWriter(java.io.PrintWriter) Collection(java.util.Collection) FilterChain(jakarta.servlet.FilterChain) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Cookie(jakarta.servlet.http.Cookie) List(java.util.List) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterConfig(jakarta.servlet.FilterConfig) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with FilterConfig

use of jakarta.servlet.FilterConfig in project OpenGrok by OpenGrok.

the class CookieFilterTest method testNoHeaders.

@Test
public void testNoHeaders() throws IOException, ServletException {
    CookieFilter filter = new CookieFilter();
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = new DummyHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    FilterConfig filterConfig = mock(FilterConfig.class);
    filter.init(filterConfig);
    filter.doFilter(request, response, chain);
    assertNull(response.getHeaders(HttpHeaders.SET_COOKIE));
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterChain(jakarta.servlet.FilterChain) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) FilterConfig(jakarta.servlet.FilterConfig) Test(org.junit.jupiter.api.Test)

Example 3 with FilterConfig

use of jakarta.servlet.FilterConfig in project spring-framework by spring-projects.

the class ConditionalDelegatingFilterProxyTests method init.

@Test
public void init() throws Exception {
    FilterConfig config = new MockFilterConfig();
    filter = new PatternMappingFilterProxy(delegate, "/");
    filter.init(config);
    assertThat(delegate.filterConfig).isEqualTo(config);
}
Also used : FilterConfig(jakarta.servlet.FilterConfig) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 4 with FilterConfig

use of jakarta.servlet.FilterConfig in project tomcat by apache.

the class TestCorsFilter method testInitDefaultFilterConfig.

@Test
public void testInitDefaultFilterConfig() throws IOException, ServletException {
    TesterHttpServletRequest request = new TesterHttpServletRequest();
    request.setHeader(CorsFilter.REQUEST_HEADER_ORIGIN, TesterFilterConfigs.HTTPS_WWW_APACHE_ORG);
    request.setMethod("GET");
    TesterHttpServletResponse response = new TesterHttpServletResponse();
    CorsFilter corsFilter = new CorsFilter();
    corsFilter.init(new FilterConfig() {

        @Override
        public ServletContext getServletContext() {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public String getFilterName() {
            return null;
        }
    });
    corsFilter.doFilter(request, response, filterChain);
    Assert.assertNull(response.getHeader(CorsFilter.RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN));
    Assert.assertTrue(((Boolean) request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_IS_CORS_REQUEST)).booleanValue());
    Assert.assertTrue(request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_ORIGIN).equals(TesterFilterConfigs.HTTPS_WWW_APACHE_ORG));
    Assert.assertTrue(request.getAttribute(CorsFilter.HTTP_REQUEST_ATTRIBUTE_REQUEST_TYPE).equals(CorsFilter.CORSRequestType.SIMPLE.name().toLowerCase(Locale.ENGLISH)));
}
Also used : Enumeration(java.util.Enumeration) ServletContext(jakarta.servlet.ServletContext) FilterConfig(jakarta.servlet.FilterConfig) Test(org.junit.Test)

Aggregations

FilterConfig (jakarta.servlet.FilterConfig)4 Test (org.junit.jupiter.api.Test)3 FilterChain (jakarta.servlet.FilterChain)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 ServletContext (jakarta.servlet.ServletContext)1 ServletException (jakarta.servlet.ServletException)1 ServletOutputStream (jakarta.servlet.ServletOutputStream)1 Cookie (jakarta.servlet.http.Cookie)1 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1