Skip to content

Commit d7aec43

Browse files
authored
Extract encoding conversions to make it easier to manage. (#535)
Use two list of values to match content types that need to be returned as text. One list with prefixes, and another list with suffixes. Signed-off-by: David Calavera <[email protected]> Signed-off-by: David Calavera <[email protected]>
1 parent 6fd5270 commit d7aec43

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

lambda-http/src/response.rs

+54-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ use std::{fmt, future::Future, pin::Pin};
2525

2626
const X_LAMBDA_HTTP_CONTENT_ENCODING: &str = "x-lambda-http-content-encoding";
2727

28+
// See list of common MIME types:
29+
// - https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
30+
// - https://github.com/ietf-wg-httpapi/mediatypes/blob/main/draft-ietf-httpapi-yaml-mediatypes.md
31+
const TEXT_ENCODING_PREFIXES: [&'static str; 5] = [
32+
"text",
33+
"application/json",
34+
"application/javascript",
35+
"application/xml",
36+
"application/yaml",
37+
];
38+
39+
const TEXT_ENCODING_SUFFIXES: [&'static str; 3] = ["+xml", "+yaml", "+json"];
40+
2841
/// Representation of Lambda response
2942
#[doc(hidden)]
3043
#[derive(Serialize, Debug)]
@@ -190,15 +203,16 @@ where
190203
return convert_to_text(self, "utf-8");
191204
};
192205

193-
// See list of common MIME types:
194-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
195-
if content_type.starts_with("text")
196-
|| content_type.starts_with("application/json")
197-
|| content_type.starts_with("application/javascript")
198-
|| content_type.starts_with("application/xml")
199-
|| content_type.ends_with("+xml")
200-
{
201-
return convert_to_text(self, content_type);
206+
for prefix in TEXT_ENCODING_PREFIXES {
207+
if content_type.starts_with(prefix) {
208+
return convert_to_text(self, content_type);
209+
}
210+
}
211+
212+
for suffix in TEXT_ENCODING_SUFFIXES {
213+
if content_type.ends_with(suffix) {
214+
return convert_to_text(self, content_type);
215+
}
202216
}
203217

204218
if let Some(value) = headers.get(X_LAMBDA_HTTP_CONTENT_ENCODING) {
@@ -446,4 +460,35 @@ mod tests {
446460
Some("image/svg")
447461
)
448462
}
463+
464+
#[tokio::test]
465+
async fn content_type_yaml_as_text() {
466+
// Drive the implementation by using `hyper::Body` instead of
467+
// of `aws_lambda_events::encodings::Body`
468+
let yaml = r#"---
469+
foo: bar
470+
"#;
471+
472+
let formats = ["application/yaml", "custom/vdn+yaml"];
473+
474+
for format in formats {
475+
let response = Response::builder()
476+
.header(CONTENT_TYPE, format)
477+
.body(HyperBody::from(yaml.as_bytes()))
478+
.expect("unable to build http::Response");
479+
let response = response.into_response().await;
480+
481+
match response.body() {
482+
Body::Text(body) => assert_eq!(yaml, body),
483+
_ => panic!("invalid body"),
484+
}
485+
assert_eq!(
486+
response
487+
.headers()
488+
.get(CONTENT_TYPE)
489+
.map(|h| h.to_str().expect("invalid header")),
490+
Some(format)
491+
)
492+
}
493+
}
449494
}

0 commit comments

Comments
 (0)