data.rs 7.9 KB

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