data.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2013-2014 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. //! Data-driven tests
  9. use std::str::FromStr;
  10. use serde_json::Value;
  11. use url::{quirks, Url};
  12. #[test]
  13. fn urltestdata() {
  14. let idna_skip_inputs = [
  15. "http://www.foo。bar.com",
  16. "http://Go.com",
  17. "http://你好你好",
  18. "https://faß.ExAmPlE/",
  19. "http://0Xc0.0250.01",
  20. "ftp://%e2%98%83",
  21. "https://%e2%98%83",
  22. "file://a\u{ad}b/p",
  23. "file://a%C2%ADb/p",
  24. "http://GOO\u{200b}\u{2060}\u{feff}goo.com",
  25. ];
  26. // Copied from https://github.com/web-platform-tests/wpt/blob/master/url/
  27. let mut json = Value::from_str(include_str!("urltestdata.json"))
  28. .expect("JSON parse error in urltestdata.json");
  29. let mut passed = true;
  30. for entry in json.as_array_mut().unwrap() {
  31. if entry.is_string() {
  32. continue; // ignore comments
  33. }
  34. let maybe_base = entry
  35. .take_key("base")
  36. .expect("missing base key")
  37. .maybe_string();
  38. let input = entry.take_string("input");
  39. let failure = entry.take_key("failure").is_some();
  40. {
  41. if idna_skip_inputs.contains(&input.as_str()) {
  42. continue;
  43. }
  44. }
  45. let res = if let Some(base) = maybe_base {
  46. let base = match Url::parse(&base) {
  47. Ok(base) => base,
  48. Err(_) if failure => continue,
  49. Err(message) => {
  50. eprint_failure(
  51. format!(" failed: error parsing base {:?}: {}", base, message),
  52. &format!("parse base for {:?}", input),
  53. None,
  54. );
  55. passed = false;
  56. continue;
  57. }
  58. };
  59. base.join(&input)
  60. } else {
  61. Url::parse(&input)
  62. };
  63. let url = match (res, failure) {
  64. (Ok(url), false) => url,
  65. (Err(_), true) => continue,
  66. (Err(message), false) => {
  67. eprint_failure(
  68. format!(" failed: {}", message),
  69. &format!("parse URL for {:?}", input),
  70. None,
  71. );
  72. passed = false;
  73. continue;
  74. }
  75. (Ok(_), true) => {
  76. eprint_failure(
  77. format!(" failed: expected parse error for URL {:?}", input),
  78. &format!("parse URL for {:?}", input),
  79. None,
  80. );
  81. passed = false;
  82. continue;
  83. }
  84. };
  85. passed &= check_invariants(&url, &format!("invariants for {:?}", input), None);
  86. for &attr in ATTRIBS {
  87. passed &= test_eq_eprint(
  88. entry.take_string(attr),
  89. get(&url, attr),
  90. &format!("{:?} - {}", input, attr),
  91. None,
  92. );
  93. }
  94. if let Some(expected_origin) = entry.take_key("origin").map(|s| s.string()) {
  95. passed &= test_eq_eprint(
  96. expected_origin,
  97. &quirks::origin(&url),
  98. &format!("origin for {:?}", input),
  99. None,
  100. );
  101. }
  102. }
  103. assert!(passed)
  104. }
  105. #[test]
  106. fn setters_tests() {
  107. let mut json = Value::from_str(include_str!("setters_tests.json"))
  108. .expect("JSON parse error in setters_tests.json");
  109. let mut passed = true;
  110. for &attr in ATTRIBS {
  111. if attr == "href" {
  112. continue;
  113. }
  114. let mut tests = json.take_key(attr).unwrap();
  115. for mut test in tests.as_array_mut().unwrap().drain(..) {
  116. let comment = test.take_key("comment").map(|s| s.string());
  117. {
  118. if let Some(comment) = comment.as_ref() {
  119. if comment.starts_with("IDNA Nontransitional_Processing") {
  120. continue;
  121. }
  122. }
  123. }
  124. let href = test.take_string("href");
  125. let new_value = test.take_string("new_value");
  126. let name = format!("{:?}.{} = {:?}", href, attr, new_value);
  127. let mut expected = test.take_key("expected").unwrap();
  128. let mut url = Url::parse(&href).unwrap();
  129. let comment_ref = comment.as_deref();
  130. passed &= check_invariants(&url, &name, comment_ref);
  131. set(&mut url, attr, &new_value);
  132. for attr in ATTRIBS {
  133. if let Some(value) = expected.take_key(attr) {
  134. passed &= test_eq_eprint(value.string(), get(&url, attr), &name, comment_ref);
  135. };
  136. }
  137. passed &= check_invariants(&url, &name, comment_ref);
  138. }
  139. }
  140. assert!(passed);
  141. }
  142. fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool {
  143. let mut passed = true;
  144. if let Err(e) = url.check_invariants() {
  145. passed = false;
  146. eprint_failure(
  147. format!(" failed: invariants checked -> {:?}", e),
  148. name,
  149. comment,
  150. );
  151. }
  152. #[cfg(feature = "serde")]
  153. {
  154. let bytes = serde_json::to_vec(url).unwrap();
  155. let new_url: Url = serde_json::from_slice(&bytes).unwrap();
  156. passed &= test_eq_eprint(url.to_string(), &new_url.to_string(), name, comment);
  157. }
  158. passed
  159. }
  160. trait JsonExt {
  161. fn take_key(&mut self, key: &str) -> Option<Value>;
  162. fn string(self) -> String;
  163. fn maybe_string(self) -> Option<String>;
  164. fn take_string(&mut self, key: &str) -> String;
  165. }
  166. impl JsonExt for Value {
  167. fn take_key(&mut self, key: &str) -> Option<Value> {
  168. self.as_object_mut().unwrap().remove(key)
  169. }
  170. fn string(self) -> String {
  171. self.maybe_string().expect("")
  172. }
  173. fn maybe_string(self) -> Option<String> {
  174. match self {
  175. Value::String(s) => Some(s),
  176. Value::Null => None,
  177. _ => panic!("Not a Value::String or Value::Null"),
  178. }
  179. }
  180. fn take_string(&mut self, key: &str) -> String {
  181. self.take_key(key).unwrap().string()
  182. }
  183. }
  184. fn get<'a>(url: &'a Url, attr: &str) -> &'a str {
  185. match attr {
  186. "href" => quirks::href(url),
  187. "protocol" => quirks::protocol(url),
  188. "username" => quirks::username(url),
  189. "password" => quirks::password(url),
  190. "hostname" => quirks::hostname(url),
  191. "host" => quirks::host(url),
  192. "port" => quirks::port(url),
  193. "pathname" => quirks::pathname(url),
  194. "search" => quirks::search(url),
  195. "hash" => quirks::hash(url),
  196. _ => unreachable!(),
  197. }
  198. }
  199. #[allow(clippy::unit_arg)]
  200. fn set(url: &mut Url, attr: &str, new: &str) {
  201. let _ = match attr {
  202. "protocol" => quirks::set_protocol(url, new),
  203. "username" => quirks::set_username(url, new),
  204. "password" => quirks::set_password(url, new),
  205. "hostname" => quirks::set_hostname(url, new),
  206. "host" => quirks::set_host(url, new),
  207. "port" => quirks::set_port(url, new),
  208. "pathname" => Ok(quirks::set_pathname(url, new)),
  209. "search" => Ok(quirks::set_search(url, new)),
  210. "hash" => Ok(quirks::set_hash(url, new)),
  211. _ => unreachable!(),
  212. };
  213. }
  214. fn test_eq_eprint(expected: String, actual: &str, name: &str, comment: Option<&str>) -> bool {
  215. if expected == actual {
  216. return true;
  217. }
  218. eprint_failure(
  219. format!("expected: {}\n actual: {}", expected, actual),
  220. name,
  221. comment,
  222. );
  223. false
  224. }
  225. fn eprint_failure(err: String, name: &str, comment: Option<&str>) {
  226. eprintln!(" test: {}\n{}", name, err);
  227. if let Some(comment) = comment {
  228. eprintln!("{}\n", comment);
  229. } else {
  230. eprintln!();
  231. }
  232. }
  233. const ATTRIBS: &[&str] = &[
  234. "href", "protocol", "username", "password", "host", "hostname", "port", "pathname", "search",
  235. "hash",
  236. ];