11use super :: * ;
22use axum:: http:: StatusCode ;
33use axum:: { body:: Body , http:: Request } ;
4+ use headers:: ContentType ;
45use http_body_util:: BodyExt ;
56use tower:: ServiceExt ;
67
78const INDEX_HTML : & str = include_str ! ( "../assets/index.html" ) ;
89const SCRIPT_JS : & str = include_str ! ( "../assets/script.js" ) ;
910
10- const JS : & str = "text/javascript" ;
11- const HTML : & str = "text/html" ;
12-
13- async fn get_page ( app : Router , path : & str ) -> ( StatusCode , String , String ) {
11+ async fn get_page ( app : Router , path : & str ) -> ( StatusCode , Option < ContentType > , String ) {
1412 let response = app
1513 . oneshot ( Request :: builder ( ) . uri ( path) . body ( Body :: empty ( ) ) . unwrap ( ) )
1614 . await
1715 . unwrap ( ) ;
1816
1917 let status = response. status ( ) ;
2018 let content_type = match response. headers ( ) . get ( "content-type" ) {
21- Some ( content_type ) => content_type . to_str ( ) . unwrap ( ) . to_owned ( ) ,
22- None => String :: new ( ) ,
19+ Some ( header ) => Some ( header . to_str ( ) . unwrap ( ) . parse :: < ContentType > ( ) . unwrap ( ) ) ,
20+ None => None ,
2321 } ;
2422
2523 let body = response. into_body ( ) ;
@@ -29,7 +27,13 @@ async fn get_page(app: Router, path: &str) -> (StatusCode, String, String) {
2927 ( status, content_type, html)
3028}
3129
32- async fn check ( app : Router , path : & str , status : StatusCode , content_type : & str , content : & str ) {
30+ async fn check (
31+ app : Router ,
32+ path : & str ,
33+ status : StatusCode ,
34+ content_type : Option < ContentType > ,
35+ content : & str ,
36+ ) {
3337 let ( actual_status, actual_content_type, actual_content) = get_page ( app, path) . await ;
3438 assert_eq ! ( status, actual_status) ;
3539 assert_eq ! ( content_type, actual_content_type) ;
@@ -43,14 +47,27 @@ async fn test_using_serve_dir() {
4347 app ( ) ,
4448 "/assets/index.html" ,
4549 StatusCode :: OK ,
46- HTML ,
50+ Some ( ContentType :: html ( ) ) ,
4751 INDEX_HTML ,
4852 )
4953 . await ;
50- check ( app ( ) , "/assets/script.js" , StatusCode :: OK , JS , SCRIPT_JS ) . await ;
51- check ( app ( ) , "/assets/" , StatusCode :: OK , HTML , INDEX_HTML ) . await ;
52-
53- check ( app ( ) , "/assets/other.html" , StatusCode :: NOT_FOUND , "" , "" ) . await ;
54+ check (
55+ app ( ) ,
56+ "/assets/script.js" ,
57+ StatusCode :: OK ,
58+ Some ( ContentType :: from ( mime:: TEXT_JAVASCRIPT ) ) ,
59+ SCRIPT_JS ,
60+ )
61+ . await ;
62+ check (
63+ app ( ) ,
64+ "/assets/" ,
65+ StatusCode :: OK ,
66+ Some ( ContentType :: html ( ) ) ,
67+ INDEX_HTML ,
68+ )
69+ . await ;
70+ check ( app ( ) , "/assets/other.html" , StatusCode :: NOT_FOUND , None , "" ) . await ;
5471}
5572
5673#[ tokio:: test]
@@ -60,18 +77,32 @@ async fn test_using_serve_dir_with_assets_fallback() {
6077 app ( ) ,
6178 "/assets/index.html" ,
6279 StatusCode :: OK ,
63- HTML ,
80+ Some ( ContentType :: html ( ) ) ,
81+ INDEX_HTML ,
82+ )
83+ . await ;
84+ check (
85+ app ( ) ,
86+ "/assets/script.js" ,
87+ StatusCode :: OK ,
88+ Some ( ContentType :: from ( mime:: TEXT_JAVASCRIPT ) ) ,
89+ SCRIPT_JS ,
90+ )
91+ . await ;
92+ check (
93+ app ( ) ,
94+ "/assets/" ,
95+ StatusCode :: OK ,
96+ Some ( ContentType :: html ( ) ) ,
6497 INDEX_HTML ,
6598 )
6699 . await ;
67- check ( app ( ) , "/assets/script.js" , StatusCode :: OK , JS , SCRIPT_JS ) . await ;
68- check ( app ( ) , "/assets/" , StatusCode :: OK , HTML , INDEX_HTML ) . await ;
69100
70101 check (
71102 app ( ) ,
72103 "/foo" ,
73104 StatusCode :: OK ,
74- "text/plain; charset=utf-8" ,
105+ Some ( ContentType :: text_utf8 ( ) ) ,
75106 "Hi from /foo" ,
76107 )
77108 . await ;
0 commit comments