tests.rs 5.7 KB

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