1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! ALB and API Gateway request adaptations
//!
//! Typically these are exposed via the [`request_context()`] or [`request_context_ref()`]
//! request extension methods provided by the [`RequestExt`] trait.
//!
//! [`request_context()`]: crate::RequestExt::request_context()
//! [`request_context_ref()`]: crate::RequestExt::request_context_ref()
//! [`RequestExt`]: crate::RequestExt
#[cfg(any(feature = "apigw_rest", feature = "apigw_http", feature = "apigw_websockets"))]
use crate::ext::extensions::{PathParameters, StageVariables};
use crate::ext::extensions::{QueryStringParameters, RawHttpPath};
#[cfg(feature = "alb")]
use aws_lambda_events::alb::{AlbTargetGroupRequest, AlbTargetGroupRequestContext};
#[cfg(feature = "apigw_rest")]
use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyRequestContext};
#[cfg(feature = "apigw_http")]
use aws_lambda_events::apigw::{ApiGatewayV2httpRequest, ApiGatewayV2httpRequestContext};
#[cfg(feature = "apigw_websockets")]
use aws_lambda_events::apigw::{ApiGatewayWebsocketProxyRequest, ApiGatewayWebsocketProxyRequestContext};
use aws_lambda_events::{encodings::Body, query_map::QueryMap};
use http::header::HeaderName;
use http::{HeaderMap, HeaderValue};
use serde::{Deserialize, Serialize};
use serde_json::error::Error as JsonError;
use std::future::Future;
use std::pin::Pin;
use std::{env, io::Read, mem};
use url::Url;

/// Internal representation of an Lambda http event from
/// ALB, API Gateway REST and HTTP API proxy event perspectives
///
/// This is not intended to be a type consumed by crate users directly. The order
/// of the variants are notable. Serde will try to deserialize in this order.
#[doc(hidden)]
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum LambdaRequest {
    #[cfg(feature = "apigw_rest")]
    ApiGatewayV1(ApiGatewayProxyRequest),
    #[cfg(feature = "apigw_http")]
    ApiGatewayV2(ApiGatewayV2httpRequest),
    #[cfg(feature = "alb")]
    Alb(AlbTargetGroupRequest),
    #[cfg(feature = "apigw_websockets")]
    WebSocket(ApiGatewayWebsocketProxyRequest),
}

impl LambdaRequest {
    /// Return the `RequestOrigin` of the request to determine where the `LambdaRequest`
    /// originated from, so that the appropriate response can be selected based on what
    /// type of response the request origin expects.
    pub fn request_origin(&self) -> RequestOrigin {
        match self {
            #[cfg(feature = "apigw_rest")]
            LambdaRequest::ApiGatewayV1 { .. } => RequestOrigin::ApiGatewayV1,
            #[cfg(feature = "apigw_http")]
            LambdaRequest::ApiGatewayV2 { .. } => RequestOrigin::ApiGatewayV2,
            #[cfg(feature = "alb")]
            LambdaRequest::Alb { .. } => RequestOrigin::Alb,
            #[cfg(feature = "apigw_websockets")]
            LambdaRequest::WebSocket { .. } => RequestOrigin::WebSocket,
        }
    }
}

/// RequestFuture type
pub type RequestFuture<'a, R, E> = Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>;

/// Represents the origin from which the lambda was requested from.
#[doc(hidden)]
#[derive(Debug, Clone)]
pub enum RequestOrigin {
    /// API Gateway request origin
    #[cfg(feature = "apigw_rest")]
    ApiGatewayV1,
    /// API Gateway v2 request origin
    #[cfg(feature = "apigw_http")]
    ApiGatewayV2,
    /// ALB request origin
    #[cfg(feature = "alb")]
    Alb,
    /// API Gateway WebSocket
    #[cfg(feature = "apigw_websockets")]
    WebSocket,
}

