footing 0.1.13

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
    }
}

#[derive(Debug, Clone)]
/// Represents test HTTP foundation.
pub struct TestHttpFoundation {
    settings: crate::http::HttpMiddlewareSettings,
}

impl Default for TestHttpFoundation {
    fn default() -> Self {
        Self::new()
    }
}

impl TestHttpFoundation {
    #[must_use]
    /// Creates a new instance.
    pub fn new() -> Self {
        Self {
            settings: crate::http::HttpMiddlewareSettings::new(),
        }
    }

    #[must_use]
    /// Creates a value from settings.
    pub fn from_settings(settings: crate::http::HttpMiddlewareSettings) -> Self {
        Self { settings }
    }

    #[must_use]
    /// Configures settings and returns the updated value.
    pub fn with_settings(
        mut self,
        update: impl FnOnce(crate::http::HttpMiddlewareSettings) -> crate::http::HttpMiddlewareSettings,
    ) -> Self {
        self.settings = update(self.settings);
        self
    }

    #[must_use]
    /// Returns the settings.
    pub const fn settings(&self) -> &crate::http::HttpMiddlewareSettings {
        &self.settings
    }

    /// Validates this value.
    pub fn validate(&self) -> Result<(), crate::settings::SettingsValidationError> {
        self.settings.validate()
    }

    #[must_use]
    /// Validates the settings and panics with test context on failure.
    pub fn assert_valid(self) -> Self {
        self.validate()
            .expect("test HTTP foundation settings should be valid");
        self
    }

    #[must_use]
    /// Returns the config.
    pub fn config(&self) -> crate::http::HttpMiddlewareConfig {
        self.settings
            .clone()
            .build()
            .expect("test HTTP foundation settings should adapt")
    }

    #[must_use]
    /// Returns the invalid paths.
    pub fn invalid_paths(&self, path: &str) -> Vec<String> {
        self.settings
            .validate_at(path)
            .map(|()| Vec::new())
            .unwrap_or_else(|error| {
                error
                    .issues()
                    .iter()
                    .map(|issue| issue.path().to_owned())
                    .collect()
            })
    }
}