mini_http_test/
lib.rs

1//! Provides a simple Hyper server wrapper that can be used for writing test
2//! servers. Vaguely inspired by Go's
3//! [httptest](https://pkg.go.dev/net/http/httptest#Server) package.
4//!
5//! Currently only supports HTTP/1.1 and does not support TLS. Only supports the
6//! Tokio async runtime.
7//!
8//! ## Example
9//!
10//! ```
11//! # // Please keep this example up-to-date with README.md, but remove all
12//! # // lines starting with `#` and their contents.
13//! use std::sync::{Arc, Mutex};
14//!
15//! use mini_http_test::{
16//!     handle_ok,
17//!     hyper::{body, Request, Response},
18//!     Server,
19//! };
20//!
21//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
22//! let val = Arc::new(Mutex::new(1234));
23//! let server = {
24//!     let val = val.clone();
25//!     Server::new(move |_: Request<body::Incoming>| async move {
26//!         let mut val = val.lock().expect("lock poisoned");
27//!         *val += 1;
28//!         handle_ok(Response::new(val.to_string().into()))
29//!     })
30//!     .await
31//!     .expect("create server")
32//! };
33//!
34//! let res = reqwest::Client::new()
35//!     .get(server.url("/").to_string())
36//!     .send()
37//!     .await
38//!     .expect("send request");
39//!
40//! assert_eq!(res.status(), 200);
41//! assert_eq!(*val.lock().expect("lock poisoned"), 1235);
42//! assert_eq!(res.text().await.expect("read response"), "1235");
43//!
44//! assert_eq!(server.req_count(), 1);
45//! # });
46//! ```
47//!
48//! There are also more examples as tests.
49
50mod error;
51mod handler;
52mod server;
53
54pub use error::*;
55pub use handler::*;
56pub use server::*;
57
58pub use hyper;