Skip to main content

ServerFnTestContext

Struct ServerFnTestContext 

Source
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

Source

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.
Source

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)
Source

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
Source

pub fn with_authenticated_user(self, user: TestUser) -> Self

👎Deprecated since 0.1.0-rc.16:

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();
Source

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
Source

pub fn with_roles<S: Into<String>>(self, roles: Vec<S>) -> Self

Set roles for the authenticated user.

§Arguments
  • roles - List of role strings to assign
Source

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
Source

pub fn with_request_headers(self, headers: HeaderMap) -> Self

Add request headers to the context.

§Arguments
  • headers - Headers to add
Source

pub fn with_header(self, name: &str, value: &str) -> Self

Add a single request header.

§Arguments
  • name - Header name
  • value - Header value
Source

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
Source

pub fn with_transaction_mode(self, mode: TransactionMode) -> Self

Set the transaction mode for database operations.

§Arguments
  • mode - The transaction mode
Source

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).

Source

pub fn with_session(self, session: MockSession) -> Self

Set a mock session directly.

§Arguments
  • session - The mock session
Source

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();
Source

pub fn with_mock_session(self) -> Self

Add a mock session with default configuration.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more