#[cfg(feature = "apigw_http")]
fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Body> {
    let http_method = ag.request_context.http.method.clone();
    let host = ag
        .headers
        .get(http::header::HOST)
        .and_then(|s| s.to_str().ok())
        .or(ag.request_context.domain_name.as_deref());
    let raw_path = ag.raw_path.unwrap_or_default();
    let path = apigw_path_with_stage(&ag.request_context.stage, &raw_path);

    // don't use the query_string_parameters from API GW v2 to
    // populate the QueryStringParameters extension because
    // the value is not compatible with the whatgw specification.
    // See: https://github.com/awslabs/aws-lambda-rust-runtime/issues/470
    // See: https://url.spec.whatwg.org/#urlencoded-parsing
    let query_string_parameters = if let Some(query) = &ag.raw_query_string {
        query.parse().unwrap() // this is Infallible
    } else {
        ag.query_string_parameters
    };

    let mut uri = build_request_uri(&path, &ag.headers, host, None);
    if let Some(query) = ag.raw_query_string {
        uri.push('?');
        uri.push_str(&query);
    }

    let builder = http::Request::builder()
        .uri(uri)
        .extension(RawHttpPath(raw_path))
        .extension(QueryStringParameters(query_string_parameters))
        .extension(PathParameters(QueryMap::from(ag.path_parameters)))
        .extension(StageVariables(QueryMap::from(ag.stage_variables)))
        .extension(RequestContext::ApiGatewayV2(ag.request_context));

    let mut headers = ag.headers;
    update_xray_trace_id_header(&mut headers);
    if let Some(cookies) = ag.cookies {
        if let Ok(header_value) = HeaderValue::from_str(&cookies.join(";")) {
            headers.insert(http::header::COOKIE, header_value);
        }
    }

    let base64 = ag.is_base64_encoded;

    let mut req = builder
        .body(
            ag.body
                .as_deref()
                .map_or_else(Body::default, |b| Body::from_maybe_encoded(base64, b)),
        )
        .expect("failed to build request");

    // no builder method that sets headers in batch
    let _ = mem::replace(req.headers_mut(), headers);
    let _ = mem::replace(req.method_mut(), http_method);

    req
}

fn update_xray_trace_id_header(headers: &mut HeaderMap) {
    if let Ok(xray_trace_id) = env::var("_X_AMZN_TRACE_ID") {
        if let Ok(header_value) = HeaderValue::from_str(&xray_trace_id) {
            headers.insert(HeaderName::from_static("x-amzn-trace-id"), header_value);
        }
    }
}
#[cfg(feature = "apigw_rest")]
fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
    let http_method = ag.http_method;
    let host = ag
        .headers
        .get(http::header::HOST)
        .and_then(|s| s.to_str().ok())
        .or(ag.request_context.domain_name.as_deref());
    let raw_path = ag.path.unwrap_or_default();
    let path = apigw_path_with_stage(&ag.request_context.stage, &raw_path);

    let builder = http::Request::builder()
        .uri(build_request_uri(
            &path,
            &ag.headers,
            host,
            Some((&ag.multi_value_query_string_parameters, &ag.query_string_parameters)),
        ))
        .extension(RawHttpPath(raw_path))
        // multi-valued query string parameters are always a super
        // set of singly valued query string parameters,
        // when present, multi-valued query string parameters are preferred
        .extension(QueryStringParameters(
            if ag.multi_value_query_string_parameters.is_empty() {
                ag.query_string_parameters
            } else {
                ag.multi_value_query_string_parameters
            },
        ))
        .extension(PathParameters(QueryMap::from(ag.path_parameters)))
        .extension(StageVariables(QueryMap::from(ag.stage_variables)))
        .extension(RequestContext::ApiGatewayV1(ag.request_context));

    // merge headers into multi_value_headers and make
    // multi-value_headers our cannoncial source of request headers
    let mut headers = ag.multi_value_headers;
    headers.extend(ag.headers);
    update_xray_trace_id_header(&mut headers);

    let base64 = ag.is_base64_encoded.unwrap_or_default();
    let mut req = builder
        .body(
            ag.body
                .as_deref()
                .map_or_else(Body::default, |b| Body::from_maybe_encoded(base64, b)),
        )
        .expect("failed to build request");

    // no builder method that sets headers in batch
    let _ = mem::replace(req.headers_mut(), headers);
    let _ = mem::replace(req.method_mut(), http_method);

    req
}

