use of org.springframework.mock.web.MockFilterConfig in project druid by alibaba.
the class DruidStatServiceTest method test_statService_getWebURIList.
public void test_statService_getWebURIList() throws Exception {
String uri = "/";
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
// first request test
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
String result = DruidStatService.getInstance().service("/weburi.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> webURIList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(webURIList.size(), equalTo(1));
Map<String, Object> webURI = webURIList.get(0);
assertThat((String) webURI.get("URI"), equalTo(uri));
assertThat((Integer) webURI.get("RequestCount"), equalTo(1));
// second request test
MockHttpServletRequest request2 = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response2 = new MockHttpServletResponse();
MockFilterChain chain2 = new MockFilterChain();
filter.doFilter(request2, response2, chain2);
result = DruidStatService.getInstance().service("/weburi.json");
resultMap = (Map<String, Object>) JSONUtils.parse(result);
webURIList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(webURIList.size(), equalTo(1));
webURI = webURIList.get(0);
assertThat((String) webURI.get("URI"), equalTo(uri));
assertThat((Integer) webURI.get("RequestCount"), equalTo(2));
filter.destroy();
}
use of org.springframework.mock.web.MockFilterConfig in project spring-security by spring-projects.
the class AbstractAuthenticationProcessingFilterTests method testFailedAuthenticationInvokesFailureHandler.
@Test
public void testFailedAuthenticationInvokesFailureHandler() throws Exception {
// Setup our HTTP request
MockHttpServletRequest request = createMockAuthenticationRequest();
// Setup our filter configuration
MockFilterConfig config = new MockFilterConfig(null, null);
// Setup our expectation that the filter chain will not be invoked, as we redirect
// to authenticationFailureUrl
MockFilterChain chain = new MockFilterChain(false);
MockHttpServletResponse response = new MockHttpServletResponse();
// Setup our test object, to deny access
MockAuthenticationFilter filter = new MockAuthenticationFilter(false);
AuthenticationFailureHandler failureHandler = mock(AuthenticationFailureHandler.class);
filter.setAuthenticationFailureHandler(failureHandler);
// Test
filter.doFilter(request, response, chain);
verify(failureHandler).onAuthenticationFailure(any(HttpServletRequest.class), any(HttpServletResponse.class), any(AuthenticationException.class));
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
use of org.springframework.mock.web.MockFilterConfig in project spring-security by spring-projects.
the class AbstractAuthenticationProcessingFilterTests method testSuccessfulAuthenticationInvokesSuccessHandlerAndSetsContext.
@Test
public void testSuccessfulAuthenticationInvokesSuccessHandlerAndSetsContext() throws Exception {
// Setup our HTTP request
MockHttpServletRequest request = createMockAuthenticationRequest();
// Setup our filter configuration
MockFilterConfig config = new MockFilterConfig(null, null);
// Setup our expectation that the filter chain will be invoked, as we want to go
// to the location requested in the session
MockFilterChain chain = new MockFilterChain(true);
MockHttpServletResponse response = new MockHttpServletResponse();
// Setup our test object, to grant access
MockAuthenticationFilter filter = new MockAuthenticationFilter(true);
filter.setFilterProcessesUrl("/j_mock_post");
AuthenticationSuccessHandler successHandler = mock(AuthenticationSuccessHandler.class);
filter.setAuthenticationSuccessHandler(successHandler);
// Test
filter.doFilter(request, response, chain);
verify(successHandler).onAuthenticationSuccess(any(HttpServletRequest.class), any(HttpServletResponse.class), any(Authentication.class));
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
}
use of org.springframework.mock.web.MockFilterConfig in project spring-security by spring-projects.
the class AbstractAuthenticationProcessingFilterTests method testNormalOperationWithDefaultFilterProcessesUrl.
@Test
public void testNormalOperationWithDefaultFilterProcessesUrl() throws Exception {
// Setup our HTTP request
MockHttpServletRequest request = createMockAuthenticationRequest();
HttpSession sessionPreAuth = request.getSession();
// Setup our filter configuration
MockFilterConfig config = new MockFilterConfig(null, null);
// Setup our expectation that the filter chain will not be invoked, as we redirect
// to defaultTargetUrl
MockFilterChain chain = new MockFilterChain(false);
MockHttpServletResponse response = new MockHttpServletResponse();
// Setup our test object, to grant access
MockAuthenticationFilter filter = new MockAuthenticationFilter(true);
filter.setFilterProcessesUrl("/j_mock_post");
filter.setSessionAuthenticationStrategy(mock(SessionAuthenticationStrategy.class));
filter.setAuthenticationSuccessHandler(successHandler);
filter.setAuthenticationFailureHandler(failureHandler);
filter.setAuthenticationManager(mock(AuthenticationManager.class));
filter.afterPropertiesSet();
// Test
filter.doFilter(request, response, chain);
assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp");
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()).isEqualTo("test");
// Should still have the same session
assertThat(request.getSession()).isEqualTo(sessionPreAuth);
}
use of org.springframework.mock.web.MockFilterConfig in project spring-security by spring-projects.
the class AbstractAuthenticationProcessingFilterTests method testIgnoresAnyServletPathOtherThanFilterProcessesUrl.
@Test
public void testIgnoresAnyServletPathOtherThanFilterProcessesUrl() throws Exception {
// Setup our HTTP request
MockHttpServletRequest request = createMockAuthenticationRequest();
request.setServletPath("/some.file.html");
request.setRequestURI("/mycontext/some.file.html");
// Setup our filter configuration
MockFilterConfig config = new MockFilterConfig(null, null);
// Setup our expectation that the filter chain will be invoked, as our request is
// for a page the filter isn't monitoring
MockFilterChain chain = new MockFilterChain(true);
MockHttpServletResponse response = new MockHttpServletResponse();
// Setup our test object, to deny access
MockAuthenticationFilter filter = new MockAuthenticationFilter(false);
// Test
filter.doFilter(request, response, chain);
}
Aggregations