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
impl TestClient
Sourcepub fn new(router: Router, session_pool: &AnyPool) -> Self
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.
pub async fn get(&mut self, path: &str) -> TestResponse
Sourcepub async fn post_form(
&mut self,
path: &str,
form: &[(&str, &str)],
) -> TestResponse
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.
Sourcepub async fn post_multipart(
&mut self,
path: &str,
csrf_token: &str,
filename: &str,
content_type: &str,
bytes: &[u8],
) -> TestResponse
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.
Sourcepub async fn post_json<T: Serialize>(
&mut self,
path: &str,
csrf_token: &str,
body: &T,
) -> TestResponse
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.
Sourcepub async fn acting_as<U: Authenticatable>(
&mut self,
user: &U,
) -> Result<(), AppError>
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§
impl !RefUnwindSafe for TestClient
impl !UnwindSafe for TestClient
impl Forget for TestClient
impl Freeze for TestClient
impl Move for TestClient
impl Send for TestClient
impl Sync for TestClient
impl Unpin for TestClient
impl UnsafeUnpin for TestClient
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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