pub struct APIClient { /* private fields */ }Expand description
Test client for making API requests
§Example
use reinhardt_testkit::APIClient;
use http::StatusCode;
use serde_json::json;
let client = APIClient::with_base_url("http://localhost:8080");
let credentials = json!({"username": "user", "password": "pass"});
client.post("/auth/login", &credentials, "json").await?;
let response = client.get("/api/users/").await?;
assert_eq!(response.status(), StatusCode::OK);Implementations§
Source§impl APIClient
impl APIClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new API client
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();
assert_eq!(client.base_url(), "http://testserver");Sourcepub fn with_base_url(base_url: impl Into<String>) -> Self
pub fn with_base_url(base_url: impl Into<String>) -> Self
Create a client with a custom base URL
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::with_base_url("https://api.example.com");
assert_eq!(client.base_url(), "https://api.example.com");Sourcepub fn from_handler(handler: impl HttpHandler + 'static) -> Self
pub fn from_handler(handler: impl HttpHandler + 'static) -> Self
Create a test client that dispatches requests directly to a
reinhardt Handler without TCP.
The Handler runs the full middleware stack in-process.
Sets base_url to "http://testserver" and injects a default
Origin header for OriginGuardMiddleware compatibility.
§Panics
Panics if called outside a tokio runtime.
§Examples
use reinhardt_testkit::APIClient;
// let router = build_routes(scope).into_server();
// let client = APIClient::from_handler(router);
// let resp = client.get("/api/health/").await.unwrap();Sourcepub fn builder() -> APIClientBuilder
pub fn builder() -> APIClientBuilder
Create a builder for customizing the client configuration
§Examples
use reinhardt_testkit::client::APIClient;
use std::time::Duration;
let client = APIClient::builder()
.base_url("http://localhost:8080")
.timeout(Duration::from_secs(30))
.build();Sourcepub fn set_handler<F>(&mut self, handler: F)
pub fn set_handler<F>(&mut self, handler: F)
Set a request handler for testing
§Examples
use reinhardt_testkit::client::APIClient;
use http::{Request, Response, StatusCode};
use http_body_util::Full;
use bytes::Bytes;
let mut client = APIClient::new();
client.set_handler(|_req| {
Response::builder()
.status(StatusCode::OK)
.body(Full::new(Bytes::from("test")))
.unwrap()
});Sourcepub async fn set_header(
&self,
name: impl AsRef<str>,
value: impl AsRef<str>,
) -> ClientResult<()>
pub async fn set_header( &self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> ClientResult<()>
Set a default header for all requests
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();
client.set_header("User-Agent", "TestClient/1.0").await.unwrap();Sourcepub async fn force_authenticate(&self, user: Option<Value>)
👎Deprecated since 0.1.0-rc.16: use client.auth().session() or client.auth().jwt() instead
pub async fn force_authenticate(&self, user: Option<Value>)
use client.auth().session() or client.auth().jwt() instead
Force authenticate as a user (for testing)
§Examples
use reinhardt_testkit::client::APIClient;
use serde_json::json;
let client = APIClient::new();
let user = json!({"id": 1, "username": "testuser"});
client.force_authenticate(Some(user)).await;Sourcepub async fn credentials(
&self,
username: &str,
password: &str,
) -> ClientResult<()>
pub async fn credentials( &self, username: &str, password: &str, ) -> ClientResult<()>
Set credentials for Basic Authentication
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();
client.credentials("username", "password").await.unwrap();Sourcepub async fn clear_auth(&self) -> ClientResult<()>
pub async fn clear_auth(&self) -> ClientResult<()>
Clear authentication and cookies
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();
client.clear_auth().await.unwrap();Set a cookie that will be sent with subsequent requests.
§Panics
Panics if name contains = or ;, or if value contains ;.
Remove a specific cookie.
Sourcepub async fn logout(&self) -> ClientResult<()>
pub async fn logout(&self) -> ClientResult<()>
Clear all authentication state (session cookies, auth headers, stored user).
This is the replacement for force_authenticate(None).
Sourcepub fn auth(&self) -> AuthBuilder<'_>
pub fn auth(&self) -> AuthBuilder<'_>
Sourcepub async fn cleanup(&self)
pub async fn cleanup(&self)
Clean up all client state for teardown
This method performs a complete cleanup of the client state including:
- Clearing authentication
- Clearing cookies
- Clearing default headers
This is typically called during test teardown to ensure clean state between tests.
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();
client.set_header("X-Custom", "value").await.unwrap();
client.cleanup().await;
// All state is now clearedSourcepub async fn get(&self, path: &str) -> ClientResult<TestResponse>
pub async fn get(&self, path: &str) -> ClientResult<TestResponse>
Make a GET request
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();Sourcepub async fn post<T: Serialize>(
&self,
path: &str,
data: &T,
format: &str,
) -> ClientResult<TestResponse>
pub async fn post<T: Serialize>( &self, path: &str, data: &T, format: &str, ) -> ClientResult<TestResponse>
Make a POST request
§Examples
use reinhardt_testkit::client::APIClient;
use serde_json::json;
let client = APIClient::new();
let data = json!({"name": "test"});Sourcepub async fn put<T: Serialize>(
&self,
path: &str,
data: &T,
format: &str,
) -> ClientResult<TestResponse>
pub async fn put<T: Serialize>( &self, path: &str, data: &T, format: &str, ) -> ClientResult<TestResponse>
Make a PUT request
§Examples
use reinhardt_testkit::client::APIClient;
use serde_json::json;
let client = APIClient::new();
let data = json!({"name": "updated"});Sourcepub async fn patch<T: Serialize>(
&self,
path: &str,
data: &T,
format: &str,
) -> ClientResult<TestResponse>
pub async fn patch<T: Serialize>( &self, path: &str, data: &T, format: &str, ) -> ClientResult<TestResponse>
Make a PATCH request
§Examples
use reinhardt_testkit::client::APIClient;
use serde_json::json;
let client = APIClient::new();
let data = json!({"name": "partial_update"});Sourcepub async fn delete(&self, path: &str) -> ClientResult<TestResponse>
pub async fn delete(&self, path: &str) -> ClientResult<TestResponse>
Make a DELETE request
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();Sourcepub async fn head(&self, path: &str) -> ClientResult<TestResponse>
pub async fn head(&self, path: &str) -> ClientResult<TestResponse>
Make a HEAD request
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();Sourcepub async fn options(&self, path: &str) -> ClientResult<TestResponse>
pub async fn options(&self, path: &str) -> ClientResult<TestResponse>
Make an OPTIONS request
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::new();Sourcepub async fn get_with_headers(
&self,
path: &str,
headers: &[(&str, &str)],
) -> ClientResult<TestResponse>
pub async fn get_with_headers( &self, path: &str, headers: &[(&str, &str)], ) -> ClientResult<TestResponse>
Make a GET request with additional per-request headers
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::with_base_url("http://localhost:8080");
// let response = client.get_with_headers("/api/data", &[("Accept", "application/json")]).await;Sourcepub async fn post_raw_with_headers(
&self,
path: &str,
body: &[u8],
content_type: &str,
headers: &[(&str, &str)],
) -> ClientResult<TestResponse>
pub async fn post_raw_with_headers( &self, path: &str, body: &[u8], content_type: &str, headers: &[(&str, &str)], ) -> ClientResult<TestResponse>
Make a POST request with raw body and additional per-request headers
Unlike post(), this method allows setting a raw body without automatic serialization.
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::with_base_url("http://localhost:8080");
// let response = client.post_raw_with_headers(
// "/api/echo",
// b"{\"test\":\"data\"}",
// "application/json",
// &[("X-Custom-Header", "value")]
// ).await;Sourcepub async fn post_raw(
&self,
path: &str,
body: &[u8],
content_type: &str,
) -> ClientResult<TestResponse>
pub async fn post_raw( &self, path: &str, body: &[u8], content_type: &str, ) -> ClientResult<TestResponse>
Make a POST request with raw body
Unlike post(), this method allows setting a raw body without automatic serialization.
§Examples
use reinhardt_testkit::client::APIClient;
let client = APIClient::with_base_url("http://localhost:8080");
// let response = client.post_raw("/api/echo", b"{\"test\":\"data\"}", "application/json").await;Trait Implementations§
Auto Trait Implementations§
impl Freeze for APIClient
impl !RefUnwindSafe for APIClient
impl Send for APIClient
impl Sync for APIClient
impl Unpin for APIClient
impl UnsafeUnpin for APIClient
impl !UnwindSafe for APIClient
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 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().