Skip to content

Commit b728047

Browse files
rwinchrstoyanchev
authored andcommitted
Improve WebSession related tests
Add missing DefaultWebSessionManagerTests .block(). Previously session.save() was invoked, but we did not ensure it was completed. This commit makes it block on session.save() Fix existingSessionIsExpired. This test is actually broken and is testing a new session is created because the session id returned by the idResolver does not match the existing WebSession. This commit ensures that the id of the WebSession found by idResolver matches the existing WebSession. DefaultWebSessionManagerTests use Mockito. To ensure we test with independence from InMemoryWebSessionStore we use Mockito for the DefaultWebessionManager collaborators. Add test for response.setComplete(). We want to ensure that when the response is completed, it saves the WebSession and writes it to the response using idResolver
1 parent 167ddc7 commit b728047

File tree

1 file changed

+53
-61
lines changed

1 file changed

+53
-61
lines changed

spring-web/src/test/java/org/springframework/web/server/session/DefaultWebSessionManagerTests.java

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
import java.time.Duration;
2020
import java.time.Instant;
2121
import java.time.ZoneId;
22-
import java.util.ArrayList;
2322
import java.util.Arrays;
2423
import java.util.Collections;
25-
import java.util.List;
2624

2725
import org.junit.Before;
2826
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mock;
29+
import org.mockito.junit.MockitoJUnitRunner;
2930
import reactor.core.publisher.Mono;
3031

3132
import org.springframework.http.codec.ServerCodecConfigurer;
32-
import org.springframework.lang.Nullable;
3333
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
3434
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
3535
import org.springframework.util.IdGenerator;
@@ -43,13 +43,18 @@
4343
import static org.junit.Assert.assertFalse;
4444
import static org.junit.Assert.assertNotNull;
4545
import static org.junit.Assert.assertNotSame;
46-
import static org.junit.Assert.assertNull;
47-
import static org.junit.Assert.assertSame;
46+
import static org.mockito.ArgumentMatchers.any;
47+
import static org.mockito.ArgumentMatchers.eq;
48+
import static org.mockito.Mockito.never;
49+
import static org.mockito.Mockito.verify;
50+
import static org.mockito.Mockito.when;
4851

4952
/**
5053
* Unit tests for {@link DefaultWebSessionManager}.
5154
* @author Rossen Stoyanchev
55+
* @author Rob Winch
5256
*/
57+
@RunWith(MockitoJUnitRunner.class)
5358
public class DefaultWebSessionManagerTests {
5459

5560
private static final Clock CLOCK = Clock.system(ZoneId.of("GMT"));
@@ -59,16 +64,19 @@ public class DefaultWebSessionManagerTests {
5964

6065
private DefaultWebSessionManager manager;
6166

62-
private TestWebSessionIdResolver idResolver;
63-
6467
private ServerWebExchange exchange;
6568

69+
@Mock
70+
private WebSessionIdResolver idResolver;
71+
72+
@Mock
73+
private WebSessionStore store;
6674

6775
@Before
6876
public void setUp() throws Exception {
6977
this.manager = new DefaultWebSessionManager();
70-
this.idResolver = new TestWebSessionIdResolver();
7178
this.manager.setSessionIdResolver(this.idResolver);
79+
this.manager.setSessionStore(this.store);
7280

7381
MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
7482
MockServerHttpResponse response = new MockServerHttpResponse();
@@ -79,45 +87,60 @@ public void setUp() throws Exception {
7987

8088
@Test
8189
public void getSessionWithoutStarting() throws Exception {
82-
this.idResolver.setIdsToResolve(Collections.emptyList());
90+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList());
8391
WebSession session = this.manager.getSession(this.exchange).block();
84-
session.save();
92+
session.save().block();
8593

8694
assertFalse(session.isStarted());
8795
assertFalse(session.isExpired());
88-
assertNull(this.idResolver.getSavedId());
89-
assertNull(this.manager.getSessionStore().retrieveSession(session.getId()).block());
96+
verify(this.store, never()).storeSession(any());
97+
verify(this.idResolver, never()).setSessionId(any(), any());
9098
}
9199

92100
@Test
93101
public void startSessionExplicitly() throws Exception {
94-
this.idResolver.setIdsToResolve(Collections.emptyList());
102+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList());
103+
when(this.store.storeSession(any())).thenReturn(Mono.empty());
95104
WebSession session = this.manager.getSession(this.exchange).block();
96105
session.start();
97-
session.save();
106+
session.save().block();
98107

99108
String id = session.getId();
100-
assertNotNull(this.idResolver.getSavedId());
101-
assertEquals(id, this.idResolver.getSavedId());
102-
assertSame(session, this.manager.getSessionStore().retrieveSession(id).block());
109+
verify(this.store).storeSession(any());
110+
verify(this.idResolver).setSessionId(any(), eq(id));
103111
}
104112

105113
@Test
106114
public void startSessionImplicitly() throws Exception {
107-
this.idResolver.setIdsToResolve(Collections.emptyList());
115+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList());
116+
when(this.store.storeSession(any())).thenReturn(Mono.empty());
108117
WebSession session = this.manager.getSession(this.exchange).block();
109118
session.getAttributes().put("foo", "bar");
110-
session.save();
119+
session.save().block();
111120

112-
assertNotNull(this.idResolver.getSavedId());
121+
verify(this.idResolver).setSessionId(any(), any());
122+
verify(this.store).storeSession(any());
123+
}
124+
125+
@Test
126+
public void exchangeWhenResponseSetCompleteThenSavesAndSetsId() throws Exception {
127+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.emptyList());
128+
when(this.store.storeSession(any())).thenReturn(Mono.empty());
129+
WebSession session = this.manager.getSession(this.exchange).block();
130+
String id = session.getId();
131+
session.getAttributes().put("foo", "bar");
132+
this.exchange.getResponse().setComplete().block();
133+
134+
verify(this.idResolver).setSessionId(any(), eq(id));
135+
verify(this.store).storeSession(any());
113136
}
114137

