Skip to main content

TestClient

Struct TestClient 

Source
pub struct TestClient { /* private fields */ }
Expand description

Drives a real axum::Router in-process (via tower::ServiceExt::oneshot - no TCP binding) and automatically threads the session cookie between requests, eliminating the boilerplate every hand-rolled test in this codebase repeats: building a Request by hand, extracting Set-Cookie, and re-attaching it to every subsequent call.

Every request method takes &mut self - a TestClient is one sequential conversation, not something to drive concurrently (its cookie is “whatever the last response set”). A second concurrent actor in one test is just a second TestClient::new(router.clone(), &pool) - axum::Router::clone() is cheap (Arc-backed), so this isn’t wasteful.

Implementations§

Source§

impl TestClient

Source

pub fn new(router: Router, session_pool: &AnyPool) -> Self

router must already have a session layer installed (e.g. via larust_http::Router::with_sessions(session_pool, ..), built from the same pool passed here) if any route needs sessions/CSRF/auth. session_store resolves to whichever backend Config:: session_driver selects (larust_http::session::resolve_backend, the same function with_sessions itself uses) - so acting_as below writes through the identical backend the router’s own session layer reads from, database- or file-backed alike.

Source

pub async fn get(&mut self, path: &str) -> TestResponse

Source

pub async fn post_form( &mut self, path: &str, form: &[(&str, &str)], ) -> TestResponse

Sends a application/x-www-form-urlencoded POST - the shape every Blade-rendered form in a Larust app submits.

Source

pub async fn post_multipart( &mut self, path: &str, csrf_token: &str, filename: &str, content_type: &str, bytes: &[u8], ) -> TestResponse

Sends a single-file multipart/form-data POST - for routes using axum’s Multipart extractor (real file uploads, e.g. UploadController::store). The field name is fixed and unconditional since every handler in this codebase that reads multipart data takes whatever the first field is (multipart.next_field()), the same way UploadController::store does - there’s nothing to name it for yet.

csrf_token goes in the X-CSRF-TOKEN header, not a form field - larust_http::csrf::verify checks that header before touching the body at all specifically so a multipart body never gets misread as application/x-www-form-urlencoded (see that function’s own doc comment). Get a token via TestResponse::csrf_token() on any rendered page first, same as post_form-based tests already do.

Source

pub async fn post_json<T: Serialize>( &mut self, path: &str, csrf_token: &str, body: &T, ) -> TestResponse

Sends a JSON POST with the CSRF token in the X-CSRF-TOKEN header - same “CSRF via header, not a form field” pattern as post_multipart, for routes consumed by fetch()/XMLHttpRequest rather than a plain <form> submission (e.g. larust_support::wire::update, and any other JS-driven endpoint). Get a token via TestResponse::csrf_token() on any rendered page first, same as post_form/post_multipart-based tests already do.

Source

pub async fn acting_as<U: Authenticatable>( &mut self, user: &U, ) -> Result<(), AppError>

Laravel’s actingAs($user): logs user in against this client’s own session store (resolve_backend - the same underlying pool/table, or the same directory, the router’s own session layer uses, so this handle behaves identically to the router’s) and adopts the resulting cookie for every request this client sends from here on - without needing a working /login route to exist in router at all. Calling this again with a different user switches identity mid-test, cleanly replacing the previously adopted cookie.

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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