data.rs 8.2 KB

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