Skip to main content

aws_lambda_events/event/alb/
mod.rs

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