From 43750ab47a3bc7901ae21f0b8ee4ce74958db225 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Mon, 18 Oct 2021 10:21:47 +0200 Subject: [PATCH] Added test draft Signed-off-by: Louis Vallat --- Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 15 ++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8580306..2b358b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,27 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "async-stream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "171374e7e3b2504e0e5236e3b59260560f9fe94bfe9ac39ba5e4e929c5590625" +dependencies = [ + "async-stream-impl", + "futures-core", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648ed8c8d2ce5409ccd57453d9d1b214b342a0d69376a6feda1fd6cae3299308" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -301,6 +322,7 @@ dependencies = [ "hyper-tls", "json", "tokio", + "tokio-test", ] [[package]] @@ -668,6 +690,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2f3f698253f03119ac0102beaa64f67a67e08074d03a22d18784104543727f" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-test" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53474327ae5e166530d17f2d956afcb4f8a004de581b3cae10f12006bc8163e3" +dependencies = [ + "async-stream", + "bytes", + "futures-core", + "tokio", + "tokio-stream", +] + [[package]] name = "tokio-util" version = "0.6.8" diff --git a/Cargo.toml b/Cargo.toml index ca601ad..5dccc1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] hyper = { version = "0.14", features = ["full"] } tokio = { version = "1", features = ["full"] } +tokio-test = "0.4.2" hyper-tls = "0.5.0" dotenv = "0.15.0" json = "0.12.4" diff --git a/src/main.rs b/src/main.rs index ad7081e..1cef7db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use json::{JsonValue, object}; use dotenv::dotenv; -async fn body_to_str(res: Response) -> String { +pub async fn body_to_str(res: Response) -> String { let body = hyper::body::to_bytes(res).await.unwrap(); let body_str = std::str::from_utf8(&body).unwrap().clone(); return body_str.to_string(); @@ -54,3 +54,16 @@ async fn main() -> Result<(), Box> { println!("{}", albums["albums"].pretty(4)); Ok(()) } + +#[cfg(test)] +mod tests { + use hyper::{Body, Response}; + use crate::body_to_str; + + #[test] + fn body_to_str_empty() { + let res = Response::new(Body::empty()); + assert_eq!("", tokio_test::block_on(body_to_str(res))); + } + +}