#[cfg(feature = "alb")]
fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
    let http_method = alb.http_method;
    let host = alb.headers.get(http::header::HOST).and_then(|s| s.to_str().ok());
    let raw_path = alb.path.unwrap_or_default();

    let query_string_parameters = decode_query_map(alb.query_string_parameters);
    let multi_value_query_string_parameters = decode_query_map(alb.multi_value_query_string_parameters);

    let builder = http::Request::builder()
        .uri(build_request_uri(
            &raw_path,
            &alb.headers,
            host,
            Some((&multi_value_query_string_parameters, &query_string_parameters)),
        ))
        .extension(RawHttpPath(raw_path))
        // multi valued query string parameters are always a super
        // set of singly valued query string parameters,
        // when present, multi-valued query string parameters are preferred
        .extension(QueryStringParameters(
            if multi_value_query_string_parameters.is_empty() {
                query_string_parameters
            } else {
                multi_value_query_string_parameters
            },
        ))
        .extension(RequestContext::Alb(alb.request_context));

    // merge headers into multi_value_headers and make
    // multi-value_headers our cannoncial source of request headers
    let mut headers = alb.multi_value_headers;
    headers.extend(alb.headers);
    update_xray_trace_id_header(&mut headers);

    let base64 = alb.is_base64_encoded;

    let mut req = builder
        .body(
            alb.body
                .as_deref()
                .map_or_else(Body::default, |b| Body::from_maybe_encoded(base64, b)),
        )
        .expect("failed to build request");

    // no builder method that sets headers in batch
    let _ = mem::replace(req.headers_mut(), headers);
    let _ = mem::replace(req.method_mut(), http_method);

    req
}

#[cfg(feature = "alb")]
fn decode_query_map(query_map: QueryMap) -> QueryMap {
    use std::str::FromStr;

    let query_string = query_map.to_query_string();
    let decoded = percent_encoding::percent_decode(query_string.as_bytes()).decode_utf8_lossy();
    QueryMap::from_str(&decoded).unwrap_or_default()
}

#[cfg(feature = "apigw_websockets")]
fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<Body> {
    let http_method = ag.http_method;
    let host = ag
        .headers
        .get(http::header::HOST)
        .and_then(|s| s.to_str().ok())
        .or(ag.request_context.domain_name.as_deref());
    let path = apigw_path_with_stage(&ag.request_context.stage, &ag.path.unwrap_or_default());

    let builder = http::Request::builder()
        .uri(build_request_uri(
            &path,
            &ag.headers,
            host,
            Some((&ag.multi_value_query_string_parameters, &ag.query_string_parameters)),
        ))
        // multi-valued query string parameters are always a super
        // set of singly valued query string parameters,
        // when present, multi-valued query string parameters are preferred
        .extension(QueryStringParameters(
            if ag.multi_value_query_string_parameters.is_empty() {
                ag.query_string_parameters
            } else {
                ag.multi_value_query_string_parameters
            },
        ))
        .extension(PathParameters(QueryMap::from(ag.path_parameters)))
        .extension(StageVariables(QueryMap::from(ag.stage_variables)))
        .extension(RequestContext::WebSocket(ag.request_context));

    // merge headers into multi_value_headers and make
    // multi-value_headers our canonical source of request headers
    let mut headers = ag.multi_value_headers;
    headers.extend(ag.headers);
    update_xray_trace_id_header(&mut headers);

    let base64 = ag.is_base64_encoded.unwrap_or_default();
    let mut req = builder
        .body(
            ag.body
                .as_deref()
                .map_or_else(Body::default, |b| Body::from_maybe_encoded(base64, b)),
        )
        .expect("failed to build request");

    // no builder method that sets headers in batch
    let _ = mem::replace(req.headers_mut(), headers);
    let _ = mem::replace(req.method_mut(), http_method.unwrap_or(http::Method::GET));

    req
}

