Search in sources :

Example 36 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class SessionManagementFilterTests method newSessionShouldNotBeCreatedIfSessionExistsAndUserIsNotAuthenticated.

@Test
public void newSessionShouldNotBeCreatedIfSessionExistsAndUserIsNotAuthenticated() throws Exception {
    SecurityContextRepository repo = mock(SecurityContextRepository.class);
    SessionManagementFilter filter = new SessionManagementFilter(repo);
    HttpServletRequest request = new MockHttpServletRequest();
    String sessionId = request.getSession().getId();
    filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
    assertThat(request.getSession().getId()).isEqualTo(sessionId);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 37 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class SessionManagementFilterTests method strategyIsInvokedIfUserIsNewlyAuthenticated.

@Test
public void strategyIsInvokedIfUserIsNewlyAuthenticated() throws Exception {
    SecurityContextRepository repo = mock(SecurityContextRepository.class);
    // repo will return false to containsContext()
    SessionAuthenticationStrategy strategy = mock(SessionAuthenticationStrategy.class);
    SessionManagementFilter filter = new SessionManagementFilter(repo, strategy);
    HttpServletRequest request = new MockHttpServletRequest();
    authenticateUser();
    filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
    verify(strategy).onAuthentication(any(Authentication.class), any(HttpServletRequest.class), any(HttpServletResponse.class));
    // Check that it is only applied once to the request
    filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
    verifyNoMoreInteractions(strategy);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 38 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class SessionManagementFilterTests method customAuthenticationTrustResolver.

@Test
public void customAuthenticationTrustResolver() throws Exception {
    AuthenticationTrustResolver trustResolver = mock(AuthenticationTrustResolver.class);
    SecurityContextRepository repo = mock(SecurityContextRepository.class);
    SessionManagementFilter filter = new SessionManagementFilter(repo);
    filter.setTrustResolver(trustResolver);
    HttpServletRequest request = new MockHttpServletRequest();
    authenticateUser();
    filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
    verify(trustResolver).isAnonymous(any(Authentication.class));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) AuthenticationTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 39 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames.

@Test
public void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() throws Exception {
    addWebServerFactoryBean();
    DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean("filterBean");
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    BeanDefinition filterBeanDefinition = beanDefinition(new IllegalStateException("Create FilterBean Failure"));
    filterBeanDefinition.setLazyInit(true);
    this.context.registerBeanDefinition("filterBean", filterBeanDefinition);
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    verify(servletContext, atMost(1)).addFilter(anyString(), this.filterCaptor.capture());
    // Up to this point the filterBean should not have been created, calling
    // the delegate proxy will trigger creation and an exception
    this.thrown.expect(BeanCreationException.class);
    this.thrown.expectMessage("Create FilterBean Failure");
    this.filterCaptor.getValue().init(new MockFilterConfig());
    this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
}
Also used : DelegatingFilterProxyRegistrationBean(org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(javax.servlet.ServletContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 40 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getResetAll.

public void test_statService_getResetAll() throws Exception {
    // data source mock
    String sql = "select 1";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.close();
    stmt.close();
    conn.close();
    String resultSQL = DruidStatService.getInstance().service("/sql.json");
    Map<String, Object> resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    List<Map<String, Object>> sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList.size(), equalTo(1));
    Map<String, Object> sqlStat = sqlList.get(0);
    assertThat((Integer) sqlStat.get("RunningCount"), equalTo(0));
    // http request mock
    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);
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    String sessionId = session.getId();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    filter.doFilter(request, response, chain);
    String resultWebSession = DruidStatService.getInstance().service("/websession.json");
    Map<String, Object> resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    List<Map<String, Object>> contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList.size(), equalTo(1));
    Map<String, Object> contentWebSessionMap = contentWebSessionList.get(0);
    assertThat((String) contentWebSessionMap.get("SESSIONID"), equalTo(sessionId));
    // spring mock
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String resultSpring = DruidStatService.getInstance().service("/spring.json");
    Map<String, Object> resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    List<Map<String, Object>> contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList.size(), equalTo(1));
    Map<String, Object> contentMap = contentSpringList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // reset all test
    String result = DruidStatService.getInstance().service("/reset-all.json");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    assertThat(resultMap.get("content"), is(nullValue()));
    // assert sql
    resultSQL = DruidStatService.getInstance().service("/sql.json");
    resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList, is(nullValue()));
    // assert web session
    resultWebSession = DruidStatService.getInstance().service("/websession.json");
    resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList, is(nullValue()));
    // assert spring
    resultSpring = DruidStatService.getInstance().service("/spring.json");
    resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList, is(nullValue()));
}
Also used : UserService(com.alibaba.druid.stat.spring.UserService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ResultSet(java.sql.ResultSet) MockHttpSession(org.springframework.mock.web.MockHttpSession) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)108 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)106 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)106 Test (org.junit.Test)77 ServletRequest (javax.servlet.ServletRequest)28 ServletResponse (javax.servlet.ServletResponse)28 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 NestedServletException (org.springframework.web.util.NestedServletException)19 Before (org.junit.Before)17 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)14 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)11 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)9 MockServletContext (org.springframework.mock.web.MockServletContext)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8