From fd6702d029707786d96fdefe69df37278cb92925 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 29 Aug 2024 13:39:23 +0400 Subject: [PATCH] test: add simple test for client I/O --- src/client/mod.rs | 3 +++ src/client/tests/mod.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/client/tests/mod.rs diff --git a/src/client/mod.rs b/src/client/mod.rs index 32d1f75..93abe81 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -3,6 +3,9 @@ pub mod builder; pub mod response; +#[cfg(test)] +pub mod tests; + pub use response::Response; #[cfg(feature = "hickory")] diff --git a/src/client/tests/mod.rs b/src/client/tests/mod.rs new file mode 100644 index 0000000..1f98b44 --- /dev/null +++ b/src/client/tests/mod.rs @@ -0,0 +1,42 @@ +use tokio::runtime::Runtime; + +use super::*; + +#[test] +fn check_parser() { + let rt = Runtime::new().unwrap(); + let client = Client::builder().dangerous_with_no_verifier().build(); + + let mut recv = Vec::new(); + let stream = tokio::io::join( + "20 text/gemini\r\n# hello world\nšŸ‘\n".as_bytes(), + &mut recv, + ); + + let mut resp = rt + .block_on(client.perform_io("gemini://unw.dc09.ru", stream)) + .unwrap(); + + { + let status = resp.status(); + assert_eq!(status.status_code(), StatusCode::Success); + assert_eq!(status.reply_type(), ReplyType::Success); + assert_eq!(status.num(), 20u8); + assert_eq!(status.second_digit(), 0u8); + } + + assert_eq!(resp.is_ok(), true); + + assert_eq!(resp.message(), "text/gemini"); + + { + let mime = resp.mime().unwrap(); + assert_eq!(mime.type_(), mime::TEXT); + assert_eq!(mime.subtype(), "gemini"); + } + + assert_eq!(rt.block_on(resp.text()).unwrap(), "# hello world\nšŸ‘\n"); + + drop(resp); // to free recv from mutable borrowing + assert_eq!(recv.as_slice(), b"gemini://unw.dc09.ru\r\n"); +}