tests.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2013-2014 Simon Sapin.
  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. extern crate url;
  9. use std::net::{Ipv4Addr, Ipv6Addr};
  10. use url::{Host, Url};
  11. #[test]
  12. fn new_file_paths() {
  13. use std::path::{Path, PathBuf};
  14. if cfg!(unix) {
  15. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  16. assert_eq!(Url::from_file_path(Path::new("../relative")), Err(()));
  17. } else {
  18. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  19. assert_eq!(Url::from_file_path(Path::new(r"..\relative")), Err(()));
  20. assert_eq!(Url::from_file_path(Path::new(r"\drive-relative")), Err(()));
  21. assert_eq!(Url::from_file_path(Path::new(r"\\ucn\")), Err(()));
  22. }
  23. if cfg!(unix) {
  24. let mut url = Url::from_file_path(Path::new("/foo/bar")).unwrap();
  25. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  26. assert_eq!(url.path(), Some(&["foo".to_string(), "bar".to_string()][..]));
  27. assert!(url.to_file_path() == Ok(PathBuf::from("/foo/bar")));
  28. url.path_mut().unwrap()[1] = "ba\0r".to_string();
  29. url.to_file_path().is_ok();
  30. url.path_mut().unwrap()[1] = "ba%00r".to_string();
  31. url.to_file_path().is_ok();
  32. }
  33. }
  34. #[test]
  35. #[cfg(unix)]
  36. fn new_path_bad_utf8() {
  37. use std::ffi::OsStr;
  38. use std::os::unix::prelude::*;
  39. use std::path::{Path, PathBuf};
  40. let url = Url::from_file_path(Path::new("/foo/ba%80r")).unwrap();
  41. let os_str = OsStr::from_bytes(b"/foo/ba\x80r");
  42. assert_eq!(url.to_file_path(), Ok(PathBuf::from(os_str)));
  43. }
  44. #[test]
  45. fn new_path_windows_fun() {
  46. if cfg!(windows) {
  47. use std::path::{Path, PathBuf};
  48. let mut url = Url::from_file_path(Path::new(r"C:\foo\bar")).unwrap();
  49. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  50. assert_eq!(url.path(), Some(&["C:".to_string(), "foo".to_string(), "bar".to_string()][..]));
  51. assert_eq!(url.to_file_path(),
  52. Ok(PathBuf::from(r"C:\foo\bar")));
  53. url.path_mut().unwrap()[2] = "ba\0r".to_string();
  54. assert!(url.to_file_path().is_ok());
  55. url.path_mut().unwrap()[2] = "ba%00r".to_string();
  56. assert!(url.to_file_path().is_ok());
  57. // Invalid UTF-8
  58. url.path_mut().unwrap()[2] = "ba%80r".to_string();
  59. assert!(url.to_file_path().is_err());
  60. // test windows canonicalized path
  61. let path = PathBuf::from(r"\\?\C:\foo\bar");
  62. assert!(Url::from_file_path(path).is_ok());
  63. }
  64. }
  65. #[test]
  66. fn new_directory_paths() {
  67. use std::path::Path;
  68. if cfg!(unix) {
  69. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  70. assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(()));
  71. let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap();
  72. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  73. assert_eq!(url.path(), Some(&["foo".to_string(), "bar".to_string(),
  74. "".to_string()][..]));
  75. } else {
  76. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  77. assert_eq!(Url::from_directory_path(Path::new(r"..\relative")), Err(()));
  78. assert_eq!(Url::from_directory_path(Path::new(r"\drive-relative")), Err(()));
  79. assert_eq!(Url::from_directory_path(Path::new(r"\\ucn\")), Err(()));
  80. let url = Url::from_directory_path(Path::new(r"C:\foo\bar")).unwrap();
  81. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  82. assert_eq!(url.path(), Some(&["C:".to_string(), "foo".to_string(),
  83. "bar".to_string(), "".to_string()][..]));
  84. }
  85. }
  86. #[test]
  87. fn from_str() {
  88. assert!("http://testing.com/this".parse::<Url>().is_ok());
  89. }
  90. #[test]
  91. fn issue_124() {
  92. let url: Url = "file:a".parse().unwrap();
  93. assert_eq!(url.path().unwrap(), ["a"]);
  94. let url: Url = "file:...".parse().unwrap();
  95. assert_eq!(url.path().unwrap(), ["..."]);
  96. let url: Url = "file:..".parse().unwrap();
  97. assert_eq!(url.path().unwrap(), [""]);
  98. }
  99. #[test]
  100. fn relative_scheme_data_equality() {
  101. use std::hash::{Hash, Hasher, SipHasher};
  102. fn check_eq(a: &Url, b: &Url) {
  103. assert_eq!(a, b);
  104. let mut h1 = SipHasher::new();
  105. a.hash(&mut h1);
  106. let mut h2 = SipHasher::new();
  107. b.hash(&mut h2);
  108. assert_eq!(h1.finish(), h2.finish());
  109. }
  110. fn url(s: &str) -> Url {
  111. let rv = s.parse().unwrap();
  112. check_eq(&rv, &rv);
  113. rv
  114. }
  115. // Doesn't care if default port is given.
  116. let a: Url = url("https://example.com/");
  117. let b: Url = url("https://example.com:443/");
  118. check_eq(&a, &b);
  119. // Different ports
  120. let a: Url = url("http://example.com/");
  121. let b: Url = url("http://example.com:8080/");
  122. assert!(a != b);
  123. // Different scheme
  124. let a: Url = url("http://example.com/");
  125. let b: Url = url("https://example.com/");
  126. assert!(a != b);
  127. // Different host
  128. let a: Url = url("http://foo.com/");
  129. let b: Url = url("http://bar.com/");
  130. assert!(a != b);
  131. // Missing path, automatically substituted. Semantically the same.
  132. let a: Url = url("http://foo.com");
  133. let b: Url = url("http://foo.com/");
  134. check_eq(&a, &b);
  135. }
  136. #[test]
  137. fn host() {
  138. let a = Host::parse("www.mozilla.org").unwrap();
  139. let b = Host::parse("1.35.33.49").unwrap();
  140. let c = Host::parse("[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]").unwrap();
  141. let d = Host::parse("1.35.+33.49").unwrap();
  142. assert_eq!(a, Host::Domain("www.mozilla.org".to_owned()));
  143. assert_eq!(b, Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  144. assert_eq!(c, Host::Ipv6(Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0x08d3,
  145. 0x1319, 0x8a2e, 0x0370, 0x7344)));
  146. assert_eq!(d, Host::Domain("1.35.+33.49".to_owned()));
  147. assert_eq!(Host::parse("[::]").unwrap(), Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)));
  148. assert_eq!(Host::parse("[::1]").unwrap(), Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
  149. assert_eq!(Host::parse("0x1.0X23.0x21.061").unwrap(), Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  150. assert_eq!(Host::parse("0x1232131").unwrap(), Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  151. assert!(Host::parse("42.0x1232131").is_err());
  152. assert_eq!(Host::parse("111").unwrap(), Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111)));
  153. assert_eq!(Host::parse("2..2.3").unwrap(), Host::Domain("2..2.3".to_owned()));
  154. assert!(Host::parse("192.168.0.257").is_err());
  155. }
  156. #[test]
  157. fn test_idna() {
  158. assert!("http://goșu.ro".parse::<Url>().is_ok());
  159. assert_eq!(Url::parse("http://☃.net/").unwrap().domain(), Some("xn--n3h.net"));
  160. }