1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{block_on, ServerConnector};
use std::{error::Error, future::Future};
use trillium::Handler;
use trillium_http::transport::BoxedTransport;
use url::Url;

/**
Starts a trillium handler bound to a random available port on
localhost, run the async tests provided as the second
argument, and then shut down the server. useful for full
integration tests that actually exercise the tcp layer.

See
[`trillium_client::Conn`](https://docs.trillium.rs/trillium_client/struct.conn)
for usage examples.
**/
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;
    });
}

/// open an in-memory connection to this handler and call an async
/// function with an open BoxedTransport
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));
    });
}