footing 0.5.1

Feature-gated foundations for Rust backend services
Documentation
use super::TestHttpRequestBuilder;

#[derive(Clone)]
/// Represents test HTTP app.
pub struct TestHttpApp {
    router: axum::Router,
}

impl TestHttpApp {
    #[must_use]
    /// Creates a new instance.
    pub fn new(router: axum::Router) -> Self {
        Self { router }
    }

    /// Executes a request against the in-memory router.
    pub async fn request(
        &self,
        request: axum::http::Request<axum::body::Body>,
    ) -> axum::response::Response {
        use tower::ServiceExt;

        self.router
            .clone()
            .oneshot(request)
            .await
            .expect("test HTTP request should complete")
    }

    /// Executes a GET request against the in-memory router.
    pub async fn get(&self, uri: impl Into<String>) -> axum::response::Response {
        self.request(TestHttpRequestBuilder::get(uri).build()).await
    }

    /// Executes a JSON POST request against the in-memory router.
    pub async fn post_json(
        &self,
        uri: impl Into<String>,
        value: &serde_json::Value,
    ) -> axum::response::Response {
        self.request(TestHttpRequestBuilder::post(uri).json_body(value).build())
            .await
    }
}