format.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. extern crate url;
  2. use url::{Url, Host};
  3. use url::format::{PathFormatter, UserInfoFormatter};
  4. #[test]
  5. fn path_formatting() {
  6. let data = [
  7. (vec![], "/"),
  8. (vec![""], "/"),
  9. (vec!["test", "path"], "/test/path"),
  10. (vec!["test", "path", ""], "/test/path/")
  11. ];
  12. for &(ref path, result) in &data {
  13. assert_eq!(PathFormatter {
  14. path: path
  15. }.to_string(), result.to_string());
  16. }
  17. }
  18. #[test]
  19. fn host() {
  20. // libstd’s `Display for Ipv6Addr` serializes 0:0:0:0:0:0:_:_ and 0:0:0:0:0:ffff:_:_
  21. // using IPv4-like syntax, as suggested in https://tools.ietf.org/html/rfc5952#section-4
  22. // but https://url.spec.whatwg.org/#concept-ipv6-serializer specifies not to.
  23. // Not [::0.0.0.2] / [::ffff:0.0.0.2]
  24. assert_eq!(Host::parse("[0::2]").unwrap().to_string(), "[::2]");
  25. assert_eq!(Host::parse("[0::ffff:0:2]").unwrap().to_string(), "[::ffff:0:2]");
  26. }
  27. #[test]
  28. fn userinfo_formatting() {
  29. // Test data as (username, password, result) tuples.
  30. let data = [
  31. ("", None, ""),
  32. ("", Some(""), ":@"),
  33. ("", Some("password"), ":password@"),
  34. ("username", None, "username@"),
  35. ("username", Some(""), "username:@"),
  36. ("username", Some("password"), "username:password@")
  37. ];
  38. for &(username, password, result) in &data {
  39. assert_eq!(UserInfoFormatter {
  40. username: username,
  41. password: password
  42. }.to_string(), result.to_string());
  43. }
  44. }
  45. #[test]
  46. fn relative_scheme_url_formatting() {
  47. let data = [
  48. ("http://example.com/", "http://example.com/"),
  49. ("http://addslash.com", "http://addslash.com/"),
  50. ("http://@emptyuser.com/", "http://emptyuser.com/"),
  51. ("http://:@emptypass.com/", "http://:@emptypass.com/"),
  52. ("http://user@user.com/", "http://user@user.com/"),
  53. ("http://user:pass@userpass.com/", "http://user:pass@userpass.com/"),
  54. ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"),
  55. ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something")
  56. ];
  57. for &(input, result) in &data {
  58. let url = Url::parse(input).unwrap();
  59. assert_eq!(url.to_string(), result.to_string());
  60. }
  61. }