Skip to content

Commit

Permalink
Fix a corner case of Url::set_host
Browse files Browse the repository at this point in the history
Some URLs can be like foo:///bar with an authority but no host.
This is different from foo:/bar with no authority.

Fix #25
  • Loading branch information
SimonSapin committed Apr 21, 2016
1 parent df3dcd6 commit ceb1878
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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('/');
Expand Down
14 changes: 14 additions & 0 deletions tests/unit.rs
Expand Up @@ -233,3 +233,17 @@ 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 mut url = Url::from_file_path("/run/pg.socket").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();
assert_eq!(url.as_str(), "postgres://me@/run/pg.socket")
}

0 comments on commit ceb1878

Please sign in to comment.