diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-05-29 21:52:13 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-01 09:28:05 +0200 |
commit | c7857f35729ac898d4efcb3e50ccefce6f4db9bb (patch) | |
tree | d133829c80026554940d212dd9024cffdaf556e1 /Tests/AK | |
parent | ebf074ca2980316b6071e504bba524af2d55ae01 (diff) | |
download | serenity-c7857f35729ac898d4efcb3e50ccefce6f4db9bb.zip |
Tests: Add more tests for AK::URL
This adds more tests for AK::URL. Furthermore, this also changes some
tests to conform to what the reworked URL class does (and the URL
specification mostly expects).
Diffstat (limited to 'Tests/AK')
-rw-r--r-- | Tests/AK/TestURL.cpp | 223 |
1 files changed, 165 insertions, 58 deletions
diff --git a/Tests/AK/TestURL.cpp b/Tests/AK/TestURL.cpp index c92ee4fc5b..3fd1413f1d 100644 --- a/Tests/AK/TestURL.cpp +++ b/Tests/AK/TestURL.cpp @@ -18,37 +18,37 @@ TEST_CASE(basic) { URL url("http://www.serenityos.org"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "http"); + EXPECT_EQ(url.scheme(), "http"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 80); EXPECT_EQ(url.path(), "/"); - EXPECT_EQ(url.query(), ""); - EXPECT_EQ(url.fragment(), ""); + EXPECT(url.query().is_null()); + EXPECT(url.fragment().is_null()); } { URL url("https://www.serenityos.org/index.html"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "https"); + EXPECT_EQ(url.scheme(), "https"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 443); EXPECT_EQ(url.path(), "/index.html"); - EXPECT_EQ(url.query(), ""); - EXPECT_EQ(url.fragment(), ""); + EXPECT(url.query().is_null()); + EXPECT(url.fragment().is_null()); } { URL url("https://localhost:1234/~anon/test/page.html"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "https"); + EXPECT_EQ(url.scheme(), "https"); EXPECT_EQ(url.host(), "localhost"); EXPECT_EQ(url.port(), 1234); EXPECT_EQ(url.path(), "/~anon/test/page.html"); - EXPECT_EQ(url.query(), ""); - EXPECT_EQ(url.fragment(), ""); + EXPECT(url.query().is_null()); + EXPECT(url.fragment().is_null()); } { URL url("http://www.serenityos.org/index.html?#"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "http"); + EXPECT_EQ(url.scheme(), "http"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 80); EXPECT_EQ(url.path(), "/index.html"); @@ -58,27 +58,27 @@ TEST_CASE(basic) { URL url("http://www.serenityos.org/index.html?foo=1&bar=2"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "http"); + EXPECT_EQ(url.scheme(), "http"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 80); EXPECT_EQ(url.path(), "/index.html"); EXPECT_EQ(url.query(), "foo=1&bar=2"); - EXPECT_EQ(url.fragment(), ""); + EXPECT(url.fragment().is_null()); } { URL url("http://www.serenityos.org/index.html#fragment"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "http"); + EXPECT_EQ(url.scheme(), "http"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 80); EXPECT_EQ(url.path(), "/index.html"); - EXPECT_EQ(url.query(), ""); + EXPECT(url.query().is_null()); EXPECT_EQ(url.fragment(), "fragment"); } { URL url("http://www.serenityos.org/index.html?foo=1&bar=2&baz=/?#frag/ment?test#"); EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "http"); + EXPECT_EQ(url.scheme(), "http"); EXPECT_EQ(url.host(), "www.serenityos.org"); EXPECT_EQ(url.port(), 80); EXPECT_EQ(url.path(), "/index.html"); @@ -89,10 +89,7 @@ TEST_CASE(basic) TEST_CASE(some_bad_urls) { - EXPECT_EQ(URL("http:serenityos.org").is_valid(), false); - EXPECT_EQ(URL("http:/serenityos.org").is_valid(), false); EXPECT_EQ(URL("http//serenityos.org").is_valid(), false); - EXPECT_EQ(URL("http:///serenityos.org").is_valid(), false); EXPECT_EQ(URL("serenityos.org").is_valid(), false); EXPECT_EQ(URL("://serenityos.org").is_valid(), false); EXPECT_EQ(URL("://:80").is_valid(), false); @@ -101,90 +98,154 @@ TEST_CASE(some_bad_urls) EXPECT_EQ(URL("http://serenityos.org:abc").is_valid(), false); EXPECT_EQ(URL("http://serenityos.org:abc:80").is_valid(), false); EXPECT_EQ(URL("http://serenityos.org:abc:80/").is_valid(), false); - EXPECT_EQ(URL("http://serenityos.org:/abc/").is_valid(), false); EXPECT_EQ(URL("data:").is_valid(), false); - EXPECT_EQ(URL("file:").is_valid(), false); - EXPECT_EQ(URL("about:").is_valid(), false); } TEST_CASE(serialization) { - EXPECT_EQ(URL("http://www.serenityos.org/").to_string(), "http://www.serenityos.org/"); - EXPECT_EQ(URL("http://www.serenityos.org:0/").to_string(), "http://www.serenityos.org/"); - EXPECT_EQ(URL("http://www.serenityos.org:80/").to_string(), "http://www.serenityos.org/"); - EXPECT_EQ(URL("http://www.serenityos.org:81/").to_string(), "http://www.serenityos.org:81/"); - EXPECT_EQ(URL("https://www.serenityos.org:443/foo/bar.html?query#fragment").to_string(), "https://www.serenityos.org/foo/bar.html?query#fragment"); + EXPECT_EQ(URL("http://www.serenityos.org/").serialize(), "http://www.serenityos.org/"); + EXPECT_EQ(URL("http://www.serenityos.org:0/").serialize(), "http://www.serenityos.org/"); + EXPECT_EQ(URL("http://www.serenityos.org:80/").serialize(), "http://www.serenityos.org/"); + EXPECT_EQ(URL("http://www.serenityos.org:81/").serialize(), "http://www.serenityos.org:81/"); + EXPECT_EQ(URL("https://www.serenityos.org:443/foo/bar.html?query#fragment").serialize(), "https://www.serenityos.org/foo/bar.html?query#fragment"); } TEST_CASE(file_url_with_hostname) { + URL url("file://courage/my/file"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "file"); + EXPECT_EQ(url.host(), "courage"); + EXPECT_EQ(url.port(), 0); + 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) +{ URL url("file://localhost/my/file"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.host(), "localhost"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "file"); + EXPECT_EQ(url.host(), ""); EXPECT_EQ(url.path(), "/my/file"); - EXPECT_EQ(url.to_string(), "file://localhost/my/file"); + EXPECT_EQ(url.serialize(), "file:///my/file"); } TEST_CASE(file_url_without_hostname) { URL url("file:///my/file"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "file"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "file"); EXPECT_EQ(url.host(), ""); EXPECT_EQ(url.path(), "/my/file"); - EXPECT_EQ(url.to_string(), "file:///my/file"); + EXPECT_EQ(url.serialize(), "file:///my/file"); +} + +TEST_CASE(file_url_with_encoded_characters) +{ + URL url("file:///my/file/test%23file.txt"); + 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) +{ + URL url("file:///my/file#fragment"); + 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) +{ + URL url("file:///"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "file"); + EXPECT_EQ(url.path(), "/"); +} + +TEST_CASE(file_url_serialization) +{ + EXPECT_EQ(URL("file://courage/my/file").serialize(), "file://courage/my/file"); + EXPECT_EQ(URL("file://localhost/my/file").serialize(), "file:///my/file"); + EXPECT_EQ(URL("file:///my/file").serialize(), "file:///my/file"); + EXPECT_EQ(URL("file:///my/directory/").serialize(), "file:///my/directory/"); + EXPECT_EQ(URL("file:///my/file%23test").serialize(), "file:///my/file%23test"); + EXPECT_EQ(URL("file:///my/file#fragment").serialize(), "file:///my/file#fragment"); } TEST_CASE(about_url) { URL url("about:blank"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "about"); - EXPECT_EQ(url.host(), ""); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "about"); + EXPECT(url.host().is_null()); EXPECT_EQ(url.path(), "blank"); - EXPECT_EQ(url.to_string(), "about:blank"); + EXPECT(url.query().is_null()); + EXPECT(url.fragment().is_null()); + EXPECT_EQ(url.serialize(), "about:blank"); +} + +TEST_CASE(mailto_url) +{ + URL url("mailto:mail@example.com"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "mailto"); + EXPECT(url.host().is_null()); + EXPECT_EQ(url.port(), 0); + 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"); } TEST_CASE(data_url) { URL url("data:text/html,test"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "data"); - EXPECT_EQ(url.host(), ""); + 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"); - EXPECT_EQ(url.to_string(), "data:text/html,test"); + EXPECT_EQ(url.serialize(), "data:text/html,test"); } TEST_CASE(data_url_encoded) { URL url("data:text/html,Hello%20friends%2C%0X%X0"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "data"); - EXPECT_EQ(url.host(), ""); + 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(), "Hello friends,%0X%X0"); - // FIXME: Surely this should be URL-encoded again?! - EXPECT_EQ(url.to_string(), "data:text/html,Hello friends,%0X%X0"); + EXPECT_EQ(url.serialize(), "data:text/html,Hello friends,%0X%X0"); } TEST_CASE(data_url_base64_encoded) { URL url("data:text/html;base64,test"); - EXPECT_EQ(url.is_valid(), true); - EXPECT_EQ(url.protocol(), "data"); - EXPECT_EQ(url.host(), ""); + 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"); - EXPECT_EQ(url.to_string(), "data:text/html;base64,test"); + EXPECT_EQ(url.serialize(), "data:text/html;base64,test"); } TEST_CASE(trailing_slash_with_complete_url) { - EXPECT_EQ(URL("http://a/b/").complete_url("c/").to_string(), "http://a/b/c/"); - EXPECT_EQ(URL("http://a/b/").complete_url("c").to_string(), "http://a/b/c"); - EXPECT_EQ(URL("http://a/b").complete_url("c/").to_string(), "http://a/c/"); - EXPECT_EQ(URL("http://a/b").complete_url("c").to_string(), "http://a/c"); + EXPECT_EQ(URL("http://a/b/").complete_url("c/").serialize(), "http://a/b/c/"); + EXPECT_EQ(URL("http://a/b/").complete_url("c").serialize(), "http://a/b/c"); + EXPECT_EQ(URL("http://a/b").complete_url("c/").serialize(), "http://a/c/"); + EXPECT_EQ(URL("http://a/b").complete_url("c").serialize(), "http://a/c"); } TEST_CASE(trailing_port) @@ -193,10 +254,56 @@ TEST_CASE(trailing_port) EXPECT_EQ(url.port(), 8086); } -TEST_CASE(port_int_overflow_wrap) +TEST_CASE(port_overflow) +{ + EXPECT_EQ(URL("http://example.com:123456789/").is_valid(), false); +} + +TEST_CASE(equality) +{ + EXPECT(URL("http://serenityos.org").equals("http://serenityos.org#test", URL::ExcludeFragment::Yes)); + EXPECT_EQ(URL("http://example.com/index.html"), URL("http://ex%61mple.com/index.html")); + EXPECT_EQ(URL("file:///my/file"), URL("file://localhost/my/file")); + EXPECT_NE(URL("http://serenityos.org/index.html"), URL("http://serenityos.org/test.html")); +} + +TEST_CASE(create_with_file_scheme) { - auto expected_port = 80; - URL url(String::formatted("http://example.com:{}/", (u32)((65536 * 1000) + expected_port))); - EXPECT_EQ(url.port(), expected_port); - EXPECT_EQ(url.is_valid(), true); + auto url = URL::create_with_file_scheme("/home/anon/README.md"); + EXPECT(url.is_valid()); + EXPECT_EQ(url.scheme(), "file"); + EXPECT_EQ(url.port(), 0); + 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/"); + + url = URL("file:///home/anon/"); + EXPECT_EQ(url.path(), "/home/anon/"); +} + +TEST_CASE(complete_url) +{ + URL base_url("http://serenityos.org/index.html#fragment"); + URL url = base_url.complete_url("test.html"); + 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); + + EXPECT(base_url.complete_url("../index.html#fragment").equals(base_url)); } |