unit.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. //! Unit tests
  9. extern crate percent_encoding;
  10. extern crate url;
  11. use std::borrow::Cow;
  12. use std::cell::{Cell, RefCell};
  13. use std::net::{Ipv4Addr, Ipv6Addr};
  14. use std::path::{Path, PathBuf};
  15. use url::{form_urlencoded, Host, Url};
  16. #[test]
  17. fn size() {
  18. use std::mem::size_of;
  19. assert_eq!(size_of::<Url>(), size_of::<Option<Url>>());
  20. }
  21. #[test]
  22. fn test_relative() {
  23. let base: Url = "sc://%C3%B1".parse().unwrap();
  24. let url = base.join("/resources/testharness.js").unwrap();
  25. assert_eq!(url.as_str(), "sc://%C3%B1/resources/testharness.js");
  26. }
  27. #[test]
  28. fn test_relative_empty() {
  29. let base: Url = "sc://%C3%B1".parse().unwrap();
  30. let url = base.join("").unwrap();
  31. assert_eq!(url.as_str(), "sc://%C3%B1");
  32. }
  33. #[test]
  34. fn test_set_empty_host() {
  35. let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
  36. base.set_username("").unwrap();
  37. assert_eq!(base.as_str(), "moz://:bar@servo/baz");
  38. base.set_host(None).unwrap();
  39. assert_eq!(base.as_str(), "moz:/baz");
  40. base.set_host(Some("servo")).unwrap();
  41. assert_eq!(base.as_str(), "moz://servo/baz");
  42. }
  43. #[test]
  44. fn test_set_empty_hostname() {
  45. use url::quirks;
  46. let mut base: Url = "moz://foo@servo/baz".parse().unwrap();
  47. assert!(
  48. quirks::set_hostname(&mut base, "").is_err(),
  49. "setting an empty hostname to a url with a username should fail"
  50. );
  51. base = "moz://:pass@servo/baz".parse().unwrap();
  52. assert!(
  53. quirks::set_hostname(&mut base, "").is_err(),
  54. "setting an empty hostname to a url with a password should fail"
  55. );
  56. base = "moz://servo/baz".parse().unwrap();
  57. quirks::set_hostname(&mut base, "").unwrap();
  58. assert_eq!(base.as_str(), "moz:///baz");
  59. }
  60. macro_rules! assert_from_file_path {
  61. ($path: expr) => {
  62. assert_from_file_path!($path, $path)
  63. };
  64. ($path: expr, $url_path: expr) => {{
  65. let url = Url::from_file_path(Path::new($path)).unwrap();
  66. assert_eq!(url.host(), None);
  67. assert_eq!(url.path(), $url_path);
  68. assert_eq!(url.to_file_path(), Ok(PathBuf::from($path)));
  69. }};
  70. }
  71. #[test]
  72. fn new_file_paths() {
  73. if cfg!(unix) {
  74. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  75. assert_eq!(Url::from_file_path(Path::new("../relative")), Err(()));
  76. }
  77. if cfg!(windows) {
  78. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  79. assert_eq!(Url::from_file_path(Path::new(r"..\relative")), Err(()));
  80. assert_eq!(Url::from_file_path(Path::new(r"\drive-relative")), Err(()));
  81. assert_eq!(Url::from_file_path(Path::new(r"\\ucn\")), Err(()));
  82. }
  83. if cfg!(unix) {
  84. assert_from_file_path!("/foo/bar");
  85. assert_from_file_path!("/foo/ba\0r", "/foo/ba%00r");
  86. assert_from_file_path!("/foo/ba%00r", "/foo/ba%2500r");
  87. }
  88. }
  89. #[test]
  90. #[cfg(unix)]
  91. fn new_path_bad_utf8() {
  92. use std::ffi::OsStr;
  93. use std::os::unix::prelude::*;
  94. let url = Url::from_file_path(Path::new(OsStr::from_bytes(b"/foo/ba\x80r"))).unwrap();
  95. let os_str = OsStr::from_bytes(b"/foo/ba\x80r");
  96. assert_eq!(url.to_file_path(), Ok(PathBuf::from(os_str)));
  97. }
  98. #[test]
  99. fn new_path_windows_fun() {
  100. if cfg!(windows) {
  101. assert_from_file_path!(r"C:\foo\bar", "/C:/foo/bar");
  102. assert_from_file_path!("C:\\foo\\ba\0r", "/C:/foo/ba%00r");
  103. // Invalid UTF-8
  104. assert!(Url::parse("file:///C:/foo/ba%80r")
  105. .unwrap()
  106. .to_file_path()
  107. .is_err());
  108. // test windows canonicalized path
  109. let path = PathBuf::from(r"\\?\C:\foo\bar");
  110. assert!(Url::from_file_path(path).is_ok());
  111. // Percent-encoded drive letter
  112. let url = Url::parse("file:///C%3A/foo/bar").unwrap();
  113. assert_eq!(url.to_file_path(), Ok(PathBuf::from(r"C:\foo\bar")));
  114. }
  115. }
  116. #[test]
  117. fn new_directory_paths() {
  118. if cfg!(unix) {
  119. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  120. assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(()));
  121. let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap();
  122. assert_eq!(url.host(), None);
  123. assert_eq!(url.path(), "/foo/bar/");
  124. }
  125. if cfg!(windows) {
  126. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  127. assert_eq!(Url::from_directory_path(Path::new(r"..\relative")), Err(()));
  128. assert_eq!(
  129. Url::from_directory_path(Path::new(r"\drive-relative")),
  130. Err(())
  131. );
  132. assert_eq!(Url::from_directory_path(Path::new(r"\\ucn\")), Err(()));
  133. let url = Url::from_directory_path(Path::new(r"C:\foo\bar")).unwrap();
  134. assert_eq!(url.host(), None);
  135. assert_eq!(url.path(), "/C:/foo/bar/");
  136. }
  137. }
  138. #[test]
  139. fn path_backslash_fun() {
  140. let mut special_url = "http://foobar.com".parse::<Url>().unwrap();
  141. special_url.path_segments_mut().unwrap().push("foo\\bar");
  142. assert_eq!(special_url.as_str(), "http://foobar.com/foo%5Cbar");
  143. let mut nonspecial_url = "thing://foobar.com".parse::<Url>().unwrap();
  144. nonspecial_url.path_segments_mut().unwrap().push("foo\\bar");
  145. assert_eq!(nonspecial_url.as_str(), "thing://foobar.com/foo\\bar");
  146. }
  147. #[test]
  148. fn from_str() {
  149. assert!("http://testing.com/this".parse::<Url>().is_ok());
  150. }
  151. #[test]
  152. fn parse_with_params() {
  153. let url = Url::parse_with_params(
  154. "http://testing.com/this?dont=clobberme",
  155. &[("lang", "rust")],
  156. )
  157. .unwrap();
  158. assert_eq!(
  159. url.as_str(),
  160. "http://testing.com/this?dont=clobberme&lang=rust"
  161. );
  162. }
  163. #[test]
  164. fn issue_124() {
  165. let url: Url = "file:a".parse().unwrap();
  166. assert_eq!(url.path(), "/a");
  167. let url: Url = "file:...".parse().unwrap();
  168. assert_eq!(url.path(), "/...");
  169. let url: Url = "file:..".parse().unwrap();
  170. assert_eq!(url.path(), "/");
  171. }
  172. #[test]
  173. fn test_equality() {
  174. use std::collections::hash_map::DefaultHasher;
  175. use std::hash::{Hash, Hasher};
  176. fn check_eq(a: &Url, b: &Url) {
  177. assert_eq!(a, b);
  178. let mut h1 = DefaultHasher::new();
  179. a.hash(&mut h1);
  180. let mut h2 = DefaultHasher::new();
  181. b.hash(&mut h2);
  182. assert_eq!(h1.finish(), h2.finish());
  183. }
  184. fn url(s: &str) -> Url {
  185. let rv = s.parse().unwrap();
  186. check_eq(&rv, &rv);
  187. rv
  188. }
  189. // Doesn't care if default port is given.
  190. let a: Url = url("https://example.com/");
  191. let b: Url = url("https://example.com:443/");
  192. check_eq(&a, &b);
  193. // Different ports
  194. let a: Url = url("http://example.com/");
  195. let b: Url = url("http://example.com:8080/");
  196. assert!(a != b, "{:?} != {:?}", a, b);
  197. // Different scheme
  198. let a: Url = url("http://example.com/");
  199. let b: Url = url("https://example.com/");
  200. assert_ne!(a, b);
  201. // Different host
  202. let a: Url = url("http://foo.com/");
  203. let b: Url = url("http://bar.com/");
  204. assert_ne!(a, b);
  205. // Missing path, automatically substituted. Semantically the same.
  206. let a: Url = url("http://foo.com");
  207. let b: Url = url("http://foo.com/");
  208. check_eq(&a, &b);
  209. }
  210. #[test]
  211. fn host() {
  212. fn assert_host(input: &str, host: Host<&str>) {
  213. assert_eq!(Url::parse(input).unwrap().host(), Some(host));
  214. }
  215. assert_host("http://www.mozilla.org", Host::Domain("www.mozilla.org"));
  216. assert_host(
  217. "http://1.35.33.49",
  218. Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)),
  219. );
  220. assert_host(
  221. "http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]",
  222. Host::Ipv6(Ipv6Addr::new(
  223. 0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344,
  224. )),
  225. );
  226. assert_host("http://1.35.+33.49", Host::Domain("1.35.+33.49"));
  227. assert_host(
  228. "http://[::]",
  229. Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)),
  230. );
  231. assert_host(
  232. "http://[::1]",
  233. Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
  234. );
  235. assert_host(
  236. "http://0x1.0X23.0x21.061",
  237. Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)),
  238. );
  239. assert_host("http://0x1232131", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  240. assert_host("http://111", Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111)));
  241. assert_host("http://2..2.3", Host::Domain("2..2.3"));
  242. assert!(Url::parse("http://42.0x1232131").is_err());
  243. assert!(Url::parse("http://192.168.0.257").is_err());
  244. }
  245. #[test]
  246. fn host_serialization() {
  247. // libstd’s `Display for Ipv6Addr` serializes 0:0:0:0:0:0:_:_ and 0:0:0:0:0:ffff:_:_
  248. // using IPv4-like syntax, as suggested in https://tools.ietf.org/html/rfc5952#section-4
  249. // but https://url.spec.whatwg.org/#concept-ipv6-serializer specifies not to.
  250. // Not [::0.0.0.2] / [::ffff:0.0.0.2]
  251. assert_eq!(
  252. Url::parse("http://[0::2]").unwrap().host_str(),
  253. Some("[::2]")
  254. );
  255. assert_eq!(
  256. Url::parse("http://[0::ffff:0:2]").unwrap().host_str(),
  257. Some("[::ffff:0:2]")
  258. );
  259. }
  260. #[test]
  261. fn test_idna() {
  262. assert!("http://goșu.ro".parse::<Url>().is_ok());
  263. assert_eq!(
  264. Url::parse("http://☃.net/").unwrap().host(),
  265. Some(Host::Domain("xn--n3h.net"))
  266. );
  267. assert!("https://r2---sn-huoa-cvhl.googlevideo.com/crossdomain.xml"
  268. .parse::<Url>()
  269. .is_ok());
  270. }
  271. #[test]
  272. fn test_serialization() {
  273. let data = [
  274. ("http://example.com/", "http://example.com/"),
  275. ("http://addslash.com", "http://addslash.com/"),
  276. ("http://@emptyuser.com/", "http://emptyuser.com/"),
  277. ("http://:@emptypass.com/", "http://emptypass.com/"),
  278. ("http://user@user.com/", "http://user@user.com/"),
  279. (
  280. "http://user:pass@userpass.com/",
  281. "http://user:pass@userpass.com/",
  282. ),
  283. (
  284. "http://slashquery.com/path/?q=something",
  285. "http://slashquery.com/path/?q=something",
  286. ),
  287. (
  288. "http://noslashquery.com/path?q=something",
  289. "http://noslashquery.com/path?q=something",
  290. ),
  291. ];
  292. for &(input, result) in &data {
  293. let url = Url::parse(input).unwrap();
  294. assert_eq!(url.as_str(), result);
  295. }
  296. }
  297. #[test]
  298. fn test_form_urlencoded() {
  299. let pairs: &[(Cow<str>, Cow<str>)] = &[
  300. ("foo".into(), "é&".into()),
  301. ("bar".into(), "".into()),
  302. ("foo".into(), "#".into()),
  303. ];
  304. let encoded = form_urlencoded::Serializer::new(String::new())
  305. .extend_pairs(pairs)
  306. .finish();
  307. assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
  308. assert_eq!(
  309. form_urlencoded::parse(encoded.as_bytes()).collect::<Vec<_>>(),
  310. pairs.to_vec()
  311. );
  312. }
  313. #[test]
  314. fn test_form_serialize() {
  315. let encoded = form_urlencoded::Serializer::new(String::new())
  316. .append_pair("foo", "é&")
  317. .append_pair("bar", "")
  318. .append_pair("foo", "#")
  319. .finish();
  320. assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
  321. }
  322. #[test]
  323. fn form_urlencoded_encoding_override() {
  324. let encoded = form_urlencoded::Serializer::new(String::new())
  325. .encoding_override(Some(&|s| s.as_bytes().to_ascii_uppercase().into()))
  326. .append_pair("foo", "bar")
  327. .finish();
  328. assert_eq!(encoded, "FOO=BAR");
  329. }
  330. #[test]
  331. /// https://github.com/servo/rust-url/issues/61
  332. fn issue_61() {
  333. let mut url = Url::parse("http://mozilla.org").unwrap();
  334. url.set_scheme("https").unwrap();
  335. assert_eq!(url.port(), None);
  336. assert_eq!(url.port_or_known_default(), Some(443));
  337. url.check_invariants().unwrap();
  338. }
  339. #[test]
  340. #[cfg(not(windows))]
  341. /// https://github.com/servo/rust-url/issues/197
  342. fn issue_197() {
  343. let mut url = Url::from_file_path("/").expect("Failed to parse path");
  344. url.check_invariants().unwrap();
  345. assert_eq!(
  346. url,
  347. Url::parse("file:///").expect("Failed to parse path + protocol")
  348. );
  349. url.path_segments_mut()
  350. .expect("path_segments_mut")
  351. .pop_if_empty();
  352. }
  353. #[test]
  354. fn issue_241() {
  355. Url::parse("mailto:").unwrap().cannot_be_a_base();
  356. }
  357. #[test]
  358. /// https://github.com/servo/rust-url/issues/222
  359. fn append_trailing_slash() {
  360. let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
  361. url.check_invariants().unwrap();
  362. url.path_segments_mut().unwrap().push("");
  363. url.check_invariants().unwrap();
  364. assert_eq!(url.to_string(), "http://localhost:6767/foo/bar/?a=b");
  365. }
  366. #[test]
  367. /// https://github.com/servo/rust-url/issues/227
  368. fn extend_query_pairs_then_mutate() {
  369. let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap();
  370. url.query_pairs_mut()
  371. .extend_pairs(vec![("auth", "my-token")].into_iter());
  372. url.check_invariants().unwrap();
  373. assert_eq!(
  374. url.to_string(),
  375. "http://localhost:6767/foo/bar?auth=my-token"
  376. );
  377. url.path_segments_mut().unwrap().push("some_other_path");
  378. url.check_invariants().unwrap();
  379. assert_eq!(
  380. url.to_string(),
  381. "http://localhost:6767/foo/bar/some_other_path?auth=my-token"
  382. );
  383. }
  384. #[test]
  385. /// https://github.com/servo/rust-url/issues/222
  386. fn append_empty_segment_then_mutate() {
  387. let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap();
  388. url.check_invariants().unwrap();
  389. url.path_segments_mut().unwrap().push("").pop();
  390. url.check_invariants().unwrap();
  391. assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b");
  392. }
  393. #[test]
  394. /// https://github.com/servo/rust-url/issues/243
  395. fn test_set_host() {
  396. let mut url = Url::parse("https://example.net/hello").unwrap();
  397. url.set_host(Some("foo.com")).unwrap();
  398. assert_eq!(url.as_str(), "https://foo.com/hello");
  399. assert!(url.set_host(None).is_err());
  400. assert_eq!(url.as_str(), "https://foo.com/hello");
  401. assert!(url.set_host(Some("")).is_err());
  402. assert_eq!(url.as_str(), "https://foo.com/hello");
  403. let mut url = Url::parse("foobar://example.net/hello").unwrap();
  404. url.set_host(None).unwrap();
  405. assert_eq!(url.as_str(), "foobar:/hello");
  406. let mut url = Url::parse("foo://ș").unwrap();
  407. assert_eq!(url.as_str(), "foo://%C8%99");
  408. url.set_host(Some("goșu.ro")).unwrap();
  409. assert_eq!(url.as_str(), "foo://go%C8%99u.ro");
  410. }
  411. #[test]
  412. // https://github.com/servo/rust-url/issues/166
  413. fn test_leading_dots() {
  414. assert_eq!(
  415. Host::parse(".org").unwrap(),
  416. Host::Domain(".org".to_owned())
  417. );
  418. assert_eq!(Url::parse("file://./foo").unwrap().domain(), Some("."));
  419. }
  420. #[test]
  421. /// https://github.com/servo/rust-url/issues/302
  422. fn test_origin_hash() {
  423. use std::collections::hash_map::DefaultHasher;
  424. use std::hash::{Hash, Hasher};
  425. fn hash<T: Hash>(value: &T) -> u64 {
  426. let mut hasher = DefaultHasher::new();
  427. value.hash(&mut hasher);
  428. hasher.finish()
  429. }
  430. let origin = &Url::parse("http://example.net/").unwrap().origin();
  431. let origins_to_compare = [
  432. Url::parse("http://example.net:80/").unwrap().origin(),
  433. Url::parse("http://example.net:81/").unwrap().origin(),
  434. Url::parse("http://example.net").unwrap().origin(),
  435. Url::parse("http://example.net/hello").unwrap().origin(),
  436. Url::parse("https://example.net").unwrap().origin(),
  437. Url::parse("ftp://example.net").unwrap().origin(),
  438. Url::parse("file://example.net").unwrap().origin(),
  439. Url::parse("http://user@example.net/").unwrap().origin(),
  440. Url::parse("http://user:pass@example.net/")
  441. .unwrap()
  442. .origin(),
  443. ];
  444. for origin_to_compare in &origins_to_compare {
  445. if origin == origin_to_compare {
  446. assert_eq!(hash(origin), hash(origin_to_compare));
  447. } else {
  448. assert_ne!(hash(origin), hash(origin_to_compare));
  449. }
  450. }
  451. let opaque_origin = Url::parse("file://example.net").unwrap().origin();
  452. let same_opaque_origin = Url::parse("file://example.net").unwrap().origin();
  453. let other_opaque_origin = Url::parse("file://other").unwrap().origin();
  454. assert_ne!(hash(&opaque_origin), hash(&same_opaque_origin));
  455. assert_ne!(hash(&opaque_origin), hash(&other_opaque_origin));
  456. }
  457. #[test]
  458. fn test_windows_unc_path() {
  459. if !cfg!(windows) {
  460. return;
  461. }
  462. let url = Url::from_file_path(Path::new(r"\\host\share\path\file.txt")).unwrap();
  463. assert_eq!(url.as_str(), "file://host/share/path/file.txt");
  464. let url = Url::from_file_path(Path::new(r"\\höst\share\path\file.txt")).unwrap();
  465. assert_eq!(url.as_str(), "file://xn--hst-sna/share/path/file.txt");
  466. let url = Url::from_file_path(Path::new(r"\\192.168.0.1\share\path\file.txt")).unwrap();
  467. assert_eq!(url.host(), Some(Host::Ipv4(Ipv4Addr::new(192, 168, 0, 1))));
  468. let path = url.to_file_path().unwrap();
  469. assert_eq!(path.to_str(), Some(r"\\192.168.0.1\share\path\file.txt"));
  470. // Another way to write these:
  471. let url = Url::from_file_path(Path::new(r"\\?\UNC\host\share\path\file.txt")).unwrap();
  472. assert_eq!(url.as_str(), "file://host/share/path/file.txt");
  473. // Paths starting with "\\.\" (Local Device Paths) are intentionally not supported.
  474. let url = Url::from_file_path(Path::new(r"\\.\some\path\file.txt"));
  475. assert!(url.is_err());
  476. }
  477. #[test]
  478. fn test_syntax_violation_callback() {
  479. use url::SyntaxViolation::*;
  480. let violation = Cell::new(None);
  481. let url = Url::options()
  482. .syntax_violation_callback(Some(&|v| violation.set(Some(v))))
  483. .parse("http:////mozilla.org:42")
  484. .unwrap();
  485. assert_eq!(url.port(), Some(42));
  486. let v = violation.take().unwrap();
  487. assert_eq!(v, ExpectedDoubleSlash);
  488. assert_eq!(v.description(), "expected //");
  489. assert_eq!(v.to_string(), "expected //");
  490. }
  491. #[test]
  492. fn test_syntax_violation_callback_lifetimes() {
  493. use url::SyntaxViolation::*;
  494. let violation = Cell::new(None);
  495. let vfn = |s| violation.set(Some(s));
  496. let url = Url::options()
  497. .syntax_violation_callback(Some(&vfn))
  498. .parse("http:////mozilla.org:42")
  499. .unwrap();
  500. assert_eq!(url.port(), Some(42));
  501. assert_eq!(violation.take(), Some(ExpectedDoubleSlash));
  502. let url = Url::options()
  503. .syntax_violation_callback(Some(&vfn))
  504. .parse("http://mozilla.org\\path")
  505. .unwrap();
  506. assert_eq!(url.path(), "/path");
  507. assert_eq!(violation.take(), Some(Backslash));
  508. }
  509. #[test]
  510. fn test_options_reuse() {
  511. use url::SyntaxViolation::*;
  512. let violations = RefCell::new(Vec::new());
  513. let vfn = |v| violations.borrow_mut().push(v);
  514. let options = Url::options().syntax_violation_callback(Some(&vfn));
  515. let url = options.parse("http:////mozilla.org").unwrap();
  516. let options = options.base_url(Some(&url));
  517. let url = options.parse("/sub\\path").unwrap();
  518. assert_eq!(url.as_str(), "http://mozilla.org/sub/path");
  519. assert_eq!(*violations.borrow(), vec!(ExpectedDoubleSlash, Backslash));
  520. }
  521. /// https://github.com/servo/rust-url/issues/505
  522. #[cfg(windows)]
  523. #[test]
  524. fn test_url_from_file_path() {
  525. use std::path::PathBuf;
  526. use url::Url;
  527. let p = PathBuf::from("c:///");
  528. let u = Url::from_file_path(p).unwrap();
  529. let path = u.to_file_path().unwrap();
  530. assert_eq!("C:\\", path.to_str().unwrap());
  531. }
  532. /// https://github.com/servo/rust-url/issues/505
  533. #[cfg(not(windows))]
  534. #[test]
  535. fn test_url_from_file_path() {
  536. use std::path::PathBuf;
  537. use url::Url;
  538. let p = PathBuf::from("/c:/");
  539. let u = Url::from_file_path(p).unwrap();
  540. let path = u.to_file_path().unwrap();
  541. assert_eq!("/c:/", path.to_str().unwrap());
  542. }