Skip to content

Commit bc8f867

Browse files
manuel-zatrflynn89
authored andcommitted
LibWebView: Additional unit tests for sanitized_url
Covers: - single word search - public domain suffixes, registered TLDs - localhost - url including words or search for url and words
1 parent 9e7436a commit bc8f867

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Tests/LibWebView/TestWebViewURL.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
3+
* Copyright (c) 2025, Manuel Zahariev <manuel@duck.com>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
@@ -25,6 +26,27 @@ static bool is_sanitized_url_the_same(StringView url)
2526
return sanitized_url->to_string() == url;
2627
}
2728

29+
static void expect_url_equals_sanitized_url(StringView test_url, StringView url, WebView::AppendTLD append_tld = WebView::AppendTLD::No)
30+
{
31+
StringView const search_engine_url = "https://ecosia.org/search?q={}"sv;
32+
33+
auto sanitized_url = WebView::sanitize_url(url, search_engine_url, append_tld);
34+
35+
EXPECT(sanitized_url.has_value());
36+
EXPECT_EQ(sanitized_url->to_string(), test_url);
37+
}
38+
39+
static void expect_search_url_equals_sanitized_url(StringView url)
40+
{
41+
StringView const search_engine_url = "https://ecosia.org/search?q={}"sv;
42+
auto const search_url = String::formatted(search_engine_url, URL::percent_encode(url));
43+
44+
auto sanitized_url = WebView::sanitize_url(url, search_engine_url);
45+
46+
EXPECT(sanitized_url.has_value());
47+
EXPECT_EQ(sanitized_url->to_string(), search_url.value());
48+
}
49+
2850
TEST_CASE(invalid_url)
2951
{
3052
EXPECT(!WebView::break_url_into_parts(""sv).has_value());
@@ -105,3 +127,49 @@ TEST_CASE(data_url)
105127
EXPECT(!is_sanitized_url_the_same("data text/html"sv));
106128
EXPECT(!is_sanitized_url_the_same("text/html data:"sv));
107129
}
130+
131+
TEST_CASE(location_to_search_or_url)
132+
{
133+
expect_search_url_equals_sanitized_url("hello"sv); // Search.
134+
expect_search_url_equals_sanitized_url("hello world"sv);
135+
expect_search_url_equals_sanitized_url("\"example.org\""sv);
136+
expect_search_url_equals_sanitized_url("\"example.org"sv);
137+
expect_search_url_equals_sanitized_url("\"http://example.org\""sv);
138+
expect_search_url_equals_sanitized_url("example.org hello"sv);
139+
expect_search_url_equals_sanitized_url("http://example.org and example sites"sv);
140+
expect_search_url_equals_sanitized_url("ftp://example.org"sv); // ftp:// is not in SUPPORTED_SCHEMES
141+
expect_search_url_equals_sanitized_url("https://exa\"mple.com/what"sv);
142+
143+
// If it can feed create_with_url_or_path -- it is a url.
144+
expect_url_equals_sanitized_url("https://example.com/%20some%20cool%20page"sv, "https://example.com/ some cool page"sv);
145+
expect_url_equals_sanitized_url("https://example.com/some%20cool%20page"sv, "https://example.com/some cool page"sv);
146+
expect_url_equals_sanitized_url("https://example.com/%22what%22"sv, "https://example.com/\"what\""sv);
147+
148+
expect_url_equals_sanitized_url("https://example.org/"sv, "example.org"sv); // Valid domain.
149+
expect_url_equals_sanitized_url("https://example.abc/"sv, "example.abc"sv); // .abc is a recognized TLD.
150+
expect_url_equals_sanitized_url("https://example.test/path"sv, "example.test/path"sv); // Reserved TLDs.
151+
expect_url_equals_sanitized_url("https://example.example/path"sv, "example.example/path"sv);
152+
expect_url_equals_sanitized_url("https://example.invalid/path"sv, "example.invalid/path"sv);
153+
expect_url_equals_sanitized_url("https://example.localhost/path"sv, "example.localhost/path"sv);
154+
155+
expect_search_url_equals_sanitized_url("example.def"sv); // Invalid domain but no scheme: search (Like Firefox or Chrome).
156+
157+
expect_url_equals_sanitized_url("https://example.org/"sv, "https://example.org"sv); // Scheme.
158+
// Respect the user if the url has a valid scheme but not a public suffix (.def is not a recognized TLD).
159+
expect_url_equals_sanitized_url("https://example.def/"sv, "https://example.def"sv);
160+
161+
expect_url_equals_sanitized_url("https://localhost/"sv, "localhost"sv); // Respect localhost.
162+
expect_url_equals_sanitized_url("https://localhost/hello"sv, "localhost/hello"sv);
163+
expect_url_equals_sanitized_url("https://localhost/hello.world"sv, "localhost/hello.world"sv);
164+
expect_url_equals_sanitized_url("https://localhost/hello.world?query=123"sv, "localhost/hello.world?query=123"sv);
165+
166+
expect_url_equals_sanitized_url("https://example.com/"sv, "example"sv, WebView::AppendTLD::Yes); // User holds down the Ctrl key.
167+
expect_url_equals_sanitized_url("https://example.def.com/"sv, "example.def"sv, WebView::AppendTLD::Yes);
168+
expect_url_equals_sanitized_url("https://com.com/"sv, "com"sv, WebView::AppendTLD::Yes);
169+
expect_url_equals_sanitized_url("https://example.com/index.html"sv, "example/index.html"sv, WebView::AppendTLD::Yes);
170+
171+
expect_search_url_equals_sanitized_url("whatever:example.com"sv); // Invalid scheme.
172+
expect_search_url_equals_sanitized_url("mailto:hello@example.com"sv); // For now, unsupported scheme.
173+
// FIXME: Add support for opening mailto: scheme (below). Firefox opens mailto: locations
174+
// expect_url_equals_sanitized_url("mailto:hello@example.com"sv, "mailto:hello@example.com"sv);
175+
}

0 commit comments

Comments
 (0)