Refactor DefaultWebSession
Use copy constructor to refresh a session with lastAccessTime and a save function referencing the current exchange. As a result both fields are now final and ConfigurableWebSession is no longer needed.
This commit is contained in:
@@ -25,6 +25,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -104,20 +105,22 @@ public class DefaultWebSessionManagerTests {
|
||||
|
||||
@Test
|
||||
public void existingSession() throws Exception {
|
||||
DefaultWebSession existing = new DefaultWebSession("1", Clock.systemDefaultZone());
|
||||
DefaultWebSession existing = new DefaultWebSession("1", Clock.systemDefaultZone(), s -> Mono.empty());
|
||||
this.manager.getSessionStore().storeSession(existing);
|
||||
this.idResolver.setIdsToResolve(Collections.singletonList("1"));
|
||||
|
||||
WebSession actual = this.manager.getSession(this.exchange).block();
|
||||
assertSame(existing, actual);
|
||||
assertNotNull(actual);
|
||||
assertEquals(existing.getId(), actual.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existingSessionIsExpired() throws Exception {
|
||||
Clock clock = Clock.systemDefaultZone();
|
||||
DefaultWebSession existing = new DefaultWebSession("1", clock);
|
||||
DefaultWebSession existing = new DefaultWebSession("1", clock, s -> Mono.empty());
|
||||
existing.start();
|
||||
existing.setLastAccessTime(Instant.now(clock).minus(Duration.ofMinutes(31)));
|
||||
Instant lastAccessTime = Instant.now(clock).minus(Duration.ofMinutes(31));
|
||||
existing = new DefaultWebSession(existing, lastAccessTime, s -> Mono.empty());
|
||||
this.manager.getSessionStore().storeSession(existing);
|
||||
this.idResolver.setIdsToResolve(Collections.singletonList("1"));
|
||||
|
||||
@@ -127,12 +130,13 @@ public class DefaultWebSessionManagerTests {
|
||||
|
||||
@Test
|
||||
public void multipleSessions() throws Exception {
|
||||
DefaultWebSession existing = new DefaultWebSession("3", Clock.systemDefaultZone());
|
||||
DefaultWebSession existing = new DefaultWebSession("3", Clock.systemDefaultZone(), s -> Mono.empty());
|
||||
this.manager.getSessionStore().storeSession(existing);
|
||||
this.idResolver.setIdsToResolve(Arrays.asList("1", "2", "3"));
|
||||
|
||||
WebSession actual = this.manager.getSession(this.exchange).block();
|
||||
assertSame(existing, actual);
|
||||
assertNotNull(actual);
|
||||
assertEquals(existing.getId(), actual.getId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -37,7 +38,6 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -116,9 +116,12 @@ public class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTe
|
||||
assertEquals(2, this.handler.getCount());
|
||||
|
||||
// Update lastAccessTime of the created session to -31 min
|
||||
WebSession session = this.sessionManager.getSessionStore().retrieveSession(id).block();
|
||||
((DefaultWebSession) session).setLastAccessTime(
|
||||
Clock.offset(this.sessionManager.getClock(), Duration.ofMinutes(-31)).instant());
|
||||
WebSessionStore store = this.sessionManager.getSessionStore();
|
||||
DefaultWebSession session = (DefaultWebSession) store.retrieveSession(id).block();
|
||||
assertNotNull(session);
|
||||
Instant lastAccessTime = Clock.offset(this.sessionManager.getClock(), Duration.ofMinutes(-31)).instant();
|
||||
session = new DefaultWebSession(session, lastAccessTime);
|
||||
store.storeSession(session);
|
||||
|
||||
// Third request: expired session, new session created
|
||||
request = RequestEntity.get(createUri("/")).header("Cookie", "SESSION=" + id).build();
|
||||
|
||||
Reference in New Issue
Block a user