aboutsummaryrefslogtreecommitdiffstats
path: root/rs/server
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2024-01-24 17:40:08 +0100
committerLibravatar Rutger Broekhoff2024-01-24 17:40:08 +0100
commit955ca49ef8a1db0e8791fc21daf1b3d302361593 (patch)
tree4a9b58b3a640ec0b84e982abf8ef999c33e4b777 /rs/server
parent0f2692e43cac41917d24a1da24a45ebb266b88ea (diff)
downloadgitolfs3-955ca49ef8a1db0e8791fc21daf1b3d302361593.tar.gz
gitolfs3-955ca49ef8a1db0e8791fc21daf1b3d302361593.zip
Test JSON decoding for BatchRequest
Diffstat (limited to 'rs/server')
-rw-r--r--rs/server/Cargo.toml1
-rw-r--r--rs/server/src/main.rs36
2 files changed, 33 insertions, 4 deletions
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"] }
12common = { path = "../common" } 12common = { path = "../common" }
13mime = "0.3" 13mime = "0.3"
14serde = { version = "1", features = ["derive"] } 14serde = { version = "1", features = ["derive"] }
15serde_json = "1"
15tokio = { version = "1.35", features = ["full"] } 16tokio = { version = "1.35", features = ["full"] }
16tokio-util = "0.7" 17tokio-util = "0.7"
17tower = "0.4" 18tower = "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 {
236enum TransferAdapter { 236enum 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)]
242enum HashAlgo { 244enum HashAlgo {
243 #[serde(rename = "sha256")] 245 #[serde(rename = "sha256")]
244 Sha256, 246 Sha256,
247 #[serde(other)]
248 Unknown,
245} 249}
246 250
247impl Default for HashAlgo { 251impl 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)]
254struct BatchRequestObject { 258struct 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)]
269struct BatchRequest { 273struct 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]
972fn 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}