001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.mock.web.server; 018 019import java.time.Clock; 020import java.time.Duration; 021import java.time.Instant; 022import java.util.Map; 023 024import reactor.core.publisher.Mono; 025 026import org.springframework.lang.Nullable; 027import org.springframework.util.Assert; 028import org.springframework.web.server.WebSession; 029import org.springframework.web.server.session.InMemoryWebSessionStore; 030 031/** 032 * Implementation of {@code WebSession} that delegates to a session instance 033 * obtained via {@link InMemoryWebSessionStore}. 034 * 035 * <p>This is intended for use with the 036 * {@link MockServerWebExchange.Builder#session(WebSession) session(WebSession)} 037 * method of the {@code MockServerWebExchange} builder, eliminating the need 038 * to use {@code WebSessionManager} or {@code WebSessionStore} altogether. 039 * 040 * @author Rossen Stoyanchev 041 * @since 5.1 042 */ 043public class MockWebSession implements WebSession { 044 045 private final WebSession delegate; 046 047 048 public MockWebSession() { 049 this(null); 050 } 051 052 public MockWebSession(@Nullable Clock clock) { 053 InMemoryWebSessionStore sessionStore = new InMemoryWebSessionStore(); 054 if (clock != null) { 055 sessionStore.setClock(clock); 056 } 057 WebSession session = sessionStore.createWebSession().block(); 058 Assert.state(session != null, "WebSession must not be null"); 059 this.delegate = session; 060 } 061 062 063 @Override 064 public String getId() { 065 return this.delegate.getId(); 066 } 067 068 @Override 069 public Map<String, Object> getAttributes() { 070 return this.delegate.getAttributes(); 071 } 072 073 @Override 074 public void start() { 075 this.delegate.start(); 076 } 077 078 @Override 079 public boolean isStarted() { 080 return this.delegate.isStarted(); 081 } 082 083 @Override 084 public Mono<Void> changeSessionId() { 085 return this.delegate.changeSessionId(); 086 } 087 088 @Override 089 public Mono<Void> invalidate() { 090 return this.delegate.invalidate(); 091 } 092 093 @Override 094 public Mono<Void> save() { 095 return this.delegate.save(); 096 } 097 098 @Override 099 public boolean isExpired() { 100 return this.delegate.isExpired(); 101 } 102 103 @Override 104 public Instant getCreationTime() { 105 return this.delegate.getCreationTime(); 106 } 107 108 @Override 109 public Instant getLastAccessTime() { 110 return this.delegate.getLastAccessTime(); 111 } 112 113 @Override 114 public void setMaxIdleTime(Duration maxIdleTime) { 115 this.delegate.setMaxIdleTime(maxIdleTime); 116 } 117 118 @Override 119 public Duration getMaxIdleTime() { 120 return this.delegate.getMaxIdleTime(); 121 } 122 123}