From 7c75eccd5e2bbf453aa043ca3627b4596b8b738a Mon Sep 17 00:00:00 2001
From: Rutger Broekhoff
Date: Wed, 24 Jan 2024 17:09:53 +0100
Subject: Properly propagate JSON parsing error

---
 rs/server/src/main.rs | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

(limited to 'rs')

diff --git a/rs/server/src/main.rs b/rs/server/src/main.rs
index 7060b15..b8428cb 100644
--- a/rs/server/src/main.rs
+++ b/rs/server/src/main.rs
@@ -287,11 +287,14 @@ enum GitLfsJsonRejection {
 
 impl IntoResponse for GitLfsJsonRejection {
     fn into_response(self) -> Response {
-        make_error_resp(
-            StatusCode::UNSUPPORTED_MEDIA_TYPE,
-            &format!("Expected request with `Content-Type: {LFS_MIME}`"),
-        )
-        .into_response()
+        match self {
+            Self::Json(rej) => rej.into_response(),
+            Self::MissingGitLfsJsonContentType => make_error_resp(
+                StatusCode::UNSUPPORTED_MEDIA_TYPE,
+                &format!("Expected request with `Content-Type: {LFS_MIME}`"),
+            )
+            .into_response(),
+        }
     }
 }
 
@@ -944,3 +947,19 @@ async fn obj_download(
 
     (headers, body).into_response()
 }
+
+#[test]
+fn test_mimetype() {
+    assert!(is_git_lfs_json_mimetype("application/vnd.git-lfs+json"));
+    assert!(!is_git_lfs_json_mimetype("application/vnd.git-lfs"));
+    assert!(!is_git_lfs_json_mimetype("application/json"));
+    assert!(is_git_lfs_json_mimetype(
+        "application/vnd.git-lfs+json; charset=utf-8"
+    ));
+    assert!(is_git_lfs_json_mimetype(
+        "application/vnd.git-lfs+json; charset=UTF-8"
+    ));
+    assert!(!is_git_lfs_json_mimetype(
+        "application/vnd.git-lfs+json; charset=ISO-8859-1"
+    ));
+}
-- 
cgit v1.2.3