Search in sources :

Example 11 with MockFilterConfig

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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 12 with MockFilterConfig

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AuthenticationException(org.springframework.security.core.AuthenticationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 13 with MockFilterConfig

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 14 with MockFilterConfig

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);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 15 with MockFilterConfig

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockFilterConfig (org.springframework.mock.web.MockFilterConfig)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 Test (org.junit.Test)12 MockFilterChain (org.springframework.mock.web.MockFilterChain)11 MockServletContext (org.springframework.mock.web.MockServletContext)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 Map (java.util.Map)5 List (java.util.List)4 WebAppStat (com.alibaba.druid.support.http.stat.WebAppStat)3 ServletException (javax.servlet.ServletException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockDatabase (org.opennms.core.test.db.MockDatabase)2 DaoTestConfigBean (org.opennms.test.DaoTestConfigBean)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 MockServletConfig (org.springframework.mock.web.MockServletConfig)2 OpenSessionInViewFilter (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)2 UserService (com.alibaba.druid.stat.spring.UserService)1