Skip to content

Commit 9f2fe9f

Browse files
committed
fix test start server listen both socket address
1 parent 5af3196 commit 9f2fe9f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/src/http/stream.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ impl AsyncWrite for Stream {
121121
async fn test_connect() {
122122
crate::tests::start_server(30000).await;
123123
let stream = Stream::connect(&Url::from("http://127.0.0.1:30000/get"), Duration::from_secs(2)).await;
124+
if let Err(error) = stream.as_ref() {
125+
println!("{:?}", error);
126+
}
124127
assert!(stream.is_ok());
125128
let stream = Stream::connect(&Url::from("http://localhost:30000/get"), Duration::from_secs(2)).await;
129+
if let Err(error) = stream.as_ref() {
130+
println!("{:?}", error);
131+
}
126132
assert!(stream.is_ok());
127133
let stream = Stream::connect(&Url::from("http://localhost:88/get"), Duration::from_secs(2)).await;
128134
assert!(stream.is_err());

lib/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub mod tests {
3737
use serde_json::json;
3838
use serde_json::Value;
3939
use std::collections::HashMap;
40+
use std::net::IpAddr;
41+
use std::net::Ipv4Addr;
42+
use std::net::Ipv6Addr;
43+
use std::net::SocketAddr;
44+
use tokio::net::TcpListener;
4045

4146
pub async fn start_server(port: u16) {
4247
let router = Router::new()
@@ -45,10 +50,19 @@ pub mod tests {
4550
.route("/json", post(handle_json))
4651
.route("/form", post(handle_form))
4752
.route("/multipart", post(handle_multipart));
48-
49-
let listener = tokio::net::TcpListener::bind(("127.0.0.1", port)).await.unwrap();
50-
tokio::spawn(async move {
51-
axum::serve(listener, router).await.unwrap();
53+
let listeners = [
54+
TcpListener::bind(SocketAddr::new(IpAddr::from(Ipv4Addr::UNSPECIFIED), port))
55+
.await
56+
.unwrap(),
57+
TcpListener::bind(SocketAddr::new(IpAddr::from(Ipv6Addr::UNSPECIFIED), port))
58+
.await
59+
.unwrap(),
60+
];
61+
listeners.into_iter().for_each(|listener| {
62+
let router = router.to_owned();
63+
tokio::spawn(async move {
64+
axum::serve(listener, router).await.unwrap();
65+
});
5266
});
5367
}
5468

0 commit comments

Comments
 (0)