Răsfoiți Sursa

Fix a corner case of Url::set_host

Some URLs can be like foo:///bar with an authority but no host.
This is different from foo:/bar with no authority.

Fix #25
Simon Sapin 10 ani în urmă
părinte
comite
bb72f19592
2 a modificat fișierele cu 17 adăugiri și 1 ștergeri
  1. 1 1
      src/lib.rs
  2. 16 0
      tests/unit.rs

+ 1 - 1
src/lib.rs

@@ -848,7 +848,7 @@ impl Url {
         let old_suffix_pos = if opt_new_port.is_some() { self.path_start } else { self.host_end };
         let suffix = self.slice(old_suffix_pos..).to_owned();
         self.serialization.truncate(self.host_start as usize);
-        if !self.has_host() {
+        if !self.has_authority() {
             debug_assert!(self.slice(self.scheme_end..self.host_start) == ":");
             debug_assert!(self.username_end == self.host_start);
             self.serialization.push('/');

+ 16 - 0
tests/unit.rs

@@ -233,3 +233,19 @@ fn test_form_serialize() {
         .finish();
     assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
 }
+
+#[test]
+/// https://github.com/servo/rust-url/issues/25
+fn issue_25() {
+    let filename = if cfg!(windows) { r"C:\run\pg.sock" } else { "/run/pg.sock" };
+    let mut url = Url::from_file_path(filename).unwrap();
+    url.assert_invariants();
+    url.set_scheme("postgres").unwrap();
+    url.assert_invariants();
+    url.set_host(Some("")).unwrap();
+    url.assert_invariants();
+    url.set_username("me").unwrap();
+    url.assert_invariants();
+    let expected = format!("postgres://me@/{}run/pg.sock", if cfg!(windows) { "C:/" } else { "" });
+    assert_eq!(url.as_str(), expected);
+}