From 955ca49ef8a1db0e8791fc21daf1b3d302361593 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Wed, 24 Jan 2024 17:40:08 +0100 Subject: Test JSON decoding for BatchRequest --- rs/Cargo.lock | 1 + rs/server/Cargo.toml | 1 + rs/server/src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) (limited to 'rs') diff --git a/rs/Cargo.lock b/rs/Cargo.lock index afc8c7b..96774ca 100644 --- a/rs/Cargo.lock +++ b/rs/Cargo.lock @@ -1633,6 +1633,7 @@ dependencies = [ "common", "mime", "serde", + "serde_json", "tokio", "tokio-util", "tower", diff --git a/rs/server/Cargo.toml b/rs/server/Cargo.toml index a784bf8..ac2b10b 100644 --- a/rs/server/Cargo.toml +++ b/rs/server/Cargo.toml @@ -12,6 +12,7 @@ chrono = { version = "0.4", features = ["serde"] } common = { path = "../common" } mime = "0.3" serde = { version = "1", features = ["derive"] } +serde_json = "1" tokio = { version = "1.35", features = ["full"] } tokio-util = "0.7" tower = "0.4" diff --git a/rs/server/src/main.rs b/rs/server/src/main.rs index cbac138..a8c6aa5 100644 --- a/rs/server/src/main.rs +++ b/rs/server/src/main.rs @@ -236,12 +236,16 @@ async fn main() -> ExitCode { enum TransferAdapter { #[serde(rename = "basic")] Basic, + #[serde(other)] + Unknown, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] enum HashAlgo { #[serde(rename = "sha256")] Sha256, + #[serde(other)] + Unknown, } impl Default for HashAlgo { @@ -250,7 +254,7 @@ impl Default for HashAlgo { } } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Deserialize, PartialEq, Eq, Clone)] struct BatchRequestObject { oid: common::Oid, size: i64, @@ -265,7 +269,7 @@ fn default_transfers() -> Vec { vec![TransferAdapter::Basic] } -#[derive(Deserialize)] +#[derive(Debug, Deserialize, PartialEq, Eq, Clone)] struct BatchRequest { operation: common::Operation, #[serde(default = "default_transfers")] @@ -344,11 +348,10 @@ where { type Rejection = GitLfsJsonRejection; - async fn from_request(mut req: Request, state: &S) -> Result { + async fn from_request(req: Request, state: &S) -> Result { if !has_git_lfs_json_content_type(&req) { return Err(GitLfsJsonRejection::MissingGitLfsJsonContentType); } - req.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json")); Json::::from_request(req, state) .await .map(GitLfsJson) @@ -964,3 +967,28 @@ fn test_mimetype() { "application/vnd.git-lfs+json; charset=ISO-8859-1" )); } + +#[test] +fn test_deserialize() { + let json = r#"{"operation":"upload","objects":[{"oid":"8f4123f9a7181f488c5e111d82cefd992e461ae5df01fd2254399e6e670b2d3c","size":170904}], + "transfers":["lfs-standalone-file","basic","ssh"],"ref":{"name":"refs/heads/main"},"hash_algo":"sha256"}"#; + let expected = BatchRequest { + operation: common::Operation::Upload, + objects: vec![BatchRequestObject { + oid: "8f4123f9a7181f488c5e111d82cefd992e461ae5df01fd2254399e6e670b2d3c" + .parse() + .unwrap(), + size: 170904, + }], + transfers: vec![ + TransferAdapter::Unknown, + TransferAdapter::Basic, + TransferAdapter::Unknown, + ], + hash_algo: HashAlgo::Sha256, + }; + assert_eq!( + serde_json::from_str::(json).unwrap(), + expected + ); +} -- cgit v1.2.3