@@ -12,7 +12,6 @@ extern crate time;
12
12
use conduit:: { box_error, header, Body , Handler , HandlerResult , RequestExt , Response , StatusCode } ;
13
13
use filetime:: FileTime ;
14
14
use std:: fs:: File ;
15
- use std:: io;
16
15
use std:: path:: { Path , PathBuf } ;
17
16
18
17
pub struct Static {
@@ -61,7 +60,7 @@ impl Handler for Static {
61
60
header:: LAST_MODIFIED ,
62
61
tm. strftime ( "%a, %d %b %Y %T GMT" ) . unwrap ( ) . to_string ( ) ,
63
62
)
64
- . body ( Box :: new ( file) as Body )
63
+ . body ( Body :: File ( file) )
65
64
. map_err ( box_error)
66
65
}
67
66
}
@@ -71,18 +70,19 @@ fn not_found() -> Response<Body> {
71
70
. status ( StatusCode :: NOT_FOUND )
72
71
. header ( header:: CONTENT_LENGTH , 0 )
73
72
. header ( header:: CONTENT_TYPE , "text/plain" )
74
- . body ( Box :: new ( io :: empty ( ) ) as Body )
73
+ . body ( Body :: empty ( ) )
75
74
. unwrap ( )
76
75
}
77
76
78
77
#[ cfg( test) ]
79
78
mod tests {
80
- extern crate conduit_test as test ;
79
+ extern crate conduit_test;
81
80
82
81
use std:: fs:: { self , File } ;
83
82
use std:: io:: prelude:: * ;
84
83
use tempdir:: TempDir ;
85
84
85
+ use self :: conduit_test:: { MockRequest , ResponseExt } ;
86
86
use conduit:: { header, Handler , Method , StatusCode } ;
87
87
use Static ;
88
88
@@ -95,16 +95,14 @@ mod tests {
95
95
. unwrap ( )
96
96
. write_all ( b"[package]" )
97
97
. unwrap ( ) ;
98
- let mut req = test:: MockRequest :: new ( Method :: GET , "/Cargo.toml" ) ;
99
- let mut res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
100
- let mut body = Vec :: new ( ) ;
101
- res. body_mut ( ) . write_body ( & mut body) . unwrap ( ) ;
102
- assert_eq ! ( body, b"[package]" ) ;
98
+ let mut req = MockRequest :: new ( Method :: GET , "/Cargo.toml" ) ;
99
+ let res = handler. call ( & mut req) . expect ( "No response" ) ;
103
100
assert_eq ! (
104
101
res. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
105
102
"text/plain"
106
103
) ;
107
104
assert_eq ! ( res. headers( ) . get( header:: CONTENT_LENGTH ) . unwrap( ) , "9" ) ;
105
+ assert_eq ! ( res. into_cow( ) , "[package]" . as_bytes( ) ) ;
108
106
}
109
107
110
108
#[ test]
@@ -115,8 +113,8 @@ mod tests {
115
113
File :: create ( & root. join ( "src/fixture.css" ) ) . unwrap ( ) ;
116
114
117
115
let handler = Static :: new ( root. clone ( ) ) ;
118
- let mut req = test :: MockRequest :: new ( Method :: GET , "/src/fixture.css" ) ;
119
- let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
116
+ let mut req = MockRequest :: new ( Method :: GET , "/src/fixture.css" ) ;
117
+ let res = handler. call ( & mut req) . expect ( "No response" ) ;
120
118
assert_eq ! ( res. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) , "text/css" ) ;
121
119
assert_eq ! ( res. headers( ) . get( header:: CONTENT_LENGTH ) . unwrap( ) , "0" ) ;
122
120
}
@@ -127,8 +125,8 @@ mod tests {
127
125
let root = td. path ( ) ;
128
126
129
127
let handler = Static :: new ( root. clone ( ) ) ;
130
- let mut req = test :: MockRequest :: new ( Method :: GET , "/nope" ) ;
131
- let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
128
+ let mut req = MockRequest :: new ( Method :: GET , "/nope" ) ;
129
+ let res = handler. call ( & mut req) . expect ( "No response" ) ;
132
130
assert_eq ! ( res. status( ) , StatusCode :: NOT_FOUND ) ;
133
131
}
134
132
@@ -140,8 +138,8 @@ mod tests {
140
138
fs:: create_dir ( & root. join ( "foo" ) ) . unwrap ( ) ;
141
139
142
140
let handler = Static :: new ( root. clone ( ) ) ;
143
- let mut req = test :: MockRequest :: new ( Method :: GET , "/foo" ) ;
144
- let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
141
+ let mut req = MockRequest :: new ( Method :: GET , "/foo" ) ;
142
+ let res = handler. call ( & mut req) . expect ( "No response" ) ;
145
143
assert_eq ! ( res. status( ) , StatusCode :: NOT_FOUND ) ;
146
144
}
147
145
@@ -151,8 +149,8 @@ mod tests {
151
149
let root = td. path ( ) ;
152
150
File :: create ( & root. join ( "test" ) ) . unwrap ( ) ;
153
151
let handler = Static :: new ( root. clone ( ) ) ;
154
- let mut req = test :: MockRequest :: new ( Method :: GET , "/test" ) ;
155
- let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
152
+ let mut req = MockRequest :: new ( Method :: GET , "/test" ) ;
153
+ let res = handler. call ( & mut req) . expect ( "No response" ) ;
156
154
assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
157
155
assert ! ( res. headers( ) . get( header:: LAST_MODIFIED ) . is_some( ) ) ;
158
156
}
0 commit comments