Search in sources :

Example 41 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class IndexDocTests method expireRepositoryDemo.

// end::repository-demo[]
@Test
public void expireRepositoryDemo() {
    ExpiringRepositoryDemo<MapSession> demo = new ExpiringRepositoryDemo<>();
    demo.repository = new MapSessionRepository(new ConcurrentHashMap<>());
    demo.demo();
}
Also used : MapSessionRepository(org.springframework.session.MapSessionRepository) MapSession(org.springframework.session.MapSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 42 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class SessionRepositoryFilterTests method doFilterAdapterGetRequestedSessionId.

// --- HttpSessionIdResolver
@Test
public void doFilterAdapterGetRequestedSessionId() throws Exception {
    SessionRepository<MapSession> sessionRepository = spy(new MapSessionRepository(new ConcurrentHashMap<>()));
    this.filter = new SessionRepositoryFilter<>(sessionRepository);
    this.filter.setHttpSessionIdResolver(this.strategy);
    final String expectedId = "HttpSessionIdResolver-requested-id";
    given(this.strategy.resolveSessionIds(any(HttpServletRequest.class))).willReturn(Collections.singletonList(expectedId));
    given(sessionRepository.findById(anyString())).willReturn(new MapSession(expectedId));
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse) throws IOException {
            String actualId = wrappedRequest.getRequestedSessionId();
            assertThat(actualId).isEqualTo(expectedId);
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MapSessionRepository(org.springframework.session.MapSessionRepository) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MapSession(org.springframework.session.MapSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 43 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class SessionRepositoryFilterTests method doFilterRequestSessionNoRequestSessionNoSessionRepositoryInteractions.

@Test
@SuppressWarnings("unchecked")
public void doFilterRequestSessionNoRequestSessionNoSessionRepositoryInteractions() throws Exception {
    SessionRepository<MapSession> sessionRepository = spy(new MapSessionRepository(new ConcurrentHashMap<>()));
    this.filter = new SessionRepositoryFilter<>(sessionRepository);
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse) throws IOException {
            wrappedRequest.getSession().getId();
        }
    });
    reset(sessionRepository);
    setupRequest();
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse) throws IOException {
        }
    });
    verifyZeroInteractions(sessionRepository);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MapSessionRepository(org.springframework.session.MapSessionRepository) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MapSession(org.springframework.session.MapSession) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 44 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class SessionRepositoryFilterTests method doFilterSetsCookieIfChanged.

@Test
public void doFilterSetsCookieIfChanged() throws Exception {
    this.sessionRepository = new MapSessionRepository(new ConcurrentHashMap<>()) {

        @Override
        public MapSession findById(String id) {
            return createSession();
        }
    };
    this.filter = new SessionRepositoryFilter<>(this.sessionRepository);
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest) {
            wrappedRequest.getSession();
        }
    });
    assertThat(this.response.getCookie("SESSION")).isNotNull();
    nextRequest();
    this.response.reset();
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest) {
            assertThat(wrappedRequest.getSession().isNew()).isFalse();
        }
    });
    assertThat(this.response.getCookie("SESSION")).isNotNull();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MapSessionRepository(org.springframework.session.MapSessionRepository) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 45 with MapSession

use of org.springframework.session.MapSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepository method getSession.

/**
 * Gets the session.
 * @param id the session id
 * @param allowExpired if true, will also include expired sessions that have not been
 * deleted. If false, will ensure expired sessions are not returned.
 * @return the Redis session
 */
private RedisSession getSession(String id, boolean allowExpired) {
    Map<Object, Object> entries = getSessionBoundHashOperations(id).entries();
    if (entries.isEmpty()) {
        return null;
    }
    MapSession loaded = loadSession(id, entries);
    if (!allowExpired && loaded.isExpired()) {
        return null;
    }
    RedisSession result = new RedisSession(loaded);
    result.originalLastAccessTime = loaded.getLastAccessedTime();
    return result;
}
Also used : MapSession(org.springframework.session.MapSession)

Aggregations

MapSession (org.springframework.session.MapSession)47 Test (org.junit.Test)37 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)7 HashMap (java.util.HashMap)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 MapSessionRepository (org.springframework.session.MapSessionRepository)6 IOException (java.io.IOException)5 Before (org.junit.Before)5 Session (org.springframework.session.Session)5 HazelcastSession (org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession)5 MapListener (com.hazelcast.map.listener.MapListener)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Map (java.util.Map)3 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)3 PreparedStatementSetter (org.springframework.jdbc.core.PreparedStatementSetter)3 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2