use armature_core::{Application, Container, Module, Provider, Router};
use std::sync::Arc;
pub struct TestApp {
pub app: Application,
pub container: Arc<Container>,
}
impl TestApp {
pub fn new(container: Container, router: Router) -> Self {
let container_arc = Arc::new(container);
let app = Application::new(Container::new(), router);
Self {
app,
container: container_arc,
}
}
pub fn get<T: Provider + Clone + 'static>(&self) -> Option<T> {
None
}
pub fn client(&self) -> crate::TestClient {
crate::TestClient::new(self.app.router.clone())
}
}
pub struct TestAppBuilder {
container: Container,
router: Router,
}
impl TestAppBuilder {
pub fn new() -> Self {
Self {
container: Container::new(),
router: Router::new(),
}
}
pub fn register<T: Provider + Clone + 'static>(self, provider: T) -> Self {
self.container.register(provider);
self
}
pub fn add_module<M: Module>(self, _module: M) -> Self {
self
}
pub fn with_container(self, container: Container) -> Self {
Self { container, ..self }
}
pub fn with_router(self, router: Router) -> Self {
Self { router, ..self }
}
pub fn with_route<F, Fut>(mut self, path: &str, handler: F) -> Self
where
F: Fn(armature_core::HttpRequest) -> Fut + Send + Sync + Clone + 'static,
Fut: std::future::Future<Output = Result<armature_core::HttpResponse, armature_core::Error>>
+ Send
+ 'static,
{
use armature_core::{HttpMethod, Route};
self.router
.add_route(Route::new(HttpMethod::GET, path, handler));
self
}
pub fn build(self) -> TestApp {
TestApp::new(self.container, self.router)
}
}
impl Default for TestAppBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builder_creation() {
let _builder = TestAppBuilder::new();
}
#[test]
fn test_app_creation() {
let builder = TestAppBuilder::new();
let _app = builder.build();
}
}