115138
@Test
116139
public void existingSession() throws Exception {
117140
DefaultWebSession existing = createDefaultWebSession();
118141
String id = existing.getId();
119-
this.manager.getSessionStore().storeSession(existing);
120-
this.idResolver.setIdsToResolve(Collections.singletonList(id));
142+
when(this.store.retrieveSession(id)).thenReturn(Mono.just(existing));
143+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.singletonList(id));
121144

122145
WebSession actual = this.manager.getSession(this.exchange).block();
123146
assertNotNull(actual);
@@ -130,19 +153,23 @@ public void existingSessionIsExpired() throws Exception {
130153
existing.start();
131154
Instant lastAccessTime = Instant.now(CLOCK).minus(Duration.ofMinutes(31));
132155
existing = new DefaultWebSession(existing, lastAccessTime, s -> Mono.empty());
133-
this.manager.getSessionStore().storeSession(existing);
134-
this.idResolver.setIdsToResolve(Collections.singletonList("1"));
156+
when(this.store.retrieveSession(existing.getId())).thenReturn(Mono.just(existing));
157+
when(this.store.removeSession(existing.getId())).thenReturn(Mono.empty());
158+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Collections.singletonList(existing.getId()));
135159

136160
WebSession actual = this.manager.getSession(this.exchange).block();
137161
assertNotSame(existing, actual);
162+
verify(this.store).removeSession(existing.getId());
163+
verify(this.idResolver).expireSession(any());
138164
}
139165

140166
@Test
141167
public void multipleSessionIds() throws Exception {
142168
DefaultWebSession existing = createDefaultWebSession();
143169
String id = existing.getId();
144-
this.manager.getSessionStore().storeSession(existing);
145-
this.idResolver.setIdsToResolve(Arrays.asList("neither-this", "nor-that", id));
170+
when(this.store.retrieveSession(any())).thenReturn(Mono.empty());
171+
when(this.store.retrieveSession(id)).thenReturn(Mono.just(existing));
172+
when(this.idResolver.resolveSessionIds(this.exchange)).thenReturn(Arrays.asList("neither-this", "nor-that", id));
146173

147174
WebSession actual = this.manager.getSession(this.exchange).block();
148175
assertNotNull(actual);
@@ -152,39 +179,4 @@ public void multipleSessionIds() throws Exception {
152179
private DefaultWebSession createDefaultWebSession() {
153180
return new DefaultWebSession(idGenerator, CLOCK, (s, session) -> Mono.empty(), s -> Mono.empty());
154181
}
155-
156-
157-
private static class TestWebSessionIdResolver implements WebSessionIdResolver {
158-
159-
private List<String> idsToResolve = new ArrayList<>();
160-
161-
@Nullable
162-
private String id = null;
163-
164-
165-
public void setIdsToResolve(List<String> idsToResolve) {
166-
this.idsToResolve = idsToResolve;
167-
}
168-
169-
@Nullable
170-
public String getSavedId() {
171-
return this.id;
172-
}
173-
174-
@Override
175-
public List<String> resolveSessionIds(ServerWebExchange exchange) {
176-
return this.idsToResolve;
177-
}
178-
179-
@Override
180-
public void setSessionId(ServerWebExchange exchange, String sessionId) {
181-
this.id = sessionId;
182-
}
183-
184-
@Override
185-
public void expireSession(ServerWebExchange exchange) {
186-
this.id = null;
187-
}
188-
}
189-
190182
}

0 commit comments

Comments
 (0)