11use crate :: ensure_eq;
22use anyhow:: Result ;
3- use hyper:: client:: HttpConnector ;
4- use hyper:: { body, Body , Client , Method , Request , Response } ;
5- use hyper_tls:: HttpsConnector ;
3+ use reqwest:: { Method , Request , Response } ;
64use std:: str;
75
86pub async fn assert_status ( url : & str , expected : u16 ) -> Result < ( ) > {
97 let resp = make_request ( Method :: GET , url, "" ) . await ?;
108 let status = resp. status ( ) ;
119
12- let response = body :: to_bytes ( resp. into_body ( ) ) . await . unwrap ( ) . to_vec ( ) ;
13- let actual_body = str:: from_utf8 ( & response ) . unwrap ( ) . to_string ( ) ;
10+ let body = resp. bytes ( ) . await ? ;
11+ let actual_body = str:: from_utf8 ( & body ) . unwrap ( ) . to_string ( ) ;
1412
1513 ensure_eq ! ( status, expected, "{}" , actual_body) ;
1614
@@ -29,8 +27,8 @@ pub async fn assert_http_response(
2927
3028 let status = res. status ( ) ;
3129 let headers = res. headers ( ) . clone ( ) ;
32- let response = body :: to_bytes ( res. into_body ( ) ) . await . unwrap ( ) . to_vec ( ) ;
33- let actual_body = str:: from_utf8 ( & response ) . unwrap ( ) . to_string ( ) ;
30+ let body = res. bytes ( ) . await ? ;
31+ let actual_body = str:: from_utf8 ( & body ) . unwrap ( ) . to_string ( ) ;
3432
3533 ensure_eq ! (
3634 expected,
@@ -55,26 +53,15 @@ pub async fn assert_http_response(
5553 Ok ( ( ) )
5654}
5755
58- pub async fn create_request ( method : Method , url : & str , body : & str ) -> Result < Request < Body > > {
59- let req = Request :: builder ( )
60- . method ( method)
61- . uri ( url)
62- . body ( Body :: from ( body. to_string ( ) ) )
63- . expect ( "request builder" ) ;
56+ pub async fn create_request ( method : Method , url : & str , body : & str ) -> Result < Request > {
57+ let mut req = reqwest:: Request :: new ( method, url. try_into ( ) ?) ;
58+ * req. body_mut ( ) = Some ( body. to_owned ( ) . into ( ) ) ;
6459
6560 Ok ( req)
6661}
6762
68- pub fn create_client ( ) -> Client < HttpsConnector < HttpConnector > > {
69- let connector = HttpsConnector :: new ( ) ;
70- Client :: builder ( )
71- . pool_max_idle_per_host ( 0 )
72- . build :: < _ , hyper:: Body > ( connector)
73- }
74-
75- pub async fn make_request ( method : Method , path : & str , body : & str ) -> Result < Response < Body > > {
76- let c = create_client ( ) ;
77- let req = create_request ( method, path, body) ;
78- let resp = c. request ( req. await ?) . await . unwrap ( ) ;
79- Ok ( resp)
63+ pub async fn make_request ( method : Method , path : & str , body : & str ) -> Result < Response > {
64+ let req = create_request ( method, path, body) . await ?;
65+ let client = reqwest:: Client :: new ( ) ;
66+ Ok ( client. execute ( req) . await ?)
8067}
0 commit comments