diff --git a/src/albums.rs b/src/albums.rs index 92fa943..edeed0d 100644 --- a/src/albums.rs +++ b/src/albums.rs @@ -12,6 +12,6 @@ pub async fn get_albums(client: &Client>, lychee_s .header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap()) .body(Body::empty()) .expect("error"); - let _res = client.request(req).await.unwrap(); - return json::parse(body_to_str(_res).await.as_str()).unwrap(); + let res = client.request(req).await.unwrap(); + return json::parse(body_to_str(res.into_body()).await.as_str()).unwrap(); } diff --git a/src/main.rs b/src/main.rs index 3f6df21..34cfb36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use hyper_tls::HttpsConnector; -use hyper::{Body, Client, Response, client::HttpConnector}; +use hyper::{Body, Client, body, client::HttpConnector}; use clap::{Arg, App, SubCommand}; use crate::session::login; @@ -13,10 +13,8 @@ pub struct LycheeClient { api_key: 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(); +pub async fn body_to_str(res: Body) -> String { + return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap(); } @@ -64,22 +62,18 @@ async fn main() -> Result<(), Box> { #[cfg(test)] mod tests_main { - use hyper::{Body, Response}; + use hyper::Body; 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))); + assert_eq!("", tokio_test::block_on(body_to_str(Body::empty()))); } #[test] fn body_to_str_not_empty() { - let res = Response::new(Body::from("demo")); - assert_eq!("demo", tokio_test::block_on(body_to_str(res))); - - let res2 = Response::new(Body::from("hello world")); - assert_eq!("hello world", tokio_test::block_on(body_to_str(res2))); + assert_eq!("demo", tokio_test::block_on(body_to_str(Body::from("demo")))); + assert_eq!("hello world", tokio_test::block_on(body_to_str(Body::from("hello world")))); } } diff --git a/src/session.rs b/src/session.rs index bf31c76..72c6194 100644 --- a/src/session.rs +++ b/src/session.rs @@ -2,7 +2,7 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, use json::object; use crate::{LycheeClient, body_to_str}; -pub async fn login(client: &LycheeClient, login: &str, password: &str) -> HeaderValue { +pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String { let login_data = object! { "username": login, "password": password }; let req = Request::builder() @@ -13,10 +13,12 @@ pub async fn login(client: &LycheeClient, login: &str, password: &str) -> Header .body(Body::from(login_data.dump())) .expect("error"); let res = client.client.request(req).await.unwrap(); - let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone(); - assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap()); + let headers = res.headers().get_all(SET_COOKIE) + .iter().find(|&x| x.to_str().unwrap().starts_with("lychee_session")).unwrap().clone(); + let body = body_to_str(res.into_body()).await; + if body != "true" { panic!("Error while logging in, expected true, server returned: {}", body); } - return lychee_session; + return headers.to_str().unwrap().to_string(); } pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) { @@ -28,7 +30,7 @@ pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) { .body(Body::empty()) .expect("error"); let res = client.client.request(req).await.unwrap(); - assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap()); + assert!(json::parse(body_to_str(res.into_body()).await.as_str()).unwrap().as_bool().unwrap()); } #[cfg(test)] @@ -82,21 +84,21 @@ mod session_tests { let m1 = mock("POST", "/api/Session::login") .match_header("content-type", "application/json") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("true") .with_status(200) .create(); - assert_eq!("demo", tokio_test::block_on(login(&client, "u", "p"))); + assert_eq!("lychee_session=demo", tokio_test::block_on(login(&client, "u", "p"))); m1.assert(); let m2 = mock("POST", "/api/Session::login") .match_header("content-type", "application/json") .with_header("content-type", "application/json") - .with_header("set-cookie", "value") + .with_header("set-cookie", "lychee_session=value") .with_body("true") .with_status(200) .create(); - assert_eq!("value", tokio_test::block_on(login(&client, "u", "p"))); + assert_eq!("lychee_session=value", tokio_test::block_on(login(&client, "u", "p"))); m2.assert(); } @@ -106,7 +108,7 @@ mod session_tests { let client = setup(); let m = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("false") .create(); @@ -119,7 +121,7 @@ mod session_tests { let mut client = setup(); let m1 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("true") .match_header("authorization", "value") .create(); @@ -129,7 +131,7 @@ mod session_tests { client.api_key = "othervalue".to_string(); let m2 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("true") .match_header("authorization", "othervalue") .create(); @@ -143,7 +145,7 @@ mod session_tests { let client = setup(); let m1 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("true") .match_body(body1.dump().as_str()) .create(); @@ -154,7 +156,7 @@ mod session_tests { let client = setup(); let m2 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") - .with_header("set-cookie", "demo") + .with_header("set-cookie", "lychee_session=demo") .with_body("true") .match_body(body2.dump().as_str()) .create();