1
1
#![ warn( rust_2018_idioms) ]
2
2
extern crate conduit;
3
- extern crate semver;
4
3
5
- use std:: collections:: HashMap ;
6
4
use std:: io:: prelude:: * ;
7
5
use std:: io:: Cursor ;
8
6
use std:: net:: { Ipv4Addr , SocketAddr , SocketAddrV4 } ;
9
7
10
- use conduit:: { Extensions , Headers , Host , Method , Scheme , TypeMap } ;
11
- use semver:: Version ;
8
+ use conduit:: {
9
+ header:: { HeaderName , HeaderValue } ,
10
+ Extensions , HeaderMap , Host , Method , Scheme , TypeMap , Version ,
11
+ } ;
12
12
13
13
pub struct MockRequest {
14
14
path : String ,
15
15
method : Method ,
16
16
query_string : Option < String > ,
17
17
body : Option < Vec < u8 > > ,
18
- build_headers : HashMap < String , String > ,
19
- headers : MockHeaders ,
18
+ headers : HeaderMap ,
20
19
extensions : TypeMap ,
21
20
reader : Option < Cursor < Vec < u8 > > > ,
22
21
}
23
22
24
23
impl MockRequest {
25
24
pub fn new ( method : Method , path : & str ) -> MockRequest {
26
- let headers = HashMap :: new ( ) ;
25
+ let headers = HeaderMap :: new ( ) ;
27
26
28
27
MockRequest {
29
28
path : path. to_string ( ) ,
30
29
extensions : TypeMap :: new ( ) ,
31
30
query_string : None ,
32
31
body : None ,
33
- build_headers : headers,
34
- headers : MockHeaders {
35
- headers : HashMap :: new ( ) ,
36
- } ,
32
+ headers,
37
33
method,
38
34
reader : None ,
39
35
}
@@ -60,50 +56,20 @@ impl MockRequest {
60
56
self
61
57
}
62
58
63
- pub fn header ( & mut self , name : & str , value : & str ) -> & mut MockRequest {
64
- self . build_headers
65
- . insert ( name. to_string ( ) , value. to_string ( ) ) ;
66
- let headers = MockHeaders {
67
- headers : self . build_headers . clone ( ) ,
68
- } ;
69
- self . headers = headers;
70
-
71
- self
72
- }
73
- }
74
-
75
- pub struct MockHeaders {
76
- headers : HashMap < String , String > ,
77
- }
78
-
79
- impl Headers for MockHeaders {
80
- fn find ( & self , key : & str ) -> Option < Vec < & str > > {
81
- self . headers . get ( key) . map ( |v| vec ! [ & v[ ..] ] )
82
- }
83
-
84
- fn has ( & self , key : & str ) -> bool {
85
- self . headers . contains_key ( key)
86
- }
87
-
88
- fn all ( & self ) -> Vec < ( & str , Vec < & str > ) > {
59
+ pub fn header ( & mut self , name : HeaderName , value : & str ) -> & mut MockRequest {
89
60
self . headers
90
- . iter ( )
91
- . map ( |( k, v) | ( & k[ ..] , vec ! [ & v[ ..] ] ) )
92
- . collect ( )
61
+ . insert ( name, HeaderValue :: from_str ( value) . unwrap ( ) ) ;
62
+ self
93
63
}
94
64
}
95
65
96
- impl conduit:: Request for MockRequest {
66
+ impl conduit:: RequestExt for MockRequest {
97
67
fn http_version ( & self ) -> Version {
98
- Version :: parse ( "1.1.0" ) . unwrap ( )
99
- }
100
-
101
- fn conduit_version ( & self ) -> Version {
102
- Version :: parse ( "0.1.0" ) . unwrap ( )
68
+ Version :: HTTP_11
103
69
}
104
70
105
- fn method ( & self ) -> Method {
106
- self . method . clone ( )
71
+ fn method ( & self ) -> & Method {
72
+ & self . method
107
73
}
108
74
fn scheme ( & self ) -> Scheme {
109
75
Scheme :: Http
@@ -131,8 +97,8 @@ impl conduit::Request for MockRequest {
131
97
self . body . as_ref ( ) . map ( |b| b. len ( ) as u64 )
132
98
}
133
99
134
- fn headers ( & self ) -> & dyn Headers {
135
- & self . headers as & dyn Headers
100
+ fn headers ( & self ) -> & HeaderMap {
101
+ & self . headers
136
102
}
137
103
138
104
fn body ( & mut self ) -> & mut dyn Read {
@@ -154,19 +120,17 @@ impl conduit::Request for MockRequest {
154
120
#[ cfg( test) ]
155
121
mod tests {
156
122
use super :: MockRequest ;
157
- use semver:: Version ;
158
123
159
124
use std:: net:: { Ipv4Addr , SocketAddr , SocketAddrV4 } ;
160
125
161
- use conduit:: { Host , Method , Request , Scheme } ;
126
+ use conduit:: { header , Host , Method , RequestExt , Scheme , Version } ;
162
127
163
128
#[ test]
164
129
fn simple_request_test ( ) {
165
- let mut req = MockRequest :: new ( Method :: Get , "/" ) ;
130
+ let mut req = MockRequest :: new ( Method :: GET , "/" ) ;
166
131
167
- assert_eq ! ( req. http_version( ) , Version :: parse( "1.1.0" ) . unwrap( ) ) ;
168
- assert_eq ! ( req. conduit_version( ) , Version :: parse( "0.1.0" ) . unwrap( ) ) ;
169
- assert_eq ! ( req. method( ) , Method :: Get ) ;
132
+ assert_eq ! ( req. http_version( ) , Version :: HTTP_11 ) ;
133
+ assert_eq ! ( req. method( ) , Method :: GET ) ;
170
134
assert_eq ! ( req. scheme( ) , Scheme :: Http ) ;
171
135
assert_eq ! ( req. host( ) , Host :: Name ( "example.com" ) ) ;
172
136
assert_eq ! ( req. virtual_root( ) , None ) ;
@@ -177,18 +141,18 @@ mod tests {
177
141
SocketAddr :: V4 ( SocketAddrV4 :: new( Ipv4Addr :: new( 127 , 0 , 0 , 1 ) , 80 ) )
178
142
) ;
179
143
assert_eq ! ( req. content_length( ) , None ) ;
180
- assert_eq ! ( req. headers( ) . all ( ) . len( ) , 0 ) ;
144
+ assert_eq ! ( req. headers( ) . len( ) , 0 ) ;
181
145
let mut s = String :: new ( ) ;
182
146
req. body ( ) . read_to_string ( & mut s) . ok ( ) . expect ( "No body" ) ;
183
147
assert_eq ! ( s, "" . to_string( ) ) ;
184
148
}
185
149
186
150
#[ test]
187
151
fn request_body_test ( ) {
188
- let mut req = MockRequest :: new ( Method :: Post , "/articles" ) ;
152
+ let mut req = MockRequest :: new ( Method :: POST , "/articles" ) ;
189
153
req. with_body ( b"Hello world" ) ;
190
154
191
- assert_eq ! ( req. method( ) , Method :: Post ) ;
155
+ assert_eq ! ( req. method( ) , Method :: POST ) ;
192
156
assert_eq ! ( req. path( ) , "/articles" ) ;
193
157
let mut s = String :: new ( ) ;
194
158
req. body ( ) . read_to_string ( & mut s) . ok ( ) . expect ( "No body" ) ;
@@ -198,20 +162,20 @@ mod tests {
198
162
199
163
#[ test]
200
164
fn request_query_test ( ) {
201
- let mut req = MockRequest :: new ( Method :: Post , "/articles" ) ;
165
+ let mut req = MockRequest :: new ( Method :: POST , "/articles" ) ;
202
166
req. with_query ( "foo=bar" ) ;
203
167
204
168
assert_eq ! ( req. query_string( ) . expect( "No query string" ) , "foo=bar" ) ;
205
169
}
206
170
207
171
#[ test]
208
172
fn request_headers ( ) {
209
- let mut req = MockRequest :: new ( Method :: Post , "/articles" ) ;
210
- req. header ( "User-Agent" , "lulz" ) ;
211
- req. header ( " DNT" , "1" ) ;
173
+ let mut req = MockRequest :: new ( Method :: POST , "/articles" ) ;
174
+ req. header ( header :: USER_AGENT , "lulz" ) ;
175
+ req. header ( header :: DNT , "1" ) ;
212
176
213
- assert_eq ! ( req. headers( ) . all ( ) . len( ) , 2 ) ;
214
- assert_eq ! ( req. headers( ) . find ( "User-Agent" ) . unwrap( ) , vec! ( "lulz" ) ) ;
215
- assert_eq ! ( req. headers( ) . find ( " DNT" ) . unwrap( ) , vec! ( "1" ) ) ;
177
+ assert_eq ! ( req. headers( ) . len( ) , 2 ) ;
178
+ assert_eq ! ( req. headers( ) . get ( header :: USER_AGENT ) . unwrap( ) , "lulz" ) ;
179
+ assert_eq ! ( req. headers( ) . get ( header :: DNT ) . unwrap( ) , "1" ) ;
216
180
}
217
181
}
0 commit comments