Skip to content

Commit 2191a43

Browse files
authored
fix(volo-http): fix wrong rule of setting header Host (#532)
In addition, some unit tests have been added here to improve test coverage. Signed-off-by: Yu Li <[email protected]>
1 parent b7cd407 commit 2191a43

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

volo-http/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volo-http"
3-
version = "0.3.0-rc.2"
3+
version = "0.3.0-rc.3"
44
edition.workspace = true
55
homepage.workspace = true
66
repository.workspace = true

volo-http/src/client/layer/header.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ where
199199
mut req: ClientRequest<B>,
200200
) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send {
201201
if !req.headers().contains_key(header::HOST) {
202-
if let Some(val) = &self.val {
203-
req.headers_mut().insert(header::HOST, val.clone());
204-
} else if let Some(val) = gen_host_by_ep(cx.rpc_info().callee()) {
202+
if let Some(val) = gen_host_by_ep(cx.rpc_info().callee()) {
205203
req.headers_mut().insert(header::HOST, val);
204+
} else if let Some(val) = &self.val {
205+
req.headers_mut().insert(header::HOST, val.clone());
206206
}
207207
}
208208
self.inner.call(cx, req)

volo-http/src/client/mod.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,11 @@ where
955955
#[cfg(feature = "json")]
956956
#[cfg(test)]
957957
mod client_tests {
958-
use std::{collections::HashMap, future::Future};
958+
use std::{
959+
collections::HashMap,
960+
future::Future,
961+
net::{IpAddr, Ipv4Addr, SocketAddr},
962+
};
959963

960964
#[cfg(feature = "cookie")]
961965
use cookie::Cookie;
@@ -1130,6 +1134,50 @@ mod client_tests {
11301134
assert_eq!(resp.url, HTTPBIN_GET);
11311135
}
11321136

1137+
#[tokio::test]
1138+
async fn client_builder_host_override() {
1139+
let mut builder = Client::builder();
1140+
builder.host("this.domain.must.be.invalid");
1141+
let client = builder.build().unwrap();
1142+
1143+
let resp = client
1144+
.get(HTTPBIN_GET)
1145+
.send()
1146+
.await
1147+
.unwrap()
1148+
.into_json::<HttpBinResponse>()
1149+
.await
1150+
.unwrap();
1151+
assert!(resp.args.is_empty());
1152+
assert_eq!(resp.url, HTTPBIN_GET);
1153+
}
1154+
1155+
#[tokio::test]
1156+
async fn client_builder_addr_override() {
1157+
let mut builder = Client::builder();
1158+
builder.default_host("httpbin.org").address(SocketAddr::new(
1159+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
1160+
8888,
1161+
));
1162+
let client = builder.build().unwrap();
1163+
1164+
let addr = DnsResolver::default()
1165+
.resolve("httpbin.org", HTTP_DEFAULT_PORT)
1166+
.await
1167+
.unwrap();
1168+
1169+
let resp = client
1170+
.get(format!("http://{addr}/get"))
1171+
.send()
1172+
.await
1173+
.unwrap()
1174+
.into_json::<HttpBinResponse>()
1175+
.await
1176+
.unwrap();
1177+
assert!(resp.args.is_empty());
1178+
assert_eq!(resp.url, HTTPBIN_GET);
1179+
}
1180+
11331181
#[cfg(feature = "__tls")]
11341182
#[tokio::test]
11351183
async fn client_builder_with_https() {

0 commit comments

Comments
 (0)