org.springframework.mock.web.server

Class MockWebSession

  • java.lang.Object
    • org.springframework.mock.web.server.MockWebSession
  • All Implemented Interfaces:
    WebSession


    public class MockWebSession
    extends java.lang.Object
    implements WebSession
    Implementation of WebSession that delegates to a session instance obtained via InMemoryWebSessionStore.

    This is intended for use with the session(WebSession) method of the MockServerWebExchange builder, eliminating the need to use WebSessionManager or WebSessionStore altogether.

    Since:
    5.1
    Author:
    Rossen Stoyanchev
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      reactor.core.publisher.Mono<java.lang.Void> changeSessionId()
      Generate a new id for the session and update the underlying session storage to reflect the new id.
      java.util.Map<java.lang.String,java.lang.Object> getAttributes()
      Return a map that holds session attributes.
      java.time.Instant getCreationTime()
      Return the time when the session was created.
      java.lang.String getId()
      Return a unique session identifier.
      java.time.Instant getLastAccessTime()
      Return the last time of session access as a result of user activity such as an HTTP request.
      java.time.Duration getMaxIdleTime()
      Return the maximum time after the lastAccessTime before a session expires.
      reactor.core.publisher.Mono<java.lang.Void> invalidate()
      Invalidate the current session and clear session storage.
      boolean isExpired()
      Return true if the session expired after maxIdleTime elapsed.
      boolean isStarted()
      Whether a session with the client has been started explicitly via WebSession.start() or implicitly by adding session attributes.
      reactor.core.publisher.Mono<java.lang.Void> save()
      Save the session through the WebSessionStore as follows: If the session is new (i.e.
      void setMaxIdleTime(java.time.Duration maxIdleTime)
      Configure the max amount of time that may elapse after the lastAccessTime before a session is considered expired.
      void start()
      Force the creation of a session causing the session id to be sent when WebSession.save() is called.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MockWebSession

        public MockWebSession()
      • MockWebSession

        public MockWebSession(@Nullable
                              java.time.Clock clock)
    • Method Detail

      • getId

        public java.lang.String getId()
        Description copied from interface: WebSession
        Return a unique session identifier.
        Specified by:
        getId in interface WebSession
      • getAttributes

        public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
        Description copied from interface: WebSession
        Return a map that holds session attributes.
        Specified by:
        getAttributes in interface WebSession
      • start

        public void start()
        Description copied from interface: WebSession
        Force the creation of a session causing the session id to be sent when WebSession.save() is called.
        Specified by:
        start in interface WebSession
      • isStarted

        public boolean isStarted()
        Description copied from interface: WebSession
        Whether a session with the client has been started explicitly via WebSession.start() or implicitly by adding session attributes. If "false" then the session id is not sent to the client and the WebSession.save() method is essentially a no-op.
        Specified by:
        isStarted in interface WebSession
      • changeSessionId

        public reactor.core.publisher.Mono<java.lang.Void> changeSessionId()
        Description copied from interface: WebSession
        Generate a new id for the session and update the underlying session storage to reflect the new id. After a successful call WebSession.getId() reflects the new session id.
        Specified by:
        changeSessionId in interface WebSession
        Returns:
        completion notification (success or error)
      • invalidate

        public reactor.core.publisher.Mono<java.lang.Void> invalidate()
        Description copied from interface: WebSession
        Invalidate the current session and clear session storage.
        Specified by:
        invalidate in interface WebSession
        Returns:
        completion notification (success or error)
      • save

        public reactor.core.publisher.Mono<java.lang.Void> save()
        Description copied from interface: WebSession
        Save the session through the WebSessionStore as 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 was invalidated and 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:
        save in interface WebSession
        Returns:
        Mono to indicate completion with success or error
      • isExpired

        public boolean isExpired()
        Description copied from interface: WebSession
        Return true if the session expired after maxIdleTime elapsed.

        Typically expiration checks should be automatically made when a session is accessed, a new WebSession instance created if necessary, at the start of request processing so that applications don't have to worry about expired session by default.

        Specified by:
        isExpired in interface WebSession
      • getCreationTime

        public java.time.Instant getCreationTime()
        Description copied from interface: WebSession
        Return the time when the session was created.
        Specified by:
        getCreationTime in interface WebSession
      • getLastAccessTime

        public java.time.Instant getLastAccessTime()
        Description copied from interface: WebSession
        Return the last time of session access as a result of user activity such as an HTTP request. Together with maxIdleTimeInSeconds this helps to determine when a session is expired.
        Specified by:
        getLastAccessTime in interface WebSession
      • setMaxIdleTime

        public void setMaxIdleTime(java.time.Duration maxIdleTime)
        Description copied from interface: WebSession
        Configure the max amount of time that may elapse after the lastAccessTime before a session is considered expired. A negative value indicates the session should not expire.
        Specified by:
        setMaxIdleTime in interface WebSession
      • getMaxIdleTime

        public java.time.Duration getMaxIdleTime()
        Description copied from interface: WebSession
        Return the maximum time after the lastAccessTime before a session expires. A negative time indicates the session doesn't expire.
        Specified by:
        getMaxIdleTime in interface WebSession