@@ -15,6 +15,7 @@ use client::{Response, get_host_and_port};
15
15
16
16
17
17
/// A client request to a remote server.
18
+ /// The W type tracks the state of the request, Fresh vs Streaming.
18
19
pub struct Request < W > {
19
20
/// The target URI for this request.
20
21
pub url : Url ,
@@ -76,7 +77,6 @@ impl Request<Fresh> {
76
77
/// returning a Streaming Request.
77
78
pub fn start ( mut self ) -> :: Result < Request < Streaming > > {
78
79
let mut uri = self . url . serialize_path ( ) . unwrap ( ) ;
79
- //TODO: this needs a test
80
80
if let Some ( ref q) = self . url . query {
81
81
uri. push ( '?' ) ;
82
82
uri. push_str ( & q[ ..] ) ;
@@ -174,35 +174,90 @@ impl Write for Request<Streaming> {
174
174
mod tests {
175
175
use std:: str:: from_utf8;
176
176
use url:: Url ;
177
- use method:: Method :: { Get , Head } ;
177
+ use method:: Method :: { Get , Head , Post } ;
178
178
use mock:: { MockStream , MockConnector } ;
179
+ use net:: Fresh ;
180
+ use header:: { ContentLength , TransferEncoding , Encoding } ;
181
+ use url:: form_urlencoded;
179
182
use super :: Request ;
180
183
184
+ fn run_request ( req : Request < Fresh > ) -> Vec < u8 > {
185
+ let req = req. start ( ) . unwrap ( ) ;
186
+ let stream = * req. body . end ( ) . unwrap ( )
187
+ . into_inner ( ) . unwrap ( ) . downcast :: < MockStream > ( ) . ok ( ) . unwrap ( ) ;
188
+ stream. write
189
+ }
190
+
191
+ fn assert_no_body ( s : & str ) {
192
+ assert ! ( !s. contains( "Content-Length:" ) ) ;
193
+ assert ! ( !s. contains( "Transfer-Encoding:" ) ) ;
194
+ }
195
+
181
196
#[ test]
182
197
fn test_get_empty_body ( ) {
183
198
let req = Request :: with_connector (
184
199
Get , Url :: parse ( "http://example.dom" ) . unwrap ( ) , & mut MockConnector
185
200
) . unwrap ( ) ;
186
- let req = req. start ( ) . unwrap ( ) ;
187
- let stream = * req. body . end ( ) . unwrap ( )
188
- . into_inner ( ) . unwrap ( ) . downcast :: < MockStream > ( ) . ok ( ) . unwrap ( ) ;
189
- let bytes = stream. write ;
201
+ let bytes = run_request ( req) ;
190
202
let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
191
- assert ! ( !s. contains( "Content-Length:" ) ) ;
192
- assert ! ( !s. contains( "Transfer-Encoding:" ) ) ;
203
+ assert_no_body ( s) ;
193
204
}
194
205
195
206
#[ test]
196
207
fn test_head_empty_body ( ) {
197
208
let req = Request :: with_connector (
198
209
Head , Url :: parse ( "http://example.dom" ) . unwrap ( ) , & mut MockConnector
199
210
) . unwrap ( ) ;
200
- let req = req. start ( ) . unwrap ( ) ;
201
- let stream = * req. body . end ( ) . unwrap ( )
202
- . into_inner ( ) . unwrap ( ) . downcast :: < MockStream > ( ) . ok ( ) . unwrap ( ) ;
203
- let bytes = stream. write ;
211
+ let bytes = run_request ( req) ;
212
+ let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
213
+ assert_no_body ( s) ;
214
+ }
215
+
216
+ #[ test]
217
+ fn test_url_query ( ) {
218
+ let url = Url :: parse ( "http://example.dom?q=value" ) . unwrap ( ) ;
219
+ let req = Request :: with_connector (
220
+ Get , url, & mut MockConnector
221
+ ) . unwrap ( ) ;
222
+ let bytes = run_request ( req) ;
223
+ let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
224
+ assert ! ( s. contains( "?q=value" ) ) ;
225
+ }
226
+
227
+ #[ test]
228
+ fn test_post_content_length ( ) {
229
+ let url = Url :: parse ( "http://example.dom" ) . unwrap ( ) ;
230
+ let mut req = Request :: with_connector (
231
+ Post , url, & mut MockConnector
232
+ ) . unwrap ( ) ;
233
+ let body = form_urlencoded:: serialize ( vec ! ( ( "q" , "value" ) ) . into_iter ( ) ) ;
234
+ req. headers_mut ( ) . set ( ContentLength ( body. len ( ) as u64 ) ) ;
235
+ let bytes = run_request ( req) ;
236
+ let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
237
+ assert ! ( s. contains( "Content-Length:" ) ) ;
238
+ }
239
+
240
+ #[ test]
241
+ fn test_post_chunked ( ) {
242
+ let url = Url :: parse ( "http://example.dom" ) . unwrap ( ) ;
243
+ let req = Request :: with_connector (
244
+ Post , url, & mut MockConnector
245
+ ) . unwrap ( ) ;
246
+ let bytes = run_request ( req) ;
204
247
let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
205
248
assert ! ( !s. contains( "Content-Length:" ) ) ;
206
- assert ! ( !s. contains( "Transfer-Encoding:" ) ) ;
249
+ }
250
+
251
+ #[ test]
252
+ fn test_post_chunked_with_encoding ( ) {
253
+ let url = Url :: parse ( "http://example.dom" ) . unwrap ( ) ;
254
+ let mut req = Request :: with_connector (
255
+ Post , url, & mut MockConnector
256
+ ) . unwrap ( ) ;
257
+ req. headers_mut ( ) . set ( TransferEncoding ( vec ! [ Encoding :: Chunked ] ) ) ;
258
+ let bytes = run_request ( req) ;
259
+ let s = from_utf8 ( & bytes[ ..] ) . unwrap ( ) ;
260
+ assert ! ( !s. contains( "Content-Length:" ) ) ;
261
+ assert ! ( s. contains( "Transfer-Encoding:" ) ) ;
207
262
}
208
263
}
0 commit comments