unit.rs 18 KB

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