aws_lambda_events/event/alb/
mod.rs

1use crate::{
2    custom_serde::{
3        deserialize_headers, deserialize_nullish_boolean, http_method, serialize_headers,
4        serialize_multi_value_headers, serialize_query_string_parameters,
5    },
6    encodings::Body,
7};
8use http::{HeaderMap, Method};
9use query_map::QueryMap;
10use serde::{Deserialize, Serialize};
11#[cfg(feature = "catch-all-fields")]
12use serde_json::Value;
13
14/// `AlbTargetGroupRequest` contains data originating from the ALB Lambda target group integration
15#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
16#[serde(rename_all = "camelCase")]
17pub struct AlbTargetGroupRequest {
18    #[serde(with = "http_method")]
19    pub http_method: Method,
20    #[serde(default)]
21    pub path: Option<String>,
22    #[serde(default)]
23    #[serde(serialize_with = "serialize_query_string_parameters")]
24    pub query_string_parameters: QueryMap,
25    #[serde(default)]
26    pub multi_value_query_string_parameters: QueryMap,
27    #[serde(deserialize_with = "deserialize_headers", default)]
28    #[serde(serialize_with = "serialize_headers")]
29    pub headers: HeaderMap,
30    #[serde(deserialize_with = "deserialize_headers", default)]
31    #[serde(serialize_with = "serialize_multi_value_headers")]
32    pub multi_value_headers: HeaderMap,
33    pub request_context: AlbTargetGroupRequestContext,
34    #[serde(default, deserialize_with = "deserialize_nullish_boolean")]
35    pub is_base64_encoded: bool,
36    pub body: Option<String>,
37    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
38    /// Enabled with Cargo feature `catch-all-fields`.
39    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
40    #[cfg(feature = "catch-all-fields")]
41    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
42    #[serde(flatten)]
43    pub other: serde_json::Map<String, Value>,
44}
45
46/// `AlbTargetGroupRequestContext` contains the information to identify the load balancer invoking the lambda
47#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
48#[serde(rename_all = "camelCase")]
49pub struct AlbTargetGroupRequestContext {
50    pub elb: ElbContext,
51    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
52    /// Enabled with Cargo feature `catch-all-fields`.
53    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
54    #[cfg(feature = "catch-all-fields")]
55    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
56    #[serde(flatten)]
57    pub other: serde_json::Map<String, Value>,
58}
59
60/// `ElbContext` contains the information to identify the ARN invoking the lambda
61#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
62#[serde(rename_all = "camelCase")]
63pub struct ElbContext {
64    /// nolint: stylecheck
65    #[serde(default)]
66    pub target_group_arn: Option<String>,
67    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
68    /// Enabled with Cargo feature `catch-all-fields`.
69    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
70    #[cfg(feature = "catch-all-fields")]
71    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
72    #[serde(flatten)]
73    pub other: serde_json::Map<String, Value>,
74}
75
76/// `AlbTargetGroupResponse` configures the response to be returned by the ALB Lambda target group for the request
77#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
78#[serde(rename_all = "camelCase")]
79pub struct AlbTargetGroupResponse {
80    pub status_code: i64,
81    #[serde(default)]
82    pub status_description: Option<String>,
83    #[serde(deserialize_with = "http_serde::header_map::deserialize", default)]
84    #[serde(serialize_with = "serialize_headers")]
85    pub headers: HeaderMap,
86    #[serde(deserialize_with = "http_serde::header_map::deserialize", default)]
87    #[serde(serialize_with = "serialize_multi_value_headers")]
88    pub multi_value_headers: HeaderMap,
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub body: Option<Body>,
91    #[serde(default, deserialize_with = "deserialize_nullish_boolean")]
92    pub is_base64_encoded: bool,
93    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
94    /// Enabled with Cargo feature `catch-all-fields`.
95    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
96    #[cfg(feature = "catch-all-fields")]
97    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
98    #[serde(flatten)]
99    pub other: serde_json::Map<String, Value>,
100}
101
102#[cfg(test)]
103mod test {
104    use super::*;
105    use serde_json::Value;
106
107    #[test]
108    #[cfg(feature = "alb")]
109    fn example_alb_lambda_target_request_headers_only() {
110        let data = include_bytes!("../../fixtures/example-alb-lambda-target-request-headers-only.json");
111        let parsed: AlbTargetGroupRequest = serde_json::from_slice(data).unwrap();
112        let output: String = serde_json::to_string(&parsed).unwrap();
113        let reparsed: AlbTargetGroupRequest = serde_json::from_slice(output.as_bytes()).unwrap();
114        assert_eq!(parsed, reparsed);
115    }
116
117    #[test]
118    #[cfg(feature = "alb")]
119    fn example_alb_lambda_target_request_multivalue_headers() {
120        let data = include_bytes!("../../fixtures/example-alb-lambda-target-request-multivalue-headers.json");
121        let parsed: AlbTargetGroupRequest = serde_json::from_slice(data).unwrap();
122        let output: String = serde_json::to_string(&parsed).unwrap();
123        let reparsed: AlbTargetGroupRequest = serde_json::from_slice(output.as_bytes()).unwrap();
124        assert_eq!(parsed, reparsed);
125    }
126
127    #[test]
128    #[cfg(feature = "alb")]
129    fn ensure_alb_lambda_target_request_query_string_parameter_value_is_string() {
130        let data = include_bytes!("../../fixtures/example-alb-lambda-target-request-headers-only.json");
131        let parsed: AlbTargetGroupRequest = serde_json::from_slice(data).unwrap();
132        let output: String = serde_json::to_string(&parsed).unwrap();
133        let reparsed: Value = serde_json::from_slice(output.as_bytes()).unwrap();
134        assert_eq!(
135            reparsed["queryStringParameters"]["key"],
136            Value::String("hello".to_string())
137        );
138    }
139
140    #[test]
141    #[cfg(feature = "alb")]
142    fn example_alb_lambda_target_response() {
143        let data = include_bytes!("../../fixtures/example-alb-lambda-target-response.json");
144        let parsed: AlbTargetGroupResponse = serde_json::from_slice(data).unwrap();
145        let output: String = serde_json::to_string(&parsed).unwrap();
146        let reparsed: AlbTargetGroupResponse = serde_json::from_slice(output.as_bytes()).unwrap();
147        assert_eq!(parsed, reparsed);
148    }
149}