use of javax.servlet.FilterConfig in project hadoop by apache.
the class TestRestCsrfPreventionFilter method testMissingHeaderMultipleIgnoreMethodsConfigGoodRequest.
@Test
public void testMissingHeaderMultipleIgnoreMethodsConfigGoodRequest() throws ServletException, IOException {
// Setup the configuration settings of the server
FilterConfig filterConfig = Mockito.mock(FilterConfig.class);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_HEADER_PARAM)).thenReturn(null);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_METHODS_TO_IGNORE_PARAM)).thenReturn("GET,OPTIONS");
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_USER_AGENT)).thenReturn(BROWSER_AGENT);
// CSRF has not been sent
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_DEFAULT)).thenReturn(null);
Mockito.when(mockReq.getMethod()).thenReturn("OPTIONS");
// Objects to verify interactions based on request
HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
FilterChain mockChain = Mockito.mock(FilterChain.class);
// Object under test
RestCsrfPreventionFilter filter = new RestCsrfPreventionFilter();
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}
use of javax.servlet.FilterConfig in project hadoop by apache.
the class TestRestCsrfPreventionFilter method testMissingHeaderNoMethodsToIgnoreConfigBadRequest.
@Test
public void testMissingHeaderNoMethodsToIgnoreConfigBadRequest() throws ServletException, IOException {
// Setup the configuration settings of the server
FilterConfig filterConfig = Mockito.mock(FilterConfig.class);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_HEADER_PARAM)).thenReturn(null);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_METHODS_TO_IGNORE_PARAM)).thenReturn("");
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_USER_AGENT)).thenReturn(BROWSER_AGENT);
// CSRF has not been sent
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_DEFAULT)).thenReturn(null);
Mockito.when(mockReq.getMethod()).thenReturn("GET");
// Objects to verify interactions based on request
HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
FilterChain mockChain = Mockito.mock(FilterChain.class);
// Object under test
RestCsrfPreventionFilter filter = new RestCsrfPreventionFilter();
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);
Mockito.verifyZeroInteractions(mockChain);
}
use of javax.servlet.FilterConfig in project hadoop by apache.
the class TestRestCsrfPreventionFilter method testNoHeaderCustomAgentConfigBadRequest.
@Test
public void testNoHeaderCustomAgentConfigBadRequest() throws ServletException, IOException {
// Setup the configuration settings of the server
FilterConfig filterConfig = Mockito.mock(FilterConfig.class);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_HEADER_PARAM)).thenReturn(null);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.CUSTOM_METHODS_TO_IGNORE_PARAM)).thenReturn(null);
Mockito.when(filterConfig.getInitParameter(RestCsrfPreventionFilter.BROWSER_USER_AGENT_PARAM)).thenReturn("^Mozilla.*,^Opera.*,curl");
// CSRF has not been sent
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_DEFAULT)).thenReturn(null);
Mockito.when(mockReq.getHeader(RestCsrfPreventionFilter.HEADER_USER_AGENT)).thenReturn("curl");
// Objects to verify interactions based on request
HttpServletResponse mockRes = Mockito.mock(HttpServletResponse.class);
FilterChain mockChain = Mockito.mock(FilterChain.class);
// Object under test
RestCsrfPreventionFilter filter = new RestCsrfPreventionFilter();
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);
verify(mockRes, atLeastOnce()).sendError(HttpServletResponse.SC_BAD_REQUEST, EXPECTED_MESSAGE);
Mockito.verifyZeroInteractions(mockChain);
}
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();
}
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));
}
Aggregations