@Override public Mono<Void> save() { return this.delegate.save(); }
@Override public Mono<Void> save() { return this.delegate.save(); }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
private WebSession insertSession() { WebSession session = this.store.createWebSession().block(); assertNotNull(session); session.start(); session.save().block(); return session; }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
@Test public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId() { when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); WebSession session = this.sessionManager.getSession(this.exchange).block(); assertSame(this.createSession, session); String sessionId = this.createSession.getId(); when(this.createSession.isStarted()).thenReturn(true); this.exchange.getResponse().setComplete().block(); verify(this.sessionStore).createWebSession(); verify(this.sessionIdResolver).setSessionId(any(), eq(sessionId)); verify(this.createSession).save(); }
@Test public void getSessionSaveWhenCreatedAndNotStartedThenNotSaved() { when(this.sessionIdResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList()); WebSession session = this.sessionManager.getSession(this.exchange).block(); this.exchange.getResponse().setComplete().block(); assertSame(this.createSession, session); assertFalse(session.isStarted()); assertFalse(session.isExpired()); verify(this.createSession, never()).save(); verify(this.sessionIdResolver, never()).setSessionId(any(), any()); }
@Before public void setUp() throws Exception { when(this.createSession.save()).thenReturn(Mono.empty()); when(this.createSession.getId()).thenReturn("create-session-id"); when(this.updateSession.getId()).thenReturn("update-session-id"); when(this.sessionStore.createWebSession()).thenReturn(Mono.just(this.createSession)); when(this.sessionStore.retrieveSession(this.updateSession.getId())).thenReturn(Mono.just(this.updateSession)); this.sessionManager = new DefaultWebSessionManager(); this.sessionManager.setSessionIdResolver(this.sessionIdResolver); this.sessionManager.setSessionStore(this.sessionStore); MockServerHttpRequest request = MockServerHttpRequest.get("/path").build(); MockServerHttpResponse response = new MockServerHttpResponse(); this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); }
@Test // SPR-17051 public void sessionInvalidatedBeforeSave() { // Request 1 creates session WebSession session1 = this.store.createWebSession().block(); assertNotNull(session1); String id = session1.getId(); session1.start(); session1.save().block(); // Request 2 retrieves session WebSession session2 = this.store.retrieveSession(id).block(); assertNotNull(session2); assertSame(session1, session2); // Request 3 retrieves and invalidates WebSession session3 = this.store.retrieveSession(id).block(); assertNotNull(session3); assertSame(session1, session3); session3.invalidate().block(); // Request 2 saves session after invalidated session2.save().block(); // Session should not be present WebSession session4 = this.store.retrieveSession(id).block(); assertNull(session4); }
@Test public void retrieveExpiredSession() { WebSession session = this.store.createWebSession().block(); assertNotNull(session); session.getAttributes().put("foo", "bar"); session.save().block(); String id = session.getId(); WebSession retrieved = this.store.retrieveSession(id).block(); assertNotNull(retrieved); assertSame(session, retrieved); // Fast-forward 31 minutes this.store.setClock(Clock.offset(this.store.getClock(), Duration.ofMinutes(31))); WebSession retrievedAgain = this.store.retrieveSession(id).block(); assertNull(retrievedAgain); }
@Test public void webCallShouldTriggerWebSessionSaveAction() { when(mockWebSession.getAttributes()).thenReturn(new HashMap<>()); when(mockWebSession.save()).thenReturn(Mono.empty()); Mono<Map> result = webClient.get() .uri("/get") .exchange() .flatMap(response -> response.body(toMono(Map.class))); StepVerifier.create(result) .consumeNextWith(response -> {/* Don't care about data, just need to catch signal */}) .expectComplete() .verify(Duration.ofMinutes(10)); verify(mockWebSession).save(); }
@Test public void lastAccessTimeIsUpdatedOnRetrieve() { WebSession session1 = this.store.createWebSession().block(); assertNotNull(session1); String id = session1.getId(); Instant time1 = session1.getLastAccessTime(); session1.start(); session1.save().block(); // Fast-forward a few seconds this.store.setClock(Clock.offset(this.store.getClock(), Duration.ofSeconds(5))); WebSession session2 = this.store.retrieveSession(id).block(); assertNotNull(session2); assertSame(session1, session2); Instant time2 = session2.getLastAccessTime(); assertTrue(time1.isBefore(time2)); }
@Override public Mono<Void> save() { return this.delegate.save(); }
@Override public Mono<Void> save() { return this.delegate.save(); }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }
private Mono<Void> save(ServerWebExchange exchange, WebSession session) { List<String> ids = getSessionIdResolver().resolveSessionIds(exchange); if (!session.isStarted() || session.isExpired()) { if (!ids.isEmpty()) { // Expired on retrieve or while processing request, or invalidated.. this.sessionIdResolver.expireSession(exchange); } return Mono.empty(); } if (ids.isEmpty() || !session.getId().equals(ids.get(0))) { this.sessionIdResolver.setSessionId(exchange, session.getId()); } return session.save(); }