Skip to content

Commit

Permalink
fix: do not duplicate headers (single val, multi val) on return (#852)
Browse files Browse the repository at this point in the history
* fix: do not duplicate headers (single val, multi val) on return

* fix: add explainer comment

* fix: rustfmt
  • Loading branch information
jreppnow authored Apr 1, 2024
1 parent c7a3014 commit 5b1de1d
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lambda-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ impl LambdaResponse {
body,
is_base64_encoded,
status_code: status_code as i64,
headers: headers.clone(),
// explicitly empty, as API gateway does not properly merge headers and
// multi-value-headers, resulting in duplicate headers
headers: HeaderMap::new(),
multi_value_headers: headers,
}),
#[cfg(feature = "apigw_http")]
Expand All @@ -91,7 +93,9 @@ impl LambdaResponse {
is_base64_encoded,
status_code: status_code as i64,
cookies,
headers: headers.clone(),
// explicitly empty, as API gateway does not properly merge headers and
// multi-value-headers, resulting in duplicate headers
headers: HeaderMap::new(),
multi_value_headers: headers,
})
}
Expand All @@ -100,7 +104,9 @@ impl LambdaResponse {
body,
status_code: status_code as i64,
is_base64_encoded,
headers: headers.clone(),
// explicitly empty, as API gateway does not properly merge headers and
// multi-value-headers, resulting in duplicate headers
headers: HeaderMap::new(),
multi_value_headers: headers,
status_description: Some(format!(
"{} {}",
Expand All @@ -113,7 +119,9 @@ impl LambdaResponse {
body,
is_base64_encoded,
status_code: status_code as i64,
headers: headers.clone(),
// explicitly empty, as API gateway does not properly merge headers and
// multi-value-headers, resulting in duplicate headers
headers: HeaderMap::new(),
multi_value_headers: headers,
}),
#[cfg(feature = "pass_through")]
Expand Down Expand Up @@ -465,7 +473,7 @@ mod tests {
let json = serde_json::to_string(&response).expect("failed to serialize to json");
assert_eq!(
json,
r#"{"statusCode":200,"headers":{"content-encoding":"gzip"},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
)
}

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

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

Expand All @@ -519,7 +527,7 @@ mod tests {
let json = serde_json::to_string(&response).expect("failed to serialize to json");
assert_eq!(
json,
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":[]}"#
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/graphql-response+json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
)
}

Expand Down Expand Up @@ -553,7 +561,7 @@ mod tests {
let json = serde_json::to_string(&res).expect("failed to serialize to json");
assert_eq!(
json,
r#"{"statusCode":200,"headers":{"multi":"a"},"multiValueHeaders":{"multi":["a","b"]},"isBase64Encoded":false}"#
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"multi":["a","b"]},"isBase64Encoded":false}"#
)
}

Expand Down

0 comments on commit 5b1de1d

Please sign in to comment.