15
15
*/
16
16
package org .springframework .web .server .session ;
17
17
18
- import java .time .Clock ;
19
- import java .time .Duration ;
20
- import java .time .Instant ;
21
- import java .time .ZoneId ;
22
18
import java .util .Arrays ;
23
19
import java .util .Collections ;
24
20
32
28
import org .springframework .http .codec .ServerCodecConfigurer ;
33
29
import org .springframework .mock .http .server .reactive .test .MockServerHttpRequest ;
34
30
import org .springframework .mock .http .server .reactive .test .MockServerHttpResponse ;
35
- import org .springframework .util .IdGenerator ;
36
- import org .springframework .util .JdkIdGenerator ;
37
31
import org .springframework .web .server .ServerWebExchange ;
38
32
import org .springframework .web .server .WebSession ;
39
33
import org .springframework .web .server .adapter .DefaultServerWebExchange ;
42
36
import static org .junit .Assert .assertEquals ;
43
37
import static org .junit .Assert .assertFalse ;
44
38
import static org .junit .Assert .assertNotNull ;
45
- import static org .junit .Assert .assertNotSame ;
46
39
import static org .mockito .ArgumentMatchers .any ;
47
40
import static org .mockito .ArgumentMatchers .eq ;
48
41
import static org .mockito .Mockito .never ;
49
42
import static org .mockito .Mockito .verify ;
43
+ import static org .mockito .Mockito .verifyZeroInteractions ;
50
44
import static org .mockito .Mockito .when ;
51
45
52
46
/**
57
51
@ RunWith (MockitoJUnitRunner .class )
58
52
public class DefaultWebSessionManagerTests {
59
53
60
- private static final Clock CLOCK = Clock .system (ZoneId .of ("GMT" ));
61
-
62
- private static final IdGenerator idGenerator = new JdkIdGenerator ();
63
-
64
-
65
54
private DefaultWebSessionManager manager ;
66
55
67
56
private ServerWebExchange exchange ;
@@ -72,10 +61,23 @@ public class DefaultWebSessionManagerTests {
72
61
@ Mock
73
62
private WebSessionStore store ;
74
63
64
+ @ Mock
65
+ private WebSession createSession ;
66
+
67
+ @ Mock
68
+ private WebSession retrieveSession ;
69
+
70
+ @ Mock
71
+ private WebSession updateSession ;
72
+
75
73
@ Before
76
74
public void setUp () throws Exception {
77
- when (this .store .createWebSession ()).thenReturn (Mono .just (createDefaultWebSession ()));
78
- when (this .store .updateLastAccessTime (any ())).thenAnswer ( invocation -> Mono .just (invocation .getArgument (0 )));
75
+ when (this .store .createWebSession ()).thenReturn (Mono .just (this .createSession ));
76
+ when (this .store .updateLastAccessTime (any ())).thenReturn (Mono .just (this .updateSession ));
77
+ when (this .store .retrieveSession (any ())).thenReturn (Mono .just (this .retrieveSession ));
78
+ when (this .createSession .save ()).thenReturn (Mono .empty ());
79
+ when (this .updateSession .getId ()).thenReturn ("update-session-id" );
80
+ when (this .retrieveSession .getId ()).thenReturn ("retrieve-session-id" );
79
81
80
82
this .manager = new DefaultWebSessionManager ();
81
83
this .manager .setSessionIdResolver (this .idResolver );
@@ -87,90 +89,71 @@ public void setUp() throws Exception {
87
89
ServerCodecConfigurer .create (), new AcceptHeaderLocaleContextResolver ());
88
90
}
89
91
90
-
91
92
@ Test
92
- public void getSessionWithoutStarting () throws Exception {
93
+ public void getSessionSaveWhenCreatedAndNotStartedThenNotSaved () throws Exception {
93
94
when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
94
95
WebSession session = this .manager .getSession (this .exchange ).block ();
95
- session . save ().block ();
96
+ this . exchange . getResponse (). setComplete ().block ();
96
97
97
98
assertFalse (session .isStarted ());
98
99
assertFalse (session .isExpired ());
99
- verify (this .store , never ()).storeSession (any ());
100
+ verifyZeroInteractions (this .retrieveSession , this .updateSession );
101
+ verify (this .createSession , never ()).save ();
100
102
verify (this .idResolver , never ()).setSessionId (any (), any ());
101
103
}
102
104
103
105
@ Test
104
- public void startSessionExplicitly () throws Exception {
106
+ public void getSessionSaveWhenCreatedAndStartedThenSavesAndSetsId () throws Exception {
105
107
when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
106
- when (this .store .storeSession (any ())).thenReturn (Mono .empty ());
107
108
WebSession session = this .manager .getSession (this .exchange ).block ();
108
- session . start ( );
109
- session . save ().block ();
109
+ when ( this . createSession . isStarted ()). thenReturn ( true );
110
+ this . exchange . getResponse (). setComplete ().block ();
110
111
111
112
String id = session .getId ();
112
113
verify (this .store ).createWebSession ();
113
- verify (this .store ). storeSession ( any () );
114
+ verify (this .createSession ). save ( );
114
115
verify (this .idResolver ).setSessionId (any (), eq (id ));
115
116
}
116
117
117
- @ Test
118
- public void startSessionImplicitly () throws Exception {
119
- when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
120
- when (this .store .storeSession (any ())).thenReturn (Mono .empty ());
121
- WebSession session = this .manager .getSession (this .exchange ).block ();
122
- session .getAttributes ().put ("foo" , "bar" );
123
- session .save ().block ();
124
-
125
- verify (this .store ).createWebSession ();
126
- verify (this .idResolver ).setSessionId (any (), any ());
127
- verify (this .store ).storeSession (any ());
128
- }
129
-
130
118
@ Test
131
119
public void exchangeWhenResponseSetCompleteThenSavesAndSetsId () throws Exception {
132
120
when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .emptyList ());
133
- when ( this .store . storeSession ( any ())). thenReturn ( Mono . empty () );
121
+ String id = this .createSession . getId ( );
134
122
WebSession session = this .manager .getSession (this .exchange ).block ();
135
- String id = session .getId ();
136
- session .getAttributes ().put ("foo" , "bar" );
123
+ when (this .createSession .isStarted ()).thenReturn (true );
137
124
this .exchange .getResponse ().setComplete ().block ();
138
125
139
126
verify (this .idResolver ).setSessionId (any (), eq (id ));
140
- verify (this .store ). storeSession ( any () );
127
+ verify (this .createSession ). save ( );
141
128
}
142
129
143
130
@ Test
144
131
public void existingSession () throws Exception {
145
- DefaultWebSession existing = createDefaultWebSession ();
146
- String id = existing .getId ();
147
- when (this .store .retrieveSession (id )).thenReturn (Mono .just (existing ));
132
+ String id = this .updateSession .getId ();
133
+ when (this .store .retrieveSession (id )).thenReturn (Mono .just (this .updateSession ));
148
134
when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .singletonList (id ));
149
135
150
136
WebSession actual = this .manager .getSession (this .exchange ).block ();
151
137
assertNotNull (actual );
152
- assertEquals (existing . getId () , actual .getId ());
138
+ assertEquals (id , actual .getId ());
153
139
}
154
140
155
141
@ Test
156
142
public void existingSessionIsExpired () throws Exception {
157
- DefaultWebSession existing = createDefaultWebSession ();
158
- existing .start ();
159
- Instant lastAccessTime = Instant .now (CLOCK ).minus (Duration .ofMinutes (31 ));
160
- existing = new DefaultWebSession (existing , lastAccessTime , s -> Mono .empty ());
161
- when (this .store .retrieveSession (existing .getId ())).thenReturn (Mono .just (existing ));
162
- when (this .store .removeSession (existing .getId ())).thenReturn (Mono .empty ());
163
- when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .singletonList (existing .getId ()));
143
+ String id = this .retrieveSession .getId ();
144
+ when (this .retrieveSession .isExpired ()).thenReturn (true );
145
+ when (this .idResolver .resolveSessionIds (this .exchange )).thenReturn (Collections .singletonList (id ));
146
+ when (this .store .removeSession (any ())).thenReturn (Mono .empty ());
164
147
165
148
WebSession actual = this .manager .getSession (this .exchange ).block ();
166
- assertNotSame ( existing , actual );
167
- verify (this .store ).removeSession (existing . getId () );
149
+ assertEquals ( this . createSession . getId () , actual . getId () );
150
+ verify (this .store ).removeSession (id );
168
151
verify (this .idResolver ).expireSession (any ());
169
152
}
170
153
171
154
@ Test
172
155
public void multipleSessionIds () throws Exception {
173
- DefaultWebSession existing = createDefaultWebSession () ;
156
+ WebSession existing = this . updateSession ;
174
157
String id = existing .getId ();
175
158
when (this .store .retrieveSession (any ())).thenReturn (Mono .empty ());
176
159
when (this .store .retrieveSession (id )).thenReturn (Mono .just (existing ));
@@ -180,8 +163,4 @@ public void multipleSessionIds() throws Exception {
180
163
assertNotNull (actual );
181
164
assertEquals (existing .getId (), actual .getId ());
182
165
}
183
-
184
- private DefaultWebSession createDefaultWebSession () {
185
- return new DefaultWebSession (idGenerator , CLOCK , (s , session ) -> Mono .empty (), s -> Mono .empty ());
186
- }
187
166
}
0 commit comments