| 1 |
// Copyright 2013-2014 The rust-url developers. |
| 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 |
|
| 9 |
//! Unit tests |
| 10 |
#![no_std] |
| 11 |
|
| 12 |
#[cfg(feature = "std")] |
| 13 |
extern crate std; |
| 14 |
|
| 15 |
#[macro_use] |
| 16 |
extern crate alloc; |
| 17 |
|
| 18 |
use alloc::borrow::Cow; |
| 19 |
use alloc::borrow::ToOwned; |
| 20 |
use alloc::string::{String, ToString}; |
| 21 |
use alloc::vec::Vec; |
| 22 |
use core::cell::{Cell, RefCell}; |
| 23 |
#[cfg(feature = "std")] |
| 24 |
use std::dbg; |
| 25 |
use url::{form_urlencoded, Host, Origin, Url}; |
| 26 |
|
| 27 |
/// `std` version of `net` |
| 28 |
#[cfg(feature = "std")] |
| 29 |
pub(crate) mod net { |
| 30 |
pub use std::net::*; |
| 31 |
} |
| 32 |
/// `no_std` nightly version of `net` |
| 33 |
#[cfg(not(feature = "std"))] |
| 34 |
pub(crate) mod net { |
| 35 |
pub use core::net::*; |
| 36 |
} |
| 37 |
|
| 38 |
use crate::net::{Ipv4Addr, Ipv6Addr}; |
| 39 |
|
| 40 |
#[cfg(feature = "std")] |
| 41 |
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] |
| 42 |
use std::path::{Path, PathBuf}; |
| 43 |
|
| 44 |
// https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/usage.html |
| 45 |
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 46 |
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; |
| 47 |
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 48 |
wasm_bindgen_test_configure!(run_in_browser); |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn size() { |
| 52 |
use core::mem::size_of; |
| 53 |
assert_eq!(size_of::<Url>(), size_of::<Option<Url>>()); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_relative() { |
| 58 |
let base: Url = "sc://%C3%B1".parse().unwrap(); |
| 59 |
let url = base.join("/resources/testharness.js").unwrap(); |
| 60 |
assert_eq!(url.as_str(), "sc://%C3%B1/resources/testharness.js"); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_relative_empty() { |
| 65 |
let base: Url = "sc://%C3%B1".parse().unwrap(); |
| 66 |
let url = base.join("").unwrap(); |
| 67 |
assert_eq!(url.as_str(), "sc://%C3%B1"); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_strip_trailing_spaces_from_opaque_path() { |
| 72 |
let mut url: Url = "data:space ?query".parse().unwrap(); |
| 73 |
url.set_query(None); |
| 74 |
assert_eq!(url.as_str(), "data:space"); |
| 75 |
|
| 76 |
let mut url: Url = "data:space #hash".parse().unwrap(); |
| 77 |
url.set_fragment(None); |
| 78 |
assert_eq!(url.as_str(), "data:space"); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_set_empty_host() { |
| 83 |
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap(); |
| 84 |
base.set_username("").unwrap(); |
| 85 |
assert_eq!(base.as_str(), "moz://:bar@servo/baz"); |
| 86 |
base.set_host(None).unwrap(); |
| 87 |
assert_eq!(base.as_str(), "moz:/baz"); |
| 88 |
base.set_host(Some("servo")).unwrap(); |
| 89 |
assert_eq!(base.as_str(), "moz://servo/baz"); |
| 90 |
|
| 91 |
let mut base: Url = "file://server/share/foo/bar".parse().unwrap(); |
| 92 |
base.set_host(None).unwrap(); |
| 93 |
assert_eq!(base.as_str(), "file:///share/foo/bar"); |
| 94 |
|
| 95 |
let mut base: Url = "file://server/share/foo/bar".parse().unwrap(); |
| 96 |
base.set_host(Some("foo")).unwrap(); |
| 97 |
assert_eq!(base.as_str(), "file://foo/share/foo/bar"); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_set_empty_username_and_password() { |
| 102 |
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap(); |
| 103 |
base.set_username("").unwrap(); |
| 104 |
assert_eq!(base.as_str(), "moz://:bar@servo/baz"); |
| 105 |
|
| 106 |
base.set_password(Some("")).unwrap(); |
| 107 |
assert_eq!(base.as_str(), "moz://servo/baz"); |
| 108 |
|
| 109 |
base.set_password(None).unwrap(); |
| 110 |
assert_eq!(base.as_str(), "moz://servo/baz"); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_set_empty_password() { |
| 115 |
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap(); |
| 116 |
|
| 117 |
base.set_password(Some("")).unwrap(); |
| 118 |
assert_eq!(base.as_str(), "moz://foo@servo/baz"); |
| 119 |
|
| 120 |
base.set_password(None).unwrap(); |
| 121 |
assert_eq!(base.as_str(), "moz://foo@servo/baz"); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_set_empty_hostname() { |
| 126 |
use url::quirks; |
| 127 |
let mut base: Url = "moz://foo@servo/baz".parse().unwrap(); |
| 128 |
assert!( |
| 129 |
quirks::set_hostname(&mut base, "").is_err(), |
| 130 |
"setting an empty hostname to a url with a username should fail" |
| 131 |
); |
| 132 |
base = "moz://:pass@servo/baz".parse().unwrap(); |
| 133 |
assert!( |
| 134 |
quirks::set_hostname(&mut base, "").is_err(), |
| 135 |
"setting an empty hostname to a url with a password should fail" |
| 136 |
); |
| 137 |
base = "moz://servo/baz".parse().unwrap(); |
| 138 |
quirks::set_hostname(&mut base, "").unwrap(); |
| 139 |
assert_eq!(base.as_str(), "moz:///baz"); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn test_set_empty_query() { |
| 144 |
let mut base: Url = "moz://example.com/path?query".parse().unwrap(); |
| 145 |
|
| 146 |
base.set_query(Some("")); |
| 147 |
assert_eq!(base.as_str(), "moz://example.com/path?"); |
| 148 |
|
| 149 |
base.set_query(None); |
| 150 |
assert_eq!(base.as_str(), "moz://example.com/path"); |
| 151 |
} |
| 152 |
|
| 153 |
#[cfg(feature = "std")] |
| 154 |
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] |
| 155 |
macro_rules! assert_from_file_path { |
| 156 |
($path: expr) => { |
| 157 |
assert_from_file_path!($path, $path) |
| 158 |
}; |
| 159 |
($path: expr, $url_path: expr) => {{ |
| 160 |
let url = Url::from_file_path(Path::new($path)).unwrap(); |
| 161 |
assert_eq!(url.host(), None); |
| 162 |
assert_eq!(url.path(), $url_path); |
| 163 |
assert_eq!(url.to_file_path(), Ok(PathBuf::from($path))); |
| 164 |
}}; |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
#[cfg(feature = "std")] |
| 169 |
#[cfg(any(unix, windows))] |
| 170 |
fn new_file_paths() { |
| 171 |
if cfg!(unix) { |
| 172 |
assert_eq!(Url::from_file_path(Path::new("relative")), Err(())); |
| 173 |
assert_eq!(Url::from_file_path(Path::new("../relative")), Err(())); |
| 174 |
} |
| 175 |
if cfg!(windows) { |
| 176 |
assert_eq!(Url::from_file_path(Path::new("relative")), Err(())); |
| 177 |
assert_eq!(Url::from_file_path(Path::new(r"..\relative")), Err(())); |
| 178 |
assert_eq!(Url::from_file_path(Path::new(r"\drive-relative")), Err(())); |
| 179 |
assert_eq!(Url::from_file_path(Path::new(r"\\ucn\")), Err(())); |
| 180 |
} |
| 181 |
|
| 182 |
if cfg!(unix) { |
| 183 |
assert_from_file_path!("/foo/bar"); |
| 184 |
assert_from_file_path!("/foo/ba\0r", "/foo/ba%00r"); |
| 185 |
assert_from_file_path!("/foo/ba%00r", "/foo/ba%2500r"); |
| 186 |
assert_from_file_path!("/foo/ba\\r", "/foo/ba%5Cr"); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
#[cfg(all(feature = "std", unix))] |
| 192 |
fn new_path_bad_utf8() { |
| 193 |
use std::ffi::OsStr; |
| 194 |
use std::os::unix::prelude::*; |
| 195 |
|
| 196 |
let url = Url::from_file_path(Path::new(OsStr::from_bytes(b"/foo/ba\x80r"))).unwrap(); |
| 197 |
let os_str = OsStr::from_bytes(b"/foo/ba\x80r"); |
| 198 |
assert_eq!(url.to_file_path(), Ok(PathBuf::from(os_str))); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
#[cfg(all(feature = "std", windows))] |
| 203 |
fn new_path_windows_fun() { |
| 204 |
assert_from_file_path!(r"C:\foo\bar", "/C:/foo/bar"); |
| 205 |
assert_from_file_path!("C:\\foo\\ba\0r", "/C:/foo/ba%00r"); |
| 206 |
|
| 207 |
// Invalid UTF-8 |
| 208 |
assert!(Url::parse("file:///C:/foo/ba%80r") |
| 209 |
.unwrap() |
| 210 |
.to_file_path() |
| 211 |
.is_err()); |
| 212 |
|
| 213 |
// test windows canonicalized path |
| 214 |
let path = PathBuf::from(r"\\?\C:\foo\bar"); |
| 215 |
assert!(Url::from_file_path(path).is_ok()); |
| 216 |
|
| 217 |
// Percent-encoded drive letter |
| 218 |
let url = Url::parse("file:///C%3A/foo/bar").unwrap(); |
| 219 |
assert_eq!(url.to_file_path(), Ok(PathBuf::from(r"C:\foo\bar"))); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
#[cfg(all( |
| 224 |
feature = "std", |
| 225 |
any(unix, windows, target_os = "redox", target_os = "wasi") |
| 226 |
))] |
| 227 |
#[cfg(any(unix, windows))] |
| 228 |
fn new_directory_paths() { |
| 229 |
if cfg!(unix) { |
| 230 |
assert_eq!(Url::from_directory_path(Path::new("relative")), Err(())); |
| 231 |
assert_eq!(Url::from_directory_path(Path::new("../relative")), Err(())); |
| 232 |
|
| 233 |
let url = Url::from_directory_path(Path::new("/foo/bar")).unwrap(); |
| 234 |
assert_eq!(url.host(), None); |
| 235 |
assert_eq!(url.path(), "/foo/bar/"); |
| 236 |
} |
| 237 |
if cfg!(windows) { |
| 238 |
assert_eq!(Url::from_directory_path(Path::new("relative")), Err(())); |
| 239 |
assert_eq!(Url::from_directory_path(Path::new(r"..\relative")), Err(())); |
| 240 |
assert_eq!( |
| 241 |
Url::from_directory_path(Path::new(r"\drive-relative")), |
| 242 |
Err(()) |
| 243 |
); |
| 244 |
assert_eq!(Url::from_directory_path(Path::new(r"\\ucn\")), Err(())); |
| 245 |
|
| 246 |
let url = Url::from_directory_path(Path::new(r"C:\foo\bar")).unwrap(); |
| 247 |
assert_eq!(url.host(), None); |
| 248 |
assert_eq!(url.path(), "/C:/foo/bar/"); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn path_backslash_fun() { |
| 254 |
let mut special_url = "http://foobar.com".parse::<Url>().unwrap(); |
| 255 |
special_url.path_segments_mut().unwrap().push("foo\\bar"); |
| 256 |
assert_eq!(special_url.as_str(), "http://foobar.com/foo%5Cbar"); |
| 257 |
|
| 258 |
let mut nonspecial_url = "thing://foobar.com".parse::<Url>().unwrap(); |
| 259 |
nonspecial_url.path_segments_mut().unwrap().push("foo\\bar"); |
| 260 |
assert_eq!(nonspecial_url.as_str(), "thing://foobar.com/foo\\bar"); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn from_str() { |
| 265 |
assert!("http://testing.com/this".parse::<Url>().is_ok()); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn parse_with_params() { |
| 270 |
let url = Url::parse_with_params( |
| 271 |
"http://testing.com/this?dont=clobberme", |
| 272 |
&[("lang", "rust")], |
| 273 |
) |
| 274 |
.unwrap(); |
| 275 |
|
| 276 |
assert_eq!( |
| 277 |
url.as_str(), |
| 278 |
"http://testing.com/this?dont=clobberme&lang=rust" |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn issue_124() { |
| 284 |
let url: Url = "file:a".parse().unwrap(); |
| 285 |
assert_eq!(url.path(), "/a"); |
| 286 |
let url: Url = "file:...".parse().unwrap(); |
| 287 |
assert_eq!(url.path(), "/..."); |
| 288 |
let url: Url = "file:..".parse().unwrap(); |
| 289 |
assert_eq!(url.path(), "/"); |
| 290 |
} |
| 291 |
|
| 292 |
#[test] |
| 293 |
#[cfg(feature = "std")] |
| 294 |
fn test_equality() { |
| 295 |
use std::collections::hash_map::DefaultHasher; |
| 296 |
use std::hash::{Hash, Hasher}; |
| 297 |
|
| 298 |
fn check_eq(a: &Url, b: &Url) { |
| 299 |
assert_eq!(a, b); |
| 300 |
|
| 301 |
let mut h1 = DefaultHasher::new(); |
| 302 |
a.hash(&mut h1); |
| 303 |
let mut h2 = DefaultHasher::new(); |
| 304 |
b.hash(&mut h2); |
| 305 |
assert_eq!(h1.finish(), h2.finish()); |
| 306 |
} |
| 307 |
|
| 308 |
fn url(s: &str) -> Url { |
| 309 |
let rv = s.parse().unwrap(); |
| 310 |
check_eq(&rv, &rv); |
| 311 |
rv |
| 312 |
} |
| 313 |
|
| 314 |
// Doesn't care if default port is given. |
| 315 |
let a: Url = url("https://example.com/"); |
| 316 |
let b: Url = url("https://example.com:443/"); |
| 317 |
check_eq(&a, &b); |
| 318 |
|
| 319 |
// Different ports |
| 320 |
let a: Url = url("http://example.com/"); |
| 321 |
let b: Url = url("http://example.com:8080/"); |
| 322 |
assert!(a != b, "{:?} != {:?}", a, b); |
| 323 |
|
| 324 |
// Different scheme |
| 325 |
let a: Url = url("http://example.com/"); |
| 326 |
let b: Url = url("https://example.com/"); |
| 327 |
assert_ne!(a, b); |
| 328 |
|
| 329 |
// Different host |
| 330 |
let a: Url = url("http://foo.com/"); |
| 331 |
let b: Url = url("http://bar.com/"); |
| 332 |
assert_ne!(a, b); |
| 333 |
|
| 334 |
// Missing path, automatically substituted. Semantically the same. |
| 335 |
let a: Url = url("http://foo.com"); |
| 336 |
let b: Url = url("http://foo.com/"); |
| 337 |
check_eq(&a, &b); |
| 338 |
} |
| 339 |
|
| 340 |
#[test] |
| 341 |
fn host() { |
| 342 |
fn assert_host(input: &str, host: Host<&str>) { |
| 343 |
assert_eq!(Url::parse(input).unwrap().host(), Some(host)); |
| 344 |
} |
| 345 |
assert_host("http://www.mozilla.org", Host::Domain("www.mozilla.org")); |
| 346 |
assert_host( |
| 347 |
"http://1.35.33.49", |
| 348 |
Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), |
| 349 |
); |
| 350 |
assert_host( |
| 351 |
"http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]", |
| 352 |
Host::Ipv6(Ipv6Addr::new( |
| 353 |
0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344, |
| 354 |
)), |
| 355 |
); |
| 356 |
assert_host( |
| 357 |
"http://[::]", |
| 358 |
Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), |
| 359 |
); |
| 360 |
assert_host( |
| 361 |
"http://[::1]", |
| 362 |
Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), |
| 363 |
); |
| 364 |
assert_host( |
| 365 |
"http://0x1.0X23.0x21.061", |
| 366 |
Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)), |
| 367 |
); |
| 368 |
assert_host("http://0x1232131", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49))); |
| 369 |
assert_host("http://111", Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111))); |
| 370 |
assert!(Url::parse("http://1.35.+33.49").is_err()); |
| 371 |
assert!(Url::parse("http://2..2.3").is_err()); |
| 372 |
assert!(Url::parse("http://42.0x1232131").is_err()); |
| 373 |
assert!(Url::parse("http://192.168.0.257").is_err()); |
| 374 |
|
| 375 |
assert_eq!(Host::Domain("foo"), Host::Domain("foo").to_owned()); |
| 376 |
assert_ne!(Host::Domain("foo"), Host::Domain("bar").to_owned()); |
| 377 |
} |
| 378 |
|
| 379 |
#[test] |
| 380 |
fn host_serialization() { |
| 381 |
// libstd’s `Display for Ipv6Addr` serializes 0:0:0:0:0:0:_:_ and 0:0:0:0:0:ffff:_:_ |
| 382 |
// using IPv4-like syntax, as suggested in https://tools.ietf.org/html/rfc5952#section-4 |
| 383 |
// but https://url.spec.whatwg.org/#concept-ipv6-serializer specifies not to. |
| 384 |
|
| 385 |
// Not [::0.0.0.2] / [::ffff:0.0.0.2] |
| 386 |
assert_eq!( |
| 387 |
Url::parse("http://[0::2]").unwrap().host_str(), |
| 388 |
Some("[::2]") |
| 389 |
); |
| 390 |
assert_eq!( |
| 391 |
Url::parse("http://[0::ffff:0:2]").unwrap().host_str(), |
| 392 |
Some("[::ffff:0:2]") |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
#[test] |
| 397 |
fn test_idna() { |
| 398 |
assert!("http://goșu.ro".parse::<Url>().is_ok()); |
| 399 |
assert_eq!( |
| 400 |
Url::parse("http://☃.net/").unwrap().host(), |
| 401 |
Some(Host::Domain("xn--n3h.net")) |
| 402 |
); |
| 403 |
assert!("https://r2---sn-huoa-cvhl.googlevideo.com/crossdomain.xml" |
| 404 |
.parse::<Url>() |
| 405 |
.is_ok()); |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn test_serialization() { |
| 410 |
let data = [ |
| 411 |
("http://example.com/", "http://example.com/"), |
| 412 |
("http://addslash.com", "http://addslash.com/"), |
| 413 |
("http://@emptyuser.com/", "http://emptyuser.com/"), |
| 414 |
("http://:@emptypass.com/", "http://emptypass.com/"), |
| 415 |
("http://[email protected]/", "http://[email protected]/"), |
| 416 |
( |
| 417 |
"http://user:[email protected]/", |
| 418 |
"http://user:[email protected]/", |
| 419 |
), |
| 420 |
( |
| 421 |
"http://slashquery.com/path/?q=something", |
| 422 |
"http://slashquery.com/path/?q=something", |
| 423 |
), |
| 424 |
( |
| 425 |
"http://noslashquery.com/path?q=something", |
| 426 |
"http://noslashquery.com/path?q=something", |
| 427 |
), |
| 428 |
]; |
| 429 |
for &(input, result) in &data { |
| 430 |
let url = Url::parse(input).unwrap(); |
| 431 |
assert_eq!(url.as_str(), result); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn test_form_urlencoded() { |
| 437 |
let pairs: &[(Cow<'_, str>, Cow<'_, str>)] = &[ |
| 438 |
("foo".into(), "é&".into()), |
| 439 |
("bar".into(), "".into()), |
| 440 |
("foo".into(), "#".into()), |
| 441 |
]; |
| 442 |
let encoded = form_urlencoded::Serializer::new(String::new()) |
| 443 |
.extend_pairs(pairs) |
| 444 |
.finish(); |
| 445 |
assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23"); |
| 446 |
assert_eq!( |
| 447 |
form_urlencoded::parse(encoded.as_bytes()).collect::<Vec<_>>(), |
| 448 |
pairs.to_vec() |
| 449 |
); |
| 450 |
} |
| 451 |
|
| 452 |
#[test] |
| 453 |
fn test_form_serialize() { |
| 454 |
let encoded = form_urlencoded::Serializer::new(String::new()) |
| 455 |
.append_pair("foo", "é&") |
| 456 |
.append_pair("bar", "") |
| 457 |
.append_pair("foo", "#") |
| 458 |
.append_key_only("json") |
| 459 |
.finish(); |
| 460 |
assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23&json"); |
| 461 |
} |
| 462 |
|
| 463 |
#[test] |
| 464 |
fn form_urlencoded_encoding_override() { |
| 465 |
let encoded = form_urlencoded::Serializer::new(String::new()) |
| 466 |
.encoding_override(Some(&|s| s.as_bytes().to_ascii_uppercase().into())) |
| 467 |
.append_pair("foo", "bar") |
| 468 |
.append_key_only("xml") |
| 469 |
.finish(); |
| 470 |
assert_eq!(encoded, "FOO=BAR&XML"); |
| 471 |
} |
| 472 |
|
| 473 |
#[test] |
| 474 |
/// https://github.com/servo/rust-url/issues/61 |
| 475 |
fn issue_61() { |
| 476 |
let mut url = Url::parse("http://mozilla.org").unwrap(); |
| 477 |
url.set_scheme("https").unwrap(); |
| 478 |
assert_eq!(url.port(), None); |
| 479 |
assert_eq!(url.port_or_known_default(), Some(443)); |
| 480 |
url.check_invariants().unwrap(); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
#[cfg(feature = "std")] |
| 485 |
#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] |
| 486 |
#[cfg(not(windows))] |
| 487 |
/// https://github.com/servo/rust-url/issues/197 |
| 488 |
fn issue_197() { |
| 489 |
let mut url = Url::from_file_path("/").expect("Failed to parse path"); |
| 490 |
url.check_invariants().unwrap(); |
| 491 |
assert_eq!( |
| 492 |
url, |
| 493 |
Url::parse("file:///").expect("Failed to parse path + protocol") |
| 494 |
); |
| 495 |
url.path_segments_mut() |
| 496 |
.expect("path_segments_mut") |
| 497 |
.pop_if_empty(); |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn issue_241() { |
| 502 |
Url::parse("mailto:").unwrap().cannot_be_a_base(); |
| 503 |
} |
| 504 |
|
| 505 |
#[test] |
| 506 |
/// https://github.com/servo/rust-url/issues/222 |
| 507 |
fn append_trailing_slash() { |
| 508 |
let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap(); |
| 509 |
url.check_invariants().unwrap(); |
| 510 |
url.path_segments_mut().unwrap().push(""); |
| 511 |
url.check_invariants().unwrap(); |
| 512 |
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar/?a=b"); |
| 513 |
} |
| 514 |
|
| 515 |
#[test] |
| 516 |
/// https://github.com/servo/rust-url/issues/227 |
| 517 |
fn extend_query_pairs_then_mutate() { |
| 518 |
let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap(); |
| 519 |
url.query_pairs_mut() |
| 520 |
.extend_pairs(vec![("auth", "my-token")]); |
| 521 |
url.check_invariants().unwrap(); |
| 522 |
assert_eq!( |
| 523 |
url.to_string(), |
| 524 |
"http://localhost:6767/foo/bar?auth=my-token" |
| 525 |
); |
| 526 |
url.path_segments_mut().unwrap().push("some_other_path"); |
| 527 |
url.check_invariants().unwrap(); |
| 528 |
assert_eq!( |
| 529 |
url.to_string(), |
| 530 |
"http://localhost:6767/foo/bar/some_other_path?auth=my-token" |
| 531 |
); |
| 532 |
} |
| 533 |
|
| 534 |
#[test] |
| 535 |
/// https://github.com/servo/rust-url/issues/222 |
| 536 |
fn append_empty_segment_then_mutate() { |
| 537 |
let mut url: Url = "http://localhost:6767/foo/bar?a=b".parse().unwrap(); |
| 538 |
url.check_invariants().unwrap(); |
| 539 |
url.path_segments_mut().unwrap().push("").pop(); |
| 540 |
url.check_invariants().unwrap(); |
| 541 |
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b"); |
| 542 |
} |
| 543 |
|
| 544 |
#[test] |
| 545 |
/// https://github.com/servo/rust-url/issues/243 |
| 546 |
fn test_set_host() { |
| 547 |
let mut url = Url::parse("https://example.net/hello").unwrap(); |
| 548 |
url.set_host(Some("foo.com")).unwrap(); |
| 549 |
assert_eq!(url.as_str(), "https://foo.com/hello"); |
| 550 |
assert!(url.set_host(None).is_err()); |
| 551 |
assert_eq!(url.as_str(), "https://foo.com/hello"); |
| 552 |
assert!(url.set_host(Some("")).is_err()); |
| 553 |
assert_eq!(url.as_str(), "https://foo.com/hello"); |
| 554 |
|
| 555 |
let mut url = Url::parse("foobar://example.net/hello").unwrap(); |
| 556 |
url.set_host(None).unwrap(); |
| 557 |
assert_eq!(url.as_str(), "foobar:/hello"); |
| 558 |
|
| 559 |
let mut url = Url::parse("foo://ș").unwrap(); |
| 560 |
assert_eq!(url.as_str(), "foo://%C8%99"); |
| 561 |
url.set_host(Some("goșu.ro")).unwrap(); |
| 562 |
assert_eq!(url.as_str(), "foo://go%C8%99u.ro"); |
| 563 |
} |
| 564 |
|
| 565 |
#[test] |
| 566 |
// https://github.com/servo/rust-url/issues/166 |
| 567 |
fn test_leading_dots() { |
| 568 |
assert_eq!( |
| 569 |
Host::parse(".org").unwrap(), |
| 570 |
Host::Domain(".org".to_owned()) |
| 571 |
); |
| 572 |
assert_eq!(Url::parse("file://./foo").unwrap().domain(), Some(".")); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
#[cfg(feature = "std")] |
| 577 |
/// https://github.com/servo/rust-url/issues/302 |
| 578 |
fn test_origin_hash() { |
| 579 |
use std::collections::hash_map::DefaultHasher; |
| 580 |
use std::hash::{Hash, Hasher}; |
| 581 |
|
| 582 |
fn hash<T: Hash>(value: &T) -> u64 { |
| 583 |
let mut hasher = DefaultHasher::new(); |
| 584 |
value.hash(&mut hasher); |
| 585 |
hasher.finish() |
| 586 |
} |
| 587 |
|
| 588 |
let origin = &Url::parse("http://example.net/").unwrap().origin(); |
| 589 |
|
| 590 |
let origins_to_compare = [ |
| 591 |
Url::parse("http://example.net:80/").unwrap().origin(), |
| 592 |
Url::parse("http://example.net:81/").unwrap().origin(), |
| 593 |
Url::parse("http://example.net").unwrap().origin(), |
| 594 |
Url::parse("http://example.net/hello").unwrap().origin(), |
| 595 |
Url::parse("https://example.net").unwrap().origin(), |
| 596 |
Url::parse("ftp://example.net").unwrap().origin(), |
| 597 |
Url::parse("file://example.net").unwrap().origin(), |
| 598 |
Url::parse("http://[email protected]/").unwrap().origin(), |
| 599 |
Url::parse("http://user:[email protected]/") |
| 600 |
.unwrap() |
| 601 |
.origin(), |
| 602 |
]; |
| 603 |
|
| 604 |
for origin_to_compare in &origins_to_compare { |
| 605 |
if origin == origin_to_compare { |
| 606 |
assert_eq!(hash(origin), hash(origin_to_compare)); |
| 607 |
} else { |
| 608 |
assert_ne!(hash(origin), hash(origin_to_compare)); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
let opaque_origin = Url::parse("file://example.net").unwrap().origin(); |
| 613 |
let same_opaque_origin = Url::parse("file://example.net").unwrap().origin(); |
| 614 |
let other_opaque_origin = Url::parse("file://other").unwrap().origin(); |
| 615 |
|
| 616 |
assert_ne!(hash(&opaque_origin), hash(&same_opaque_origin)); |
| 617 |
assert_ne!(hash(&opaque_origin), hash(&other_opaque_origin)); |
| 618 |
} |
| 619 |
|
| 620 |
#[test] |
| 621 |
fn test_origin_blob_equality() { |
| 622 |
let origin = &Url::parse("http://example.net/").unwrap().origin(); |
| 623 |
let blob_origin = &Url::parse("blob:http://example.net/").unwrap().origin(); |
| 624 |
|
| 625 |
assert_eq!(origin, blob_origin); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn test_origin_opaque() { |
| 630 |
assert!(!Origin::new_opaque().is_tuple()); |
| 631 |
assert!(!&Url::parse("blob:malformed//").unwrap().origin().is_tuple()) |
| 632 |
} |
| 633 |
|
| 634 |
#[test] |
| 635 |
fn test_origin_unicode_serialization() { |
| 636 |
let data = [ |
| 637 |
("http://😅.com", "http://😅.com"), |
| 638 |
("ftp://😅:🙂@🙂.com", "ftp://🙂.com"), |
| 639 |
("https://user@😅.com", "https://😅.com"), |
| 640 |
("http://😅.🙂:40", "http://😅.🙂:40"), |
| 641 |
]; |
| 642 |
for &(unicode_url, expected_serialization) in &data { |
| 643 |
let origin = Url::parse(unicode_url).unwrap().origin(); |
| 644 |
assert_eq!(origin.unicode_serialization(), *expected_serialization); |
| 645 |
} |
| 646 |
|
| 647 |
let ascii_origins = [ |
| 648 |
Url::parse("http://example.net/").unwrap().origin(), |
| 649 |
Url::parse("http://example.net:80/").unwrap().origin(), |
| 650 |
Url::parse("http://example.net:81/").unwrap().origin(), |
| 651 |
Url::parse("http://example.net").unwrap().origin(), |
| 652 |
Url::parse("http://example.net/hello").unwrap().origin(), |
| 653 |
Url::parse("https://example.net").unwrap().origin(), |
| 654 |
Url::parse("ftp://example.net").unwrap().origin(), |
| 655 |
Url::parse("file://example.net").unwrap().origin(), |
| 656 |
Url::parse("http://[email protected]/").unwrap().origin(), |
| 657 |
Url::parse("http://user:[email protected]/") |
| 658 |
.unwrap() |
| 659 |
.origin(), |
| 660 |
Url::parse("http://127.0.0.1").unwrap().origin(), |
| 661 |
]; |
| 662 |
for ascii_origin in &ascii_origins { |
| 663 |
assert_eq!( |
| 664 |
ascii_origin.ascii_serialization(), |
| 665 |
ascii_origin.unicode_serialization() |
| 666 |
); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
#[cfg(feature = "std")] |
| 672 |
#[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))] |
| 673 |
fn test_socket_addrs() { |
| 674 |
use std::net::ToSocketAddrs; |
| 675 |
|
| 676 |
let data = [ |
| 677 |
("https://127.0.0.1/", "127.0.0.1", 443), |
| 678 |
("https://127.0.0.1:9742/", "127.0.0.1", 9742), |
| 679 |
("custom-protocol://127.0.0.1:9742/", "127.0.0.1", 9742), |
| 680 |
("custom-protocol://127.0.0.1/", "127.0.0.1", 9743), |
| 681 |
("https://[::1]/", "::1", 443), |
| 682 |
("https://[::1]:9742/", "::1", 9742), |
| 683 |
("custom-protocol://[::1]:9742/", "::1", 9742), |
| 684 |
("custom-protocol://[::1]/", "::1", 9743), |
| 685 |
("https://localhost/", "localhost", 443), |
| 686 |
("https://localhost:9742/", "localhost", 9742), |
| 687 |
("custom-protocol://localhost:9742/", "localhost", 9742), |
| 688 |
("custom-protocol://localhost/", "localhost", 9743), |
| 689 |
]; |
| 690 |
|
| 691 |
for (url_string, host, port) in &data { |
| 692 |
let url = url::Url::parse(url_string).unwrap(); |
| 693 |
let addrs = url |
| 694 |
.socket_addrs(|| match url.scheme() { |
| 695 |
"custom-protocol" => Some(9743), |
| 696 |
_ => None, |
| 697 |
}) |
| 698 |
.unwrap(); |
| 699 |
assert_eq!( |
| 700 |
Some(addrs[0]), |
| 701 |
(*host, *port).to_socket_addrs().unwrap().next() |
| 702 |
); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
#[test] |
| 707 |
fn test_no_base_url() { |
| 708 |
let mut no_base_url = Url::parse("mailto:[email protected]").unwrap(); |
| 709 |
|
| 710 |
assert!(no_base_url.cannot_be_a_base()); |
| 711 |
assert!(no_base_url.path_segments().is_none()); |
| 712 |
assert!(no_base_url.path_segments_mut().is_err()); |
| 713 |
assert!(no_base_url.set_host(Some("foo")).is_err()); |
| 714 |
assert!(no_base_url |
| 715 |
.set_ip_host("127.0.0.1".parse().unwrap()) |
| 716 |
.is_err()); |
| 717 |
|
| 718 |
no_base_url.set_path("/foo"); |
| 719 |
assert_eq!(no_base_url.path(), "%2Ffoo"); |
| 720 |
} |
| 721 |
|
| 722 |
#[test] |
| 723 |
fn test_domain() { |
| 724 |
let url = Url::parse("https://127.0.0.1/").unwrap(); |
| 725 |
assert_eq!(url.domain(), None); |
| 726 |
|
| 727 |
let url = Url::parse("mailto:[email protected]").unwrap(); |
| 728 |
assert_eq!(url.domain(), None); |
| 729 |
|
| 730 |
let url = Url::parse("https://example.com/").unwrap(); |
| 731 |
assert_eq!(url.domain(), Some("example.com")); |
| 732 |
} |
| 733 |
|
| 734 |
#[test] |
| 735 |
fn test_query() { |
| 736 |
let url = Url::parse("https://example.com/products?page=2#fragment").unwrap(); |
| 737 |
assert_eq!(url.query(), Some("page=2")); |
| 738 |
assert_eq!( |
| 739 |
url.query_pairs().next(), |
| 740 |
Some((Cow::Borrowed("page"), Cow::Borrowed("2"))) |
| 741 |
); |
| 742 |
|
| 743 |
let url = Url::parse("https://example.com/products").unwrap(); |
| 744 |
assert!(url.query().is_none()); |
| 745 |
assert_eq!(url.query_pairs().count(), 0); |
| 746 |
|
| 747 |
let url = Url::parse("https://example.com/?country=español").unwrap(); |
| 748 |
assert_eq!(url.query(), Some("country=espa%C3%B1ol")); |
| 749 |
assert_eq!( |
| 750 |
url.query_pairs().next(), |
| 751 |
Some((Cow::Borrowed("country"), Cow::Borrowed("español"))) |
| 752 |
); |
| 753 |
|
| 754 |
let url = Url::parse("https://example.com/products?page=2&sort=desc").unwrap(); |
| 755 |
assert_eq!(url.query(), Some("page=2&sort=desc")); |
| 756 |
let mut pairs = url.query_pairs(); |
| 757 |
assert_eq!(pairs.count(), 2); |
| 758 |
assert_eq!( |
| 759 |
pairs.next(), |
| 760 |
Some((Cow::Borrowed("page"), Cow::Borrowed("2"))) |
| 761 |
); |
| 762 |
assert_eq!( |
| 763 |
pairs.next(), |
| 764 |
Some((Cow::Borrowed("sort"), Cow::Borrowed("desc"))) |
| 765 |
); |
| 766 |
} |
| 767 |
|
| 768 |
#[test] |
| 769 |
fn test_fragment() { |
| 770 |
let url = Url::parse("https://example.com/#fragment").unwrap(); |
| 771 |
assert_eq!(url.fragment(), Some("fragment")); |
| 772 |
|
| 773 |
let url = Url::parse("https://example.com/").unwrap(); |
| 774 |
assert_eq!(url.fragment(), None); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn test_set_ip_host() { |
| 779 |
let mut url = Url::parse("http://example.com").unwrap(); |
| 780 |
|
| 781 |
url.set_ip_host("127.0.0.1".parse().unwrap()).unwrap(); |
| 782 |
assert_eq!(url.host_str(), Some("127.0.0.1")); |
| 783 |
|
| 784 |
url.set_ip_host("::1".parse().unwrap()).unwrap(); |
| 785 |
assert_eq!(url.host_str(), Some("[::1]")); |
| 786 |
} |
| 787 |
|
| 788 |
#[test] |
| 789 |
fn test_set_href() { |
| 790 |
use url::quirks::set_href; |
| 791 |
|
| 792 |
let mut url = Url::parse("https://existing.url").unwrap(); |
| 793 |
|
| 794 |
assert!(set_href(&mut url, "mal//formed").is_err()); |
| 795 |
|
| 796 |
assert!(set_href( |
| 797 |
&mut url, |
| 798 |
"https://user:[email protected]:9742/path/file.ext?key=val&key2=val2#fragment" |
| 799 |
) |
| 800 |
.is_ok()); |
| 801 |
assert_eq!( |
| 802 |
url, |
| 803 |
Url::parse("https://user:[email protected]:9742/path/file.ext?key=val&key2=val2#fragment") |
| 804 |
.unwrap() |
| 805 |
); |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn test_domain_encoding_quirks() { |
| 810 |
use url::quirks::{domain_to_ascii, domain_to_unicode}; |
| 811 |
|
| 812 |
let data = [ |
| 813 |
("http://example.com", "", ""), |
| 814 |
("😅.🙂", "xn--j28h.xn--938h", "😅.🙂"), |
| 815 |
("example.com", "example.com", "example.com"), |
| 816 |
("mailto:[email protected]", "", ""), |
| 817 |
]; |
| 818 |
|
| 819 |
for url in &data { |
| 820 |
assert_eq!(domain_to_ascii(url.0), url.1); |
| 821 |
assert_eq!(domain_to_unicode(url.0), url.2); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
#[cfg(feature = "expose_internals")] |
| 826 |
#[test] |
| 827 |
fn test_expose_internals() { |
| 828 |
use url::quirks::internal_components; |
| 829 |
use url::quirks::InternalComponents; |
| 830 |
|
| 831 |
let url = Url::parse("https://example.com/path/file.ext?key=val&key2=val2#fragment").unwrap(); |
| 832 |
let InternalComponents { |
| 833 |
scheme_end, |
| 834 |
username_end, |
| 835 |
host_start, |
| 836 |
host_end, |
| 837 |
port, |
| 838 |
path_start, |
| 839 |
query_start, |
| 840 |
fragment_start, |
| 841 |
} = internal_components(&url); |
| 842 |
|
| 843 |
assert_eq!(scheme_end, 5); |
| 844 |
assert_eq!(username_end, 8); |
| 845 |
assert_eq!(host_start, 8); |
| 846 |
assert_eq!(host_end, 19); |
| 847 |
assert_eq!(port, None); |
| 848 |
assert_eq!(path_start, 19); |
| 849 |
assert_eq!(query_start, Some(33)); |
| 850 |
assert_eq!(fragment_start, Some(51)); |
| 851 |
} |
| 852 |
|
| 853 |
#[test] |
| 854 |
#[cfg(feature = "std")] |
| 855 |
#[cfg(windows)] |
| 856 |
fn test_windows_unc_path() { |
| 857 |
let url = Url::from_file_path(Path::new(r"\\host\share\path\file.txt")).unwrap(); |
| 858 |
assert_eq!(url.as_str(), "file://host/share/path/file.txt"); |
| 859 |
|
| 860 |
let url = Url::from_file_path(Path::new(r"\\höst\share\path\file.txt")).unwrap(); |
| 861 |
assert_eq!(url.as_str(), "file://xn--hst-sna/share/path/file.txt"); |
| 862 |
|
| 863 |
let url = Url::from_file_path(Path::new(r"\\192.168.0.1\share\path\file.txt")).unwrap(); |
| 864 |
assert_eq!(url.host(), Some(Host::Ipv4(Ipv4Addr::new(192, 168, 0, 1)))); |
| 865 |
|
| 866 |
let path = url.to_file_path().unwrap(); |
| 867 |
assert_eq!(path.to_str(), Some(r"\\192.168.0.1\share\path\file.txt")); |
| 868 |
|
| 869 |
// Another way to write these: |
| 870 |
let url = Url::from_file_path(Path::new(r"\\?\UNC\host\share\path\file.txt")).unwrap(); |
| 871 |
assert_eq!(url.as_str(), "file://host/share/path/file.txt"); |
| 872 |
|
| 873 |
// Paths starting with "\\.\" (Local Device Paths) are intentionally not supported. |
| 874 |
let url = Url::from_file_path(Path::new(r"\\.\some\path\file.txt")); |
| 875 |
assert!(url.is_err()); |
| 876 |
} |
| 877 |
|
| 878 |
#[test] |
| 879 |
fn test_syntax_violation_callback() { |
| 880 |
use url::SyntaxViolation::*; |
| 881 |
let violation = Cell::new(None); |
| 882 |
let url = Url::options() |
| 883 |
.syntax_violation_callback(Some(&|v| violation.set(Some(v)))) |
| 884 |
.parse("http:////mozilla.org:42") |
| 885 |
.unwrap(); |
| 886 |
assert_eq!(url.port(), Some(42)); |
| 887 |
|
| 888 |
let v = violation.take().unwrap(); |
| 889 |
assert_eq!(v, ExpectedDoubleSlash); |
| 890 |
assert_eq!(v.description(), "expected //"); |
| 891 |
assert_eq!(v.to_string(), "expected //"); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn test_syntax_violation_callback_lifetimes() { |
| 896 |
use url::SyntaxViolation::*; |
| 897 |
let violation = Cell::new(None); |
| 898 |
let vfn = |s| violation.set(Some(s)); |
| 899 |
|
| 900 |
let url = Url::options() |
| 901 |
.syntax_violation_callback(Some(&vfn)) |
| 902 |
.parse("http:////mozilla.org:42") |
| 903 |
.unwrap(); |
| 904 |
assert_eq!(url.port(), Some(42)); |
| 905 |
assert_eq!(violation.take(), Some(ExpectedDoubleSlash)); |
| 906 |
|
| 907 |
let url = Url::options() |
| 908 |
.syntax_violation_callback(Some(&vfn)) |
| 909 |
.parse("http://mozilla.org\\path") |
| 910 |
.unwrap(); |
| 911 |
assert_eq!(url.path(), "/path"); |
| 912 |
assert_eq!(violation.take(), Some(Backslash)); |
| 913 |
} |
| 914 |
|
| 915 |
#[test] |
| 916 |
fn test_syntax_violation_callback_types() { |
| 917 |
use url::SyntaxViolation::*; |
| 918 |
|
| 919 |
let data = [ |
| 920 |
("http://mozilla.org/\\foo", Backslash, "backslash"), |
| 921 |
(" http://mozilla.org", C0SpaceIgnored, "leading or trailing control or space character are ignored in URLs"), |
| 922 |
("http://user:[email protected]", EmbeddedCredentials, "embedding authentication information (username or password) in an URL is not recommended"), |
| 923 |
("http:///mozilla.org", ExpectedDoubleSlash, "expected //"), |
| 924 |
("file:/foo.txt", ExpectedFileDoubleSlash, "expected // after file:"), |
| 925 |
("file://mozilla.org/c:/file.txt", FileWithHostAndWindowsDrive, "file: with host and Windows drive letter"), |
| 926 |
("http://mozilla.org/^", NonUrlCodePoint, "non-URL code point"), |
| 927 |
("http://mozilla.org/#\x000", NullInFragment, "NULL characters are ignored in URL fragment identifiers"), |
| 928 |
("http://mozilla.org/%1", PercentDecode, "expected 2 hex digits after %"), |
| 929 |
("http://mozilla.org\t/foo", TabOrNewlineIgnored, "tabs or newlines are ignored in URLs"), |
| 930 |
("http://user@:[email protected]", UnencodedAtSign, "unencoded @ sign in username or password") |
| 931 |
]; |
| 932 |
|
| 933 |
for test_case in &data { |
| 934 |
let violation = Cell::new(None); |
| 935 |
Url::options() |
| 936 |
.syntax_violation_callback(Some(&|v| violation.set(Some(v)))) |
| 937 |
.parse(test_case.0) |
| 938 |
.unwrap(); |
| 939 |
|
| 940 |
let v = violation.take(); |
| 941 |
assert_eq!(v, Some(test_case.1)); |
| 942 |
assert_eq!(v.unwrap().description(), test_case.2); |
| 943 |
assert_eq!(v.unwrap().to_string(), test_case.2); |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
#[test] |
| 948 |
fn test_options_reuse() { |
| 949 |
use url::SyntaxViolation::*; |
| 950 |
let violations = RefCell::new(Vec::new()); |
| 951 |
let vfn = |v| violations.borrow_mut().push(v); |
| 952 |
|
| 953 |
let options = Url::options().syntax_violation_callback(Some(&vfn)); |
| 954 |
let url = options.parse("http:////mozilla.org").unwrap(); |
| 955 |
|
| 956 |
let options = options.base_url(Some(&url)); |
| 957 |
let url = options.parse("/sub\\path").unwrap(); |
| 958 |
assert_eq!(url.as_str(), "http://mozilla.org/sub/path"); |
| 959 |
assert_eq!(*violations.borrow(), vec!(ExpectedDoubleSlash, Backslash)); |
| 960 |
} |
| 961 |
|
| 962 |
/// https://github.com/servo/rust-url/issues/505 |
| 963 |
#[test] |
| 964 |
#[cfg(all(feature = "std", windows))] |
| 965 |
fn test_url_from_file_path() { |
| 966 |
use std::path::PathBuf; |
| 967 |
use url::Url; |
| 968 |
|
| 969 |
let p = PathBuf::from("c:///"); |
| 970 |
let u = Url::from_file_path(p).unwrap(); |
| 971 |
let path = u.to_file_path().unwrap(); |
| 972 |
assert_eq!("C:\\", path.to_str().unwrap()); |
| 973 |
} |
| 974 |
|
| 975 |
/// https://github.com/servo/rust-url/issues/505 |
| 976 |
#[cfg(feature = "std")] |
| 977 |
#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] |
| 978 |
#[cfg(not(windows))] |
| 979 |
#[test] |
| 980 |
fn test_url_from_file_path() { |
| 981 |
use std::path::PathBuf; |
| 982 |
use url::Url; |
| 983 |
|
| 984 |
let p = PathBuf::from("/c:/"); |
| 985 |
let u = Url::from_file_path(p).unwrap(); |
| 986 |
let path = u.to_file_path().unwrap(); |
| 987 |
assert_eq!("/c:/", path.to_str().unwrap()); |
| 988 |
} |
| 989 |
|
| 990 |
#[test] |
| 991 |
fn test_non_special_path() { |
| 992 |
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap(); |
| 993 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/"); |
| 994 |
db_url.set_path("diesel_foo"); |
| 995 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/diesel_foo"); |
| 996 |
assert_eq!(db_url.path(), "/diesel_foo"); |
| 997 |
} |
| 998 |
|
| 999 |
#[test] |
| 1000 |
fn test_non_special_path2() { |
| 1001 |
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap(); |
| 1002 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/"); |
| 1003 |
db_url.set_path(""); |
| 1004 |
assert_eq!(db_url.path(), ""); |
| 1005 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost"); |
| 1006 |
db_url.set_path("foo"); |
| 1007 |
assert_eq!(db_url.path(), "/foo"); |
| 1008 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo"); |
| 1009 |
db_url.set_path("/bar"); |
| 1010 |
assert_eq!(db_url.path(), "/bar"); |
| 1011 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/bar"); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[test] |
| 1015 |
fn test_non_special_path3() { |
| 1016 |
let mut db_url = url::Url::parse("postgres://postgres@localhost/").unwrap(); |
| 1017 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/"); |
| 1018 |
db_url.set_path("/"); |
| 1019 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/"); |
| 1020 |
assert_eq!(db_url.path(), "/"); |
| 1021 |
db_url.set_path("/foo"); |
| 1022 |
assert_eq!(db_url.as_str(), "postgres://postgres@localhost/foo"); |
| 1023 |
assert_eq!(db_url.path(), "/foo"); |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[test] |
| 1027 |
fn test_set_scheme_to_file_with_host() { |
| 1028 |
let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap(); |
| 1029 |
let result = url.set_scheme("file"); |
| 1030 |
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar"); |
| 1031 |
assert_eq!(result, Err(())); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn test_set_scheme_empty_err() { |
| 1036 |
let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap(); |
| 1037 |
let result = url.set_scheme(""); |
| 1038 |
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar"); |
| 1039 |
assert_eq!(result, Err(())); |
| 1040 |
} |
| 1041 |
|
| 1042 |
#[test] |
| 1043 |
fn no_panic() { |
| 1044 |
let mut url = Url::parse("arhttpsps:/.//eom/dae.com/\\\\t\\:").unwrap(); |
| 1045 |
url::quirks::set_hostname(&mut url, "//eom/datcom/\\\\t\\://eom/data.cs").unwrap(); |
| 1046 |
} |
| 1047 |
|
| 1048 |
#[test] |
| 1049 |
fn test_null_host_with_leading_empty_path_segment() { |
| 1050 |
// since Note in item 3 of URL serializing in the URL Standard |
| 1051 |
// https://url.spec.whatwg.org/#url-serializing |
| 1052 |
let url = Url::parse("m:/.//\\").unwrap(); |
| 1053 |
let encoded = url.as_str(); |
| 1054 |
let reparsed = Url::parse(encoded).unwrap(); |
| 1055 |
assert_eq!(reparsed, url); |
| 1056 |
} |
| 1057 |
|
| 1058 |
#[test] |
| 1059 |
fn pop_if_empty_in_bounds() { |
| 1060 |
let mut url = Url::parse("m://").unwrap(); |
| 1061 |
let mut segments = url.path_segments_mut().unwrap(); |
| 1062 |
segments.pop_if_empty(); |
| 1063 |
segments.pop(); |
| 1064 |
} |
| 1065 |
|
| 1066 |
#[test] |
| 1067 |
fn test_slicing() { |
| 1068 |
use url::Position::*; |
| 1069 |
|
| 1070 |
#[derive(Default)] |
| 1071 |
struct ExpectedSlices<'a> { |
| 1072 |
full: &'a str, |
| 1073 |
scheme: &'a str, |
| 1074 |
username: &'a str, |
| 1075 |
password: &'a str, |
| 1076 |
host: &'a str, |
| 1077 |
port: &'a str, |
| 1078 |
path: &'a str, |
| 1079 |
query: &'a str, |
| 1080 |
fragment: &'a str, |
| 1081 |
} |
| 1082 |
|
| 1083 |
let data = [ |
| 1084 |
ExpectedSlices { |
| 1085 |
full: "https://user:[email protected]:9742/path/file.ext?key=val&key2=val2#fragment", |
| 1086 |
scheme: "https", |
| 1087 |
username: "user", |
| 1088 |
password: "pass", |
| 1089 |
host: "domain.com", |
| 1090 |
port: "9742", |
| 1091 |
path: "/path/file.ext", |
| 1092 |
query: "key=val&key2=val2", |
| 1093 |
fragment: "fragment", |
| 1094 |
}, |
| 1095 |
ExpectedSlices { |
| 1096 |
full: "https://domain.com:9742/path/file.ext#fragment", |
| 1097 |
scheme: "https", |
| 1098 |
host: "domain.com", |
| 1099 |
port: "9742", |
| 1100 |
path: "/path/file.ext", |
| 1101 |
fragment: "fragment", |
| 1102 |
..Default::default() |
| 1103 |
}, |
| 1104 |
ExpectedSlices { |
| 1105 |
full: "https://domain.com:9742/path/file.ext", |
| 1106 |
scheme: "https", |
| 1107 |
host: "domain.com", |
| 1108 |
port: "9742", |
| 1109 |
path: "/path/file.ext", |
| 1110 |
..Default::default() |
| 1111 |
}, |
| 1112 |
ExpectedSlices { |
| 1113 |
full: "blob:blob-info", |
| 1114 |
scheme: "blob", |
| 1115 |
path: "blob-info", |
| 1116 |
..Default::default() |
| 1117 |
}, |
| 1118 |
]; |
| 1119 |
|
| 1120 |
for expected_slices in &data { |
| 1121 |
let url = Url::parse(expected_slices.full).unwrap(); |
| 1122 |
assert_eq!(&url[..], expected_slices.full); |
| 1123 |
assert_eq!(&url[BeforeScheme..AfterScheme], expected_slices.scheme); |
| 1124 |
assert_eq!( |
| 1125 |
&url[BeforeUsername..AfterUsername], |
| 1126 |
expected_slices.username |
| 1127 |
); |
| 1128 |
assert_eq!( |
| 1129 |
&url[BeforePassword..AfterPassword], |
| 1130 |
expected_slices.password |
| 1131 |
); |
| 1132 |
assert_eq!(&url[BeforeHost..AfterHost], expected_slices.host); |
| 1133 |
assert_eq!(&url[BeforePort..AfterPort], expected_slices.port); |
| 1134 |
assert_eq!(&url[BeforePath..AfterPath], expected_slices.path); |
| 1135 |
assert_eq!(&url[BeforeQuery..AfterQuery], expected_slices.query); |
| 1136 |
assert_eq!( |
| 1137 |
&url[BeforeFragment..AfterFragment], |
| 1138 |
expected_slices.fragment |
| 1139 |
); |
| 1140 |
assert_eq!(&url[..AfterFragment], expected_slices.full); |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
#[test] |
| 1145 |
fn test_make_relative() { |
| 1146 |
let tests = [ |
| 1147 |
( |
| 1148 |
"http://127.0.0.1:8080/test", |
| 1149 |
"http://127.0.0.1:8080/test", |
| 1150 |
"", |
| 1151 |
), |
| 1152 |
( |
| 1153 |
"http://127.0.0.1:8080/test", |
| 1154 |
"http://127.0.0.1:8080/test/", |
| 1155 |
"test/", |
| 1156 |
), |
| 1157 |
( |
| 1158 |
"http://127.0.0.1:8080/test/", |
| 1159 |
"http://127.0.0.1:8080/test", |
| 1160 |
"../test", |
| 1161 |
), |
| 1162 |
( |
| 1163 |
"http://127.0.0.1:8080/", |
| 1164 |
"http://127.0.0.1:8080/?foo=bar#123", |
| 1165 |
"?foo=bar#123", |
| 1166 |
), |
| 1167 |
( |
| 1168 |
"http://127.0.0.1:8080/", |
| 1169 |
"http://127.0.0.1:8080/test/video", |
| 1170 |
"test/video", |
| 1171 |
), |
| 1172 |
( |
| 1173 |
"http://127.0.0.1:8080/test", |
| 1174 |
"http://127.0.0.1:8080/test/video", |
| 1175 |
"test/video", |
| 1176 |
), |
| 1177 |
( |
| 1178 |
"http://127.0.0.1:8080/test/", |
| 1179 |
"http://127.0.0.1:8080/test/video", |
| 1180 |
"video", |
| 1181 |
), |
| 1182 |
( |
| 1183 |
"http://127.0.0.1:8080/test", |
| 1184 |
"http://127.0.0.1:8080/test2/video", |
| 1185 |
"test2/video", |
| 1186 |
), |
| 1187 |
( |
| 1188 |
"http://127.0.0.1:8080/test/", |
| 1189 |
"http://127.0.0.1:8080/test2/video", |
| 1190 |
"../test2/video", |
| 1191 |
), |
| 1192 |
( |
| 1193 |
"http://127.0.0.1:8080/test/bla", |
| 1194 |
"http://127.0.0.1:8080/test2/video", |
| 1195 |
"../test2/video", |
| 1196 |
), |
| 1197 |
( |
| 1198 |
"http://127.0.0.1:8080/test/bla/", |
| 1199 |
"http://127.0.0.1:8080/test2/video", |
| 1200 |
"../../test2/video", |
| 1201 |
), |
| 1202 |
( |
| 1203 |
"http://127.0.0.1:8080/test/?foo=bar#123", |
| 1204 |
"http://127.0.0.1:8080/test/video", |
| 1205 |
"video", |
| 1206 |
), |
| 1207 |
( |
| 1208 |
"http://127.0.0.1:8080/test/", |
| 1209 |
"http://127.0.0.1:8080/test/video?baz=meh#456", |
| 1210 |
"video?baz=meh#456", |
| 1211 |
), |
| 1212 |
( |
| 1213 |
"http://127.0.0.1:8080/test", |
| 1214 |
"http://127.0.0.1:8080/test?baz=meh#456", |
| 1215 |
"?baz=meh#456", |
| 1216 |
), |
| 1217 |
( |
| 1218 |
"http://127.0.0.1:8080/test/", |
| 1219 |
"http://127.0.0.1:8080/test?baz=meh#456", |
| 1220 |
"../test?baz=meh#456", |
| 1221 |
), |
| 1222 |
( |
| 1223 |
"http://127.0.0.1:8080/test/", |
| 1224 |
"http://127.0.0.1:8080/test/?baz=meh#456", |
| 1225 |
"?baz=meh#456", |
| 1226 |
), |
| 1227 |
( |
| 1228 |
"http://127.0.0.1:8080/test/?foo=bar#123", |
| 1229 |
"http://127.0.0.1:8080/test/video?baz=meh#456", |
| 1230 |
"video?baz=meh#456", |
| 1231 |
), |
| 1232 |
( |
| 1233 |
"http://127.0.0.1:8080/file.txt", |
| 1234 |
"http://127.0.0.1:8080/test/file.txt", |
| 1235 |
"test/file.txt", |
| 1236 |
), |
| 1237 |
( |
| 1238 |
"http://127.0.0.1:8080/not_equal.txt", |
| 1239 |
"http://127.0.0.1:8080/test/file.txt", |
| 1240 |
"test/file.txt", |
| 1241 |
), |
| 1242 |
]; |
| 1243 |
|
| 1244 |
for (base, uri, relative) in &tests { |
| 1245 |
let base_uri = url::Url::parse(base).unwrap(); |
| 1246 |
let relative_uri = url::Url::parse(uri).unwrap(); |
| 1247 |
let make_relative = base_uri.make_relative(&relative_uri).unwrap(); |
| 1248 |
assert_eq!( |
| 1249 |
make_relative, *relative, |
| 1250 |
"base: {base}, uri: {uri}, relative: {relative}" |
| 1251 |
); |
| 1252 |
assert_eq!( |
| 1253 |
base_uri.join(relative).unwrap().as_str(), |
| 1254 |
*uri, |
| 1255 |
"base: {base}, uri: {uri}, relative: {relative}" |
| 1256 |
); |
| 1257 |
} |
| 1258 |
|
| 1259 |
let error_tests = [ |
| 1260 |
("http://127.0.0.1:8080/", "https://127.0.0.1:8080/test/"), |
| 1261 |
("http://127.0.0.1:8080/", "http://127.0.0.1:8081/test/"), |
| 1262 |
("http://127.0.0.1:8080/", "http://127.0.0.2:8080/test/"), |
| 1263 |
("mailto:[email protected]", "mailto:[email protected]"), |
| 1264 |
]; |
| 1265 |
|
| 1266 |
for (base, uri) in &error_tests { |
| 1267 |
let base_uri = url::Url::parse(base).unwrap(); |
| 1268 |
let relative_uri = url::Url::parse(uri).unwrap(); |
| 1269 |
let make_relative = base_uri.make_relative(&relative_uri); |
| 1270 |
assert_eq!(make_relative, None, "base: {base}, uri: {uri}"); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
#[test] |
| 1275 |
fn test_has_authority() { |
| 1276 |
let url = Url::parse("mailto:[email protected]").unwrap(); |
| 1277 |
assert!(!url.has_authority()); |
| 1278 |
let url = Url::parse("unix:/run/foo.socket").unwrap(); |
| 1279 |
assert!(!url.has_authority()); |
| 1280 |
let url = Url::parse("file:///tmp/foo").unwrap(); |
| 1281 |
assert!(url.has_authority()); |
| 1282 |
let url = Url::parse("http://example.com/tmp/foo").unwrap(); |
| 1283 |
assert!(url.has_authority()); |
| 1284 |
} |
| 1285 |
|
| 1286 |
#[test] |
| 1287 |
fn test_authority() { |
| 1288 |
let url = Url::parse("mailto:[email protected]").unwrap(); |
| 1289 |
assert_eq!(url.authority(), ""); |
| 1290 |
let url = Url::parse("unix:/run/foo.socket").unwrap(); |
| 1291 |
assert_eq!(url.authority(), ""); |
| 1292 |
let url = Url::parse("file:///tmp/foo").unwrap(); |
| 1293 |
assert_eq!(url.authority(), ""); |
| 1294 |
let url = Url::parse("http://example.com/tmp/foo").unwrap(); |
| 1295 |
assert_eq!(url.authority(), "example.com"); |
| 1296 |
let url = Url::parse("ftp://127.0.0.1:21/").unwrap(); |
| 1297 |
assert_eq!(url.authority(), "127.0.0.1"); |
| 1298 |
let url = Url::parse("ftp://[email protected]:2121/").unwrap(); |
| 1299 |
assert_eq!(url.authority(), "[email protected]:2121"); |
| 1300 |
let url = Url::parse("https://:@example.com/").unwrap(); |
| 1301 |
assert_eq!(url.authority(), "example.com"); |
| 1302 |
let url = Url::parse("https://:password@[::1]:8080/").unwrap(); |
| 1303 |
assert_eq!(url.authority(), ":password@[::1]:8080"); |
| 1304 |
let url = Url::parse("gopher://user:@àlex.example.com:70").unwrap(); |
| 1305 |
assert_eq!(url.authority(), "user@%C3%A0lex.example.com:70"); |
| 1306 |
let url = Url::parse("irc://àlex:àlex@àlex.рф.example.com:6667/foo").unwrap(); |
| 1307 |
assert_eq!( |
| 1308 |
url.authority(), |
| 1309 |
"%C3%A0lex:%C3%A0lex@%C3%A0lex.%D1%80%D1%84.example.com:6667" |
| 1310 |
); |
| 1311 |
let url = Url::parse("https://àlex:àlex@àlex.рф.example.com:443/foo").unwrap(); |
| 1312 |
assert_eq!( |
| 1313 |
url.authority(), |
| 1314 |
"%C3%A0lex:%C3%[email protected]" |
| 1315 |
); |
| 1316 |
} |
| 1317 |
|
| 1318 |
#[test] |
| 1319 |
/// https://github.com/servo/rust-url/issues/838 |
| 1320 |
fn test_file_with_drive() { |
| 1321 |
let s1 = "fIlE:p:?../"; |
| 1322 |
let url = url::Url::parse(s1).unwrap(); |
| 1323 |
assert_eq!(url.to_string(), "file:///p:?../"); |
| 1324 |
assert_eq!(url.path(), "/p:"); |
| 1325 |
|
| 1326 |
let testcases = [ |
| 1327 |
("a", "file:///p:/a"), |
| 1328 |
("", "file:///p:?../"), |
| 1329 |
("?x", "file:///p:?x"), |
| 1330 |
(".", "file:///p:/"), |
| 1331 |
("..", "file:///p:/"), |
| 1332 |
("../", "file:///p:/"), |
| 1333 |
]; |
| 1334 |
|
| 1335 |
for case in &testcases { |
| 1336 |
let url2 = url::Url::join(&url, case.0).unwrap(); |
| 1337 |
assert_eq!(url2.to_string(), case.1); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[test] |
| 1342 |
/// Similar to test_file_with_drive, but with a path |
| 1343 |
/// that could be confused for a drive. |
| 1344 |
fn test_file_with_drive_and_path() { |
| 1345 |
let s1 = "fIlE:p:/x|?../"; |
| 1346 |
let url = url::Url::parse(s1).unwrap(); |
| 1347 |
assert_eq!(url.to_string(), "file:///p:/x|?../"); |
| 1348 |
assert_eq!(url.path(), "/p:/x|"); |
| 1349 |
let s2 = "a"; |
| 1350 |
let url2 = url::Url::join(&url, s2).unwrap(); |
| 1351 |
assert_eq!(url2.to_string(), "file:///p:/a"); |
| 1352 |
} |
| 1353 |
|
| 1354 |
#[cfg(feature = "std")] |
| 1355 |
#[test] |
| 1356 |
fn issue_864() { |
| 1357 |
let mut url = url::Url::parse("file://").unwrap(); |
| 1358 |
dbg!(&url); |
| 1359 |
url.set_path("x"); |
| 1360 |
dbg!(&url); |
| 1361 |
} |
| 1362 |
|
| 1363 |
#[test] |
| 1364 |
fn issue_974() { |
| 1365 |
let mut url = url::Url::parse("http://example.com:8000").unwrap(); |
| 1366 |
let _ = url::quirks::set_port(&mut url, "\u{0000}9000"); |
| 1367 |
assert_eq!(url.port(), Some(8000)); |
| 1368 |
} |
| 1369 |
|
| 1370 |
#[cfg(feature = "serde")] |
| 1371 |
#[test] |
| 1372 |
fn serde_error_message() { |
| 1373 |
use serde_derive::Deserialize; |
| 1374 |
#[derive(Debug, Deserialize)] |
| 1375 |
#[allow(dead_code)] |
| 1376 |
struct TypeWithUrl { |
| 1377 |
url: Url, |
| 1378 |
} |
| 1379 |
|
| 1380 |
let err = serde_json::from_str::<TypeWithUrl>(r#"{"url": "§invalid#+#*Ä"}"#).unwrap_err(); |
| 1381 |
assert_eq!( |
| 1382 |
err.to_string(), |
| 1383 |
r#"relative URL without a base: "§invalid#+#*Ä" at line 1 column 25"# |
| 1384 |
); |
| 1385 |
} |
| 1386 |
|
| 1387 |
#[test] |
| 1388 |
fn test_parse_url_with_single_byte_control_host() { |
| 1389 |
let input = "l://\x01:"; |
| 1390 |
|
| 1391 |
let url1 = Url::parse(input).unwrap(); |
| 1392 |
let url2 = Url::parse(url1.as_str()).unwrap(); |
| 1393 |
assert_eq!(url2, url1); |
| 1394 |
} |