Skip to content

Commit 15182b2

Browse files
committed
Add MockWebSession for use with MockServerWebExchange
Issue: SPR-16772
1 parent 7f954eb commit 15182b2

File tree

4 files changed

+245
-10
lines changed

4 files changed

+245
-10
lines changed

spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ public Builder(MockServerHttpRequest request) {
109109

110110
/**
111111
* Set the session to use for the exchange.
112-
* <p>This is mutually exclusive with {@link #sessionManager(WebSessionManager)}.
112+
* <p>This method is mutually exclusive with
113+
* {@link #sessionManager(WebSessionManager)}.
113114
* @param session the session to use
115+
* @see MockWebSession
114116
*/
115117
public Builder session(WebSession session) {
116118
this.sessionManager = exchange -> Mono.just(session);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.web.server;
17+
18+
import java.time.Clock;
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.util.Map;
22+
23+
import reactor.core.publisher.Mono;
24+
25+
import org.springframework.web.server.WebSession;
26+
import org.springframework.web.server.session.InMemoryWebSessionStore;
27+
28+
/**
29+
* Implementation of {@code WebSession} that delegates to a session instance
30+
* obtained via {@link InMemoryWebSessionStore}.
31+
*
32+
* <p>This is intended for use with the
33+
* {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
34+
* method of the {@code MockServerWebExchange} builder, eliminating the need
35+
* to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
36+
*
37+
* @author Rossen Stoyanchev
38+
* @since 5.1
39+
*/
40+
public class MockWebSession implements WebSession {
41+
42+
private final WebSession delegate;
43+
44+
45+
public MockWebSession() {
46+
this(null);
47+
}
48+
49+
@SuppressWarnings("ConstantConditions")
50+
public MockWebSession(Clock clock) {
51+
InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
52+
if (clock != null) {
53+
sessionStore.setClock(clock);
54+
}
55+
this.delegate = sessionStore.createWebSession().block();
56+
}
57+
58+
59+
@Override
60+
public String getId() {
61+
return this.delegate.getId();
62+
}
63+
64+
@Override
65+
public Map<String, Object> getAttributes() {
66+
return this.delegate.getAttributes();
67+
}
68+
69+
@Override
70+
public void start() {
71+
this.delegate.start();
72+
}
73+
74+
@Override
75+
public boolean isStarted() {
76+
return this.delegate.isStarted();
77+
}
78+
79+
@Override
80+
public Mono<Void> changeSessionId() {
81+
return this.delegate.changeSessionId();
82+
}
83+
84+
@Override
85+
public Mono<Void> invalidate() {
86+
return this.delegate.invalidate();
87+
}
88+
89+
@Override
90+
public Mono<Void> save() {
91+
return this.delegate.save();
92+
}
93+
94+
@Override
95+
public boolean isExpired() {
96+
return this.delegate.isExpired();
97+
}
98+
99+
@Override
100+
public Instant getCreationTime() {
101+
return this.delegate.getCreationTime();
102+
}
103+
104+
@Override
105+
public Instant getLastAccessTime() {
106+
return this.delegate.getLastAccessTime();
107+
}
108+
109+
@Override
110+
public void setMaxIdleTime(Duration maxIdleTime) {
111+
this.delegate.setMaxIdleTime(maxIdleTime);
112+
}
113+
114+
@Override
115+
public Duration getMaxIdleTime() {
116+
return this.delegate.getMaxIdleTime();
117+
}
118+
119+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.web.test.server;
17+
18+
import java.time.Clock;
19+
import java.time.Duration;
20+
import java.time.Instant;
21+
import java.util.Map;
22+
23+
import reactor.core.publisher.Mono;
24+
25+
import org.springframework.web.server.WebSession;
26+
import org.springframework.web.server.session.InMemoryWebSessionStore;
27+
28+
/**
29+
* Implementation of {@code WebSession} that delegates to a session instance
30+
* obtained via {@link InMemoryWebSessionStore}.
31+
*
32+
* <p>This is intended for use with the
33+
* {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
34+
* method of the {@code MockServerWebExchange} builder, eliminating the need
35+
* to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
36+
*
37+
* @author Rossen Stoyanchev
38+
* @since 5.1
39+
*/
40+
public class MockWebSession implements WebSession {
41+
42+
private final WebSession delegate;
43+
44+
45+
public MockWebSession() {
46+
this(null);
47+
}
48+
49+
@SuppressWarnings("ConstantConditions")
50+
public MockWebSession(Clock clock) {
51+
InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
52+
if (clock != null) {
53+
sessionStore.setClock(clock);
54+
}
55+
this.delegate = sessionStore.createWebSession().block();
56+
}
57+
58+
59+
@Override
60+
public String getId() {
61+
return this.delegate.getId();
62+
}
63+
64+
@Override
65+
public Map<String, Object> getAttributes() {
66+
return this.delegate.getAttributes();
67+
}
68+
69+
@Override
70+
public void start() {
71+
this.delegate.start();
72+
}
73+
74+
@Override
75+
public boolean isStarted() {
76+
return this.delegate.isStarted();
77+
}
78+
79+
@Override
80+
public Mono<Void> changeSessionId() {
81+
return this.delegate.changeSessionId();
82+
}
83+
84+
@Override
85+
public Mono<Void> invalidate() {
86+
return this.delegate.invalidate();
87+
}
88+
89+
@Override
90+
public Mono<Void> save() {
91+
return this.delegate.save();
92+
}
93+
94+
@Override
95+
public boolean isExpired() {
96+
return this.delegate.isExpired();
97+
}
98+
99+
@Override
100+
public Instant getCreationTime() {
101+
return this.delegate.getCreationTime();
102+
}
103+
104+
@Override
105+
public Instant getLastAccessTime() {
106+
return this.delegate.getLastAccessTime();
107+
}
108+
109+
@Override
110+
public void setMaxIdleTime(Duration maxIdleTime) {
111+
this.delegate.setMaxIdleTime(maxIdleTime);
112+
}
113+
114+
@Override
115+
public Duration getMaxIdleTime() {
116+
return this.delegate.getMaxIdleTime();
117+
}
118+
119+
}

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/SessionAttributesHandlerTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
import org.junit.Test;
2323

24+
import org.springframework.mock.web.test.server.MockWebSession;
2425
import org.springframework.tests.sample.beans.TestBean;
2526
import org.springframework.ui.ModelMap;
2627
import org.springframework.web.bind.annotation.SessionAttributes;
2728
import org.springframework.web.server.WebSession;
28-
import org.springframework.web.server.session.InMemoryWebSessionStore;
2929

3030
import static java.util.Arrays.*;
3131
import static org.junit.Assert.*;
@@ -50,9 +50,7 @@ public void isSessionAttribute() {
5050

5151
@Test
5252
public void retrieveAttributes() {
53-
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
54-
assertNotNull(session);
55-
53+
WebSession session = new MockWebSession();
5654
session.getAttributes().put("attr1", "value1");
5755
session.getAttributes().put("attr2", "value2");
5856
session.getAttributes().put("attr3", new TestBean());
@@ -72,9 +70,7 @@ public void retrieveAttributes() {
7270

7371
@Test
7472
public void cleanupAttributes() {
75-
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
76-
assertNotNull(session);
77-
73+
WebSession session = new MockWebSession();
7874
session.getAttributes().put("attr1", "value1");
7975
session.getAttributes().put("attr2", "value2");
8076
session.getAttributes().put("attr3", new TestBean());
@@ -94,14 +90,13 @@ public void cleanupAttributes() {
9490

9591
@Test
9692
public void storeAttributes() {
97-
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
98-
assertNotNull(session);
9993

10094
ModelMap model = new ModelMap();
10195
model.put("attr1", "value1");
10296
model.put("attr2", "value2");
10397
model.put("attr3", new TestBean());
10498

99+
WebSession session = new MockWebSession();
105100
sessionAttributesHandler.storeAttributes(session, model);
106101

107102
assertEquals("value1", session.getAttributes().get("attr1"));

0 commit comments

Comments
 (0)