Skip to content

Commit

Permalink
fix(volo-http): fix wrong rule of setting header Host (#532)
Browse files Browse the repository at this point in the history
In addition, some unit tests have been added here to improve test
coverage.

Signed-off-by: Yu Li <[email protected]>
  • Loading branch information
yukiiiteru authored Nov 12, 2024
1 parent b7cd407 commit 2191a43
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions volo-http/src/client/layer/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ where
mut req: ClientRequest<B>,
) -> impl Future<Output = Result<Self::Response, Self::Error>> + 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)
Expand Down
50 changes: 49 additions & 1 deletion volo-http/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<HttpBinResponse>()
.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::<HttpBinResponse>()
.await
.unwrap();
assert!(resp.args.is_empty());
assert_eq!(resp.url, HTTPBIN_GET);
}

#[cfg(feature = "__tls")]
#[tokio::test]
async fn client_builder_with_https() {
Expand Down

0 comments on commit 2191a43

Please sign in to comment.