tests.rs 8.0 KB

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