2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-02-25 18:35:51 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2021-08-04 16:29:06 +00:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-10 15:27:56 +00:00
|
|
|
|
2022-02-25 18:35:51 +00:00
|
|
|
#include <AK/Base64.h>
|
2019-08-10 15:27:56 +00:00
|
|
|
#include <AK/URL.h>
|
2022-12-27 19:17:30 +00:00
|
|
|
#include <AK/URLParser.h>
|
2019-08-10 15:27:56 +00:00
|
|
|
|
|
|
|
TEST_CASE(construct)
|
|
|
|
{
|
|
|
|
EXPECT_EQ(URL().is_valid(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(basic)
|
|
|
|
{
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://www.serenityos.org"sv);
|
2019-08-10 15:27:56 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 80);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.path(), "/");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
2020-04-11 22:38:28 +00:00
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("https://www.serenityos.org/index.html"sv);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "https");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 443);
|
2019-08-10 15:27:56 +00:00
|
|
|
EXPECT_EQ(url.path(), "/index.html");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
2019-08-10 15:27:56 +00:00
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("https://localhost:1234/~anon/test/page.html"sv);
|
2019-08-10 15:27:56 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "https");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "localhost");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 1234);
|
2019-08-10 15:27:56 +00:00
|
|
|
EXPECT_EQ(url.path(), "/~anon/test/page.html");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
2020-04-11 22:38:28 +00:00
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://www.serenityos.org/index.html?#"sv);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 80);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.path(), "/index.html");
|
|
|
|
EXPECT_EQ(url.query(), "");
|
|
|
|
EXPECT_EQ(url.fragment(), "");
|
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://www.serenityos.org/index.html?foo=1&bar=2"sv);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 80);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.path(), "/index.html");
|
|
|
|
EXPECT_EQ(url.query(), "foo=1&bar=2");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.fragment().is_null());
|
2020-04-11 22:38:28 +00:00
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://www.serenityos.org/index.html#fragment"sv);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 80);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.path(), "/index.html");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.query().is_null());
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.fragment(), "fragment");
|
|
|
|
}
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://www.serenityos.org/index.html?foo=1&bar=2&baz=/?#frag/ment?test#"sv);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.is_valid(), true);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.host(), "www.serenityos.org");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 80);
|
2020-04-11 22:38:28 +00:00
|
|
|
EXPECT_EQ(url.path(), "/index.html");
|
|
|
|
EXPECT_EQ(url.query(), "foo=1&bar=2&baz=/?");
|
|
|
|
EXPECT_EQ(url.fragment(), "frag/ment?test#");
|
2019-08-10 15:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(some_bad_urls)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT_EQ(URL("http//serenityos.org"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("serenityos.org"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("://serenityos.org"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("://:80"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("http://serenityos.org:80:80/"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("http://serenityos.org:80:80"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("http://serenityos.org:abc"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("http://serenityos.org:abc:80"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("http://serenityos.org:abc:80/"sv).is_valid(), false);
|
|
|
|
EXPECT_EQ(URL("data:"sv).is_valid(), false);
|
2019-08-10 15:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(serialization)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT_EQ(URL("http://www.serenityos.org/"sv).serialize(), "http://www.serenityos.org/");
|
|
|
|
EXPECT_EQ(URL("http://www.serenityos.org:0/"sv).serialize(), "http://www.serenityos.org:0/");
|
|
|
|
EXPECT_EQ(URL("http://www.serenityos.org:80/"sv).serialize(), "http://www.serenityos.org/");
|
|
|
|
EXPECT_EQ(URL("http://www.serenityos.org:81/"sv).serialize(), "http://www.serenityos.org:81/");
|
|
|
|
EXPECT_EQ(URL("https://www.serenityos.org:443/foo/bar.html?query#fragment"sv).serialize(), "https://www.serenityos.org/foo/bar.html?query#fragment");
|
2019-08-10 15:27:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 14:47:05 +00:00
|
|
|
TEST_CASE(file_url_with_hostname)
|
2021-05-29 19:52:13 +00:00
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file://courage/my/file"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
|
|
|
EXPECT_EQ(url.host(), "courage");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 0);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.path(), "/my/file");
|
|
|
|
EXPECT_EQ(url.serialize(), "file://courage/my/file");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_with_localhost)
|
2020-05-09 14:47:05 +00:00
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file://localhost/my/file"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
|
|
|
EXPECT_EQ(url.host(), "");
|
2020-05-09 14:47:05 +00:00
|
|
|
EXPECT_EQ(url.path(), "/my/file");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.serialize(), "file:///my/file");
|
2020-05-09 14:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_without_hostname)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file:///my/file"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
2020-05-09 14:47:05 +00:00
|
|
|
EXPECT_EQ(url.host(), "");
|
|
|
|
EXPECT_EQ(url.path(), "/my/file");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.serialize(), "file:///my/file");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_with_encoded_characters)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file:///my/file/test%23file.txt"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
|
|
|
EXPECT_EQ(url.path(), "/my/file/test#file.txt");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_with_fragment)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file:///my/file#fragment"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
|
|
|
EXPECT_EQ(url.path(), "/my/file");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT_EQ(url.fragment(), "fragment");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_with_root_path)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("file:///"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
|
|
|
EXPECT_EQ(url.path(), "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(file_url_serialization)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT_EQ(URL("file://courage/my/file"sv).serialize(), "file://courage/my/file");
|
|
|
|
EXPECT_EQ(URL("file://localhost/my/file"sv).serialize(), "file:///my/file");
|
|
|
|
EXPECT_EQ(URL("file:///my/file"sv).serialize(), "file:///my/file");
|
|
|
|
EXPECT_EQ(URL("file:///my/directory/"sv).serialize(), "file:///my/directory/");
|
|
|
|
EXPECT_EQ(URL("file:///my/file%23test"sv).serialize(), "file:///my/file%23test");
|
|
|
|
EXPECT_EQ(URL("file:///my/file#fragment"sv).serialize(), "file:///my/file#fragment");
|
2020-05-09 14:47:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 09:11:48 +00:00
|
|
|
TEST_CASE(about_url)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("about:blank"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "about");
|
|
|
|
EXPECT(url.host().is_null());
|
2020-05-10 09:11:48 +00:00
|
|
|
EXPECT_EQ(url.path(), "blank");
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
EXPECT_EQ(url.serialize(), "about:blank");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(mailto_url)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("mailto:mail@example.com"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "mailto");
|
|
|
|
EXPECT(url.host().is_null());
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 0);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.paths().size(), 1u);
|
|
|
|
EXPECT_EQ(url.paths()[0], "mail@example.com");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
EXPECT_EQ(url.serialize(), "mailto:mail@example.com");
|
2020-05-10 09:11:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 06:20:20 +00:00
|
|
|
TEST_CASE(data_url)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:text/html,test"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
2020-11-04 06:20:20 +00:00
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/html");
|
2020-11-30 00:28:27 +00:00
|
|
|
EXPECT_EQ(url.data_payload(), "test");
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(!url.data_payload_is_base64());
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.serialize(), "data:text/html,test");
|
2020-11-04 06:20:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 16:29:06 +00:00
|
|
|
TEST_CASE(data_url_default_mime_type)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:,test"sv);
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/plain");
|
|
|
|
EXPECT_EQ(url.data_payload(), "test");
|
|
|
|
EXPECT(!url.data_payload_is_base64());
|
|
|
|
EXPECT_EQ(url.serialize(), "data:text/plain,test");
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:28:27 +00:00
|
|
|
TEST_CASE(data_url_encoded)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:text/html,Hello%20friends%2C%0X%X0"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
2020-11-30 00:28:27 +00:00
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/html");
|
|
|
|
EXPECT_EQ(url.data_payload(), "Hello friends,%0X%X0");
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(!url.data_payload_is_base64());
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.serialize(), "data:text/html,Hello friends,%0X%X0");
|
2020-11-30 00:28:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 06:20:20 +00:00
|
|
|
TEST_CASE(data_url_base64_encoded)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:text/html;base64,test"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
2020-11-04 06:20:20 +00:00
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/html");
|
2020-11-30 00:28:27 +00:00
|
|
|
EXPECT_EQ(url.data_payload(), "test");
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(url.data_payload_is_base64());
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.serialize(), "data:text/html;base64,test");
|
2020-11-04 06:20:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 16:29:06 +00:00
|
|
|
TEST_CASE(data_url_base64_encoded_default_mime_type)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:;base64,test"sv);
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/plain");
|
|
|
|
EXPECT_EQ(url.data_payload(), "test");
|
|
|
|
EXPECT(url.data_payload_is_base64());
|
|
|
|
EXPECT_EQ(url.serialize(), "data:text/plain;base64,test");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(data_url_base64_encoded_with_whitespace)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data: text/html ; bAsE64 , test with whitespace "sv);
|
2021-08-04 16:29:06 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/html");
|
|
|
|
EXPECT_EQ(url.data_payload(), " test with whitespace ");
|
|
|
|
EXPECT(url.data_payload_is_base64());
|
|
|
|
EXPECT_EQ(url.serialize(), "data:text/html;base64, test with whitespace ");
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:35:51 +00:00
|
|
|
TEST_CASE(data_url_base64_encoded_with_inline_whitespace)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207%20"sv);
|
2022-02-25 18:35:51 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "data");
|
|
|
|
EXPECT(url.host().is_null());
|
|
|
|
EXPECT_EQ(url.data_mime_type(), "text/javascript");
|
|
|
|
EXPECT(url.data_payload_is_base64());
|
|
|
|
EXPECT_EQ(url.data_payload(), " ZD Qg\r\nPS An Zm91cic\r\n 7 "sv);
|
|
|
|
auto decode_result = decode_base64(url.data_payload());
|
|
|
|
EXPECT_EQ(decode_result.is_error(), false);
|
|
|
|
EXPECT_EQ(StringView(decode_result.value()), "d4 = 'four';"sv);
|
|
|
|
}
|
|
|
|
|
2020-05-17 11:41:36 +00:00
|
|
|
TEST_CASE(trailing_slash_with_complete_url)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT_EQ(URL("http://a/b/"sv).complete_url("c/"sv).serialize(), "http://a/b/c/");
|
|
|
|
EXPECT_EQ(URL("http://a/b/"sv).complete_url("c"sv).serialize(), "http://a/b/c");
|
|
|
|
EXPECT_EQ(URL("http://a/b"sv).complete_url("c/"sv).serialize(), "http://a/c/");
|
|
|
|
EXPECT_EQ(URL("http://a/b"sv).complete_url("c"sv).serialize(), "http://a/c");
|
2020-05-17 11:41:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 16:24:19 +00:00
|
|
|
TEST_CASE(trailing_port)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url("http://example.com:8086"sv);
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 8086);
|
2020-12-12 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 19:52:13 +00:00
|
|
|
TEST_CASE(port_overflow)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT_EQ(URL("http://example.com:123456789/"sv).is_valid(), false);
|
2021-05-29 19:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(equality)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT(URL("http://serenityos.org"sv).equals("http://serenityos.org#test"sv, URL::ExcludeFragment::Yes));
|
|
|
|
EXPECT_EQ(URL("http://example.com/index.html"sv), URL("http://ex%61mple.com/index.html"sv));
|
|
|
|
EXPECT_EQ(URL("file:///my/file"sv), URL("file://localhost/my/file"sv));
|
|
|
|
EXPECT_NE(URL("http://serenityos.org/index.html"sv), URL("http://serenityos.org/test.html"sv));
|
2021-05-29 19:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(create_with_file_scheme)
|
2021-03-17 07:42:46 +00:00
|
|
|
{
|
2021-05-29 19:52:13 +00:00
|
|
|
auto url = URL::create_with_file_scheme("/home/anon/README.md");
|
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "file");
|
2021-09-13 20:12:16 +00:00
|
|
|
EXPECT_EQ(url.port_or_default(), 0);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.paths().size(), 3u);
|
|
|
|
EXPECT_EQ(url.paths()[0], "home");
|
|
|
|
EXPECT_EQ(url.paths()[1], "anon");
|
|
|
|
EXPECT_EQ(url.paths()[2], "README.md");
|
|
|
|
EXPECT_EQ(url.path(), "/home/anon/README.md");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
|
|
|
|
url = URL::create_with_file_scheme("/home/anon/");
|
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.paths().size(), 3u);
|
|
|
|
EXPECT_EQ(url.paths()[0], "home");
|
|
|
|
EXPECT_EQ(url.paths()[1], "anon");
|
|
|
|
EXPECT_EQ(url.paths()[2], "");
|
|
|
|
EXPECT_EQ(url.path(), "/home/anon/");
|
|
|
|
|
2022-07-11 20:59:08 +00:00
|
|
|
url = URL("file:///home/anon/"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT_EQ(url.path(), "/home/anon/");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(complete_url)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL base_url("http://serenityos.org/index.html#fragment"sv);
|
|
|
|
URL url = base_url.complete_url("test.html"sv);
|
2021-05-29 19:52:13 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.scheme(), "http");
|
|
|
|
EXPECT_EQ(url.host(), "serenityos.org");
|
|
|
|
EXPECT_EQ(url.path(), "/test.html");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT_EQ(url.cannot_be_a_base_url(), false);
|
|
|
|
|
2022-07-11 20:59:08 +00:00
|
|
|
EXPECT(base_url.complete_url("../index.html#fragment"sv).equals(base_url));
|
2021-03-17 07:42:46 +00:00
|
|
|
}
|
2021-06-01 11:11:14 +00:00
|
|
|
|
|
|
|
TEST_CASE(leading_whitespace)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url { " https://foo.com/"sv };
|
2021-06-01 11:11:14 +00:00
|
|
|
EXPECT(url.is_valid());
|
2022-12-06 01:12:49 +00:00
|
|
|
EXPECT_EQ(url.to_deprecated_string(), "https://foo.com/");
|
2021-06-01 11:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(trailing_whitespace)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url { "https://foo.com/ "sv };
|
2021-06-01 11:11:14 +00:00
|
|
|
EXPECT(url.is_valid());
|
2022-12-06 01:12:49 +00:00
|
|
|
EXPECT_EQ(url.to_deprecated_string(), "https://foo.com/");
|
2021-06-01 11:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(leading_and_trailing_whitespace)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url { " https://foo.com/ "sv };
|
2021-06-01 11:11:14 +00:00
|
|
|
EXPECT(url.is_valid());
|
2022-12-06 01:12:49 +00:00
|
|
|
EXPECT_EQ(url.to_deprecated_string(), "https://foo.com/");
|
2021-06-01 11:11:14 +00:00
|
|
|
}
|
2021-06-03 10:36:02 +00:00
|
|
|
|
|
|
|
TEST_CASE(unicode)
|
|
|
|
{
|
2022-07-11 20:59:08 +00:00
|
|
|
URL url { "http://example.com/_ünicöde_téxt_©"sv };
|
2021-06-03 10:36:02 +00:00
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.path(), "/_ünicöde_téxt_©");
|
|
|
|
EXPECT(url.query().is_null());
|
|
|
|
EXPECT(url.fragment().is_null());
|
|
|
|
}
|
2022-09-20 13:38:53 +00:00
|
|
|
|
|
|
|
TEST_CASE(complete_file_url_with_base)
|
|
|
|
{
|
|
|
|
URL url { "file:///home/index.html" };
|
|
|
|
EXPECT(url.is_valid());
|
|
|
|
EXPECT_EQ(url.path(), "/home/index.html");
|
|
|
|
EXPECT_EQ(url.paths().size(), 2u);
|
|
|
|
EXPECT_EQ(url.paths()[0], "home");
|
|
|
|
EXPECT_EQ(url.paths()[1], "index.html");
|
|
|
|
|
2023-02-13 17:42:27 +00:00
|
|
|
auto sub_url = url.complete_url("js/app.js"sv);
|
2022-09-20 13:38:53 +00:00
|
|
|
EXPECT(sub_url.is_valid());
|
|
|
|
EXPECT_EQ(sub_url.path(), "/home/js/app.js");
|
|
|
|
}
|
2022-12-27 19:17:30 +00:00
|
|
|
|
|
|
|
TEST_CASE(empty_url_with_base_url)
|
|
|
|
{
|
|
|
|
URL base_url { "https://foo.com/"sv };
|
2023-04-11 12:53:40 +00:00
|
|
|
URL parsed_url = URLParser::parse(""sv, base_url);
|
2022-12-27 19:17:30 +00:00
|
|
|
EXPECT_EQ(parsed_url.is_valid(), true);
|
|
|
|
EXPECT(base_url.equals(parsed_url));
|
|
|
|
}
|