| 1 | #[cfg(feature = "net")] |
| 2 | use core::net::{Ipv4Addr, Ipv6Addr}; |
| 3 | |
| 4 | use fluent_uri::{component::Host, pct_enc::EStr, ParseErrorKind, Uri, UriRef}; |
| 5 | |
| 6 | #[test] |
| 7 | fn parse_absolute() { |
| 8 | let r = UriRef::parse("file:///etc/hosts").unwrap(); |
| 9 | assert_eq!(r.as_str(), "file:///etc/hosts"); |
| 10 | assert_eq!(r.scheme().unwrap().as_str(), "file"); |
| 11 | let a = r.authority().unwrap(); |
| 12 | assert_eq!(a.as_str(), ""); |
| 13 | assert_eq!(a.userinfo(), None); |
| 14 | assert_eq!(a.host(), ""); |
| 15 | assert!(matches!(a.host_parsed(), Host::RegName(n) if n.is_empty())); |
| 16 | assert_eq!(a.port(), None); |
| 17 | assert_eq!(r.path(), "/etc/hosts"); |
| 18 | assert_eq!(r.query(), None); |
| 19 | assert_eq!(r.fragment(), None); |
| 20 | |
| 21 | let r = UriRef::parse("ftp://ftp.is.co.za/rfc/rfc1808.txt").unwrap(); |
| 22 | assert_eq!(r.scheme().unwrap().as_str(), "ftp"); |
| 23 | let a = r.authority().unwrap(); |
| 24 | assert_eq!(a.as_str(), "ftp.is.co.za"); |
| 25 | assert_eq!(a.userinfo(), None); |
| 26 | assert_eq!(a.host(), "ftp.is.co.za"); |
| 27 | assert!(matches!(a.host_parsed(), Host::RegName(name) if name == "ftp.is.co.za")); |
| 28 | assert_eq!(a.port(), None); |
| 29 | assert_eq!(r.path(), "/rfc/rfc1808.txt"); |
| 30 | assert_eq!(r.query(), None); |
| 31 | assert_eq!(r.fragment(), None); |
| 32 | |
| 33 | let r = UriRef::parse("http://www.ietf.org/rfc/rfc2396.txt").unwrap(); |
| 34 | assert_eq!(r.scheme().unwrap().as_str(), "http"); |
| 35 | let a = r.authority().unwrap(); |
| 36 | assert_eq!(a.as_str(), "www.ietf.org"); |
| 37 | assert_eq!(a.userinfo(), None); |
| 38 | assert_eq!(a.host(), "www.ietf.org"); |
| 39 | assert!(matches!(a.host_parsed(), Host::RegName(name) if name == "www.ietf.org")); |
| 40 | assert_eq!(a.port(), None); |
| 41 | assert_eq!(r.path(), "/rfc/rfc2396.txt"); |
| 42 | assert_eq!(r.query(), None); |
| 43 | assert_eq!(r.fragment(), None); |
| 44 | |
| 45 | let r = UriRef::parse("ldap://[2001:db8::7]/c=GB?objectClass?one").unwrap(); |
| 46 | assert_eq!(r.scheme().unwrap().as_str(), "ldap"); |
| 47 | let a = r.authority().unwrap(); |
| 48 | assert_eq!(a.as_str(), "[2001:db8::7]"); |
| 49 | assert_eq!(a.userinfo(), None); |
| 50 | assert_eq!(a.host(), "[2001:db8::7]"); |
| 51 | #[cfg(feature = "net")] |
| 52 | assert!(matches!( |
| 53 | a.host_parsed(), |
| 54 | Host::Ipv6(addr) if addr == Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0x7) |
| 55 | )); |
| 56 | assert_eq!(a.port(), None); |
| 57 | assert_eq!(r.path(), "/c=GB"); |
| 58 | assert_eq!(r.query(), Some(EStr::new_or_panic("objectClass?one"))); |
| 59 | assert_eq!(r.fragment(), None); |
| 60 | |
| 61 | let r = UriRef::parse("mailto:[email protected]").unwrap(); |
| 62 | assert_eq!(r.scheme().unwrap().as_str(), "mailto"); |
| 63 | assert!(r.authority().is_none()); |
| 64 | assert_eq!(r.path(), "[email protected]"); |
| 65 | assert_eq!(r.query(), None); |
| 66 | assert_eq!(r.fragment(), None); |
| 67 | |
| 68 | let r = UriRef::parse("news:comp.infosystems.www.servers.unix").unwrap(); |
| 69 | assert_eq!(r.scheme().unwrap().as_str(), "news"); |
| 70 | assert!(r.authority().is_none()); |
| 71 | assert_eq!(r.path(), "comp.infosystems.www.servers.unix"); |
| 72 | assert_eq!(r.query(), None); |
| 73 | assert_eq!(r.fragment(), None); |
| 74 | |
| 75 | let r = UriRef::parse("tel:+1-816-555-1212").unwrap(); |
| 76 | assert_eq!(r.scheme().unwrap().as_str(), "tel"); |
| 77 | assert!(r.authority().is_none()); |
| 78 | assert_eq!(r.path(), "+1-816-555-1212"); |
| 79 | assert_eq!(r.query(), None); |
| 80 | assert_eq!(r.fragment(), None); |
| 81 | |
| 82 | let r = UriRef::parse("telnet://192.0.2.16:80/").unwrap(); |
| 83 | assert_eq!(r.scheme().unwrap().as_str(), "telnet"); |
| 84 | let a = r.authority().unwrap(); |
| 85 | assert_eq!(a.as_str(), "192.0.2.16:80"); |
| 86 | assert_eq!(a.userinfo(), None); |
| 87 | assert_eq!(a.host(), "192.0.2.16"); |
| 88 | #[cfg(feature = "net")] |
| 89 | assert!(matches!(a.host_parsed(), Host::Ipv4(addr) if addr == Ipv4Addr::new(192, 0, 2, 16))); |
| 90 | assert_eq!(a.port(), Some(EStr::new_or_panic("80"))); |
| 91 | assert_eq!(r.path(), "/"); |
| 92 | assert_eq!(r.query(), None); |
| 93 | assert_eq!(r.fragment(), None); |
| 94 | |
| 95 | let r = UriRef::parse("urn:oasis:names:specification:docbook:dtd:xml:4.1.2").unwrap(); |
| 96 | assert_eq!(r.scheme().unwrap().as_str(), "urn"); |
| 97 | assert!(r.authority().is_none()); |
| 98 | assert_eq!(r.path(), "oasis:names:specification:docbook:dtd:xml:4.1.2"); |
| 99 | assert_eq!(r.query(), None); |
| 100 | assert_eq!(r.fragment(), None); |
| 101 | |
| 102 | let r = UriRef::parse("foo://example.com:8042/over/there?name=ferret#nose").unwrap(); |
| 103 | assert_eq!(r.scheme().unwrap().as_str(), "foo"); |
| 104 | let a = r.authority().unwrap(); |
| 105 | assert_eq!(a.as_str(), "example.com:8042"); |
| 106 | assert_eq!(a.userinfo(), None); |
| 107 | assert_eq!(a.host(), "example.com"); |
| 108 | assert!(matches!(a.host_parsed(), Host::RegName(name) if name == "example.com")); |
| 109 | assert_eq!(a.port(), Some(EStr::new_or_panic("8042"))); |
| 110 | assert_eq!(r.path(), "/over/there"); |
| 111 | assert_eq!(r.query(), Some(EStr::new_or_panic("name=ferret"))); |
| 112 | assert_eq!(r.fragment(), Some(EStr::new_or_panic("nose"))); |
| 113 | |
| 114 | let r = |
| 115 | UriRef::parse("ftp://cnn.example.com&[email protected]/top_story.htm").unwrap(); |
| 116 | assert_eq!(r.scheme().unwrap().as_str(), "ftp"); |
| 117 | let a = r.authority().unwrap(); |
| 118 | assert_eq!(a.as_str(), "cnn.example.com&[email protected]"); |
| 119 | assert_eq!( |
| 120 | a.userinfo(), |
| 121 | Some(EStr::new_or_panic("cnn.example.com&story=breaking_news")) |
| 122 | ); |
| 123 | assert_eq!(a.host(), "10.0.0.1"); |
| 124 | #[cfg(feature = "net")] |
| 125 | assert!(matches!(a.host_parsed(), Host::Ipv4(addr) if addr == Ipv4Addr::new(10, 0, 0, 1))); |
| 126 | assert_eq!(a.port(), None); |
| 127 | assert_eq!(r.path(), "/top_story.htm"); |
| 128 | assert_eq!(r.query(), None); |
| 129 | assert_eq!(r.fragment(), None); |
| 130 | |
| 131 | let r = UriRef::parse("http://[vFe.foo.bar]").unwrap(); |
| 132 | assert_eq!(r.scheme().unwrap().as_str(), "http"); |
| 133 | let a = r.authority().unwrap(); |
| 134 | assert_eq!(a.as_str(), "[vFe.foo.bar]"); |
| 135 | assert_eq!(a.userinfo(), None); |
| 136 | assert_eq!(a.host(), "[vFe.foo.bar]"); |
| 137 | assert!(matches!(a.host_parsed(), Host::IpvFuture { .. })); |
| 138 | assert_eq!(a.port(), None); |
| 139 | assert_eq!(r.path(), ""); |
| 140 | assert_eq!(r.query(), None); |
| 141 | assert_eq!(r.fragment(), None); |
| 142 | |
| 143 | let r = UriRef::parse("http://127.0.0.1:/").unwrap(); |
| 144 | assert_eq!(r.scheme().unwrap().as_str(), "http"); |
| 145 | let a = r.authority().unwrap(); |
| 146 | assert_eq!(a.as_str(), "127.0.0.1:"); |
| 147 | assert_eq!(a.userinfo(), None); |
| 148 | assert_eq!(a.host(), "127.0.0.1"); |
| 149 | #[cfg(feature = "net")] |
| 150 | assert!(matches!(a.host_parsed(), Host::Ipv4(addr) if addr == Ipv4Addr::new(127, 0, 0, 1))); |
| 151 | assert_eq!(a.port(), Some(EStr::EMPTY)); |
| 152 | assert_eq!(r.path(), "/"); |
| 153 | assert_eq!(r.query(), None); |
| 154 | assert_eq!(r.fragment(), None); |
| 155 | |
| 156 | let r = UriRef::parse("http://127.0.0.1:8080/").unwrap(); |
| 157 | assert_eq!(r.scheme().unwrap().as_str(), "http"); |
| 158 | let a = r.authority().unwrap(); |
| 159 | assert_eq!(a.as_str(), "127.0.0.1:8080"); |
| 160 | assert_eq!(a.userinfo(), None); |
| 161 | assert_eq!(a.host(), "127.0.0.1"); |
| 162 | #[cfg(feature = "net")] |
| 163 | assert!(matches!(a.host_parsed(), Host::Ipv4(addr) if addr == Ipv4Addr::new(127, 0, 0, 1))); |
| 164 | assert_eq!(a.port(), Some(EStr::new_or_panic("8080"))); |
| 165 | assert_eq!(r.path(), "/"); |
| 166 | assert_eq!(r.query(), None); |
| 167 | assert_eq!(r.fragment(), None); |
| 168 | |
| 169 | let r = UriRef::parse("http://127.0.0.1:80808/").unwrap(); |
| 170 | assert_eq!(r.scheme().unwrap().as_str(), "http"); |
| 171 | let a = r.authority().unwrap(); |
| 172 | assert_eq!(a.as_str(), "127.0.0.1:80808"); |
| 173 | assert_eq!(a.userinfo(), None); |
| 174 | assert_eq!(a.host(), "127.0.0.1"); |
| 175 | #[cfg(feature = "net")] |
| 176 | assert!(matches!(a.host_parsed(), Host::Ipv4(addr) if addr == Ipv4Addr::new(127, 0, 0, 1))); |
| 177 | assert_eq!(a.port(), Some(EStr::new_or_panic("80808"))); |
| 178 | assert_eq!(r.path(), "/"); |
| 179 | assert_eq!(r.query(), None); |
| 180 | assert_eq!(r.fragment(), None); |
| 181 | } |
| 182 | |
| 183 | #[test] |
| 184 | fn parse_relative() { |
| 185 | let r = UriRef::parse("").unwrap(); |
| 186 | assert!(r.scheme().is_none()); |
| 187 | assert!(r.authority().is_none()); |
| 188 | assert_eq!(r.path(), ""); |
| 189 | assert_eq!(r.query(), None); |
| 190 | assert_eq!(r.fragment(), None); |
| 191 | |
| 192 | let r = UriRef::parse("foo.txt").unwrap(); |
| 193 | assert!(r.scheme().is_none()); |
| 194 | assert!(r.authority().is_none()); |
| 195 | assert_eq!(r.path(), "foo.txt"); |
| 196 | assert_eq!(r.query(), None); |
| 197 | assert_eq!(r.fragment(), None); |
| 198 | |
| 199 | let r = UriRef::parse(".").unwrap(); |
| 200 | assert!(r.scheme().is_none()); |
| 201 | assert!(r.authority().is_none()); |
| 202 | assert_eq!(r.path(), "."); |
| 203 | assert_eq!(r.query(), None); |
| 204 | assert_eq!(r.fragment(), None); |
| 205 | |
| 206 | let r = UriRef::parse("./this:that").unwrap(); |
| 207 | assert!(r.scheme().is_none()); |
| 208 | assert!(r.authority().is_none()); |
| 209 | assert_eq!(r.path(), "./this:that"); |
| 210 | assert_eq!(r.query(), None); |
| 211 | assert_eq!(r.fragment(), None); |
| 212 | |
| 213 | let r = UriRef::parse("//example.com").unwrap(); |
| 214 | assert!(r.scheme().is_none()); |
| 215 | let a = r.authority().unwrap(); |
| 216 | assert_eq!(a.as_str(), "example.com"); |
| 217 | assert_eq!(a.userinfo(), None); |
| 218 | assert_eq!(a.host(), "example.com"); |
| 219 | assert!(matches!(a.host_parsed(), Host::RegName(name) if name == "example.com")); |
| 220 | assert_eq!(a.port(), None); |
| 221 | assert_eq!(r.path(), ""); |
| 222 | assert_eq!(r.query(), None); |
| 223 | assert_eq!(r.fragment(), None); |
| 224 | |
| 225 | let r = UriRef::parse("?query").unwrap(); |
| 226 | assert!(r.scheme().is_none()); |
| 227 | assert!(r.authority().is_none()); |
| 228 | assert_eq!(r.path(), ""); |
| 229 | assert_eq!(r.query(), Some(EStr::new_or_panic("query"))); |
| 230 | assert_eq!(r.fragment(), None); |
| 231 | |
| 232 | let r = UriRef::parse("#fragment").unwrap(); |
| 233 | assert!(r.scheme().is_none()); |
| 234 | assert!(r.authority().is_none()); |
| 235 | assert_eq!(r.path(), ""); |
| 236 | assert_eq!(r.query(), None); |
| 237 | assert_eq!(r.fragment(), Some(EStr::new_or_panic("fragment"))); |
| 238 | } |
| 239 | |
| 240 | use ParseErrorKind::*; |
| 241 | |
| 242 | #[test] |
| 243 | fn parse_error_uri() { |
| 244 | #[track_caller] |
| 245 | fn fail(input: &str, index: usize, kind: ParseErrorKind) { |
| 246 | let e = Uri::parse(input).unwrap_err(); |
| 247 | assert_eq!(e.index(), index); |
| 248 | assert_eq!(e.kind(), kind); |
| 249 | } |
| 250 | |
| 251 | // No scheme |
| 252 | fail("foo", 3, UnexpectedChar); |
| 253 | |
| 254 | // Empty scheme |
| 255 | fail(":hello", 0, UnexpectedChar); |
| 256 | |
| 257 | // Scheme starts with non-letter |
| 258 | fail("3ttp://a.com", 0, UnexpectedChar); |
| 259 | |
| 260 | // Unexpected char in scheme |
| 261 | fail("exam=ple:foo", 4, UnexpectedChar); |
| 262 | fail("(:", 0, UnexpectedChar); |
| 263 | |
| 264 | // Percent-encoded scheme |
| 265 | fail("a%20:foo", 1, UnexpectedChar); |
| 266 | } |
| 267 | |
| 268 | #[track_caller] |
| 269 | fn fail(input: &str, index: usize, kind: ParseErrorKind) { |
| 270 | let e = UriRef::parse(input).unwrap_err(); |
| 271 | assert_eq!(e.index(), index); |
| 272 | assert_eq!(e.kind(), kind); |
| 273 | } |
| 274 | |
| 275 | #[test] |
| 276 | fn parse_error_uri_ref() { |
| 277 | // Empty scheme |
| 278 | fail(":hello", 0, UnexpectedChar); |
| 279 | |
| 280 | // Scheme starts with non-letter |
| 281 | fail("3ttp://a.com", 0, UnexpectedChar); |
| 282 | |
| 283 | // After rewriting the parser, the following two cases are interpreted as |
| 284 | // containing colon in the first path segment of a relative reference. |
| 285 | |
| 286 | // Unexpected char in scheme |
| 287 | fail("exam=ple:foo", 8, UnexpectedChar); |
| 288 | fail("(:", 1, UnexpectedChar); |
| 289 | |
| 290 | // Percent-encoded scheme |
| 291 | fail("a%20:foo", 4, UnexpectedChar); |
| 292 | |
| 293 | // Unexpected char in path |
| 294 | fail("foo\\bar", 3, UnexpectedChar); |
| 295 | |
| 296 | // Non-hexadecimal percent-encoded octet |
| 297 | fail("foo%xxd", 3, InvalidPctEncodedOctet); |
| 298 | |
| 299 | // Incomplete percent-encoded octet |
| 300 | fail("text%a", 4, InvalidPctEncodedOctet); |
| 301 | |
| 302 | // A single percent |
| 303 | fail("%", 0, InvalidPctEncodedOctet); |
| 304 | |
| 305 | // Non-decimal port |
| 306 | fail("http://example.com:80ab", 21, UnexpectedChar); |
| 307 | fail("http://[email protected]:80ab", 26, UnexpectedChar); |
| 308 | |
| 309 | // Multiple colons in authority |
| 310 | fail("http://user:pass:example.com/", 16, UnexpectedChar); |
| 311 | |
| 312 | // Unclosed bracket |
| 313 | fail("https://[::1/", 12, UnexpectedChar); |
| 314 | |
| 315 | // Not port after IP literal |
| 316 | fail("https://[::1]wrong", 13, UnexpectedChar); |
| 317 | |
| 318 | // IP literal too short |
| 319 | fail("http://[:]", 8, InvalidIpv6Addr); |
| 320 | fail("http://[]", 8, UnexpectedChar); |
| 321 | |
| 322 | // Non-hexadecimal version in IPvFuture |
| 323 | fail("http://[vG.addr]", 9, UnexpectedChar); |
| 324 | |
| 325 | // Empty version in IPvFuture |
| 326 | fail("http://[v.addr]", 9, UnexpectedChar); |
| 327 | |
| 328 | // Empty address in IPvFuture |
| 329 | fail("ftp://[vF.]", 10, UnexpectedChar); |
| 330 | |
| 331 | // Percent-encoded address in IPvFuture |
| 332 | fail("ftp://[vF.%20]", 10, UnexpectedChar); |
| 333 | |
| 334 | // With zone identifier |
| 335 | fail("ftp://[fe80::abcd%eth0]", 17, UnexpectedChar); |
| 336 | |
| 337 | // Invalid IPv6 address |
| 338 | fail("example://[44:55::66::77]", 11, InvalidIpv6Addr); |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn strict_ip_addr() { |
| 343 | let r = UriRef::parse("//127.0.0.001").unwrap(); |
| 344 | let a = r.authority().unwrap(); |
| 345 | assert!(matches!(a.host_parsed(), Host::RegName(_))); |
| 346 | |
| 347 | let r = UriRef::parse("//127.1").unwrap(); |
| 348 | let a = r.authority().unwrap(); |
| 349 | assert!(matches!(a.host_parsed(), Host::RegName(_))); |
| 350 | |
| 351 | let r = UriRef::parse("//127.00.00.1").unwrap(); |
| 352 | let a = r.authority().unwrap(); |
| 353 | assert!(matches!(a.host_parsed(), Host::RegName(_))); |
| 354 | |
| 355 | assert!(UriRef::parse("//[::1.1.1.1]").is_ok()); |
| 356 | assert!(UriRef::parse("//[::ffff:1.1.1.1]").is_ok()); |
| 357 | assert!(UriRef::parse("//[0000:0000:0000:0000:0000:0000:255.255.255.255]").is_ok()); |
| 358 | |
| 359 | fail("//[::01.1.1.1]", 3, InvalidIpv6Addr); |
| 360 | fail("//[::00.1.1.1]", 3, InvalidIpv6Addr); |
| 361 | } |