1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
// Copyright 2013-2014 The rust-url developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Data-driven tests imported from web-platform-tests
use serde_json::Value;
use std::collections::HashMap;
use std::fmt::Write;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use std::sync::Mutex;
use url::Url;
// https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/usage.html
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use wasm_bindgen_test::{console_log, wasm_bindgen_test, wasm_bindgen_test_configure};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
wasm_bindgen_test_configure!(run_in_browser);
// wpt has its own test driver, but we shoe-horn this into wasm_bindgen_test
// which will discard stdout and stderr. So, we make println! go to
// console.log(), so we see failures that do not result in panics.
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
static PRINT_BUF: Mutex<Option<String>> = Mutex::new(None);
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
macro_rules! print {
($($arg:tt)*) => {
let v = format!($($arg)*);
{
let mut buf = PRINT_BUF.lock().unwrap();
if let Some(buf) = buf.as_mut() {
buf.push_str(&v);
} else {
*buf = Some(v);
}
}
};
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
macro_rules! println {
() => {
let buf = PRINT_BUF.lock().unwrap().take();
match buf {
Some(buf) => console_log!("{buf}"),
None => console_log!(""),
}
};
($($arg:tt)*) => {
let buf = PRINT_BUF.lock().unwrap().take();
match buf {
Some(buf) => {
let v = format!($($arg)*);
console_log!("{buf}{v}");
},
None => console_log!($($arg)*),
}
}
}
#[derive(Debug, serde::Deserialize)]
struct UrlTest {
input: String,
base: Option<String>,
#[serde(flatten)]
result: UrlTestResult,
}
#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
enum UrlTestResult {
Ok(UrlTestOk),
Fail(UrlTestFail),
}
#[derive(Debug, serde::Deserialize)]
struct UrlTestOk {
href: String,
protocol: String,
username: String,
password: String,
host: String,
hostname: String,
port: String,
pathname: String,
search: String,
hash: String,
}
#[derive(Debug, serde::Deserialize)]
struct UrlTestFail {
failure: bool,
}
#[derive(Debug, serde::Deserialize)]
struct SetterTest {
href: String,
new_value: String,
expected: SetterTestExpected,
}
#[derive(Debug, serde::Deserialize)]
struct SetterTestExpected {
href: Option<String>,
protocol: Option<String>,
username: Option<String>,
password: Option<String>,
host: Option<String>,
hostname: Option<String>,
port: Option<String>,
pathname: Option<String>,
search: Option<String>,
hash: Option<String>,
}
#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), wasm_bindgen_test)]
fn main() {
let mut filter = None;
let mut args = std::env::args().skip(1);
while filter.is_none() {
if let Some(arg) = args.next() {
if arg == "--test-threads" {
args.next();
continue;
}
filter = Some(arg);
} else {
break;
}
}
let mut expected_failures = include_str!("expected_failures.txt")
.lines()
.collect::<Vec<_>>();
let mut errors = vec![];
// Copied from https://github.com/web-platform-tests/wpt/blob/master/url/
let url_json: Vec<Value> = serde_json::from_str(include_str!("urltestdata.json"))
.expect("JSON parse error in urltestdata.json");
let url_tests = url_json
.into_iter()
.filter(|val| val.is_object())
.map(|val| serde_json::from_value::<UrlTest>(val).expect("parsing failed"))
.collect::<Vec<_>>();
let setter_json: HashMap<String, Value> =
serde_json::from_str(include_str!("setters_tests.json"))
.expect("JSON parse error in setters_tests.json");
let setter_tests = setter_json
.into_iter()
.filter(|(k, _)| k != "comment")
.map(|(k, v)| {
let test = serde_json::from_value::<Vec<SetterTest>>(v).expect("parsing failed");
(k, test)
})
.collect::<HashMap<_, _>>();
for url_test in url_tests {
let mut name = format!("<{}>", url_test.input.escape_default());
if let Some(base) = &url_test.base {
write!(&mut name, " against <{}>", base.escape_default()).unwrap();
}
if should_skip(&name, filter.as_deref()) {
continue;
}
print!("{} ... ", name);
let res = run_url_test(url_test);
report(name, res, &mut errors, &mut expected_failures);
}
for (kind, tests) in setter_tests {
for test in tests {
let name = format!(
"<{}> set {} to <{}>",
test.href.escape_default(),
kind,
test.new_value.escape_default()
);
if should_skip(&name, filter.as_deref()) {
continue;
}
print!("{} ... ", name);
let res = run_setter_test(&kind, test);
report(name, res, &mut errors, &mut expected_failures);
}
}
println!();
println!("====================");
println!();
if !errors.is_empty() {
println!("errors:");
println!();
for (name, err) in errors {
println!(" name: {}", name);
println!(" err: {}", err);
println!();
}
std::process::exit(1);
} else {
println!("all tests passed");
}
if !expected_failures.is_empty() && filter.is_none() {
println!();
println!("====================");
println!();
println!("tests were expected to fail but did not run:");
println!();
for name in expected_failures {
println!(" {}", name);
}
println!();
println!("if these tests were removed, update expected_failures.txt");
println!();
std::process::exit(1);
}
}
fn should_skip(name: &str, filter: Option<&str>) -> bool {
match filter {
Some(filter) => !name.contains(filter),
None => false,
}
}
fn report(
name: String,
res: Result<(), String>,
errors: &mut Vec<(String, String)>,
expected_failures: &mut Vec<&str>,
) {
let expected_failure = expected_failures.contains(&&*name);
expected_failures.retain(|&s| s != &*name);
match res {
Ok(()) => {
if expected_failure {
println!("🟠 (unexpected success)");
errors.push((name, "unexpected success".to_string()));
} else {
println!("✅");
}
}
Err(err) => {
if expected_failure {
println!("✅ (expected fail)");
} else {
println!("❌");
errors.push((name, err));
}
}
}
}
fn run_url_test(
UrlTest {
base,
input,
result,
}: UrlTest,
) -> Result<(), String> {
let base = match base {
Some(base) => {
let base =
Url::parse(&base).map_err(|e| format!("errored while parsing base: {}", e))?;
Some(base)
}
None => None,
};
let res = Url::options()
.base_url(base.as_ref())
.parse(&input)
.map_err(|e| format!("errored while parsing input: {}", e));
match result {
UrlTestResult::Ok(ok) => check_url_ok(res, ok),
UrlTestResult::Fail(fail) => {
assert!(fail.failure);
if res.is_ok() {
return Err("expected failure, but parsed successfully".to_string());
}
Ok(())
}
}
}
fn check_url_ok(res: Result<Url, String>, ok: UrlTestOk) -> Result<(), String> {
let url = match res {
Ok(url) => url,
Err(err) => {
return Err(format!("expected success, but errored: {:?}", err));
}
};
let href = url::quirks::href(&url);
if href != ok.href {
return Err(format!("expected href {:?}, but got {:?}", ok.href, href));
}
let protocol = url::quirks::protocol(&url);
if protocol != ok.protocol {
return Err(format!(
"expected protocol {:?}, but got {:?}",
ok.protocol, protocol
));
}
let username = url::quirks::username(&url);
if username != ok.username {
return Err(format!(
"expected username {:?}, but got {:?}",
ok.username, username
));
}
let password = url::quirks::password(&url);
if password != ok.password {
return Err(format!(
"expected password {:?}, but got {:?}",
ok.password, password
));
}
let host = url::quirks::host(&url);
if host != ok.host {
return Err(format!("expected host {:?}, but got {:?}", ok.host, host));
}
let hostname = url::quirks::hostname(&url);
if hostname != ok.hostname {
return Err(format!(
"expected hostname {:?}, but got {:?}",
ok.hostname, hostname
));
}
let port = url::quirks::port(&url);
if port != ok.port {
return Err(format!("expected port {:?}, but got {:?}", ok.port, port));
}
let pathname = url::quirks::pathname(&url);
if pathname != ok.pathname {
return Err(format!(
"expected pathname {:?}, but got {:?}",
ok.pathname, pathname
));
}
let search = url::quirks::search(&url);
if search != ok.search {
return Err(format!(
"expected search {:?}, but got {:?}",
ok.search, search
));
}
let hash = url::quirks::hash(&url);
if hash != ok.hash {
return Err(format!("expected hash {:?}, but got {:?}", ok.hash, hash));
}
Ok(())
}
fn run_setter_test(
kind: &str,
SetterTest {
href,
new_value,
expected,
}: SetterTest,
) -> Result<(), String> {
let mut url = Url::parse(&href).map_err(|e| format!("errored while parsing href: {}", e))?;
match kind {
"protocol" => {
url::quirks::set_protocol(&mut url, &new_value).ok();
}
"username" => {
url::quirks::set_username(&mut url, &new_value).ok();
}
"password" => {
url::quirks::set_password(&mut url, &new_value).ok();
}
"host" => {
url::quirks::set_host(&mut url, &new_value).ok();
}
"hostname" => {
url::quirks::set_hostname(&mut url, &new_value).ok();
}
"port" => {
url::quirks::set_port(&mut url, &new_value).ok();
}
"pathname" => url::quirks::set_pathname(&mut url, &new_value),
"search" => url::quirks::set_search(&mut url, &new_value),
"hash" => url::quirks::set_hash(&mut url, &new_value),
_ => {
return Err(format!("unknown setter kind: {:?}", kind));
}
}
if let Some(expected_href) = expected.href {
let href = url::quirks::href(&url);
if href != expected_href {
return Err(format!(
"expected href {:?}, but got {:?}",
expected_href, href
));
}
}
if let Some(expected_protocol) = expected.protocol {
let protocol = url::quirks::protocol(&url);
if protocol != expected_protocol {
return Err(format!(
"expected protocol {:?}, but got {:?}",
expected_protocol, protocol
));
}
}
if let Some(expected_username) = expected.username {
let username = url::quirks::username(&url);
if username != expected_username {
return Err(format!(
"expected username {:?}, but got {:?}",
expected_username, username
));
}
}
if let Some(expected_password) = expected.password {
let password = url::quirks::password(&url);
if password != expected_password {
return Err(format!(
"expected password {:?}, but got {:?}",
expected_password, password
));
}
}
if let Some(expected_host) = expected.host {
let host = url::quirks::host(&url);
if host != expected_host {
return Err(format!(
"expected host {:?}, but got {:?}",
expected_host, host
));
}
}
if let Some(expected_hostname) = expected.hostname {
let hostname = url::quirks::hostname(&url);
if hostname != expected_hostname {
return Err(format!(
"expected hostname {:?}, but got {:?}",
expected_hostname, hostname
));
}
}
if let Some(expected_port) = expected.port {
let port = url::quirks::port(&url);
if port != expected_port {
return Err(format!(
"expected port {:?}, but got {:?}",
expected_port, port
));
}
}
if let Some(expected_pathname) = expected.pathname {
let pathname = url::quirks::pathname(&url);
if pathname != expected_pathname {
return Err(format!(
"expected pathname {:?}, but got {:?}",
expected_pathname, pathname
));
}
}
if let Some(expected_search) = expected.search {
let search = url::quirks::search(&url);
if search != expected_search {
return Err(format!(
"expected search {:?}, but got {:?}",
expected_search, search
));
}
}
if let Some(expected_hash) = expected.hash {
let hash = url::quirks::hash(&url);
if hash != expected_hash {
return Err(format!(
"expected hash {:?}, but got {:?}",
expected_hash, hash
));
}
}
Ok(())
}
|