Преглед на файлове

Add Display for HostAndPort

Tomasz преди 9 години
родител
ревизия
133b64ef39
променени са 2 файла, в които са добавени 34 реда и са изтрити 1 реда
  1. 9 0
      src/host.rs
  2. 25 1
      tests/unit.rs

+ 9 - 0
src/host.rs

@@ -192,6 +192,15 @@ impl<'a> HostAndPort<&'a str> {
     }
     }
 }
 }
 
 
+impl<S: AsRef<str>> fmt::Display for HostAndPort<S> {
+    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+        try!(self.host.fmt(f));
+        try!(f.write_str(":"));
+        self.port.fmt(f)
+    }
+}
+
+
 impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
 impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
     type Iter = SocketAddrs;
     type Iter = SocketAddrs;
 
 

+ 25 - 1
tests/unit.rs

@@ -13,7 +13,7 @@ extern crate url;
 use std::borrow::Cow;
 use std::borrow::Cow;
 use std::net::{Ipv4Addr, Ipv6Addr};
 use std::net::{Ipv4Addr, Ipv6Addr};
 use std::path::{Path, PathBuf};
 use std::path::{Path, PathBuf};
-use url::{Host, Url, form_urlencoded};
+use url::{Host, HostAndPort, Url, form_urlencoded};
 
 
 #[test]
 #[test]
 fn size() {
 fn size() {
@@ -254,6 +254,30 @@ fn test_form_serialize() {
     assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
     assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
 }
 }
 
 
+#[test]
+fn host_and_port_display() {
+    let data = [
+        (HostAndPort{ host: Host::Domain("www.mozilla.org"), port: 80}, "www.mozilla.org:80"),
+        (
+            HostAndPort{ host: Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), port: 65535 },
+            "1.35.33.49:65535"
+        ),
+        (
+            HostAndPort{
+                host: Host::Ipv6(Ipv6Addr::new(
+                    0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344
+                )),
+                port: 1337
+            },
+            "[2001:db8:85a3:8d3:1319:8a2e:370:7344]:1337"
+        )
+    ];
+
+    for &(ref input, result) in &data {
+        assert_eq!(format!("{}", input), result)
+    }
+}
+
 #[test]
 #[test]
 /// https://github.com/servo/rust-url/issues/25
 /// https://github.com/servo/rust-url/issues/25
 fn issue_25() {
 fn issue_25() {