Add MockWebSession for use with MockServerWebExchange
Issue: SPR-16772
This commit is contained in:
@@ -109,8 +109,10 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
|
||||
|
||||
/**
|
||||
* Set the session to use for the exchange.
|
||||
* <p>This is mutually exclusive with {@link #sessionManager(WebSessionManager)}.
|
||||
* <p>This method is mutually exclusive with
|
||||
* {@link #sessionManager(WebSessionManager)}.
|
||||
* @param session the session to use
|
||||
* @see MockWebSession
|
||||
*/
|
||||
public Builder session(WebSession session) {
|
||||
this.sessionManager = exchange -> Mono.just(session);
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.mock.web.server;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.session.InMemoryWebSessionStore;
|
||||
|
||||
/**
|
||||
* Implementation of {@code WebSession} that delegates to a session instance
|
||||
* obtained via {@link InMemoryWebSessionStore}.
|
||||
*
|
||||
* <p>This is intended for use with the
|
||||
* {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
|
||||
* method of the {@code MockServerWebExchange} builder, eliminating the need
|
||||
* to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.1
|
||||
*/
|
||||
public class MockWebSession implements WebSession {
|
||||
|
||||
private final WebSession delegate;
|
||||
|
||||
|
||||
public MockWebSession() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public MockWebSession(Clock clock) {
|
||||
InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
|
||||
if (clock != null) {
|
||||
sessionStore.setClock(clock);
|
||||
}
|
||||
this.delegate = sessionStore.createWebSession().block();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.delegate.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAttributes() {
|
||||
return this.delegate.getAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
this.delegate.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStarted() {
|
||||
return this.delegate.isStarted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> changeSessionId() {
|
||||
return this.delegate.changeSessionId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> invalidate() {
|
||||
return this.delegate.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> save() {
|
||||
return this.delegate.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExpired() {
|
||||
return this.delegate.isExpired();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant getCreationTime() {
|
||||
return this.delegate.getCreationTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant getLastAccessTime() {
|
||||
return this.delegate.getLastAccessTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxIdleTime(Duration maxIdleTime) {
|
||||
this.delegate.setMaxIdleTime(maxIdleTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Duration getMaxIdleTime() {
|
||||
return this.delegate.getMaxIdleTime();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.mock.web.test.server;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.session.InMemoryWebSessionStore;
|
||||
|
||||
/**
|
||||
* Implementation of {@code WebSession} that delegates to a session instance
|
||||
* obtained via {@link InMemoryWebSessionStore}.
|
||||
*
|
||||
* <p>This is intended for use with the
|
||||
* {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)}
|
||||
* method of the {@code MockServerWebExchange} builder, eliminating the need
|
||||
* to use {@code WebSessionManager} or {@code WebSessionStore} altogether.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.1
|
||||
*/
|
||||
public class MockWebSession implements WebSession {
|
||||
|
||||
private final WebSession delegate;
|
||||
|
||||
|
||||
public MockWebSession() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public MockWebSession(Clock clock) {
|
||||
InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore();
|
||||
if (clock != null) {
|
||||
sessionStore.setClock(clock);
|
||||
}
|
||||
this.delegate = sessionStore.createWebSession().block();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.delegate.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAttributes() {
|
||||
return this.delegate.getAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
this.delegate.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStarted() {
|
||||
return this.delegate.isStarted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> changeSessionId() {
|
||||
return this.delegate.changeSessionId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> invalidate() {
|
||||
return this.delegate.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> save() {
|
||||
return this.delegate.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExpired() {
|
||||
return this.delegate.isExpired();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant getCreationTime() {
|
||||
return this.delegate.getCreationTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant getLastAccessTime() {
|
||||
return this.delegate.getLastAccessTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxIdleTime(Duration maxIdleTime) {
|
||||
this.delegate.setMaxIdleTime(maxIdleTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Duration getMaxIdleTime() {
|
||||
return this.delegate.getMaxIdleTime();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,11 +21,11 @@ import java.util.HashSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.test.server.MockWebSession;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.session.InMemoryWebSessionStore;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -50,9 +50,7 @@ public class SessionAttributesHandlerTests {
|
||||
|
||||
@Test
|
||||
public void retrieveAttributes() {
|
||||
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
|
||||
assertNotNull(session);
|
||||
|
||||
WebSession session = new MockWebSession();
|
||||
session.getAttributes().put("attr1", "value1");
|
||||
session.getAttributes().put("attr2", "value2");
|
||||
session.getAttributes().put("attr3", new TestBean());
|
||||
@@ -72,9 +70,7 @@ public class SessionAttributesHandlerTests {
|
||||
|
||||
@Test
|
||||
public void cleanupAttributes() {
|
||||
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
|
||||
assertNotNull(session);
|
||||
|
||||
WebSession session = new MockWebSession();
|
||||
session.getAttributes().put("attr1", "value1");
|
||||
session.getAttributes().put("attr2", "value2");
|
||||
session.getAttributes().put("attr3", new TestBean());
|
||||
@@ -94,14 +90,13 @@ public class SessionAttributesHandlerTests {
|
||||
|
||||
@Test
|
||||
public void storeAttributes() {
|
||||
WebSession session = new InMemoryWebSessionStore().createWebSession().block();
|
||||
assertNotNull(session);
|
||||
|
||||
ModelMap model = new ModelMap();
|
||||
model.put("attr1", "value1");
|
||||
model.put("attr2", "value2");
|
||||
model.put("attr3", new TestBean());
|
||||
|
||||
WebSession session = new MockWebSession();
|
||||
sessionAttributesHandler.storeAttributes(session, model);
|
||||
|
||||
assertEquals("value1", session.getAttributes().get("attr1"));
|
||||
|
||||
Reference in New Issue
Block a user