Skip to main content

oxidite_testing/
server.rs

1use oxidite_core::{Router, OxiditeRequest, OxiditeResponse, Result};
2use tower::Service;
3use std::future::Future;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7/// Test server for integration testing
8pub struct TestServer<S> {
9    service: S,
10}
11
12impl<S> TestServer<S>
13where
14    S: Service<OxiditeRequest, Response = OxiditeResponse> + Clone + Send + 'static,
15    S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
16    S::Future: Send,
17{
18    /// Create a new test server from a service
19    pub fn new(service: S) -> Self {
20        Self { service }
21    }
22
23    /// Send a request to the test server
24    pub async fn call(&mut self, request: OxiditeRequest) -> Result<OxiditeResponse> {
25        use tower::ServiceExt;
26        self.service
27            .ready()
28            .await
29            .map_err(|e| oxidite_core::Error::InternalServerError(format!("Service not ready: {:?}", e.into())))?
30            .call(request)
31            .await
32            .map_err(|e| oxidite_core::Error::InternalServerError(format!("Request failed: {:?}", e.into())))
33    }
34}
35
36/// Helper to test a router
37pub fn test_router(router: Router) -> TestServer<Router> {
38    TestServer::new(router)
39}