Skip to content

Commit 3e195f6

Browse files
authored
Fixed media type suffix detection (#714)
1 parent c215812 commit 3e195f6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lambda-http/src/response.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ where
282282
}
283283

284284
for suffix in TEXT_ENCODING_SUFFIXES {
285-
if content_type.ends_with(suffix) {
285+
let mut parts = content_type.trim().split(';');
286+
let mime_type = parts.next().unwrap_or_default();
287+
if mime_type.ends_with(suffix) {
286288
return convert_to_text(self, content_type);
287289
}
288290
}
@@ -484,6 +486,24 @@ mod tests {
484486
)
485487
}
486488

489+
#[tokio::test]
490+
async fn charset_content_type_header_suffix() {
491+
// Drive the implementation by using `hyper::Body` instead of
492+
// of `aws_lambda_events::encodings::Body`
493+
let response = Response::builder()
494+
.header(CONTENT_TYPE, "application/graphql-response+json; charset=utf-16")
495+
.body(HyperBody::from("000000".as_bytes()))
496+
.expect("unable to build http::Response");
497+
let response = response.into_response().await;
498+
let response = LambdaResponse::from_response(&RequestOrigin::ApiGatewayV2, response);
499+
500+
let json = serde_json::to_string(&response).expect("failed to serialize to json");
501+
assert_eq!(
502+
json,
503+
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":[]}"#
504+
)
505+
}
506+
487507
#[tokio::test]
488508
async fn content_headers_unset() {
489509
// Drive the implementation by using `hyper::Body` instead of

0 commit comments

Comments
 (0)