use super::TestHttpRequestBuilder;
#[derive(Clone)]
pub struct TestHttpApp {
router: axum::Router,
}
impl TestHttpApp {
#[must_use]
pub fn new(router: axum::Router) -> Self {
Self { 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")
}
pub async fn get(&self, uri: impl Into<String>) -> axum::response::Response {
self.request(TestHttpRequestBuilder::get(uri).build()).await
}
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)]
pub struct TestHttpFoundation {
settings: crate::http::HttpMiddlewareSettings,
}
impl Default for TestHttpFoundation {
fn default() -> Self {
Self::new()
}
}
impl TestHttpFoundation {
#[must_use]
pub fn new() -> Self {
Self {
settings: crate::http::HttpMiddlewareSettings::new(),
}
}
#[must_use]
pub fn from_settings(settings: crate::http::HttpMiddlewareSettings) -> Self {
Self { settings }
}
#[must_use]
pub fn with_settings(
mut self,
update: impl FnOnce(crate::http::HttpMiddlewareSettings) -> crate::http::HttpMiddlewareSettings,
) -> Self {
self.settings = update(self.settings);
self
}
#[must_use]
pub const fn settings(&self) -> &crate::http::HttpMiddlewareSettings {
&self.settings
}
pub fn validate(&self) -> Result<(), crate::settings::SettingsValidationError> {
self.settings.validate()
}
#[must_use]
pub fn assert_valid(self) -> Self {
self.validate()
.expect("test HTTP foundation settings should be valid");
self
}
#[must_use]
pub fn config(&self) -> crate::http::HttpMiddlewareConfig {
self.settings
.clone()
.build()
.expect("test HTTP foundation settings should adapt")
}
#[must_use]
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()
})
}
}