Skip to content

Commit ed40f5a

Browse files
authored
Pass cookies from LambdaRequest::ApiGatewayV2 to Request (#258)
1 parent a9de2fc commit ed40f5a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lambda-http/src/request.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,22 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
311311
LambdaRequest::ApiGatewayV2 {
312312
raw_path,
313313
raw_query_string,
314-
headers,
314+
mut headers,
315315
query_string_parameters,
316316
path_parameters,
317317
stage_variables,
318318
body,
319319
is_base64_encoded,
320320
request_context,
321+
cookies,
321322
..
322323
} => {
324+
if let Some(cookies) = cookies {
325+
if let Ok(header_value) = http::header::HeaderValue::from_str(&cookies.join(";")) {
326+
headers.append(http::header::COOKIE, header_value);
327+
}
328+
}
329+
323330
let builder = http::Request::builder()
324331
.method(request_context.http.method.as_ref())
325332
.uri({
@@ -569,8 +576,15 @@ mod tests {
569576
format!("event was not parsed as expected {:?} given {}", result, input)
570577
);
571578
let req = result.expect("failed to parse request");
579+
let cookie_header = req
580+
.headers()
581+
.get(http::header::COOKIE)
582+
.ok_or_else(|| "Cookie header not found".to_string())
583+
.and_then(|v| v.to_str().map_err(|e| e.to_string()));
584+
572585
assert_eq!(req.method(), "POST");
573586
assert_eq!(req.uri(), "https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1&parameter1=value2&parameter2=value");
587+
assert_eq!(cookie_header, Ok("cookie1=value1;cookie2=value2"));
574588
}
575589

576590
#[test]

lambda-http/tests/data/apigw_v2_proxy_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"routeKey": "$default",
44
"rawPath": "/my/path",
55
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6-
"cookies": [ "cookie1", "cookie2" ],
6+
"cookies": [ "cookie1=value1", "cookie2=value2" ],
77
"headers": {
88
"Header1": "value1",
99
"Header2": "value2"

0 commit comments

Comments
 (0)