Class MockHttpSession
- All Implemented Interfaces:
jakarta.servlet.http.HttpSession,Serializable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface jakarta.servlet.http.HttpSession
jakarta.servlet.http.HttpSession.Accessor -
Constructor Summary
ConstructorsConstructorDescriptionMockHttpSession(jakarta.servlet.http.HttpSession session) Creates a copy of the given session, carrying over its id, context, settings and all of its attributes.MockHttpSession(String sessionId, jakarta.servlet.ServletContext servletContext, long creationTime, int maxInactiveInterval) Creates a session with the given id and settings. -
Method Summary
Modifier and TypeMethodDescriptionAssigns a new ID to this session, keeping its identity and all of its attributes intact, and returns the new ID.copyAttributes(jakarta.servlet.http.HttpSession httpSession) Copies all attributes of the given session into this one, keeping the attributes already present.static MockHttpSessioncreate(jakarta.servlet.ServletContext ctx) Creates a session in the given context, with a fresh sequential id and a 30 second inactive interval.voiddestroy()Drops all attributes, leaving the session itself valid.getAttribute(String name) longgetId()longintjakarta.servlet.ServletContextvoidbooleanisNew()booleanisValid()Tells whether this session is still usable, i.e. has not beeninvalidated.voidremoveAttribute(String name) voidsetAttribute(String name, Object value) voidsetMaxInactiveInterval(int interval) toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface jakarta.servlet.http.HttpSession
getAccessor
-
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 idservletContext- the context this session belongs tocreationTime- the creation timestamp reported bygetCreationTime()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 beeninvalidated.- Returns:
truewhile the session is valid
-
destroy
public void destroy()Drops all attributes, leaving the session itself valid. -
getCreationTime
public long getCreationTime()- Specified by:
getCreationTimein interfacejakarta.servlet.http.HttpSession
-
getId
- Specified by:
getIdin interfacejakarta.servlet.http.HttpSession
-
getLastAccessedTime
public long getLastAccessedTime()- Specified by:
getLastAccessedTimein interfacejakarta.servlet.http.HttpSession
-
getServletContext
public jakarta.servlet.ServletContext getServletContext()- Specified by:
getServletContextin interfacejakarta.servlet.http.HttpSession
-
setMaxInactiveInterval
public void setMaxInactiveInterval(int interval) - Specified by:
setMaxInactiveIntervalin interfacejakarta.servlet.http.HttpSession
-
getMaxInactiveInterval
public int getMaxInactiveInterval()- Specified by:
getMaxInactiveIntervalin interfacejakarta.servlet.http.HttpSession
-
getAttribute
- Specified by:
getAttributein interfacejakarta.servlet.http.HttpSession
-
getAttributeNames
- Specified by:
getAttributeNamesin interfacejakarta.servlet.http.HttpSession
-
setAttribute
- Specified by:
setAttributein interfacejakarta.servlet.http.HttpSession
-
removeAttribute
- Specified by:
removeAttributein interfacejakarta.servlet.http.HttpSession
-
copyAttributes
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:
invalidatein interfacejakarta.servlet.http.HttpSession
-
isNew
public boolean isNew()- Specified by:
isNewin interfacejakarta.servlet.http.HttpSession
-
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
-
create
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
-