Skip to content

Commit 6b1b3f0

Browse files
authored
fix: add support for null/invalid type for headers (#371)
1 parent 154cf31 commit 6b1b3f0

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

lambda-http/src/request.rs

+61-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ where
352352
}
353353
}
354354

355-
deserializer.deserialize_map(HeaderVisitor)
355+
Ok(deserializer.deserialize_map(HeaderVisitor).unwrap_or_default())
356356
}
357357

358358
/// deserializes (json) null values to their default values
@@ -633,6 +633,17 @@ mod tests {
633633
let req = result.expect("failed to parse request");
634634
assert_eq!(req.method(), "GET");
635635
assert_eq!(req.uri(), "https://xxx.execute-api.us-east-1.amazonaws.com/");
636+
637+
// Ensure this is an APIGWv2 request
638+
let req_context = req.request_context();
639+
assert!(
640+
match req_context {
641+
RequestContext::ApiGatewayV2(_) => true,
642+
_ => false,
643+
},
644+
"expected ApiGatewayV2 context, got {:?}",
645+
req_context
646+
);
636647
}
637648

638649
#[test]
@@ -657,6 +668,17 @@ mod tests {
657668
assert_eq!(req.method(), "POST");
658669
assert_eq!(req.uri(), "https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1&parameter1=value2&parameter2=value");
659670
assert_eq!(cookie_header, Ok("cookie1=value1;cookie2=value2"));
671+
672+
// Ensure this is an APIGWv2 request
673+
let req_context = req.request_context();
674+
assert!(
675+
match req_context {
676+
RequestContext::ApiGatewayV2(_) => true,
677+
_ => false,
678+
},
679+
"expected ApiGatewayV2 context, got {:?}",
680+
req_context
681+
);
660682
}
661683

662684
#[test]
@@ -678,6 +700,17 @@ mod tests {
678700
req.uri(),
679701
"https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello"
680702
);
703+
704+
// Ensure this is an APIGW request
705+
let req_context = req.request_context();
706+
assert!(
707+
match req_context {
708+
RequestContext::ApiGateway(_) => true,
709+
_ => false,
710+
},
711+
"expected ApiGateway context, got {:?}",
712+
req_context
713+
);
681714
}
682715

683716
#[test]
@@ -695,6 +728,17 @@ mod tests {
695728
let req = result.expect("failed to parse request");
696729
assert_eq!(req.method(), "GET");
697730
assert_eq!(req.uri(), "https://lambda-846800462-us-east-2.elb.amazonaws.com/");
731+
732+
// Ensure this is an ALB request
733+
let req_context = req.request_context();
734+
assert!(
735+
match req_context {
736+
RequestContext::Alb(_) => true,
737+
_ => false,
738+
},
739+
"expected Alb context, got {:?}",
740+
req_context
741+
);
698742
}
699743

700744
#[test]
@@ -808,4 +852,20 @@ mod tests {
808852
Test { foo: HashMap::new() }
809853
)
810854
}
855+
856+
#[test]
857+
fn deserialize_null_headers() {
858+
#[derive(Debug, PartialEq, Deserialize)]
859+
struct Test {
860+
#[serde(deserialize_with = "deserialize_headers")]
861+
headers: http::HeaderMap,
862+
}
863+
864+
assert_eq!(
865+
serde_json::from_str::<Test>(r#"{"headers":null}"#).expect("failed to deserialize"),
866+
Test {
867+
headers: http::HeaderMap::new()
868+
}
869+
)
870+
}
811871
}

0 commit comments

Comments
 (0)