diff --git a/AK/Tests/TestURL.cpp b/AK/Tests/TestURL.cpp index 1c9f2e388e..53251a3cca 100644 --- a/AK/Tests/TestURL.cpp +++ b/AK/Tests/TestURL.cpp @@ -130,4 +130,22 @@ TEST_CASE(serialization) EXPECT_EQ(URL("https://www.serenityos.org:443/foo/bar.html?query#fragment").to_string(), "https://www.serenityos.org/foo/bar.html?query#fragment"); } +TEST_CASE(file_url_with_hostname) +{ + URL url("file://localhost/my/file"); + EXPECT_EQ(url.is_valid(), true); + EXPECT_EQ(url.host(), "localhost"); + EXPECT_EQ(url.path(), "/my/file"); + EXPECT_EQ(url.to_string(), "file://localhost/my/file"); +} + +TEST_CASE(file_url_without_hostname) +{ + URL url("file:///my/file"); + EXPECT_EQ(url.is_valid(), true); + EXPECT_EQ(url.host(), ""); + EXPECT_EQ(url.path(), "/my/file"); + EXPECT_EQ(url.to_string(), "file:///my/file"); +} + TEST_MAIN(URL) diff --git a/AK/URL.cpp b/AK/URL.cpp index 1d60fb53eb..1f84604202 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -112,8 +112,14 @@ bool URL::parse(const StringView& string) buffer.append(consume()); continue; } - if (buffer.is_empty()) + if (buffer.is_empty()) { + if (m_protocol == "file") { + m_host = ""; + state = State::InPath; + continue; + } return false; + } m_host = String::copy(buffer); buffer.clear(); if (peek() == ':') {