#[cfg(any(feature = "apigw_rest", feature = "apigw_http", feature = "apigw_websockets"))]
fn apigw_path_with_stage(stage: &Option<String>, path: &str) -> String {
    match stage {
        None => path.into(),
        Some(stage) if stage == "$default" => path.into(),
        Some(stage) => format!("/{stage}{path}"),
    }
}

/// Event request context as an enumeration of request contexts
/// for both ALB and API Gateway and HTTP API events
#[derive(Deserialize, Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum RequestContext {
    /// API Gateway proxy request context
    #[cfg(feature = "apigw_rest")]
    ApiGatewayV1(ApiGatewayProxyRequestContext),
    /// API Gateway v2 request context
    #[cfg(feature = "apigw_http")]
    ApiGatewayV2(ApiGatewayV2httpRequestContext),
    /// ALB request context
    #[cfg(feature = "alb")]
    Alb(AlbTargetGroupRequestContext),
    /// WebSocket request context
    #[cfg(feature = "apigw_websockets")]
    WebSocket(ApiGatewayWebsocketProxyRequestContext),
}

/// Converts LambdaRequest types into `http::Request<Body>` types
impl From<LambdaRequest> for http::Request<Body> {
    fn from(value: LambdaRequest) -> Self {
        match value {
            #[cfg(feature = "apigw_rest")]
            LambdaRequest::ApiGatewayV1(ag) => into_proxy_request(ag),
            #[cfg(feature = "apigw_http")]
            LambdaRequest::ApiGatewayV2(ag) => into_api_gateway_v2_request(ag),
            #[cfg(feature = "alb")]
            LambdaRequest::Alb(alb) => into_alb_request(alb),
            #[cfg(feature = "apigw_websockets")]
            LambdaRequest::WebSocket(ag) => into_websocket_request(ag),
        }
    }
}

/// Deserializes a `Request` from a `Read` impl providing JSON events.
///
/// # Example
///
/// ```rust,no_run
/// use lambda_http::request::from_reader;
/// use std::fs::File;
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
///     let request = from_reader(
///         File::open("path/to/request.json")?
///     )?;
///     Ok(println!("{:#?}", request))
/// }
/// ```
pub fn from_reader<R>(rdr: R) -> Result<crate::Request, JsonError>
where
    R: Read,
{
    serde_json::from_reader(rdr).map(LambdaRequest::into)
}

/// Deserializes a `Request` from a string of JSON text.
///
/// # Example
///
/// ```rust,no_run
/// use lambda_http::request::from_str;
/// use std::fs::File;
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
///     let request = from_str(
///         r#"{ ...raw json here... }"#
///     )?;
///     Ok(println!("{:#?}", request))
/// }
/// ```
pub fn from_str(s: &str) -> Result<crate::Request, JsonError> {
    serde_json::from_str(s).map(LambdaRequest::into)
}

fn x_forwarded_proto() -> HeaderName {
    HeaderName::from_static("x-forwarded-proto")
}

