unit.rs 19 KB

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