diff --git a/Cargo.lock b/Cargo.lock index aacf5e46..bbace764 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4075,7 +4075,7 @@ dependencies = [ [[package]] name = "volo-http" -version = "0.3.0-rc.2" +version = "0.3.0-rc.3" dependencies = [ "ahash", "async-broadcast", diff --git a/volo-http/Cargo.toml b/volo-http/Cargo.toml index f584faf3..d39cc8f5 100644 --- a/volo-http/Cargo.toml +++ b/volo-http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "volo-http" -version = "0.3.0-rc.2" +version = "0.3.0-rc.3" edition.workspace = true homepage.workspace = true repository.workspace = true diff --git a/volo-http/src/client/layer/header.rs b/volo-http/src/client/layer/header.rs index 811e92a3..6af134be 100644 --- a/volo-http/src/client/layer/header.rs +++ b/volo-http/src/client/layer/header.rs @@ -199,10 +199,10 @@ where mut req: ClientRequest, ) -> impl Future> + Send { if !req.headers().contains_key(header::HOST) { - if let Some(val) = &self.val { - req.headers_mut().insert(header::HOST, val.clone()); - } else if let Some(val) = gen_host_by_ep(cx.rpc_info().callee()) { + if let Some(val) = gen_host_by_ep(cx.rpc_info().callee()) { req.headers_mut().insert(header::HOST, val); + } else if let Some(val) = &self.val { + req.headers_mut().insert(header::HOST, val.clone()); } } self.inner.call(cx, req) diff --git a/volo-http/src/client/mod.rs b/volo-http/src/client/mod.rs index bfdad7d5..6562ca25 100644 --- a/volo-http/src/client/mod.rs +++ b/volo-http/src/client/mod.rs @@ -955,7 +955,11 @@ where #[cfg(feature = "json")] #[cfg(test)] mod client_tests { - use std::{collections::HashMap, future::Future}; + use std::{ + collections::HashMap, + future::Future, + net::{IpAddr, Ipv4Addr, SocketAddr}, + }; #[cfg(feature = "cookie")] use cookie::Cookie; @@ -1130,6 +1134,50 @@ mod client_tests { assert_eq!(resp.url, HTTPBIN_GET); } + #[tokio::test] + async fn client_builder_host_override() { + let mut builder = Client::builder(); + builder.host("this.domain.must.be.invalid"); + let client = builder.build().unwrap(); + + let resp = client + .get(HTTPBIN_GET) + .send() + .await + .unwrap() + .into_json::() + .await + .unwrap(); + assert!(resp.args.is_empty()); + assert_eq!(resp.url, HTTPBIN_GET); + } + + #[tokio::test] + async fn client_builder_addr_override() { + let mut builder = Client::builder(); + builder.default_host("httpbin.org").address(SocketAddr::new( + IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + 8888, + )); + let client = builder.build().unwrap(); + + let addr = DnsResolver::default() + .resolve("httpbin.org", HTTP_DEFAULT_PORT) + .await + .unwrap(); + + let resp = client + .get(format!("http://{addr}/get")) + .send() + .await + .unwrap() + .into_json::() + .await + .unwrap(); + assert!(resp.args.is_empty()); + assert_eq!(resp.url, HTTPBIN_GET); + } + #[cfg(feature = "__tls")] #[tokio::test] async fn client_builder_with_https() {