Skip to content

Commit 17df82f

Browse files
authored
Fix double cookie header for Lambda Function URL requests (#586)
1 parent e042103 commit 17df82f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

lambda-http/src/request.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
122122
update_xray_trace_id_header(&mut headers);
123123
if let Some(cookies) = ag.cookies {
124124
if let Ok(header_value) = HeaderValue::from_str(&cookies.join(";")) {
125-
headers.append(http::header::COOKIE, header_value);
125+
headers.insert(http::header::COOKIE, header_value);
126126
}
127127
}
128128

@@ -545,6 +545,42 @@ mod tests {
545545
);
546546
}
547547

548+
#[test]
549+
fn deserializes_lambda_function_url_request_events() {
550+
// from the docs
551+
// https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads
552+
let input = include_str!("../tests/data/lambda_function_url_request.json");
553+
let result = from_str(input);
554+
assert!(
555+
result.is_ok(),
556+
"event was not parsed as expected {:?} given {}",
557+
result,
558+
input
559+
);
560+
let req = result.expect("failed to parse request");
561+
let cookie_header = req
562+
.headers()
563+
.get_all(http::header::COOKIE)
564+
.iter()
565+
.map(|v| v.to_str().unwrap().to_string())
566+
.reduce(|acc, nxt| [acc, nxt].join(";"));
567+
568+
assert_eq!(req.method(), "GET");
569+
assert_eq!(
570+
req.uri(),
571+
"https://id.lambda-url.eu-west-2.on.aws/my/path?parameter1=value1&parameter1=value2&parameter2=value"
572+
);
573+
assert_eq!(cookie_header, Some("test=hi".to_string()));
574+
575+
// Ensure this is an APIGWv2 request (Lambda Function URL requests confirm to API GW v2 Request format)
576+
let req_context = req.request_context();
577+
assert!(
578+
matches!(req_context, RequestContext::ApiGatewayV2(_)),
579+
"expected ApiGatewayV2 context, got {:?}",
580+
req_context
581+
);
582+
}
583+
548584
#[test]
549585
fn deserializes_alb_request_events() {
550586
// from the docs
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/my/path",
5+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6+
"cookies": [
7+
"test=hi"
8+
],
9+
"headers": {
10+
"x-amzn-tls-cipher-suite": "ECDHE-RSA-AES128-GCM-SHA256",
11+
"x-amzn-tls-version": "TLSv1.2",
12+
"x-amzn-trace-id": "Root=1-5eb33c07-de25b420912dee103a5db434",
13+
"cookie": "test=hi",
14+
"x-forwarded-proto": "https",
15+
"host": "id.lambda-url.eu-west-2.on.aws",
16+
"x-forwarded-port": "443",
17+
"x-forwarded-for": "65.78.31.245",
18+
"accept": "*/*",
19+
"user-agent": "curl/7.68.0"
20+
},
21+
"requestContext": {
22+
"accountId": "123456789012",
23+
"apiId": "xxx",
24+
"domainName": "id.lambda-url.eu-west-2.on.aws",
25+
"domainPrefix": "id",
26+
"http": {
27+
"method": "GET",
28+
"path": "/my/path",
29+
"protocol": "HTTP/1.1",
30+
"sourceIp": "65.78.31.245",
31+
"userAgent": "curl/7.68.0"
32+
},
33+
"requestId": "MIZRNhJtIAMEMDw=",
34+
"routeKey": "$default",
35+
"stage": "$default",
36+
"time": "11/Jan/2023:11:45:34 +0000",
37+
"timeEpoch": 1673437534837
38+
},
39+
"isBase64Encoded": false
40+
}

0 commit comments

Comments
 (0)