format.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extern crate url;
  2. use url::Url;
  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 userinfo_formatting() {
  20. // Test data as (username, password, result) tuples.
  21. let data = [
  22. ("", None, ""),
  23. ("", Some(""), ":@"),
  24. ("", Some("password"), ":password@"),
  25. ("username", None, "username@"),
  26. ("username", Some(""), "username:@"),
  27. ("username", Some("password"), "username:password@")
  28. ];
  29. for &(username, password, result) in &data {
  30. assert_eq!(UserInfoFormatter {
  31. username: username,
  32. password: password
  33. }.to_string(), result.to_string());
  34. }
  35. }
  36. #[test]
  37. fn relative_scheme_url_formatting() {
  38. let data = [
  39. ("http://example.com/", "http://example.com/"),
  40. ("http://addslash.com", "http://addslash.com/"),
  41. ("http://@emptyuser.com/", "http://emptyuser.com/"),
  42. ("http://:@emptypass.com/", "http://:@emptypass.com/"),
  43. ("http://user@user.com/", "http://user@user.com/"),
  44. ("http://user:pass@userpass.com/", "http://user:pass@userpass.com/"),
  45. ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"),
  46. ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something")
  47. ];
  48. for &(input, result) in &data {
  49. let url = Url::parse(input).unwrap();
  50. assert_eq!(url.to_string(), result.to_string());
  51. }
  52. }