@@ -25,6 +25,19 @@ use std::{fmt, future::Future, pin::Pin};
25
25
26
26
const X_LAMBDA_HTTP_CONTENT_ENCODING : & str = "x-lambda-http-content-encoding" ;
27
27
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
+
28
41
/// Representation of Lambda response
29
42
#[ doc( hidden) ]
30
43
#[ derive( Serialize , Debug ) ]
@@ -190,15 +203,16 @@ where
190
203
return convert_to_text ( self , "utf-8" ) ;
191
204
} ;
192
205
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
+ }
202
216
}
203
217
204
218
if let Some ( value) = headers. get ( X_LAMBDA_HTTP_CONTENT_ENCODING ) {
@@ -446,4 +460,35 @@ mod tests {
446
460
Some ( "image/svg" )
447
461
)
448
462
}
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
+ }
449
494
}
0 commit comments