diff --git a/src/host.rs b/src/host.rs index 50488772..8cae0521 100644 --- a/src/host.rs +++ b/src/host.rs @@ -176,7 +176,7 @@ impl> fmt::Display for Host { /// This mostly exists because coherence rules don’t allow us to implement /// `ToSocketAddrs for (Host, u16)`. -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct HostAndPort { pub host: Host, pub port: u16, @@ -212,6 +212,16 @@ impl> ToSocketAddrs for HostAndPort { } } +impl> fmt::Display for HostAndPort { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + use std::fmt::Write; + self.host.fmt(f)?; + f.write_char(':')?; + self.port.fmt(f) + } +} + + /// Socket addresses for an URL. pub struct SocketAddrs { state: SocketAddrsState diff --git a/tests/unit.rs b/tests/unit.rs index 2d13340b..14f06701 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -14,7 +14,7 @@ extern crate url; use std::borrow::Cow; use std::net::{Ipv4Addr, Ipv6Addr}; use std::path::{Path, PathBuf}; -use url::{Host, Url, form_urlencoded}; +use url::{Host, HostAndPort, Url, form_urlencoded}; #[test] fn size() { @@ -197,6 +197,22 @@ fn host() { assert!(Url::parse("http://192.168.0.257").is_err()); } +#[test] +fn host_and_port() { + assert_eq!("www.mozilla.org:443", &format!("{}", HostAndPort { + host: Host::Domain("www.mozilla.org"), + port: 443 + })); + assert_eq!("127.0.0.1:9000", &format!("{}", HostAndPort::<&str> { + host: Host::Ipv4(Ipv4Addr::new(127, 0, 0, 1)), + port: 9000 + })); + assert_eq!("[::1]:80", &format!("{}", HostAndPort::<&str> { + host: Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), + port: 80 + })); +} + #[test] fn host_serialization() { // libstd’s `Display for Ipv6Addr` serializes 0:0:0:0:0:0:_:_ and 0:0:0:0:0:ffff:_:_