From 773b0780f6f1580647b726dbd4661ed249dc066f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 16:42:12 -0400 Subject: [PATCH] Added code to get response content --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 456a9e9..ab51e52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -206,6 +206,36 @@ mod tests { } } + async fn song_queue_req(app: &axum::Router) -> Result { + // Create multipart form + let mut form = MultipartForm::default(); + let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUESONG) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap(); + app.clone().oneshot(req).await + } + + pub async fn get_resp_data(response: axum::response::Response) -> Data + + where + Data: for<'a>serde::Deserialize<'a> + { + let body = axum::body::to_bytes(response.into_body(), usize::MAX) + .await + .unwrap(); + let resp: Data = + serde_json::from_slice(&body).unwrap(); + resp + } + #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -225,29 +255,18 @@ mod tests { let app = init::app(pool).await; - // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/Machine_gun/track01.flac"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - // Send request - match app.oneshot(req).await { + match song_queue_req(&app).await { Ok(response) => { + /* let body = axum::body::to_bytes(response.into_body(), usize::MAX) .await .unwrap(); println!("Body: {:?}", body); let resp: crate::callers::song::response::Response = serde_json::from_slice(&body).unwrap(); + */ + let resp = get_resp_data::(response).await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); }