diff --git a/src/main.rs b/src/main.rs index f2d73dc..f3cc9e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,9 +117,8 @@ mod tests { use common_multipart_rfc7578::client::multipart::{ Body as MultipartBody, Form as MultipartForm, }; - use std::{time::Duration, usize}; + use std::usize; use tower::ServiceExt; - use tower_http::timeout::TimeoutLayer; mod db_mgr { use std::str::FromStr; @@ -197,6 +196,47 @@ mod tests { } } + mod init { + pub async fn app(pool: sqlx::PgPool) -> axum::Router { + crate::init::routes() + .await + .layer(axum::Extension(pool)) + .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) + .layer(tower_http::timeout::TimeoutLayer::new( + std::time::Duration::from_secs(300), + )) + } + } + + 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(); + serde_json::from_slice(&body).unwrap() + } + #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -214,35 +254,13 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); - - // 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(); + let app = init::app(pool).await; // 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"); } @@ -271,35 +289,13 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); - - // 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(); + let app = init::app(pool).await; // Send request - match app.clone().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"); @@ -312,11 +308,10 @@ mod tests { match app.clone().oneshot(fetch_req).await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - let resp: crate::callers::song::response::fetch_queue_song::Response = - serde_json::from_slice(&body).unwrap(); + let resp = get_resp_data::< + crate::callers::song::response::fetch_queue_song::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { @@ -349,35 +344,13 @@ mod tests { let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); db::migrations(&pool).await; - let app = crate::init::routes() - .await - .layer(axum::Extension(pool)) - .layer(axum::extract::DefaultBodyLimit::max(1024 * 1024 * 1024)) - .layer(TimeoutLayer::new(Duration::from_secs(300))); - - // 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(); + let app = init::app(pool).await; // Send request - match app.clone().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"); let new_payload: serde_json::Value = serde_json::json!( @@ -409,11 +382,9 @@ mod tests { .await { Ok(response) => { - let body = axum::body::to_bytes(response.into_body(), usize::MAX) - .await - .unwrap(); - 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"); } Err(err) => {