Class MockHttpSession

java.lang.Object
com.vaadin.browserless.mocks.MockHttpSession
All Implemented Interfaces:
jakarta.servlet.http.HttpSession, Serializable

public class MockHttpSession extends Object implements jakarta.servlet.http.HttpSession, Serializable
A standalone HttpSession, backed by an in-memory attribute map.

Attribute storage, invalidation and HttpServletRequest.changeSessionId() behave the way a servlet container implements them, including the failure modes: the attribute accessors, the timestamps, isNew() and invalidate() throw IllegalStateException once the session has been invalidated. Unlike in a container, getId(), getServletContext() and the max inactive interval stay readable. Ids come from a sequential counter rather than being random, so test output stays readable.

What is not modeled: the session is never expired on its own, so getMaxInactiveInterval() is stored and reported but never acted on, and getLastAccessedTime() always reports 0. There is no request loop to update it.

For internal use only. May be renamed or removed in a future release.

See Also:
  • Constructor Details

    • MockHttpSession

      public MockHttpSession(String sessionId, jakarta.servlet.ServletContext servletContext, long creationTime, int maxInactiveInterval)
      Creates a session with the given id and settings.
      Parameters:
      sessionId - the session id
      servletContext - the context this session belongs to
      creationTime - the creation timestamp reported by getCreationTime()
      maxInactiveInterval - the inactive interval, in seconds; stored and reported, never acted on
    • MockHttpSession

      public MockHttpSession(jakarta.servlet.http.HttpSession session)
      Creates a copy of the given session, carrying over its id, context, settings and all of its attributes.
      Parameters:
      session - the session to copy
  • Method Details

    • isValid

      public boolean isValid()
      Tells whether this session is still usable, i.e. has not been invalidated.
      Returns:
      true while the session is valid
    • destroy

      public void destroy()
      Drops all attributes, leaving the session itself valid.
    • getCreationTime

      public long getCreationTime()
      Specified by:
      getCreationTime in interface jakarta.servlet.http.HttpSession
    • getId

      public String getId()
      Specified by:
      getId in interface jakarta.servlet.http.HttpSession
    • getLastAccessedTime

      public long getLastAccessedTime()
      Specified by:
      getLastAccessedTime in interface jakarta.servlet.http.HttpSession
    • getServletContext

      public jakarta.servlet.ServletContext getServletContext()
      Specified by:
      getServletContext in interface jakarta.servlet.http.HttpSession
    • setMaxInactiveInterval

      public void setMaxInactiveInterval(int interval)
      Specified by:
      setMaxInactiveInterval in interface jakarta.servlet.http.HttpSession
    • getMaxInactiveInterval

      public int getMaxInactiveInterval()
      Specified by:
      getMaxInactiveInterval in interface jakarta.servlet.http.HttpSession
    • getAttribute

      public Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.http.HttpSession
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.http.HttpSession
    • setAttribute

      public void setAttribute(String name, Object value)
      Specified by:
      setAttribute in interface jakarta.servlet.http.HttpSession
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.http.HttpSession
    • copyAttributes

      public MockHttpSession copyAttributes(jakarta.servlet.http.HttpSession httpSession)
      Copies all attributes of the given session into this one, keeping the attributes already present.
      Parameters:
      httpSession - the session to copy the attributes from
      Returns:
      this session
    • invalidate

      public void invalidate()
      Specified by:
      invalidate in interface jakarta.servlet.http.HttpSession
    • isNew

      public boolean isNew()
      Specified by:
      isNew in interface jakarta.servlet.http.HttpSession
    • changeSessionId

      public String changeSessionId()
      Assigns a new ID to this session, keeping its identity and all of its attributes intact, and returns the new ID.

      This mirrors what a servlet container does for HttpServletRequest.changeSessionId(): apps which implement session-fixation protection themselves (vanilla and Java EE apps, i.e. those not relying on Spring Security) call it after a successful login.

      Returns:
      the new session id
      Throws:
      IllegalStateException - if the session has been invalidated
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • create

      public static MockHttpSession create(jakarta.servlet.ServletContext ctx)
      Creates a session in the given context, with a fresh sequential id and a 30 second inactive interval.
      Parameters:
      ctx - the context the session belongs to
      Returns:
      the new session