@@ -66,16 +66,31 @@ impl HttpClient for H1Client {
6666 let raw_stream = async_std:: net:: TcpStream :: connect ( addr) . await ?;
6767 req. set_peer_addr ( raw_stream. peer_addr ( ) . ok ( ) ) ;
6868 req. set_local_addr ( raw_stream. local_addr ( ) . ok ( ) ) ;
69-
70- let stream = async_native_tls:: connect ( host, raw_stream) . await ?;
71-
72- client:: connect ( stream, req) . await
69+ let tls_stream = add_tls ( host, raw_stream) . await ?;
70+ client:: connect ( tls_stream, req) . await
7371 }
7472 _ => unreachable ! ( ) ,
7573 }
7674 }
7775}
7876
77+ #[ cfg( not( feature = "h1_client_rustls" ) ) ]
78+ async fn add_tls (
79+ host : String ,
80+ stream : async_std:: net:: TcpStream ,
81+ ) -> Result < async_native_tls:: TlsStream < async_std:: net:: TcpStream > , async_native_tls:: Error > {
82+ async_native_tls:: connect ( host, stream) . await
83+ }
84+
85+ #[ cfg( feature = "h1_client_rustls" ) ]
86+ async fn add_tls (
87+ host : String ,
88+ stream : async_std:: net:: TcpStream ,
89+ ) -> std:: io:: Result < async_tls:: client:: TlsStream < async_std:: net:: TcpStream > > {
90+ let connector = async_tls:: TlsConnector :: default ( ) ;
91+ connector. connect ( host, stream) . await
92+ }
93+
7994#[ cfg( test) ]
8095mod tests {
8196 use super :: * ;
@@ -120,4 +135,18 @@ mod tests {
120135
121136 Ok ( ( ) )
122137 }
138+
139+ #[ async_std:: test]
140+ async fn https_functionality ( ) -> Result < ( ) > {
141+ task:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
142+ // Send a POST request to https://httpbin.org/post
143+ // The result should be a JSon string similar to what you get with:
144+ // curl -X POST "https://httpbin.org/post" -H "accept: application/json" -H "Content-Type: text/plain;charset=utf-8" -d "hello"
145+ let request = build_test_request ( Url :: parse ( "https://httpbin.org/post" ) . unwrap ( ) ) ;
146+ let mut response: Response = H1Client :: new ( ) . send ( request) . await ?;
147+ let json_val: serde_json:: value:: Value =
148+ serde_json:: from_str ( & response. body_string ( ) . await . unwrap ( ) ) ?;
149+ assert_eq ! ( * json_val. get( "data" ) . unwrap( ) , serde_json:: json!( "hello" ) ) ;
150+ Ok ( ( ) )
151+ }
123152}
0 commit comments