Skip to content

Commit 5b1de1d

Browse files
authored
fix: do not duplicate headers (single val, multi val) on return (#852)
* fix: do not duplicate headers (single val, multi val) on return * fix: add explainer comment * fix: rustfmt
1 parent c7a3014 commit 5b1de1d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lambda-http/src/response.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ impl LambdaResponse {
6969
body,
7070
is_base64_encoded,
7171
status_code: status_code as i64,
72-
headers: headers.clone(),
72+
// explicitly empty, as API gateway does not properly merge headers and
73+
// multi-value-headers, resulting in duplicate headers
74+
headers: HeaderMap::new(),
7375
multi_value_headers: headers,
7476
}),
7577
#[cfg(feature = "apigw_http")]
@@ -91,7 +93,9 @@ impl LambdaResponse {
9193
is_base64_encoded,
9294
status_code: status_code as i64,
9395
cookies,
94-
headers: headers.clone(),
96+
// explicitly empty, as API gateway does not properly merge headers and
97+
// multi-value-headers, resulting in duplicate headers
98+
headers: HeaderMap::new(),
9599
multi_value_headers: headers,
96100
})
97101
}
@@ -100,7 +104,9 @@ impl LambdaResponse {
100104
body,
101105
status_code: status_code as i64,
102106
is_base64_encoded,
103-
headers: headers.clone(),
107+
// explicitly empty, as API gateway does not properly merge headers and
108+
// multi-value-headers, resulting in duplicate headers
109+
headers: HeaderMap::new(),
104110
multi_value_headers: headers,
105111
status_description: Some(format!(
106112
"{} {}",
@@ -113,7 +119,9 @@ impl LambdaResponse {
113119
body,
114120
is_base64_encoded,
115121
status_code: status_code as i64,
116-
headers: headers.clone(),
122+
// explicitly empty, as API gateway does not properly merge headers and
123+
// multi-value-headers, resulting in duplicate headers
124+
headers: HeaderMap::new(),
117125
multi_value_headers: headers,
118126
}),
119127
#[cfg(feature = "pass_through")]
@@ -465,7 +473,7 @@ mod tests {
465473
let json = serde_json::to_string(&response).expect("failed to serialize to json");
466474
assert_eq!(
467475
json,
468-
r#"{"statusCode":200,"headers":{"content-encoding":"gzip"},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
476+
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
469477
)
470478
}
471479

@@ -483,7 +491,7 @@ mod tests {
483491
let json = serde_json::to_string(&response).expect("failed to serialize to json");
484492
assert_eq!(
485493
json,
486-
r#"{"statusCode":200,"headers":{"content-type":"application/json"},"multiValueHeaders":{"content-type":["application/json"]},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
494+
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/json"]},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
487495
)
488496
}
489497

@@ -501,7 +509,7 @@ mod tests {
501509
let json = serde_json::to_string(&response).expect("failed to serialize to json");
502510
assert_eq!(
503511
json,
504-
r#"{"statusCode":200,"headers":{"content-type":"application/json; charset=utf-16"},"multiValueHeaders":{"content-type":["application/json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
512+
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
505513
)
506514
}
507515

@@ -519,7 +527,7 @@ mod tests {
519527
let json = serde_json::to_string(&response).expect("failed to serialize to json");
520528
assert_eq!(
521529
json,
522-
r#"{"statusCode":200,"headers":{"content-type":"application/graphql-response+json; charset=utf-16"},"multiValueHeaders":{"content-type":["application/graphql-response+json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
530+
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/graphql-response+json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
523531
)
524532
}
525533

@@ -553,7 +561,7 @@ mod tests {
553561
let json = serde_json::to_string(&res).expect("failed to serialize to json");
554562
assert_eq!(
555563
json,
556-
r#"{"statusCode":200,"headers":{"multi":"a"},"multiValueHeaders":{"multi":["a","b"]},"isBase64Encoded":false}"#
564+
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"multi":["a","b"]},"isBase64Encoded":false}"#
557565
)
558566
}
559567

0 commit comments

Comments
 (0)