diff options
author | Rutger Broekhoff | 2024-01-24 17:40:08 +0100 |
---|---|---|
committer | Rutger Broekhoff | 2024-01-24 17:40:08 +0100 |
commit | 955ca49ef8a1db0e8791fc21daf1b3d302361593 (patch) | |
tree | 4a9b58b3a640ec0b84e982abf8ef999c33e4b777 | |
parent | 0f2692e43cac41917d24a1da24a45ebb266b88ea (diff) | |
download | gitolfs3-955ca49ef8a1db0e8791fc21daf1b3d302361593.tar.gz gitolfs3-955ca49ef8a1db0e8791fc21daf1b3d302361593.zip |
Test JSON decoding for BatchRequest
-rw-r--r-- | rs/Cargo.lock | 1 | ||||
-rw-r--r-- | rs/server/Cargo.toml | 1 | ||||
-rw-r--r-- | rs/server/src/main.rs | 36 |
3 files changed, 34 insertions, 4 deletions
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 = [ | |||
1633 | "common", | 1633 | "common", |
1634 | "mime", | 1634 | "mime", |
1635 | "serde", | 1635 | "serde", |
1636 | "serde_json", | ||
1636 | "tokio", | 1637 | "tokio", |
1637 | "tokio-util", | 1638 | "tokio-util", |
1638 | "tower", | 1639 | "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"] } | |||
12 | common = { path = "../common" } | 12 | common = { path = "../common" } |
13 | mime = "0.3" | 13 | mime = "0.3" |
14 | serde = { version = "1", features = ["derive"] } | 14 | serde = { version = "1", features = ["derive"] } |
15 | serde_json = "1" | ||
15 | tokio = { version = "1.35", features = ["full"] } | 16 | tokio = { version = "1.35", features = ["full"] } |
16 | tokio-util = "0.7" | 17 | tokio-util = "0.7" |
17 | tower = "0.4" | 18 | 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 { | |||
236 | enum TransferAdapter { | 236 | enum TransferAdapter { |
237 | #[serde(rename = "basic")] | 237 | #[serde(rename = "basic")] |
238 | Basic, | 238 | Basic, |
239 | #[serde(other)] | ||
240 | Unknown, | ||
239 | } | 241 | } |
240 | 242 | ||
241 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] | 243 | #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)] |
242 | enum HashAlgo { | 244 | enum HashAlgo { |
243 | #[serde(rename = "sha256")] | 245 | #[serde(rename = "sha256")] |
244 | Sha256, | 246 | Sha256, |
247 | #[serde(other)] | ||
248 | Unknown, | ||
245 | } | 249 | } |
246 | 250 | ||
247 | impl Default for HashAlgo { | 251 | impl Default for HashAlgo { |
@@ -250,7 +254,7 @@ impl Default for HashAlgo { | |||
250 | } | 254 | } |
251 | } | 255 | } |
252 | 256 | ||
253 | #[derive(Debug, Deserialize, Clone)] | 257 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] |
254 | struct BatchRequestObject { | 258 | struct BatchRequestObject { |
255 | oid: common::Oid, | 259 | oid: common::Oid, |
256 | size: i64, | 260 | size: i64, |
@@ -265,7 +269,7 @@ fn default_transfers() -> Vec<TransferAdapter> { | |||
265 | vec![TransferAdapter::Basic] | 269 | vec![TransferAdapter::Basic] |
266 | } | 270 | } |
267 | 271 | ||
268 | #[derive(Deserialize)] | 272 | #[derive(Debug, Deserialize, PartialEq, Eq, Clone)] |
269 | struct BatchRequest { | 273 | struct BatchRequest { |
270 | operation: common::Operation, | 274 | operation: common::Operation, |
271 | #[serde(default = "default_transfers")] | 275 | #[serde(default = "default_transfers")] |
@@ -344,11 +348,10 @@ where | |||
344 | { | 348 | { |
345 | type Rejection = GitLfsJsonRejection; | 349 | type Rejection = GitLfsJsonRejection; |
346 | 350 | ||
347 | async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> { | 351 | async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
348 | if !has_git_lfs_json_content_type(&req) { | 352 | if !has_git_lfs_json_content_type(&req) { |
349 | return Err(GitLfsJsonRejection::MissingGitLfsJsonContentType); | 353 | return Err(GitLfsJsonRejection::MissingGitLfsJsonContentType); |
350 | } | 354 | } |
351 | req.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json")); | ||
352 | Json::<T>::from_request(req, state) | 355 | Json::<T>::from_request(req, state) |
353 | .await | 356 | .await |
354 | .map(GitLfsJson) | 357 | .map(GitLfsJson) |
@@ -964,3 +967,28 @@ fn test_mimetype() { | |||
964 | "application/vnd.git-lfs+json; charset=ISO-8859-1" | 967 | "application/vnd.git-lfs+json; charset=ISO-8859-1" |
965 | )); | 968 | )); |
966 | } | 969 | } |
970 | |||
971 | #[test] | ||
972 | fn test_deserialize() { | ||
973 | let json = r#"{"operation":"upload","objects":[{"oid":"8f4123f9a7181f488c5e111d82cefd992e461ae5df01fd2254399e6e670b2d3c","size":170904}], | ||
974 | "transfers":["lfs-standalone-file","basic","ssh"],"ref":{"name":"refs/heads/main"},"hash_algo":"sha256"}"#; | ||
975 | let expected = BatchRequest { | ||
976 | operation: common::Operation::Upload, | ||
977 | objects: vec![BatchRequestObject { | ||
978 | oid: "8f4123f9a7181f488c5e111d82cefd992e461ae5df01fd2254399e6e670b2d3c" | ||
979 | .parse() | ||
980 | .unwrap(), | ||
981 | size: 170904, | ||
982 | }], | ||
983 | transfers: vec![ | ||
984 | TransferAdapter::Unknown, | ||
985 | TransferAdapter::Basic, | ||
986 | TransferAdapter::Unknown, | ||
987 | ], | ||
988 | hash_algo: HashAlgo::Sha256, | ||
989 | }; | ||
990 | assert_eq!( | ||
991 | serde_json::from_str::<BatchRequest>(json).unwrap(), | ||
992 | expected | ||
993 | ); | ||
994 | } | ||