From 3118b4623278f716c53c5ef65ad9689be0c32d06 Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 28 Jan 2022 18:25:40 +0100 Subject: [PATCH 1/2] fix testdata runner --- url/tests/data.rs | 51 ++++++++++++++++++++++++-------------- url/tests/urltestdata.json | 11 ++++++++ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/url/tests/data.rs b/url/tests/data.rs index 79a70e1f4..e0191999b 100644 --- a/url/tests/data.rs +++ b/url/tests/data.rs @@ -30,7 +30,7 @@ fn urltestdata() { "http://GOO\u{200b}\u{2060}\u{feff}goo.com", ]; - // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/ + // Copied from https://github.com/web-platform-tests/wpt/blob/master/url/ let mut json = Value::from_str(include_str!("urltestdata.json")) .expect("JSON parse error in urltestdata.json"); @@ -40,7 +40,10 @@ fn urltestdata() { continue; // ignore comments } - let base = entry.take_string("base"); + let maybe_base = entry + .take_key("base") + .expect("missing base key") + .maybe_string(); let input = entry.take_string("input"); let failure = entry.take_key("failure").is_some(); @@ -51,21 +54,26 @@ fn urltestdata() { } } - let base = match Url::parse(&base) { - Ok(base) => base, - Err(_) if failure => continue, - Err(message) => { - eprint_failure( - format!(" failed: error parsing base {:?}: {}", base, message), - &format!("parse base for {:?}", input), - None, - ); - passed = false; - continue; - } + let res = if let Some(base) = maybe_base { + let base = match Url::parse(&base) { + Ok(base) => base, + Err(_) if failure => continue, + Err(message) => { + eprint_failure( + format!(" failed: error parsing base {:?}: {}", base, message), + &format!("parse base for {:?}", input), + None, + ); + passed = false; + continue; + } + }; + base.join(&input) + } else { + Url::parse(&input) }; - let url = match (base.join(&input), failure) { + let url = match (res, failure) { (Ok(url), false) => url, (Err(_), true) => continue, (Err(message), false) => { @@ -182,6 +190,7 @@ fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool { trait JsonExt { fn take_key(&mut self, key: &str) -> Option; fn string(self) -> String; + fn maybe_string(self) -> Option; fn take_string(&mut self, key: &str) -> String; } @@ -191,10 +200,14 @@ impl JsonExt for Value { } fn string(self) -> String { - if let Value::String(s) = self { - s - } else { - panic!("Not a Value::String") + self.maybe_string().expect("") + } + + fn maybe_string(self) -> Option { + match self { + Value::String(s) => Some(s), + Value::Null => None, + _ => panic!("Not a Value::String or Value::Null"), } } diff --git a/url/tests/urltestdata.json b/url/tests/urltestdata.json index 290932785..c86d50e93 100644 --- a/url/tests/urltestdata.json +++ b/url/tests/urltestdata.json @@ -7688,5 +7688,16 @@ "protocol": "abc:", "search": "", "username": "" + }, + "Empty query and fragment with blank should throw an error", + { + "input": "#", + "base": null, + "failure": true + }, + { + "input": "?", + "base": null, + "failure": true } ] From 74e0cfbd2fa39d7c9afca25d6f77a193ede4f242 Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 28 Jan 2022 18:46:26 +0100 Subject: [PATCH 2/2] fmt --- url/src/host.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/url/src/host.rs b/url/src/host.rs index d0a559589..92cb4df61 100644 --- a/url/src/host.rs +++ b/url/src/host.rs @@ -90,24 +90,20 @@ impl Host { } let is_invalid_domain_char = |c| { - matches!( - c, - | '\0'..='\u{001F}' - | ' ' - | '#' - | '%' - | '/' - | ':' - | '<' - | '>' - | '?' - | '@' - | '[' - | '\\' - | ']' - | '^' - | '\u{007F}' - ) + matches!(c, |'\0'..='\u{001F}'| ' ' + | '#' + | '%' + | '/' + | ':' + | '<' + | '>' + | '?' + | '@' + | '[' + | '\\' + | ']' + | '^' + | '\u{007F}') }; if domain.find(is_invalid_domain_char).is_some() {