tests.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 std::path::{Path, PathBuf};
  11. use url::{Host, Url};
  12. macro_rules! assert_from_file_path {
  13. ($path: expr) => { assert_from_file_path!($path, $path) };
  14. ($path: expr, $url_path: expr) => {{
  15. let url = Url::from_file_path(Path::new($path)).unwrap();
  16. assert_eq!(url.host(), None);
  17. assert_eq!(url.path(), $url_path);
  18. assert_eq!(url.to_file_path(), Ok(PathBuf::from($path)));
  19. }};
  20. }
  21. #[test]
  22. fn new_file_paths() {
  23. if cfg!(unix) {
  24. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  25. assert_eq!(Url::from_file_path(Path::new("../relative")), Err(()));
  26. }
  27. if cfg!(windows) {
  28. assert_eq!(Url::from_file_path(Path::new("relative")), Err(()));
  29. assert_eq!(Url::from_file_path(Path::new(r"..\relative")), Err(()));
  30. assert_eq!(Url::from_file_path(Path::new(r"\drive-relative")), Err(()));
  31. assert_eq!(Url::from_file_path(Path::new(r"\\ucn\")), Err(()));
  32. }
  33. if cfg!(unix) {
  34. assert_from_file_path!("/foo/bar");
  35. assert_from_file_path!("/foo/ba\0r", "/foo/ba%00r");
  36. assert_from_file_path!("/foo/ba%00r", "/foo/ba%2500r");
  37. }
  38. }
  39. #[test]
  40. #[cfg(unix)]
  41. fn new_path_bad_utf8() {
  42. use std::ffi::OsStr;
  43. use std::os::unix::prelude::*;
  44. let url = Url::from_file_path(Path::new(OsStr::from_bytes(b"/foo/ba\x80r"))).unwrap();
  45. let os_str = OsStr::from_bytes(b"/foo/ba\x80r");
  46. assert_eq!(url.to_file_path(), Ok(PathBuf::from(os_str)));
  47. }
  48. #[test]
  49. fn new_path_windows_fun() {
  50. if cfg!(windows) {
  51. assert_from_file_path!(r"C:\foo\bar", "/C:/foo/bar");
  52. assert_from_file_path!("C:\\foo\\ba\0r", "/C:/foo/ba%00r");
  53. // Invalid UTF-8
  54. assert!(Url::parse("file:///C:/foo/ba%80r").unwrap().to_file_path().is_err());
  55. // test windows canonicalized path
  56. let path = PathBuf::from(r"\\?\C:\foo\bar");
  57. assert!(Url::from_file_path(path).is_ok());
  58. }
  59. }
  60. #[test]
  61. fn new_directory_paths() {
  62. if cfg!(unix) {
  63. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  64. assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(()));
  65. let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap();
  66. assert_eq!(url.host(), None);
  67. assert_eq!(url.path(), "/foo/bar/");
  68. }
  69. if cfg!(windows) {
  70. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  71. assert_eq!(Url::from_directory_path(Path::new(r"..\relative")), Err(()));
  72. assert_eq!(Url::from_directory_path(Path::new(r"\drive-relative")), Err(()));
  73. assert_eq!(Url::from_directory_path(Path::new(r"\\ucn\")), Err(()));
  74. let url = Url::from_directory_path(Path::new(r"C:\foo\bar")).unwrap();
  75. assert_eq!(url.host(), None);
  76. assert_eq!(url.path(), "/C:/foo/bar/");
  77. }
  78. }
  79. #[test]
  80. fn from_str() {
  81. assert!("http://testing.com/this".parse::<Url>().is_ok());
  82. }
  83. #[test]
  84. fn issue_124() {
  85. let url: Url = "file:a".parse().unwrap();
  86. assert_eq!(url.path(), "/a");
  87. let url: Url = "file:...".parse().unwrap();
  88. assert_eq!(url.path(), "/...");
  89. let url: Url = "file:..".parse().unwrap();
  90. assert_eq!(url.path(), "/");
  91. }
  92. #[test]
  93. fn test_equality() {
  94. use std::hash::{Hash, Hasher, SipHasher};
  95. fn check_eq(a: &Url, b: &Url) {
  96. assert_eq!(a, b);
  97. let mut h1 = SipHasher::new();
  98. a.hash(&mut h1);
  99. let mut h2 = SipHasher::new();
  100. b.hash(&mut h2);
  101. assert_eq!(h1.finish(), h2.finish());
  102. }
  103. fn url(s: &str) -> Url {
  104. let rv = s.parse().unwrap();
  105. check_eq(&rv, &rv);
  106. rv
  107. }
  108. // Doesn't care if default port is given.
  109. let a: Url = url("https://example.com/");
  110. let b: Url = url("https://example.com:443/");
  111. check_eq(&a, &b);
  112. // Different ports
  113. let a: Url = url("http://example.com/");
  114. let b: Url = url("http://example.com:8080/");
  115. assert!(a != b, "{:?} != {:?}", a, b);
  116. // Different scheme
  117. let a: Url = url("http://example.com/");
  118. let b: Url = url("https://example.com/");
  119. assert!(a != b);
  120. // Different host
  121. let a: Url = url("http://foo.com/");
  122. let b: Url = url("http://bar.com/");
  123. assert!(a != b);
  124. // Missing path, automatically substituted. Semantically the same.
  125. let a: Url = url("http://foo.com");
  126. let b: Url = url("http://foo.com/");
  127. check_eq(&a, &b);
  128. }
  129. #[test]
  130. fn host() {
  131. fn assert_host(input: &str, host: Host<&str>) {
  132. assert_eq!(Url::parse(input).unwrap().host(), Some(host));
  133. }
  134. assert_host("http://www.mozilla.org", Host::Domain("www.mozilla.org"));
  135. assert_host("http://1.35.33.49", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  136. assert_host("http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]", Host::Ipv6(Ipv6Addr::new(
  137. 0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344)));
  138. assert_host("http://1.35.+33.49", Host::Domain("1.35.+33.49"));
  139. assert_host("http://[::]", Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)));
  140. assert_host("http://[::1]", Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
  141. assert_host("http://0x1.0X23.0x21.061", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  142. assert_host("http://0x1232131", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  143. assert_host("http://111", Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111)));
  144. assert_host("http://2..2.3", Host::Domain("2..2.3"));
  145. assert!(Url::parse("http://42.0x1232131").is_err());
  146. assert!(Url::parse("http://192.168.0.257").is_err());
  147. }
  148. #[test]
  149. fn host_serialization() {
  150. // libstd’s `Display for Ipv6Addr` serializes 0:0:0:0:0:0:_:_ and 0:0:0:0:0:ffff:_:_
  151. // using IPv4-like syntax, as suggested in https://tools.ietf.org/html/rfc5952#section-4
  152. // but https://url.spec.whatwg.org/#concept-ipv6-serializer specifies not to.
  153. // Not [::0.0.0.2] / [::ffff:0.0.0.2]
  154. assert_eq!(Url::parse("http://[0::2]").unwrap().host_str(), Some("[::2]"));
  155. assert_eq!(Url::parse("http://[0::ffff:0:2]").unwrap().host_str(), Some("[::ffff:0:2]"));
  156. }
  157. #[test]
  158. fn test_idna() {
  159. assert!("http://goșu.ro".parse::<Url>().is_ok());
  160. assert_eq!(Url::parse("http://☃.net/").unwrap().host(), Some(Host::Domain("xn--n3h.net")));
  161. }
  162. #[test]
  163. fn test_serialization() {
  164. let data = [
  165. ("http://example.com/", "http://example.com/"),
  166. ("http://addslash.com", "http://addslash.com/"),
  167. ("http://@emptyuser.com/", "http://emptyuser.com/"),
  168. ("http://:@emptypass.com/", "http://:@emptypass.com/"),
  169. ("http://user@user.com/", "http://user@user.com/"),
  170. ("http://user:pass@userpass.com/", "http://user:pass@userpass.com/"),
  171. ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"),
  172. ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something")
  173. ];
  174. for &(input, result) in &data {
  175. let url = Url::parse(input).unwrap();
  176. assert_eq!(url.as_str(), result);
  177. }
  178. }