pub struct MockSession {
pub id: String,
pub user: Option<TestUser>,
pub data: HashMap<String, Value>,
pub csrf_token: String,
pub created_at: i64,
pub expires_at: Option<i64>,
pub invalidated: bool,
}Expand description
A mock session for testing session-based functionality.
This simulates a server-side session that can store user information and arbitrary session data.
§Example
// Anonymous session
let anon_session = MockSession::anonymous();
// Authenticated session
let auth_session = MockSession::authenticated(TestUser::authenticated("alice"));
// Session with custom data
let session = MockSession::anonymous()
.with_data("cart_id", serde_json::json!("abc123"))
.with_data("theme", serde_json::json!("dark"));Fields§
§id: StringSession ID.
user: Option<TestUser>The authenticated user, if any.
data: HashMap<String, Value>Session data storage.
csrf_token: StringCSRF token for the session.
created_at: i64Session creation timestamp (Unix epoch seconds).
expires_at: Option<i64>Session expiration timestamp (Unix epoch seconds).
invalidated: boolWhether the session has been invalidated.
Implementations§
Source§impl MockSession
impl MockSession
Sourcepub fn authenticated(user: TestUser) -> Self
pub fn authenticated(user: TestUser) -> Self
Create an authenticated session with the given user.
Sourcepub fn from_identity(identity: &SessionIdentity) -> Self
pub fn from_identity(identity: &SessionIdentity) -> Self
Create an authenticated session from a SessionIdentity.
Bridges the shared identity type to the server_fn test context.
Creates a stub TestUser internally to maintain is_authenticated() compatibility.
Sourcepub fn with_data(self, key: impl Into<String>, value: impl Into<Value>) -> Self
pub fn with_data(self, key: impl Into<String>, value: impl Into<Value>) -> Self
Add session data.
Sourcepub fn with_csrf_token(self, token: impl Into<String>) -> Self
pub fn with_csrf_token(self, token: impl Into<String>) -> Self
Set the CSRF token.
Sourcepub fn with_expiration(self, expires_at: i64) -> Self
pub fn with_expiration(self, expires_at: i64) -> Self
Set the session expiration.
Sourcepub fn expires_in(self, seconds: i64) -> Self
pub fn expires_in(self, seconds: i64) -> Self
Set session to expire after the given duration in seconds.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Check if the session is authenticated.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Check if the session has expired.
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Check if the session is valid (not invalidated and not expired).
Sourcepub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>
pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T>
Get session data by key.
Sourcepub fn clear_data(&mut self)
pub fn clear_data(&mut self)
Clear all session data (but keep user).
Sourcepub fn invalidate(&mut self)
pub fn invalidate(&mut self)
Invalidate the session.
Sourcepub fn regenerate_id(&mut self)
pub fn regenerate_id(&mut self)
Regenerate the session ID (for security after login).
Sourcepub fn regenerate_csrf(&mut self)
pub fn regenerate_csrf(&mut self)
Regenerate the CSRF token.
Sourcepub fn verify_csrf(&self, token: &str) -> bool
pub fn verify_csrf(&self, token: &str) -> bool
Verify a CSRF token.
Trait Implementations§
Source§impl Clone for MockSession
impl Clone for MockSession
Source§fn clone(&self) -> MockSession
fn clone(&self) -> MockSession
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MockSession
impl Debug for MockSession
Source§impl Default for MockSession
impl Default for MockSession
Source§fn default() -> MockSession
fn default() -> MockSession
Source§impl<'de> Deserialize<'de> for MockSession
impl<'de> Deserialize<'de> for MockSession
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for MockSession
impl RefUnwindSafe for MockSession
impl Send for MockSession
impl Sync for MockSession
impl Unpin for MockSession
impl UnsafeUnpin for MockSession
impl UnwindSafe for MockSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().