pub struct TestApp { /* private fields */ }Expand description
In-memory test application backed by an Axum router.
TestApp drives requests through the router with Tower’s in-memory service
path, so no TCP listener is started. Use it for handler, middleware, module,
and provider integration tests.
use axum::{Json, Router, routing::get};
use http::StatusCode;
use nidus_testing::TestApp;
use serde_json::json;
async fn health() -> Json<serde_json::Value> {
Json(json!({ "ok": true }))
}
#[tokio::test]
async fn health_returns_json() {
let app = TestApp::from_router(
Router::new().route("/health", get(health)),
);
let response = app.get("/health").send().await;
response.assert_status(StatusCode::OK);
response.assert_json(json!({ "ok": true }));
}Implementations§
Source§impl TestApp
impl TestApp
Sourcepub fn bootstrap<M>() -> Result<TestAppBuilder>where
M: Module,
pub fn bootstrap<M>() -> Result<TestAppBuilder>where
M: Module,
Creates a test application builder after validating a root Nidus module.
Sourcepub fn bootstrap_with_router<M>(router: Router) -> Result<TestAppBuilder>where
M: Module,
pub fn bootstrap_with_router<M>(router: Router) -> Result<TestAppBuilder>where
M: Module,
Creates a test application builder with a router after validating a root Nidus module.
Sourcepub fn bootstrap_with_modules<M, I>(modules: I) -> Result<TestAppBuilder>
pub fn bootstrap_with_modules<M, I>(modules: I) -> Result<TestAppBuilder>
Creates a test application builder after validating an explicit module graph.
Sourcepub fn bootstrap_with_modules_and_router<M, I>(
modules: I,
router: Router,
) -> Result<TestAppBuilder>
pub fn bootstrap_with_modules_and_router<M, I>( modules: I, router: Router, ) -> Result<TestAppBuilder>
Creates a test application builder with a router after validating an explicit module graph.
Sourcepub fn from_router(router: Router) -> Self
pub fn from_router(router: Router) -> Self
Creates a test application from an Axum router.
This is the shortest path for HTTP-only tests. It installs an empty Nidus container extension so handlers that need the container can still extract it.
Sourcepub fn builder(router: Router) -> TestAppBuilder
pub fn builder(router: Router) -> TestAppBuilder
Creates a configurable test application builder.
Sourcepub fn get(&self, path: impl Into<String>) -> TestRequest
pub fn get(&self, path: impl Into<String>) -> TestRequest
Starts a GET request.
Sourcepub fn post(&self, path: impl Into<String>) -> TestRequest
pub fn post(&self, path: impl Into<String>) -> TestRequest
Starts a POST request.
Sourcepub fn put(&self, path: impl Into<String>) -> TestRequest
pub fn put(&self, path: impl Into<String>) -> TestRequest
Starts a PUT request.
Sourcepub fn patch(&self, path: impl Into<String>) -> TestRequest
pub fn patch(&self, path: impl Into<String>) -> TestRequest
Starts a PATCH request.
Sourcepub fn delete(&self, path: impl Into<String>) -> TestRequest
pub fn delete(&self, path: impl Into<String>) -> TestRequest
Starts a DELETE request.
Sourcepub fn request(&self, method: Method, path: impl Into<String>) -> TestRequest
pub fn request(&self, method: Method, path: impl Into<String>) -> TestRequest
Starts a request with an arbitrary HTTP method.
The returned TestRequest can set headers, query parameters, and body
content before TestRequest::send executes it against the in-memory
router.
Sourcepub fn request_scope(&self) -> RequestScope<'_>
pub fn request_scope(&self) -> RequestScope<'_>
Creates a request scope for resolving request-lifetime providers in tests.