data.rs 8.3 KB

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