Skip to content

Commit 6637563

Browse files
committed
test(legacy): add test to overriding the resolved socket's port
1 parent a13b97c commit 6637563

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/client/legacy/connect/http.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,7 @@ where
433433
.map_err(ConnectError::dns)?;
434434
let addrs = addrs
435435
.map(|mut addr| {
436-
// Respect explicit ports in the URI,
437-
// and non `0` ports resolved from a custom dns resolver.
438-
if dst.port().is_some() || addr.port() == 0 {
439-
addr.set_port(port)
440-
};
436+
set_port(&mut addr, port, dst.port().is_some());
441437

442438
addr
443439
})
@@ -830,9 +826,19 @@ impl ConnectingTcp<'_> {
830826
}
831827
}
832828

829+
/// Respect explicit ports in the URI, if none, either
830+
/// keep non `0` ports resolved from a custom dns resolver,
831+
/// or use the default port for the scheme.
832+
fn set_port(addr: &mut SocketAddr, host_port: u16, explicit: bool) {
833+
if explicit || addr.port() == 0 {
834+
addr.set_port(host_port)
835+
};
836+
}
837+
833838
#[cfg(test)]
834839
mod tests {
835840
use std::io;
841+
use std::net::SocketAddr;
836842

837843
use ::http::Uri;
838844

@@ -841,6 +847,8 @@ mod tests {
841847
use super::super::sealed::{Connect, ConnectSvc};
842848
use super::{Config, ConnectError, HttpConnector};
843849

850+
use super::set_port;
851+
844852
async fn connect<C>(
845853
connector: C,
846854
dst: Uri,
@@ -1239,4 +1247,22 @@ mod tests {
12391247
panic!("test failed");
12401248
}
12411249
}
1250+
1251+
#[test]
1252+
fn test_set_port() {
1253+
// Respect explicit ports no matter what the resolved port is.
1254+
let mut addr = SocketAddr::from(([0, 0, 0, 0], 6881));
1255+
set_port(&mut addr, 42, true);
1256+
assert_eq!(addr.port(), 42);
1257+
1258+
// Ignore default host port, and use the socket port instead.
1259+
let mut addr = SocketAddr::from(([0, 0, 0, 0], 6881));
1260+
set_port(&mut addr, 443, false);
1261+
assert_eq!(addr.port(), 6881);
1262+
1263+
// Use the default port if the resolved port is `0`.
1264+
let mut addr = SocketAddr::from(([0, 0, 0, 0], 0));
1265+
set_port(&mut addr, 443, false);
1266+
assert_eq!(addr.port(), 443);
1267+
}
12421268
}

0 commit comments

Comments
 (0)