unit.rs 8.0 KB

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