data.rs 7.8 KB

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