use crate::{block_on, ServerConnector};
use std::{error::Error, future::Future};
use trillium::Handler;
use trillium_http::transport::BoxedTransport;
use url::Url;
pub fn with_server<H, Fun, Fut>(handler: H, tests: Fun)
where
H: Handler,
Fun: FnOnce(Url) -> Fut,
Fut: Future<Output = Result<(), Box<dyn Error>>>,
{
block_on(async move {
let port = portpicker::pick_unused_port().expect("could not pick a port");
let url = format!("http://localhost:{port}").parse().unwrap();
let handle = crate::config()
.with_host("localhost")
.with_port(port)
.spawn(handler);
handle.info().await;
tests(url).await.unwrap();
handle.stop().await;
});
}
pub fn with_transport<H, Fun, Fut>(handler: H, tests: Fun)
where
H: Handler,
Fun: FnOnce(BoxedTransport) -> Fut,
Fut: Future<Output = Result<(), Box<dyn Error>>>,
{
block_on(async move {
let transport = ServerConnector::new(handler).connect(false).await;
tests(BoxedTransport::new(transport));
});
}