tests.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  61. }
  62. #[test]
  63. fn new_directory_paths() {
  64. use std::path::Path;
  65. if cfg!(unix) {
  66. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  67. assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(()));
  68. let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap();
  69. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  70. assert_eq!(url.path(), Some(&["foo".to_string(), "bar".to_string(),
  71. "".to_string()][..]));
  72. } else {
  73. assert_eq!(Url::from_directory_path(Path::new("relative")), Err(()));
  74. assert_eq!(Url::from_directory_path(Path::new(r"..\relative")), Err(()));
  75. assert_eq!(Url::from_directory_path(Path::new(r"\drive-relative")), Err(()));
  76. assert_eq!(Url::from_directory_path(Path::new(r"\\ucn\")), Err(()));
  77. let url = Url::from_directory_path(Path::new(r"C:\foo\bar")).unwrap();
  78. assert_eq!(url.host(), Some(&Host::Domain("".to_string())));
  79. assert_eq!(url.path(), Some(&["C:".to_string(), "foo".to_string(),
  80. "bar".to_string(), "".to_string()][..]));
  81. }
  82. }
  83. #[test]
  84. fn from_str() {
  85. assert!("http://testing.com/this".parse::<Url>().is_ok());
  86. }
  87. #[test]
  88. fn issue_124() {
  89. let url: Url = "file:a".parse().unwrap();
  90. assert_eq!(url.path().unwrap(), ["a"]);
  91. let url: Url = "file:...".parse().unwrap();
  92. assert_eq!(url.path().unwrap(), ["..."]);
  93. let url: Url = "file:..".parse().unwrap();
  94. assert_eq!(url.path().unwrap(), [""]);
  95. }
  96. #[test]
  97. fn relative_scheme_data_equality() {
  98. use std::hash::{Hash, Hasher, SipHasher};
  99. fn check_eq(a: &Url, b: &Url) {
  100. assert_eq!(a, b);
  101. let mut h1 = SipHasher::new();
  102. a.hash(&mut h1);
  103. let mut h2 = SipHasher::new();
  104. b.hash(&mut h2);
  105. assert_eq!(h1.finish(), h2.finish());
  106. }
  107. fn url(s: &str) -> Url {
  108. let rv = s.parse().unwrap();
  109. check_eq(&rv, &rv);
  110. rv
  111. }
  112. // Doesn't care if default port is given.
  113. let a: Url = url("https://example.com/");
  114. let b: Url = url("https://example.com:443/");
  115. check_eq(&a, &b);
  116. // Different ports
  117. let a: Url = url("http://example.com/");
  118. let b: Url = url("http://example.com:8080/");
  119. assert!(a != b);
  120. // Different scheme
  121. let a: Url = url("http://example.com/");
  122. let b: Url = url("https://example.com/");
  123. assert!(a != b);
  124. // Different host
  125. let a: Url = url("http://foo.com/");
  126. let b: Url = url("http://bar.com/");
  127. assert!(a != b);
  128. // Missing path, automatically substituted. Semantically the same.
  129. let a: Url = url("http://foo.com");
  130. let b: Url = url("http://foo.com/");
  131. check_eq(&a, &b);
  132. }
  133. #[test]
  134. fn host() {
  135. let a = Host::parse("www.mozilla.org").unwrap();
  136. let b = Host::parse("1.35.33.49").unwrap();
  137. let c = Host::parse("[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]").unwrap();
  138. let d = Host::parse("1.35.+33.49").unwrap();
  139. assert_eq!(a, Host::Domain("www.mozilla.org".to_owned()));
  140. assert_eq!(b, Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  141. assert_eq!(c, Host::Ipv6(Ipv6Addr::new(0x2001, 0x0db8, 0x85a3, 0x08d3,
  142. 0x1319, 0x8a2e, 0x0370, 0x7344)));
  143. assert_eq!(d, Host::Domain("1.35.+33.49".to_owned()));
  144. assert_eq!(Host::parse("[::]").unwrap(), Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)));
  145. assert_eq!(Host::parse("[::1]").unwrap(), Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)));
  146. assert_eq!(Host::parse("0x1.0X23.0x21.061").unwrap(), Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  147. assert_eq!(Host::parse("0x1232131").unwrap(), Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
  148. assert!(Host::parse("42.0x1232131").is_err());
  149. assert_eq!(Host::parse("111").unwrap(), Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111)));
  150. assert_eq!(Host::parse("2..2.3").unwrap(), Host::Domain("2..2.3".to_owned()));
  151. assert!(Host::parse("192.168.0.257").is_err());
  152. }
  153. #[test]
  154. fn test_idna() {
  155. assert!("http://goșu.ro".parse::<Url>().is_ok());
  156. assert_eq!(Url::parse("http://☃.net/").unwrap().domain(), Some("xn--n3h.net"));
  157. }