Skip to content

Commit 6c53290

Browse files
committed
add:http stream client test
1 parent 3ad3ef0 commit 6c53290

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/core/stream.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ pub trait Stream {
1313
Ok(())
1414
}
1515
// to get the stream feature
16-
fn feature(&self) -> Box<dyn Feature>;
16+
// fn feature(&self) -> impl Feature;
17+
1718
fn handle(bs: Vec<u8>) -> Result<(), Box<dyn Error>> {
1819
warn!("not impl");
1920
Ok(())
2021
}
22+
23+
async fn dail() -> Result<(), Box<dyn Error>> {
24+
return Ok(())
25+
}
2126
}

src/proxy/http.rs

+53-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
use std::convert::Infallible;
12
use std::error::Error;
23
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
4+
use std::str::FromStr;
5+
36
use bytes::{Bytes, BytesMut};
7+
use http_body_util::Full;
48
use hyper::{Request, Response};
59
use hyper::server::conn::http1;
610
use hyper::service::service_fn;
711
use hyper_util::rt::{TokioExecutor, TokioIo};
812
use hyper_util::server::conn::auto;
913
use log::error;
1014
use tokio::net::{TcpListener, TcpStream};
11-
use crate::core::stream::Stream;
15+
1216
use crate::core::feature::Feature;
13-
use std::convert::Infallible;
14-
use std::str::FromStr;
15-
use http_body_util::Full;
17+
use crate::core::stream::Stream;
1618

1719
pub enum HttpVersion {
1820
V1,
@@ -77,23 +79,23 @@ impl Stream for HttpStream {
7779
}
7880
});
7981
}
80-
Ok(())
8182
}
8283

8384
fn close(&self) -> Result<(), Box<dyn Error>> {
8485
Ok(())
8586
}
8687

87-
fn feature(&self) -> Box<dyn Feature> {
88-
Box::new(Self {
89-
config: HttpConfig {
90-
addr: "".to_string(),
91-
port: 0,
92-
interface: "".to_string(),
93-
version: HttpVersion::V1,
94-
}
95-
})
96-
}
88+
// fn feature(&self) -> impl Feature {
89+
// // Box::new(Self {
90+
// // config: HttpConfig {
91+
// // addr: "".to_string(),
92+
// // port: 0,
93+
// // interface: "".to_string(),
94+
// // version: HttpVersion::V1,
95+
// // }
96+
// // })
97+
// *self
98+
// }
9799

98100
fn handle(bs: Vec<u8>) -> Result<(), Box<dyn Error>> {
99101
BytesMut::with_capacity(10);
@@ -105,6 +107,12 @@ impl Feature for HttpStream {}
105107

106108
#[cfg(test)]
107109
mod test {
110+
use std::time;
111+
112+
use http_body_util::BodyStream;
113+
use hyper_util::rt::TokioIo;
114+
use tokio::net::TcpStream;
115+
108116
use crate::core::stream::Stream;
109117
use crate::proxy::http::{HttpConfig, HttpStream, HttpVersion};
110118

@@ -118,4 +126,34 @@ mod test {
118126
});
119127
server.start().await.unwrap();
120128
}
129+
130+
#[tokio::test]
131+
async fn test_http_client() {
132+
// Parse our URL...
133+
let url = "http://127.0.0.1:80".parse::<hyper::Uri>().unwrap();
134+
135+
// Get the host and the port
136+
let host = url.host().expect("uri has no host");
137+
let port = url.port_u16().unwrap_or(80);
138+
139+
let address = format!("{}:{}", host, port);
140+
141+
// Open a TCP connection to the remote host
142+
let stream = TcpStream::connect(address).await.unwrap();
143+
144+
// Use an adapter to access something implementing `tokio::io` traits as if they implement
145+
// `hyper::rt` IO traits.
146+
let io = TokioIo::new(stream);
147+
148+
// Create the Hyper client
149+
let (mut sender, conn) = hyper::client::conn::http1::handshake::<TokioIo<TcpStream>, BodyStream<String>>(io).await.unwrap();
150+
151+
// Spawn a task to poll the connection, driving the HTTP state
152+
tokio::task::spawn(async move {
153+
if let Err(err) = conn.await {
154+
println!("Connection failed: {:?}", err);
155+
}
156+
});
157+
tokio::time::sleep(time::Duration::from_secs(1000)).await;
158+
}
121159
}

0 commit comments

Comments
 (0)