tests.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2013 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. use std::char;
  9. use std::u32;
  10. use super::{URL, RelativeSchemeData, SchemeRelativeURL, UserInfo, OtherSchemeData};
  11. #[test]
  12. fn test_url_parsing() {
  13. for test in parse_test_data(include_str!("urltestdata.txt")).move_iter() {
  14. let Test {
  15. input: input,
  16. base: base,
  17. scheme: expected_scheme,
  18. username: expected_username,
  19. password: expected_password,
  20. host: expected_host,
  21. port: expected_port,
  22. path: expected_path,
  23. query: expected_query,
  24. fragment: expected_fragment
  25. } = test;
  26. let base = match URL::parse(base, None) {
  27. Ok(base) => base,
  28. Err(message) => fail!("Error parsing base {:?}: {}", base, message)
  29. };
  30. let url = URL::parse(input, Some(&base));
  31. if expected_scheme.is_none() {
  32. assert!(url.is_err(), "Expected a parse error for URL {:?}", input);
  33. continue
  34. }
  35. let URL { scheme, scheme_data, query, fragment } = match url {
  36. Ok(url) => url,
  37. Err(message) => fail!("Error parsing URL {:?}: {}", input, message)
  38. };
  39. assert_eq!(Some(scheme.as_str_ascii().to_owned()), expected_scheme);
  40. match scheme_data {
  41. RelativeSchemeData(SchemeRelativeURL { userinfo, host, port, path }) => {
  42. let (username, password) = match userinfo {
  43. None => (~"", None),
  44. Some(UserInfo { username, password }) => (
  45. username.as_str_ascii().to_owned(),
  46. password.map(|p| p.as_str_ascii().to_owned())),
  47. };
  48. assert_eq!(username, expected_username);
  49. assert_eq!(password, expected_password);
  50. let host = host.serialize();
  51. assert_eq!(host.as_str_ascii().to_owned(), expected_host)
  52. assert_eq!(port.as_str_ascii().to_owned(), expected_port);
  53. assert_eq!(Some("/" + path.map(|p| p.as_str_ascii().to_owned()).connect("/")),
  54. expected_path);
  55. },
  56. OtherSchemeData(scheme_data) => {
  57. assert_eq!(Some(scheme_data.as_str_ascii().to_owned()), expected_path);
  58. assert_eq!(~"", expected_username);
  59. assert_eq!(None, expected_password);
  60. assert_eq!(~"", expected_host);
  61. assert_eq!(~"", expected_port);
  62. },
  63. }
  64. assert_eq!(query.map(|p| "?" + p.as_str_ascii().to_owned()), expected_query);
  65. assert_eq!(fragment.map(|p| "#" + p.as_str_ascii().to_owned()), expected_fragment);
  66. }
  67. }
  68. struct Test {
  69. input: ~str,
  70. base: ~str,
  71. scheme: Option<~str>,
  72. username: ~str,
  73. password: Option<~str>,
  74. host: ~str,
  75. port: ~str,
  76. path: Option<~str>,
  77. query: Option<~str>,
  78. fragment: Option<~str>,
  79. }
  80. fn parse_test_data(input: &str) -> ~[Test] {
  81. let mut tests: ~[Test] = ~[];
  82. for line in input.lines() {
  83. if line == "" || line[0] == ('#' as u8) {
  84. continue
  85. }
  86. let mut pieces = line.split(' ').to_owned_vec();
  87. let input = unescape(pieces.shift());
  88. let mut test = Test {
  89. input: input,
  90. base: if pieces.is_empty() || pieces[0] == "" {
  91. tests[tests.len() - 1].base.to_owned()
  92. } else {
  93. unescape(pieces.shift())
  94. },
  95. scheme: None,
  96. username: ~"",
  97. password: None,
  98. host: ~"",
  99. port: ~"",
  100. path: None,
  101. query: None,
  102. fragment: None,
  103. };
  104. for piece in pieces.move_iter() {
  105. if piece == "" || piece[0] == ('#' as u8) {
  106. continue
  107. }
  108. let colon = piece.find(':').unwrap();
  109. let value = unescape(piece.slice_from(colon + 1));
  110. match piece.slice_to(colon) {
  111. "s" => test.scheme = Some(value),
  112. "u" => test.username = value,
  113. "pass" => test.password = Some(value),
  114. "h" => test.host = value,
  115. "port" => test.port = value,
  116. "p" => test.path = Some(value),
  117. "q" => test.query = Some(value),
  118. "f" => test.fragment = Some(value),
  119. _ => fail!("Invalid token")
  120. }
  121. }
  122. tests.push(test)
  123. }
  124. tests
  125. }
  126. fn unescape(input: &str) -> ~str {
  127. let mut output = ~"";
  128. let mut chars = input.chars();
  129. loop {
  130. match chars.next() {
  131. None => return output,
  132. Some(c) => output.push_char(
  133. if c == '\\' {
  134. match chars.next().unwrap() {
  135. '\\' => '\\',
  136. 'n' => '\n',
  137. 'r' => '\r',
  138. 's' => ' ',
  139. 't' => '\t',
  140. 'f' => '\x0C',
  141. 'u' => {
  142. let mut hex = ~"";
  143. hex.push_char(chars.next().unwrap());
  144. hex.push_char(chars.next().unwrap());
  145. hex.push_char(chars.next().unwrap());
  146. hex.push_char(chars.next().unwrap());
  147. u32::parse_bytes(hex.as_bytes(), 16)
  148. .and_then(char::from_u32).unwrap()
  149. }
  150. _ => fail!("Invalid test data input"),
  151. }
  152. } else {
  153. c
  154. }
  155. )
  156. }
  157. }
  158. }