fn build_request_uri(
    path: &str,
    headers: &HeaderMap,
    host: Option<&str>,
    queries: Option<(&QueryMap, &QueryMap)>,
) -> String {
    let mut url = match host {
        None => {
            let rel_url = Url::parse(&format!("http://localhost{path}")).unwrap();
            rel_url.path().to_string()
        }
        Some(host) => {
            let scheme = headers
                .get(x_forwarded_proto())
                .and_then(|s| s.to_str().ok())
                .unwrap_or("https");
            let url = format!("{scheme}://{host}{path}");
            Url::parse(&url).unwrap().to_string()
        }
    };

    if let Some((mv, sv)) = queries {
        if !mv.is_empty() {
            url.push('?');
            url.push_str(&mv.to_query_string());
        } else if !sv.is_empty() {
            url.push('?');
            url.push_str(&sv.to_query_string());
        }
    }

    url
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ext::RequestExt;
    use std::fs::File;

    #[test]
    fn deserializes_apigw_request_events_from_readables() {
        // from the docs
        // https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-api-gateway-request
        // note: file paths are relative to the directory of the crate at runtime
        let result = from_reader(File::open("tests/data/apigw_proxy_request.json").expect("expected file"));
        assert!(result.is_ok(), "event was not parsed as expected {result:?}");
    }

    #[test]
    fn deserializes_minimal_apigw_http_request_events() {
        // from the docs
        // https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-api-gateway-request
        let input = include_str!("../tests/data/apigw_v2_proxy_request_minimal.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(req.uri(), "https://xxx.execute-api.us-east-1.amazonaws.com/");

        // Ensure this is an APIGWv2 request
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::ApiGatewayV2(_)),
            "expected ApiGatewayV2 context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_apigw_http_request_events() {
        // from the docs
        // https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-api-gateway-request
        let input = include_str!("../tests/data/apigw_v2_proxy_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        let cookie_header = req
            .headers()
            .get(http::header::COOKIE)
            .ok_or_else(|| "Cookie header not found".to_string())
            .and_then(|v| v.to_str().map_err(|e| e.to_string()));

        assert_eq!(req.method(), "POST");
        assert_eq!(req.uri(), "https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1&parameter1=value2&parameter2=value");
        assert_eq!(cookie_header, Ok("cookie1=value1;cookie2=value2"));

        // Ensure this is an APIGWv2 request
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::ApiGatewayV2(_)),
            "expected ApiGatewayV2 context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_apigw_request_events() {
        // from the docs
        // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
        // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
        let input = include_str!("../tests/data/apigw_proxy_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(
            req.uri(),
            "https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/test/hello?name=me"
        );

        // Ensure this is an APIGW request
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::ApiGatewayV1(_)),
            "expected ApiGateway context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_lambda_function_url_request_events() {
        // from the docs
        // https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
        let input = include_str!("../tests/data/lambda_function_url_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        let cookie_header = req
            .headers()
            .get_all(http::header::COOKIE)
            .iter()
            .map(|v| v.to_str().unwrap().to_string())
            .reduce(|acc, nxt| [acc, nxt].join(";"));

        assert_eq!(req.method(), "GET");
        assert_eq!(
            req.uri(),
            "https://id.lambda-url.eu-west-2.on.aws/my/path?parameter1=value1&parameter1=value2&parameter2=value"
        );
        assert_eq!(cookie_header, Some("test=hi".to_string()));

        // Ensure this is an APIGWv2 request (Lambda Function URL requests confirm to API GW v2 Request format)
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::ApiGatewayV2(_)),
            "expected ApiGatewayV2 context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_alb_request_events() {
        // from the docs
        // https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
        let input = include_str!("../tests/data/alb_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(
            req.uri(),
            "https://lambda-846800462-us-east-2.elb.amazonaws.com/?myKey=val2"
        );

        // Ensure this is an ALB request
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::Alb(_)),
            "expected Alb context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_alb_request_encoded_query_parameters_events() {
        // from the docs
        // https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers
        let input = include_str!("../tests/data/alb_request_encoded_query_parameters.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(
            req.uri(),
            "https://lambda-846800462-us-east-2.elb.amazonaws.com/?myKey=%3FshowAll%3Dtrue"
        );

        // Ensure this is an ALB request
        let req_context = req.request_context_ref().expect("Request is missing RequestContext");
        assert!(
            matches!(req_context, &RequestContext::Alb(_)),
            "expected Alb context, got {req_context:?}"
        );
    }

    #[test]
    fn deserializes_apigw_multi_value_request_events() {
        // from docs
        // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
        let input = include_str!("../tests/data/apigw_multi_value_proxy_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event is was not parsed as expected {result:?} given {input}"
        );
        let request = result.expect("failed to parse request");

        assert!(!request
            .query_string_parameters_ref()
            .expect("Request is missing query parameters")
            .is_empty());

        // test RequestExt#query_string_parameters_ref does the right thing
        assert_eq!(
            request
                .query_string_parameters_ref()
                .and_then(|params| params.all("multivalueName")),
            Some(vec!["you", "me"])
        );
    }

    #[test]
    fn deserializes_alb_multi_value_request_events() {
        // from docs
        // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
        let input = include_str!("../tests/data/alb_multi_value_request.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event is was not parsed as expected {result:?} given {input}"
        );
        let request = result.expect("failed to parse request");
        assert!(!request
            .query_string_parameters_ref()
            .expect("Request is missing query parameters")
            .is_empty());

        // test RequestExt#query_string_parameters_ref does the right thing
        assert_eq!(
            request
                .query_string_parameters_ref()
                .and_then(|params| params.all("myKey")),
            Some(vec!["val1", "val2"])
        );
    }

    #[test]
    fn deserializes_alb_multi_value_request_encoded_query_parameters_events() {
        // from docs
        // https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
        let input = include_str!("../tests/data/alb_multi_value_request_encoded_query_parameters.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event is was not parsed as expected {result:?} given {input}"
        );
        let request = result.expect("failed to parse request");
        assert!(!request
            .query_string_parameters_ref()
            .expect("Request is missing query parameters")
            .is_empty());

        // test RequestExt#query_string_parameters_ref does the right thing
        assert_eq!(
            request
                .query_string_parameters_ref()
                .and_then(|params| params.all("myKey")),
            Some(vec!["?showAll=true", "?showAll=false"])
        );
    }

    #[test]
    fn deserialize_apigw_http_sam_local() {
        // manually generated from AWS SAM CLI
        // Steps to recreate:
        // * sam init
        // * Use, Zip Python 3.9, and Hello World example
        // * Change the template to use HttpApi instead of Api
        // * Change the function code to return the Lambda event serialized
        // * sam local start-api
        // * Invoke the API
        let input = include_str!("../tests/data/apigw_v2_sam_local.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(req.uri(), "http://127.0.0.1:3000/hello");
    }

    #[test]
    fn deserialize_apigw_no_host() {
        // generated from the 'apigateway-aws-proxy' test event template in the Lambda console
        let input = include_str!("../tests/data/apigw_no_host.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(req.uri(), "/test/test/hello?name=me");
    }

    #[test]
    fn deserialize_alb_no_host() {
        // generated from ALB health checks
        let input = include_str!("../tests/data/alb_no_host.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.method(), "GET");
        assert_eq!(req.uri(), "/v1/health/");
    }

    #[test]
    fn deserialize_apigw_path_with_space() {
        // generated from ALB health checks
        let input = include_str!("../tests/data/apigw_request_path_with_space.json");
        let result = from_str(input);
        assert!(
            result.is_ok(),
            "event was not parsed as expected {result:?} given {input}"
        );
        let req = result.expect("failed to parse request");
        assert_eq!(req.uri(), "https://id.execute-api.us-east-1.amazonaws.com/my/path-with%20space?parameter1=value1&parameter1=value2&parameter2=value");
    }

    #[test]
    fn parse_paths_with_spaces() {
        let url = build_request_uri("/path with spaces/and multiple segments", &HeaderMap::new(), None, None);
        assert_eq!("/path%20with%20spaces/and%20multiple%20segments", url);
    }
}