axum_test/transport_layer/into_transport_layer/
axum_service.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use anyhow::Result;
use axum::Router;
use shuttle_axum::AxumService;

use crate::transport_layer::IntoTransportLayer;
use crate::transport_layer::TransportLayer;
use crate::transport_layer::TransportLayerBuilder;

impl IntoTransportLayer for AxumService {
    fn into_http_transport_layer(
        self,
        builder: TransportLayerBuilder,
    ) -> Result<Box<dyn TransportLayer>> {
        Router::into_http_transport_layer(self.0, builder)
    }

    fn into_mock_transport_layer(self) -> Result<Box<dyn TransportLayer>> {
        Router::into_mock_transport_layer(self.0)
    }
}

#[cfg(test)]
mod test_into_http_transport_layer_for_axum_service {
    use super::*;

    use axum::extract::State;
    use axum::routing::get;
    use axum::Router;

    use crate::TestServer;

    async fn get_state(State(count): State<u32>) -> String {
        format!("count is {}", count)
    }

    #[tokio::test]
    async fn it_should_run() {
        // Build an application with a route.
        let app: AxumService = Router::new()
            .route("/count", get(get_state))
            .with_state(123)
            .into();

        // Run the server.
        let server = TestServer::builder()
            .http_transport()
            .build(app)
            .expect("Should create test server");

        // Get the request.
        server.get(&"/count").await.assert_text(&"count is 123");
    }
}

#[cfg(test)]
mod test_into_mock_transport_layer_for_axum_service {
    use super::*;

    use axum::extract::State;
    use axum::routing::get;
    use axum::Router;

    use crate::TestServer;

    async fn get_state(State(count): State<u32>) -> String {
        format!("count is {}", count)
    }

    #[tokio::test]
    async fn it_should_run() {
        // Build an application with a route.
        let app: AxumService = Router::new()
            .route("/count", get(get_state))
            .with_state(123)
            .into();

        // Run the server.
        let server = TestServer::builder()
            .mock_transport()
            .build(app)
            .expect("Should create test server");

        // Get the request.
        server.get(&"/count").await.assert_text(&"count is 123");
    }
}