1
1
#![ warn( rust_2018_idioms) ]
2
2
extern crate conduit;
3
3
4
- use std:: io :: prelude :: * ;
5
- use std:: io:: Cursor ;
4
+ use std:: borrow :: Cow ;
5
+ use std:: io:: { Cursor , Read } ;
6
6
use std:: net:: { Ipv4Addr , SocketAddr , SocketAddrV4 } ;
7
7
8
8
use conduit:: {
9
9
header:: { HeaderValue , IntoHeaderName } ,
10
- Extensions , HeaderMap , Host , Method , Scheme , TypeMap , Version ,
10
+ Body , Extensions , HeaderMap , Host , Method , Response , Scheme , TypeMap , Version ,
11
11
} ;
12
12
13
+ pub trait ResponseExt {
14
+ fn into_cow ( self ) -> Cow < ' static , [ u8 ] > ;
15
+ }
16
+
17
+ impl ResponseExt for Response < Body > {
18
+ /// Convert the request into a copy-on-write body
19
+ ///
20
+ /// # Blocking
21
+ ///
22
+ /// This function may block if the value is a `Body::File`.
23
+ ///
24
+ /// # Panics
25
+ ///
26
+ /// This function panics if there is an error reading a `Body::File`.
27
+ fn into_cow ( self ) -> Cow < ' static , [ u8 ] > {
28
+ use conduit:: Body :: * ;
29
+
30
+ match self . into_body ( ) {
31
+ Static ( slice) => slice. into ( ) ,
32
+ Owned ( vec) => vec. into ( ) ,
33
+ File ( mut file) => {
34
+ let mut vec = Vec :: new ( ) ;
35
+ std:: io:: copy ( & mut file, & mut vec) . unwrap ( ) ;
36
+ vec. into ( )
37
+ }
38
+ }
39
+ }
40
+ }
41
+
13
42
pub struct MockRequest {
14
43
path : String ,
15
44
method : Method ,
@@ -146,7 +175,7 @@ mod tests {
146
175
assert_eq ! ( req. content_length( ) , None ) ;
147
176
assert_eq ! ( req. headers( ) . len( ) , 0 ) ;
148
177
let mut s = String :: new ( ) ;
149
- req. body ( ) . read_to_string ( & mut s) . ok ( ) . expect ( "No body" ) ;
178
+ req. body ( ) . read_to_string ( & mut s) . expect ( "No body" ) ;
150
179
assert_eq ! ( s, "" . to_string( ) ) ;
151
180
}
152
181
@@ -158,7 +187,7 @@ mod tests {
158
187
assert_eq ! ( req. method( ) , Method :: POST ) ;
159
188
assert_eq ! ( req. path( ) , "/articles" ) ;
160
189
let mut s = String :: new ( ) ;
161
- req. body ( ) . read_to_string ( & mut s) . ok ( ) . expect ( "No body" ) ;
190
+ req. body ( ) . read_to_string ( & mut s) . expect ( "No body" ) ;
162
191
assert_eq ! ( s, "Hello world" . to_string( ) ) ;
163
192
assert_eq ! ( req. content_length( ) , Some ( 11 ) ) ;
164
193
}
0 commit comments