diff --git a/src/main.rs b/src/main.rs index 0497a11..852696d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -193,251 +193,251 @@ mod tests { pub async fn song_queue_req( app: &axum::Router, - ) -> Result { + ) -> Result { // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/I/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::queue::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Multipart(( + "flac".to_string(), + "tests/I/track01.flac".to_string(), + ))), + crate::callers::queue::endpoints::QUEUESONG, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn song_queue_link_req( app: &axum::Router, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::link_user_to_queued_song(song_queue_id, user_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUESONGLINKUSERID) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUESONGLINKUSERID, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_queue_req( app: &axum::Router, - ) -> Result { - let fetch_req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(crate::callers::queue::endpoints::NEXTQUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(); - app.clone().oneshot(fetch_req).await + ) -> Result { + match run_post( + None, + crate::callers::queue::endpoints::NEXTQUEUESONG, + axum::http::Method::GET, + false, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_metadata_queue_req( app: &axum::Router, id: &uuid::Uuid, - ) -> Result { + ) -> Result { let uri = format!( "{}?id={}", crate::callers::queue::endpoints::QUEUEMETADATA, id ); - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(); - - app.clone().oneshot(req).await + match run_post(None, &uri, axum::http::Method::GET, false).await { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_queue_data_req( app: &axum::Router, id: &uuid::Uuid, - ) -> Result { + ) -> Result { let raw_uri = String::from(crate::callers::queue::endpoints::QUEUESONGDATA); let end_index = raw_uri.len() - 4; let mut uri: String = (&raw_uri[..end_index]).to_string(); uri += &id.to_string(); - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header( - axum::http::header::CONTENT_TYPE, - simeta::detection::song::constants::mime::FLAC, - ) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(); - app.clone().oneshot(req).await + match run_post(None, &uri, axum::http::Method::GET, false).await { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn upload_coverart_queue_req( app: &axum::Router, - ) -> Result { - let mut form = MultipartForm::default(); - let _ = form.add_file( - simeta::detection::coverart::constants::JPEG_TYPE, - "tests/I/Coverart-1.jpg", - ); - - // 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::queue::endpoints::QUEUECOVERART) - .header(axum::http::header::CONTENT_TYPE, content_type) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - - app.clone().oneshot(req).await + ) -> Result { + match run_post( + Some(ReqBody::Multipart(( + simeta::detection::coverart::constants::JPEG_TYPE.to_string(), + "tests/I/Coverart-1.jpg".to_string(), + ))), + crate::callers::queue::endpoints::QUEUECOVERART, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn queue_metadata_req( app: &axum::Router, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::queue_metadata(&song_queue_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::queue::endpoints::QUEUEMETADATA) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUEMETADATA, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn coverart_queue_song_queue_link_req( app: &axum::Router, coverart_id: &uuid::Uuid, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::link_queued_coverart_to_queued_song( song_queue_id, coverart_id, ) .await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUECOVERARTLINK) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUECOVERARTLINK, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn create_coverart_req( app: &axum::Router, song_id: &uuid::Uuid, coverart_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::create_coverart(song_id, coverart_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::CREATECOVERART) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::endpoints::CREATECOVERART, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn create_song_req( app: &axum::Router, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::create_song(song_queue_id, user_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::CREATESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::endpoints::CREATESONG, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn update_song_queue_status_req( app: &axum::Router, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::update_song_queue_status_to_ready(song_queue_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUESONG, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn get_queued_coverart( app: &axum::Router, coverart_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let uri = format!( "{}?id={}", crate::callers::queue::endpoints::QUEUECOVERART, @@ -446,13 +446,9 @@ mod tests { match run_post(None, &uri, axum::http::Method::GET, false).await { Ok(request) => match app.clone().oneshot(request).await { - Ok(response) => { - Ok(response) - } - Err(err) => { - Err(axum::http::Error::from(err)) - } - } + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, Err(err) => { Err(err) // Err(std::convert::Infallible::from(err)) @@ -478,7 +474,7 @@ mod tests { pub enum ReqBody { Json(serde_json::Value), - + Multipart((String, String)), } pub async fn run_post( @@ -496,8 +492,16 @@ mod tests { ); // axum::body::Body::from(payload.unwrap().to_string()) match payload.unwrap() { - ReqBody::Json(val) => { - axum::body::Body::from(val.to_string()) + ReqBody::Json(val) => axum::body::Body::from(val.to_string()), + ReqBody::Multipart((t, p)) => { + let mut form = MultipartForm::default(); + let _ = form.add_file(t, p); + + // Create request + let content_type = form.content_type(); + println!("Content: {content_type:?}"); + let body = MultipartBody::from(form); + axum::body::Body::from_stream(body) } } } else { @@ -526,7 +530,7 @@ mod tests { // Flow for queueing song pub async fn queue_song_flow( app: &axum::Router, - ) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { + ) -> Result<(axum::response::Response, uuid::Uuid), axum::http::Error> { match super::request::song_queue_req(&app).await { Ok(response) => { let resp = super::util::get_resp_data::< @@ -569,6 +573,10 @@ mod tests { Err(err) => Err(err), } } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } } } Err(err) => Err(err), @@ -618,15 +626,23 @@ mod tests { Err(err) => Err(err), } } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } } } + Err(err) => { + assert!(false, "Error: {:?}", err); + Err(err) + } } } // Returns coverart response and song_queue_id pub async fn queue_song_and_coverart_flow( app: &axum::Router, - ) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { + ) -> Result<(axum::response::Response, uuid::Uuid), axum::http::Error> { match queue_song_flow(&app).await { Ok((song_response, user_id)) => { let resp = super::util::get_resp_data::< @@ -1711,6 +1727,9 @@ mod tests { } } } + Err(err) => { + assert!(false, "Error: {:?}", err); + } } } Err(err) => {