@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2017 the original author or authors .
* Copyright 2002 - 2018 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -17,6 +17,7 @@ package org.springframework.web.server.session;
@@ -17,6 +17,7 @@ package org.springframework.web.server.session;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import org.junit.Before ;
import org.junit.Test ;
@ -33,14 +34,10 @@ import org.springframework.web.server.WebSession;
@@ -33,14 +34,10 @@ import org.springframework.web.server.WebSession;
import org.springframework.web.server.adapter.DefaultServerWebExchange ;
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.* ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.Mockito.never ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.when ;
import static org.mockito.Mockito.* ;
/ * *
* Unit tests for { @link DefaultWebSessionManager } .
@ -50,15 +47,15 @@ import static org.mockito.Mockito.when;
@@ -50,15 +47,15 @@ import static org.mockito.Mockito.when;
@RunWith ( MockitoJUnitRunner . class )
public class DefaultWebSessionManagerTests {
private DefaultWebSessionManager m anager;
private DefaultWebSessionManager sessionM anager;
private ServerWebExchange exchange ;
@Mock
private WebSessionIdResolver idResolver ;
private WebSessionIdResolver sess ionI dResolver;
@Mock
private WebSessionStore store ;
private WebSessionStore sessionS tore ;
@Mock
private WebSession createSession ;
@ -69,78 +66,75 @@ public class DefaultWebSessionManagerTests {
@@ -69,78 +66,75 @@ public class DefaultWebSessionManagerTests {
@Before
public void setUp ( ) throws Exception {
when ( this . store . createWebSession ( ) ) . thenReturn ( Mono . just ( this . createSession ) ) ;
when ( this . createSession . save ( ) ) . thenReturn ( Mono . empty ( ) ) ;
when ( this . createSession . getId ( ) ) . thenReturn ( "create-session-id" ) ;
when ( this . updateSession . getId ( ) ) . thenReturn ( "update-session-id" ) ;
this . manager = new DefaultWebSessionManager ( ) ;
this . manager . setSessionIdResolver ( this . idResolver ) ;
this . manager . setSessionStore ( this . store ) ;
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 . m anager,
this . exchange = new DefaultServerWebExchange ( request , response , this . sessionM anager,
ServerCodecConfigurer . create ( ) , new AcceptHeaderLocaleContextResolver ( ) ) ;
}
@Test
public void getSessionSaveWhenCreatedAndNotStartedThenNotSaved ( ) throws Exception {
when ( this . idResolver . resolveSessionIds ( this . exchange ) ) . thenReturn ( Collections . emptyList ( ) ) ;
WebSession session = this . manager . getSession ( this . exchange ) . block ( ) ;
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 . idResolver , never ( ) ) . setSessionId ( any ( ) , any ( ) ) ;
verify ( this . sess ionI dResolver, never ( ) ) . setSessionId ( any ( ) , any ( ) ) ;
}
@Test
public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId ( ) throws Exception {
when ( this . idResolver . resolveSessionIds ( this . exchange ) ) . thenReturn ( Collections . emptyList ( ) ) ;
WebSession session = this . manager . getSession ( this . exchange ) . block ( ) ;
when ( this . createSession . isStarted ( ) ) . thenReturn ( true ) ;
this . exchange . getResponse ( ) . setComplete ( ) . block ( ) ;
public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId ( ) {
String id = session . getId ( ) ;
verify ( this . store ) . createWebSession ( ) ;
verify ( this . createSession ) . save ( ) ;
verify ( this . idResolver ) . setSessionId ( any ( ) , eq ( id ) ) ;
}
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 ( ) ;
@Test
public void exchangeWhenResponseSetCompleteThenSavesAndSetsId ( ) throws Exception {
when ( this . idResolver . resolveSessionIds ( this . exchange ) ) . thenReturn ( Collections . emptyList ( ) ) ;
String id = this . createSession . getId ( ) ;
WebSession session = this . manager . getSession ( this . exchange ) . block ( ) ;
when ( this . createSession . isStarted ( ) ) . thenReturn ( true ) ;
this . exchange . getResponse ( ) . setComplete ( ) . block ( ) ;
verify ( this . idResolver ) . setSessionId ( any ( ) , eq ( id ) ) ;
verify ( this . sessionStore ) . createWebSession ( ) ;
verify ( this . sessionIdResolver ) . setSessionId ( any ( ) , eq ( sessionId ) ) ;
verify ( this . createSession ) . save ( ) ;
}
@Test
public void existingSession ( ) throws Exception {
String id = this . updateSession . getId ( ) ;
when ( this . store . retrieveSession ( id ) ) . thenReturn ( Mono . just ( this . updateSession ) ) ;
when ( this . idResolver . resolveSessionIds ( this . exchange ) ) . thenReturn ( Collections . singletonList ( id ) ) ;
public void existingSession ( ) {
String sessionId = this . updateSession . getId ( ) ;
when ( this . sess ionI dResolver. resolveSessionIds ( this . exchange ) ) . thenReturn ( Collections . singletonList ( sess ionI d) ) ;
WebSession actual = this . m anager. getSession ( this . exchange ) . block ( ) ;
WebSession actual = this . sessionM anager. getSession ( this . exchange ) . block ( ) ;
assertNotNull ( actual ) ;
assertEquals ( id , actual . getId ( ) ) ;
assertEquals ( sess ionI d, actual . getId ( ) ) ;
}
@Test
public void multipleSessionIds ( ) throws Exception {
WebSession existing = this . updateSession ;
String id = existing . getId ( ) ;
when ( this . store . retrieveSession ( any ( ) ) ) . thenReturn ( Mono . empty ( ) ) ;
when ( this . store . retrieveSession ( id ) ) . thenReturn ( Mono . just ( existing ) ) ;
when ( this . idResolver . resolveSessionIds ( this . exchange ) ) . thenReturn ( Arrays . asList ( "neither-this" , "nor-that" , id ) ) ;
WebSession actual = this . manager . getSession ( this . exchange ) . block ( ) ;
public void multipleSessionIds ( ) {
List < String > ids = Arrays . asList ( "not-this" , "not-that" , this . updateSession . getId ( ) ) ;
when ( this . sessionS tore . retrieveSession ( "not-this" ) ) . thenReturn ( Mono . empty ( ) ) ;
when ( this . sessionS tore . retrieveSession ( "not-that" ) ) . thenReturn ( Mono . empty ( ) ) ;
when ( this . sess ionI dResolver. resolveSessionIds ( this . exchange ) ) . thenReturn ( ids ) ;
WebSession actual = this . sessionManager . getSession ( this . exchange ) . block ( ) ;
assertNotNull ( actual ) ;
assertEquals ( existing . getId ( ) , actual . getId ( ) ) ;
assertEquals ( this . updateSession . getId ( ) , actual . getId ( ) ) ;
}
}