Search in sources :

Example 11 with MapSession

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

the class SessionRepositoryFilterTests method doFilterCreateSetsLastAccessedTime.

@Test
public void doFilterCreateSetsLastAccessedTime() throws Exception {
    MapSession session = new MapSession();
    session.setLastAccessedTime(Instant.EPOCH);
    this.sessionRepository = spy(this.sessionRepository);
    given(this.sessionRepository.createSession()).willReturn(session);
    this.filter = new SessionRepositoryFilter<>(this.sessionRepository);
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest) {
            HttpSession session = wrappedRequest.getSession();
            long now = System.currentTimeMillis();
            long fiveSecondsAgo = now - TimeUnit.SECONDS.toMillis(5);
            assertThat(session.getLastAccessedTime()).isLessThanOrEqualTo(now);
            assertThat(session.getLastAccessedTime()).isGreaterThanOrEqualTo(fiveSecondsAgo);
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 12 with MapSession

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

the class SessionRepositoryFilterTests method doFilterLazySessionUpdates.

@Test
public void doFilterLazySessionUpdates() throws Exception {
    MapSession session = this.sessionRepository.createSession();
    this.sessionRepository.save(session);
    SessionRepository<MapSession> sessionRepository = spy(this.sessionRepository);
    setSessionCookie(session.getId());
    this.filter = new SessionRepositoryFilter<>(sessionRepository);
    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) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MapSession(org.springframework.session.MapSession) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with MapSession

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

the class SessionRepositoryFilterTests method doFilterLazySessionCreation.

@Test
public void doFilterLazySessionCreation() 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 {
        }
    });
    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 14 with MapSession

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

the class WebSocketRegistryListenerTests method setup.

@Before
public void setup() {
    String sessionId = "session-id";
    MapSession session = new MapSession(sessionId);
    this.attributes = new HashMap<>();
    SessionRepositoryMessageInterceptor.setSessionId(this.attributes, sessionId);
    given(this.wsSession.getAttributes()).willReturn(this.attributes);
    given(this.wsSession.getPrincipal()).willReturn(this.principal);
    given(this.wsSession.getId()).willReturn("wsSession-id");
    given(this.wsSession2.getAttributes()).willReturn(this.attributes);
    given(this.wsSession2.getPrincipal()).willReturn(this.principal);
    given(this.wsSession2.getId()).willReturn("wsSession-id2");
    Map<String, Object> headers = new HashMap<>();
    headers.put(SimpMessageHeaderAccessor.SESSION_ATTRIBUTES, this.attributes);
    given(this.message.getHeaders()).willReturn(new MessageHeaders(headers));
    this.listener = new WebSocketRegistryListener();
    this.connect = new SessionConnectEvent(this.listener, this.wsSession);
    this.connect2 = new SessionConnectEvent(this.listener, this.wsSession2);
    this.disconnect = new SessionDisconnectEvent(this.listener, this.message, this.wsSession.getId(), CloseStatus.NORMAL);
    this.deleted = new SessionDeletedEvent(this.listener, session);
    this.expired = new SessionExpiredEvent(this.listener, session);
}
Also used : HashMap(java.util.HashMap) SessionDeletedEvent(org.springframework.session.events.SessionDeletedEvent) SessionDisconnectEvent(org.springframework.web.socket.messaging.SessionDisconnectEvent) SessionExpiredEvent(org.springframework.session.events.SessionExpiredEvent) MapSession(org.springframework.session.MapSession) MessageHeaders(org.springframework.messaging.MessageHeaders) SessionConnectEvent(org.springframework.session.web.socket.events.SessionConnectEvent) Before(org.junit.Before)

Example 15 with MapSession

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

the class ReactiveRedisOperationsSessionRepositoryTests method saveSessionNothingChanged.

@Test
public void saveSessionNothingChanged() {
    ReactiveRedisOperationsSessionRepository.RedisSession session = this.repository.new RedisSession(new MapSession());
    Mono<Void> result = this.repository.save(session);
    StepVerifier.create(result).expectNextMatches(predicate -> {
        verifyZeroInteractions(this.redisOperations);
        return true;
    });
}
Also used : MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

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