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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// 📦🦋 charted TestKit: testing library for Axum services with testcontainers support
// Copyright (c) 2024 Noelware, LLC. <team@noelware.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![doc(html_logo_url = "https://cdn.floofy.dev/images/trans.png")]
#![doc = include_str!("../README.md")]

#[cfg(feature = "macros")]
pub use charted_testkit_macros::*;

mod macros;

use axum::{body::Bytes, extract::Request, Router};
use http_body_util::Full;
use hyper::{body::Incoming, Method};
use hyper_util::{
    client::legacy::{connect::HttpConnector, Client, ResponseFuture},
    rt::{TokioExecutor, TokioIo},
};
use std::{fmt::Debug, net::SocketAddr};
use tokio::{net::TcpListener, task::JoinHandle};
use tower::{Service, ServiceExt};

pub struct TestContext {
    _handle: Option<JoinHandle<()>>,
    client: Client<HttpConnector, http_body_util::Full<Bytes>>,
    http1: bool,
    addr: Option<SocketAddr>,

    // TODO(@auguwu): should `containers` be a `HashMap<TypeId, Box<dyn Any>>` to easily
    //                identify a image?
    #[cfg(feature = "testcontainers")]
    containers: Vec<Box<dyn ::std::any::Any + Send + Sync>>,

    #[cfg(feature = "http2")]
    http2: bool,
}

impl Debug for TestContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TestContext").field("local_addr", &self.addr).finish()
    }
}

impl Default for TestContext {
    fn default() -> Self {
        TestContext {
            _handle: None,
            client: Client::builder(TokioExecutor::new()).build_http(),
            http1: true,
            addr: None,

            #[cfg(feature = "testcontainers")]
            containers: Vec::new(),

            #[cfg(feature = "http2")]
            http2: false,
        }
    }
}

impl TestContext {
    /// Allows HTTP/1 connections to be used. By disabling this, the ephermeral TCP listener
    /// won't know what to do unless HTTP/2 connections are allowed.
    pub fn allow_http1(mut self, yes: bool) -> Self {
        self.http1 = yes;
        self
    }

    /// Allows HTTP/2 connections to be used. By default, only HTTP/1 connections are allowed.
    #[cfg(feature = "http2")]
    pub fn allow_http2(mut self, yes: bool) -> Self {
        self.http2 = yes;
        self
    }

    /// Checks whenever if the ephermeral TCP listener should allow both HTTP/1 and HTTP/2 connections.
    #[cfg(feature = "http2")]
    pub fn allows_both(&self) -> bool {
        self.http1 && self.http2
    }

    /// Checks whenever if the ephermeral TCP listener should allow both HTTP/1 and HTTP/2 connections.
    #[cfg(not(feature = "http2"))]
    pub fn allows_both(&self) -> bool {
        self.http1
    }

    /// Returns a mutable [`Vec`] of allocated type-erased objects that should be [`ContainerAsync`].
    #[cfg(feature = "testcontainers")]
    pub fn containers_mut(&mut self) -> &mut Vec<Box<dyn ::std::any::Any + Send + Sync>> {
        &mut self.containers
    }

