data.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 form https://github.com/w3c/web-platform-tests/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 base = entry.take_string("base");
  37. let input = entry.take_string("input");
  38. let failure = entry.take_key("failure").is_some();
  39. #[cfg(not(feature = "idna"))]
  40. if idna_skip_inputs.contains(&input.as_str()) {
  41. continue;
  42. }
  43. let base = match Url::parse(&base) {
  44. Ok(base) => base,
  45. Err(_) if failure => continue,
  46. Err(message) => {
  47. eprint_failure(
  48. format!(" failed: error parsing base {:?}: {}", base, message),
  49. &format!("parse base for {:?}", input),
  50. None,
  51. );
  52. passed = false;
  53. continue;
  54. }
  55. };
  56. let url = match (base.join(&input), failure) {
  57. (Ok(url), false) => url,
  58. (Err(_), true) => continue,
  59. (Err(message), false) => {
  60. eprint_failure(
  61. format!(" failed: {}", message),
  62. &format!("parse URL for {:?}", input),
  63. None,
  64. );
  65. passed = false;
  66. continue;
  67. }
  68. (Ok(_), true) => {
  69. eprint_failure(
  70. format!(" failed: expected parse error for URL {:?}", input),
  71. &format!("parse URL for {:?}", input),
  72. None,
  73. );
  74. passed = false;
  75. continue;
  76. }
  77. };
  78. passed &= check_invariants(&url, &format!("invariants for {:?}", input), None);
  79. for &attr in ATTRIBS {
  80. passed &= test_eq_eprint(
  81. entry.take_string(attr),
  82. get(&url, attr),
  83. &format!("{:?} - {}", input, attr),
  84. None,
  85. );
  86. }
  87. if let Some(expected_origin) = entry.take_key("origin").map(|s| s.string()) {
  88. passed &= test_eq_eprint(
  89. expected_origin,
  90. &quirks::origin(&url),
  91. &format!("origin for {:?}", input),
  92. None,
  93. );
  94. }
  95. }
  96. assert!(passed)
  97. }
  98. #[allow(clippy::option_as_ref_deref)] // introduced in 1.40, MSRV is 1.36
  99. #[test]
  100. fn setters_tests() {
  101. let mut json = Value::from_str(include_str!("setters_tests.json"))
  102. .expect("JSON parse error in setters_tests.json");
  103. let mut passed = true;
  104. for &attr in ATTRIBS {
  105. if attr == "href" {
  106. continue;
  107. }
  108. let mut tests = json.take_key(attr).unwrap();
  109. for mut test in tests.as_array_mut().unwrap().drain(..) {
  110. let comment = test.take_key("comment").map(|s| s.string());
  111. #[cfg(not(feature = "idna"))]
  112. if let Some(comment) = comment.as_ref() {
  113. if comment.starts_with("IDNA Nontransitional_Processing") {
  114. continue;
  115. }
  116. }
  117. let href = test.take_string("href");
  118. let new_value = test.take_string("new_value");
  119. let name = format!("{:?}.{} = {:?}", href, attr, new_value);
  120. let mut expected = test.take_key("expected").unwrap();
  121. let mut url = Url::parse(&href).unwrap();
  122. let comment_ref = comment.as_ref().map(|s| s.deref());
  123. passed &= check_invariants(&url, &name, comment_ref);
  124. let _ = set(&mut url, attr, &new_value);
  125. for attr in ATTRIBS {
  126. if let Some(value) = expected.take_key(attr) {
  127. passed &= test_eq_eprint(value.string(), get(&url, attr), &name, comment_ref);
  128. };
  129. }
  130. passed &= check_invariants(&url, &name, comment_ref);
  131. }
  132. }
  133. assert!(passed);
  134. }
  135. fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool {
  136. let mut passed = true;
  137. if let Err(e) = url.check_invariants() {
  138. passed = false;
  139. eprint_failure(
  140. format!(" failed: invariants checked -> {:?}", e),
  141. name,
  142. comment,
  143. );
  144. }
  145. #[cfg(feature = "serde")]
  146. {
  147. let bytes = serde_json::to_vec(url).unwrap();
  148. let new_url: Url = serde_json::from_slice(&bytes).unwrap();
  149. passed &= test_eq_eprint(url.to_string(), &new_url.to_string(), name, comment);
  150. }
  151. passed
  152. }
  153. trait JsonExt {
  154. fn take_key(&mut self, key: &str) -> Option<Value>;
  155. fn string(self) -> String;
  156. fn take_string(&mut self, key: &str) -> String;
  157. }
  158. impl JsonExt for Value {
  159. fn take_key(&mut self, key: &str) -> Option<Value> {
  160. self.as_object_mut().unwrap().remove(key)
  161. }
  162. fn string(self) -> String {
  163. if let Value::String(s) = self {
  164. s
  165. } else {
  166. panic!("Not a Value::String")
  167. }
  168. }
  169. fn take_string(&mut self, key: &str) -> String {
  170. self.take_key(key).unwrap().string()
  171. }
  172. }
  173. fn get<'a>(url: &'a Url, attr: &str) -> &'a str {
  174. match attr {
  175. "href" => quirks::href(url),
  176. "protocol" => quirks::protocol(url),
  177. "username" => quirks::username(url),
  178. "password" => quirks::password(url),
  179. "hostname" => quirks::hostname(url),
  180. "host" => quirks::host(url),
  181. "port" => quirks::port(url),
  182. "pathname" => quirks::pathname(url),
  183. "search" => quirks::search(url),
  184. "hash" => quirks::hash(url),
  185. _ => unreachable!(),
  186. }
  187. }
  188. #[allow(clippy::unit_arg)]
  189. fn set<'a>(url: &'a mut Url, attr: &str, new: &str) {
  190. let _ = match attr {
  191. "protocol" => quirks::set_protocol(url, new),
  192. "username" => quirks::set_username(url, new),
  193. "password" => quirks::set_password(url, new),
  194. "hostname" => quirks::set_hostname(url, new),
  195. "host" => quirks::set_host(url, new),
  196. "port" => quirks::set_port(url, new),
  197. "pathname" => Ok(quirks::set_pathname(url, new)),
  198. "search" => Ok(quirks::set_search(url, new)),
  199. "hash" => Ok(quirks::set_hash(url, new)),
  200. _ => unreachable!(),
  201. };
  202. }
  203. fn test_eq_eprint(expected: String, actual: &str, name: &str, comment: Option<&str>) -> bool {
  204. if expected == actual {
  205. return true;
  206. }
  207. eprint_failure(
  208. format!("expected: {}\n actual: {}", expected, actual),
  209. name,
  210. comment,
  211. );
  212. false
  213. }
  214. fn eprint_failure(err: String, name: &str, comment: Option<&str>) {
  215. eprintln!(" test: {}\n{}", name, err);
  216. if let Some(comment) = comment {
  217. eprintln!("{}\n", comment);
  218. } else {
  219. eprintln!();
  220. }
  221. }
  222. const ATTRIBS: &[&str] = &[
  223. "href", "protocol", "username", "password", "host", "hostname", "port", "pathname", "search",
  224. "hash",
  225. ];