Added test draft

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-18 10:21:47 +02:00
parent a07c11228f
commit 43750ab47a
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
3 changed files with 61 additions and 1 deletions

46
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -4,7 +4,7 @@ use json::{JsonValue, object};
use dotenv::dotenv;
async fn body_to_str(res: Response<Body>) -> String {
pub async fn body_to_str(res: Response<Body>) -> 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<dyn std::error::Error + Send + Sync>> {
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)));
}
}