    /// Returns a [`ContainerAsync`] of a spawned container that can be accessed.
    #[cfg(feature = "testcontainers")]
    pub fn container<I: ::testcontainers::Image + 'static>(&self) -> Option<&::testcontainers::ContainerAsync<I>> {
        match self
            .containers
            .iter()
            .find(|x| x.is::<::testcontainers::ContainerAsync<I>>())
        {
            Some(container) => container.downcast_ref(),
            None => None,
        }
    }

    /// Returns a optional reference to a [socket address][SocketAddr] if [`TestContext::serve`] was called
    /// after this call.
    ///
    /// ## Example
    /// ```rust
    /// # use charted_testkit::TestContext;
    /// #
    /// let mut ctx = TestContext::default();
    /// assert!(ctx.server_addr().is_none());
    ///
    /// # // `IGNORE` is used since we don't actually want to spawn a server!
    /// # const IGNORE: &str = stringify! {
    /// ctx.serve(axum::Router::new()).await;
    /// assert!(ctx.server_addr().is_some());
    /// # };
    /// ```
    pub fn server_addr(&self) -> Option<&SocketAddr> {
        self.addr.as_ref()
    }

    /// Sends a request to the ephemeral server and returns a [`ResponseFuture`].
    ///
    /// ## Example
    /// ```rust,no_run
    /// # use charted_testkit::TestContext;
    /// # use axum::{routing, http::Method, body::Bytes};
    /// #
    /// # #[tokio::main]
    /// # async fn main() {
    /// async fn handler() -> &'static str {
    ///     "Hello, world!"
    /// }
    ///
    /// let mut ctx = TestContext::default();
    /// ctx.serve(axum::Router::new().route("/", routing::get(handler))).await;
    ///
    /// let res = ctx
    ///     .request::<_, Bytes, _>("/", Method::GET, None, |_| {})
    ///     .await
    ///     .expect("was unable to send request to ephermeral server");
    ///
    /// charted_testkit::assert_successful!(res);
    /// assert_eq!(charted_testkit::consume_body!(res), Bytes::from_static(b"Hello, world!"));
    /// # }
    /// ```
    pub fn request<U: AsRef<str> + 'static, B: Into<Bytes>, F: Fn(&mut Request<Full<Bytes>>)>(
        &self,
        uri: U,
        method: Method,
        body: Option<B>,
        build: F,
    ) -> ResponseFuture {
        let addr = self.server_addr().expect("failed to get socket address");

        let mut req = Request::<Full<Bytes>>::new(Full::new(body.map(Into::into).unwrap_or_default()));
        *req.method_mut() = method;
        *req.uri_mut() = format!("http://{addr}{}", uri.as_ref())
            .parse()
            .expect("failed to parse into `hyper::Uri`");

        build(&mut req);
        self.client.request(req)
    }

    /// Serves the ephermeral server.
    pub async fn serve(&mut self, router: Router) {
        if self._handle.is_some() {
            panic!("ephermeral server is already serving");
        }

        let allows_both = self.allows_both();
        let http1 = self.http1;

        #[cfg(feature = "http2")]
        let http2 = self.http2;

        #[cfg(not(feature = "http2"))]
        let http2 = false;

        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("failed to create tcp listener");

        self.addr = Some(listener.local_addr().expect("unable to get local addr"));

        // based off https://github.com/tokio-rs/axum/blob/934b1aac067dba596feb617817409345f9835db5/examples/serve-with-hyper/src/main.rs#L79-L118
        // since we don't need `axum::serve` and we want to customise the HTTP transport to use (i.e, if you want
        // to test HTTP/2 usage and not HTTP/1 usage)
        self._handle = Some(tokio::spawn(async move {
            let mut make_service = router.into_make_service_with_connect_info::<SocketAddr>();

            loop {
                let (socket, addr) = listener.accept().await.expect("failed to accept connection");
                let service = match make_service.call(addr).await {
                    Ok(service) => service,
                    Err(e) => match e {},
                };

                tokio::spawn(async move {
                    let socket = TokioIo::new(socket);
                    let hyper_service =
                        hyper::service::service_fn(move |request: Request<Incoming>| service.clone().oneshot(request));

                    if allows_both {
                        #[cfg(feature = "http2")]
                        if let Err(err) = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new())
                            .serve_connection_with_upgrades(socket, hyper_service)
                            .await
                        {
                            eprintln!("failed to serve connection: {err:#}");
                        }

                        #[cfg(not(feature = "http2"))]
                        if let Err(err) = hyper::server::conn::http1::Builder::new()
                            .serve_connection(socket, hyper_service)
                            .await
                        {
                            eprintln!("failed to serve HTTP/1 connection: {err:#}");
                        }
                    } else if http2 {
                        #[cfg(feature = "http2")]
                        if let Err(err) = hyper::server::conn::http2::Builder::new(TokioExecutor::new())
                            .serve_connection(socket, hyper_service)
                            .await
                        {
                            eprintln!("failed to serve HTTP/2 connection: {err:#}");
                        }
                    } else if http1 {
                        if let Err(err) = hyper::server::conn::http1::Builder::new()
                            .serve_connection(socket, hyper_service)
                            .await
                        {
                            eprintln!("failed to serve HTTP/1 connection: {err:#}");
                        }
                    } else {
                        panic!("unable to serve connection due to no HTTP stream to process");
                    }
                });
            }
        }));
    }
}

// Private APIs used by macros; do not use!
#[doc(hidden)]
pub mod __private {
    pub use http_body_util::BodyExt;
}

#[cfg(test)]
mod tests {
    use crate::{assert_successful, consume_body, TestContext};
    use axum::{body::Bytes, routing, Router};
    use hyper::Method;

    async fn hello() -> &'static str {
        "Hello, world!"
    }

    fn router() -> Router {
        Router::new().route("/", routing::get(hello))
    }

    #[tokio::test]
    #[cfg_attr(
        windows,
        ignore = "fails on Windows: hyper_util::client::legacy::Error(Connect, ConnectError(\"tcp connect error\", Os { code: 10049, kind: AddrNotAvailable, message: \"The requested address is not valid in its context.\" })))"
    )]
    async fn test_usage() {
        let mut ctx = TestContext::default();
        ctx.serve(router()).await;

        let res = ctx
            .request("/", Method::GET, None::<axum::body::Bytes>, |_| {})
            .await
            .expect("unable to send request");

        assert_successful!(res);
        assert_eq!(consume_body!(res), Bytes::from_static(b"Hello, world!"));
    }

    #[cfg(feature = "testcontainers")]
    #[tokio::test]
    #[cfg_attr(
        not(target_os = "linux"),
        ignore = "this will only probably work on Linux (requires a working Docker daemon)"
    )]
    async fn test_testcontainers_in_ctx() {
        use testcontainers::runners::AsyncRunner;

        let mut ctx = TestContext::default();
        let valkey = ::testcontainers::GenericImage::new("valkey/valkey", "7.2.6")
            .start()
            .await
            .expect("failed to start valkey image");

        ctx.containers_mut().push(Box::new(valkey));
        assert!(ctx.container::<::testcontainers::GenericImage>().is_some());
    }
}