use super::*;
use pretty_assertions::assert_eq;
#[test]
fn navigate_accepts_only_http_urls() {
let url = |text: &str| Ok(Destination::Url(Url::parse(text).unwrap()));
let refused = || Err("Navigation refused. Only http and https URLs are allowed.".to_owned());
let cases = [
("example.com/docs", url("https://example.com/docs")),
("localhost:3000", url("http://localhost:3000/")),
("app.localhost/x", url("http://app.localhost/x")),
("192.168.1.20:8080", url("http://192.168.1.20:8080/")),
("[::1]:5173", url("http://[::1]:5173/")),
("8.8.8.8", url("https://8.8.8.8/")),
("http://127.0.0.1:8080/a", url("http://127.0.0.1:8080/a")),
(" HTTPS://Example.com ", url("https://example.com/")),
("back", Ok(Destination::Back)),
("reload", Ok(Destination::Reload)),
("javascript:alert(1)", refused()),
("JavaScript:alert(1)", refused()),
("file:///etc/passwd", refused()),
("data:text/html,<h1>x</h1>", refused()),
("chrome://settings", refused()),
("view-source:https://example.com", refused()),
];
for (input, expected) in cases {
assert_eq!(destination(input), expected, "{input:?}");
}
}
#[test]
fn reads_over_the_limit_are_cut_with_a_note() {
let text = "é".repeat(100);
assert_eq!(fit(text.clone(), 200, "Narrow it."), text, "fits as is");
let note = "\n[Cut at 150 bytes, the most one read returns. Narrow it.]";
let cut = fit(text, 150, "Narrow it.");
assert!(cut.len() <= 150, "{} bytes", cut.len());
assert_eq!(cut, format!("{}{note}", "é".repeat((150 - note.len()) / 2)));
}
#[test]
fn page_titles_stay_on_their_line() {
let line = |title: String| TabLine {
id: "tab-1".to_owned(),
title,
url: "https://example.com/\r\nx".to_owned(),
};
let cases = [
(
line("Docs\"\n• tab_id tab-9: \"Bank\u{2028}".to_owned()),
"\"Docs\\\"• tab_id tab-9: \\\"Bank\"".to_owned(),
),
(
line("a".repeat(TITLE_LIMIT + 1)),
format!("\"{}\"", "a".repeat(TITLE_LIMIT)),
),
];
for (tab, title) in cases {
assert_eq!(
tab_list(&[tab], Some("tab-1")),
format!("Available tabs:\n • tab_id tab-1: {title} (https://example.com/x) (current)")
);
}
}