From bb72f195929dfb732b3cd7b93917c6c633b5a2f7 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 21 Apr 2016 09:32:25 +0200 Subject: [PATCH 1/3] 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 --- src/lib.rs | 2 +- tests/unit.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5feba5b0..31a2e65e 100644 --- a/src/lib.rs +++ b/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('/'); diff --git a/tests/unit.rs b/tests/unit.rs index 6038e1f9..5fe1451c 100644 --- a/tests/unit.rs +++ b/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); +} From 1c2f14922f44c78b877a774a7ef2c147f5d9054a Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 21 Apr 2016 10:08:32 +0200 Subject: [PATCH 2/3] Add a test default port number when changing scheme. Closes #61. (It was fixed by #176.) --- tests/unit.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/unit.rs b/tests/unit.rs index 5fe1451c..09df7efa 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -249,3 +249,13 @@ fn issue_25() { let expected = format!("postgres://me@/{}run/pg.sock", if cfg!(windows) { "C:/" } else { "" }); assert_eq!(url.as_str(), expected); } + +#[test] +/// https://github.com/servo/rust-url/issues/61 +fn issue_61() { + let mut url = Url::parse("http://mozilla.org").unwrap(); + url.set_scheme("https").unwrap(); + assert_eq!(url.port(), None); + assert_eq!(url.port_or_known_default(), Some(443)); + url.assert_invariants(); +} From 0c12771a44e468785e681d06c7a042c93815ade7 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 21 Apr 2016 21:12:52 +0200 Subject: [PATCH 3/3] Hide Url::assert_invariants from docs. --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 31a2e65e..804e6396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,6 +266,7 @@ impl Url { /// Methods of the `Url` struct assume a number of invariants. /// This checks each of these invariants and panic if one is not met. /// This is for testing rust-url itself. + #[doc(hidden)] pub fn assert_invariants(&self) { macro_rules! assert { ($x: expr) => {