From 0f0ad250f78f7b8e023d4e57f4383937953465bb Mon Sep 17 00:00:00 2001 From: amirRamirfatahi <30861397+amirRamirfatahi@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:12:01 +0100 Subject: [PATCH] fix empty stream http status code (#261) * fix empty stream http status code * fix tests assertions * handle response body for no content 204 response --- src/error.rs | 7 ++++++- tests/service/stream/post/reach/engagement.rs | 2 +- tests/service/stream/post/reach/timeline.rs | 2 +- tests/service/stream/post/tags.rs | 2 +- tests/service/stream/user/reach.rs | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/error.rs b/src/error.rs index af84d817..27caa3b4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,7 +32,7 @@ impl IntoResponse for Error { let status_code = match self { Error::UserNotFound { .. } => StatusCode::NOT_FOUND, Error::PostNotFound { .. } => StatusCode::NOT_FOUND, - Error::EmptyStream { .. } => StatusCode::NOT_FOUND, + Error::EmptyStream { .. } => StatusCode::NO_CONTENT, Error::FileNotFound { .. } => StatusCode::NOT_FOUND, Error::BookmarksNotFound { .. } => StatusCode::NOT_FOUND, Error::TagsNotFound { .. } => StatusCode::NOT_FOUND, @@ -63,6 +63,11 @@ impl IntoResponse for Error { Error::InternalServerError { source } => error!("Internal server error: {:?}", source), }; + // Handle NO_CONTENT status code with an empty body + if status_code == StatusCode::NO_CONTENT { + return (status_code, ()).into_response(); + } + let body = serde_json::json!({ "error": self.to_string() }); diff --git a/tests/service/stream/post/reach/engagement.rs b/tests/service/stream/post/reach/engagement.rs index aeef2f88..a9079160 100644 --- a/tests/service/stream/post/reach/engagement.rs +++ b/tests/service/stream/post/reach/engagement.rs @@ -248,7 +248,7 @@ async fn test_stream_posts_by_engagement_reach_friends_with_tag() -> Result<()> #[tokio::test] async fn test_stream_not_found_posts_by_engagement_reach_friends_with_tag() -> Result<()> { let path = format!("{ROOT_PATH}?sorting=total_engagement&tags=opensource&source=friends&observer_id={EIXAMPLE}&skip=2"); - make_wrong_request(&path, Some(404)).await?; + make_wrong_request(&path, Some(204)).await?; Ok(()) } diff --git a/tests/service/stream/post/reach/timeline.rs b/tests/service/stream/post/reach/timeline.rs index 6d99754b..93e96df1 100644 --- a/tests/service/stream/post/reach/timeline.rs +++ b/tests/service/stream/post/reach/timeline.rs @@ -489,7 +489,7 @@ async fn test_stream_not_found_posts_by_timeline_reach_friends_with_tag() -> Res let path = format!( "{ROOT_PATH}?sorting=timeline&tags=opensource&source=friends&observer_id={EIXAMPLE}&skip=2" ); - make_wrong_request(&path, Some(404)).await?; + make_wrong_request(&path, Some(204)).await?; Ok(()) } diff --git a/tests/service/stream/post/tags.rs b/tests/service/stream/post/tags.rs index d6ff2c81..f6a3b657 100644 --- a/tests/service/stream/post/tags.rs +++ b/tests/service/stream/post/tags.rs @@ -357,7 +357,7 @@ async fn test_stream_tag_posts_by_engagement_with_end_skip_and_limit() -> Result #[tokio::test] async fn test_post_specific_tag_with_no_result() -> Result<()> { let path = format!("{}?tags={}", ROOT_PATH, "randommm"); - make_wrong_request(&path, None).await?; + make_wrong_request(&path, Some(204)).await?; Ok(()) } diff --git a/tests/service/stream/user/reach.rs b/tests/service/stream/user/reach.rs index 4a3f6d71..dbf4d055 100644 --- a/tests/service/stream/user/reach.rs +++ b/tests/service/stream/user/reach.rs @@ -58,7 +58,7 @@ async fn test_stream_following() -> Result<()> { "bad_user_id" )) .await?; - assert_eq!(res.status(), 404); + assert_eq!(res.status(), 204); Ok(()) }