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)));
}
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));
}
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);
}
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)));
}
Aggregations