pub struct ServerFnTestContext { /* private fields */ }Expand description
Enhanced test context builder for server function testing.
This builder extends the basic ServerFnTestContext with:
- Authentication/authorization mocking
- HTTP request/response simulation
- Transaction management
- CSRF token handling
§Example
let ctx = ServerFnTestContext::new(singleton_scope)
.with_authenticated_user(TestUser::authenticated("alice"))
.with_permissions(vec!["read", "write"])
.with_csrf_token("test-token")
.build();Implementations§
Source§impl ServerFnTestContext
impl ServerFnTestContext
Sourcepub fn new(singleton_scope: Arc<SingletonScope>) -> Self
pub fn new(singleton_scope: Arc<SingletonScope>) -> Self
Create a new server function test context builder.
§Arguments
singleton_scope- The singleton scope for dependency resolution.
Sourcepub fn with_database<P: Clone + Send + Sync + 'static>(self, pool: P) -> Self
pub fn with_database<P: Clone + Send + Sync + 'static>(self, pool: P) -> Self
Add a database connection override to the test context.
§Arguments
pool- The database connection pool (typically from TestContainers)
Sourcepub fn with_singleton<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
pub fn with_singleton<T: Clone + Send + Sync + 'static>(self, value: T) -> Self
Add a custom singleton dependency to the test context.
§Arguments
value- The singleton value to register
Sourcepub fn with_authenticated_user(self, user: TestUser) -> Self
👎Deprecated since 0.1.0-rc.16: use .auth().session(&user).done() instead
pub fn with_authenticated_user(self, user: TestUser) -> Self
use .auth().session(&user).done() instead
Set the authenticated user for this test.
This configures the test context to simulate an authenticated user, allowing you to test protected endpoints.
§Arguments
user- The test user to authenticate as
§Example
let ctx = ServerFnTestContext::new(singleton)
.with_authenticated_user(TestUser::admin())
.build();Sourcepub fn with_permissions<S: Into<String>>(self, permissions: Vec<S>) -> Self
pub fn with_permissions<S: Into<String>>(self, permissions: Vec<S>) -> Self
Set permissions for the authenticated user.
This is a convenience method that modifies the test user’s permissions.
§Arguments
permissions- List of permission strings to grant
Sourcepub fn with_roles<S: Into<String>>(self, roles: Vec<S>) -> Self
pub fn with_roles<S: Into<String>>(self, roles: Vec<S>) -> Self
Sourcepub fn with_request(self, request: MockHttpRequest) -> Self
pub fn with_request(self, request: MockHttpRequest) -> Self
Set a mock HTTP request for the context.
This is useful for testing server functions that access request data like headers, cookies, or body.
§Arguments
request- The mock HTTP request
Sourcepub fn with_request_headers(self, headers: HeaderMap) -> Self
pub fn with_request_headers(self, headers: HeaderMap) -> Self
Sourcepub fn with_header(self, name: &str, value: &str) -> Self
pub fn with_header(self, name: &str, value: &str) -> Self
Sourcepub fn with_csrf_token(self, token: &str) -> Self
pub fn with_csrf_token(self, token: &str) -> Self
Set a CSRF token for the request.
This automatically adds the token to both headers and session.
§Arguments
token- The CSRF token string
Sourcepub fn with_transaction_mode(self, mode: TransactionMode) -> Self
pub fn with_transaction_mode(self, mode: TransactionMode) -> Self
Sourcepub fn with_transaction_rollback(self) -> Self
pub fn with_transaction_rollback(self) -> Self
Enable automatic transaction rollback after the test.
This is a convenience method for with_transaction_mode(TransactionMode::Rollback).
Sourcepub fn with_session(self, session: MockSession) -> Self
pub fn with_session(self, session: MockSession) -> Self
Sourcepub fn auth(self) -> ServerFnAuthBuilder
pub fn auth(self) -> ServerFnAuthBuilder
Start building auth configuration for this server_fn test context.
Mirrors crate::client::APIClient::auth() API for consistent developer experience.
§Examples
let env = ServerFnTestContext::new(scope.clone())
.with_database(pool.clone())
.auth()
.session(&user)
.with_staff(true)
.done()
.build();Sourcepub fn with_mock_session(self) -> Self
pub fn with_mock_session(self) -> Self
Add a mock session with default configuration.
Sourcepub fn build(self) -> ServerFnTestEnv
pub fn build(self) -> ServerFnTestEnv
Build the test environment with all configured options.
Returns a ServerFnTestEnv containing the injection context and
any additional test state.
Sourcepub fn build_context(self) -> InjectionContext
pub fn build_context(self) -> InjectionContext
Build and return just the injection context.
This is a convenience method when you don’t need the full test environment.
Auto Trait Implementations§
impl !Freeze for ServerFnTestContext
impl !RefUnwindSafe for ServerFnTestContext
impl Send for ServerFnTestContext
impl !Sync for ServerFnTestContext
impl Unpin for ServerFnTestContext
impl UnsafeUnpin for ServerFnTestContext
impl !UnwindSafe for ServerFnTestContext
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> 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 more