@@ -15,11 +15,8 @@ impl Static {
15
15
path : path. as_ref ( ) . to_path_buf ( ) ,
16
16
}
17
17
}
18
- }
19
18
20
- impl Handler for Static {
21
- fn call ( & self , request : & mut dyn RequestExt ) -> HandlerResult {
22
- let request_path = request. path ( ) ;
19
+ pub fn lookup ( & self , request_path : & str ) -> HandlerResult {
23
20
let request_path = request_path. strip_prefix ( '/' ) . unwrap_or ( request_path) ;
24
21
if request_path. contains ( ".." ) {
25
22
return Ok ( not_found ( ) ) ;
@@ -47,6 +44,12 @@ impl Handler for Static {
47
44
}
48
45
}
49
46
47
+ impl Handler for Static {
48
+ fn call ( & self , request : & mut dyn RequestExt ) -> HandlerResult {
49
+ self . lookup ( request. path ( ) )
50
+ }
51
+ }
52
+
50
53
fn not_found ( ) -> Response < Body > {
51
54
Response :: builder ( )
52
55
. status ( StatusCode :: NOT_FOUND )
@@ -85,6 +88,25 @@ mod tests {
85
88
assert_eq ! ( * res. into_cow( ) , b"[package]" [ ..] ) ;
86
89
}
87
90
91
+ #[ test]
92
+ fn test_lookup ( ) {
93
+ let td = TempDir :: new ( "conduit-static" ) . unwrap ( ) ;
94
+ let root = td. path ( ) ;
95
+ let handler = Static :: new ( root) ;
96
+ File :: create ( & root. join ( "Cargo.toml" ) )
97
+ . unwrap ( )
98
+ . write_all ( b"[package]" )
99
+ . unwrap ( ) ;
100
+
101
+ let res = handler. lookup ( "Cargo.toml" ) . expect ( "No response" ) ;
102
+ assert_eq ! (
103
+ res. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
104
+ "application/toml"
105
+ ) ;
106
+ assert_eq ! ( res. headers( ) . get( header:: CONTENT_LENGTH ) . unwrap( ) , "9" ) ;
107
+ assert_eq ! ( * res. into_cow( ) , b"[package]" [ ..] ) ;
108
+ }
109
+
88
110
#[ test]
89
111
fn test_mime_types ( ) {
90
112
let td = TempDir :: new ( "conduit-static" ) . unwrap ( ) ;
0 commit comments