Class MockWebSession
- java.lang.Object
- org.springframework.mock.web.server.MockWebSession
- All Implemented Interfaces:
WebSession
public class MockWebSession extends Object implements WebSession
Implementation ofWebSessionthat delegates to a session instance obtained viaInMemoryWebSessionStore.This is intended for use with the
session(WebSession)method of theMockServerWebExchangebuilder, eliminating the need to useWebSessionManagerorWebSessionStorealtogether.- Since:
- 5.1
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description MockWebSession()MockWebSession(Clock clock)
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description reactor.core.publisher.Mono<Void>changeSessionId()Generate a new id for the session and update the underlying session storage to reflect the new id.Map<String,Object>getAttributes()Return a map that holds session attributes.InstantgetCreationTime()Return the time when the session was created.StringgetId()Return a unique session identifier.InstantgetLastAccessTime()Return the last time of session access as a result of user activity such as an HTTP request.DurationgetMaxIdleTime()Return the maximum time after thelastAccessTimebefore a session expires.reactor.core.publisher.Mono<Void>invalidate()Invalidate the current session and clear session storage.booleanisExpired()Returntrueif the session expired aftermaxIdleTimeelapsed.booleanisStarted()Whether a session with the client has been started explicitly viaWebSession.start()or implicitly by adding session attributes.reactor.core.publisher.Mono<Void>save()Save the session through theWebSessionStoreas follows: If the session is new (i.e.voidsetMaxIdleTime(Duration maxIdleTime)Configure the max amount of time that may elapse after thelastAccessTimebefore a session is considered expired.voidstart()Force the creation of a session causing the session id to be sent whenWebSession.save()is called.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.web.server.WebSession
getAttribute, getAttributeOrDefault, getRequiredAttribute
Constructor Detail
MockWebSession
public MockWebSession()
MockWebSession
public MockWebSession(@Nullable Clock clock)
Method Detail
getId
public String getId()
Description copied from interface:WebSessionReturn a unique session identifier.- Specified by:
getIdin interfaceWebSession
getAttributes
public Map<String,Object> getAttributes()
Description copied from interface:WebSessionReturn a map that holds session attributes.- Specified by:
getAttributesin interfaceWebSession
start
public void start()
Description copied from interface:WebSessionForce the creation of a session causing the session id to be sent whenWebSession.save()is called.- Specified by:
startin interfaceWebSession
isStarted
public boolean isStarted()
Description copied from interface:WebSessionWhether a session with the client has been started explicitly viaWebSession.start()or implicitly by adding session attributes. If "false" then the session id is not sent to the client and theWebSession.save()method is essentially a no-op.- Specified by:
isStartedin interfaceWebSession
changeSessionId
public reactor.core.publisher.Mono<Void> changeSessionId()
Description copied from interface:WebSessionGenerate a new id for the session and update the underlying session storage to reflect the new id. After a successful callWebSession.getId()reflects the new session id.- Specified by:
changeSessionIdin interfaceWebSession- Returns:
- completion notification (success or error)
invalidate
public reactor.core.publisher.Mono<Void> invalidate()
Description copied from interface:WebSessionInvalidate the current session and clear session storage.- Specified by:
invalidatein interfaceWebSession- Returns:
- completion notification (success or error)
save
public reactor.core.publisher.Mono<Void> save()
Description copied from interface:WebSessionSave the session through theWebSessionStoreas follows:- If the session is new (i.e. created but never persisted), it must have been started explicitly via
WebSession.start()or implicitly by adding attributes, or otherwise this method should have no effect. - If the session was retrieved through the
WebSessionStore, the implementation for this method must check whether the session wasinvalidatedand if so return an error.
Note that this method is not intended for direct use by applications. Instead it is automatically invoked just before the response is committed.
- Specified by:
savein interfaceWebSession- Returns:
Monoto indicate completion with success or error
- If the session is new (i.e. created but never persisted), it must have been started explicitly via
isExpired
public boolean isExpired()
Description copied from interface:WebSessionReturntrueif the session expired aftermaxIdleTimeelapsed.Typically expiration checks should be automatically made when a session is accessed, a new
WebSessioninstance created if necessary, at the start of request processing so that applications don't have to worry about expired session by default.- Specified by:
isExpiredin interfaceWebSession
getCreationTime
public Instant getCreationTime()
Description copied from interface:WebSessionReturn the time when the session was created.- Specified by:
getCreationTimein interfaceWebSession
getLastAccessTime
public Instant getLastAccessTime()
Description copied from interface:WebSessionReturn the last time of session access as a result of user activity such as an HTTP request. Together withmaxIdleTimeInSecondsthis helps to determine when a session isexpired.- Specified by:
getLastAccessTimein interfaceWebSession
setMaxIdleTime
public void setMaxIdleTime(Duration maxIdleTime)
Description copied from interface:WebSessionConfigure the max amount of time that may elapse after thelastAccessTimebefore a session is considered expired. A negative value indicates the session should not expire.- Specified by:
setMaxIdleTimein interfaceWebSession
getMaxIdleTime
public Duration getMaxIdleTime()
Description copied from interface:WebSessionReturn the maximum time after thelastAccessTimebefore a session expires. A negative time indicates the session doesn't expire.- Specified by:
getMaxIdleTimein interfaceWebSession