Skip to content

Commit

Permalink
fix empty stream http status code (#261)
Browse files Browse the repository at this point in the history
* fix empty stream http status code

* fix tests assertions

* handle response body for no content 204 response
  • Loading branch information
amirRamirfatahi authored Jan 13, 2025
1 parent de24c37 commit 0f0ad25
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
});
Expand Down
2 changes: 1 addition & 1 deletion tests/service/stream/post/reach/engagement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion tests/service/stream/post/reach/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion tests/service/stream/post/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion tests/service/stream/user/reach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 0f0ad25

Please sign in to comment.