From 00b370988953ecb356efcc8aff4c460751333c2d Mon Sep 17 00:00:00 2001 From: Pablo Andres Fuente Date: Thu, 15 Aug 2024 17:44:49 -0300 Subject: [PATCH] Add unit tests for String::parse_url() --- tests/core/string/test_string.h | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 301771a3de1..99f66f0221b 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -1988,6 +1988,48 @@ TEST_CASE("[String] Variant ptr indexed set") { CHECK_EQ(s, String("azcd")); } +TEST_CASE("[String][URL] Parse URL") { +#define CHECK_URL(m_url_to_parse, m_expected_schema, m_expected_host, m_expected_port, m_expected_path, m_expected_error) \ + do { \ + int port; \ + String url(m_url_to_parse), schema, host, path; \ + \ + CHECK_EQ(m_expected_error, url.parse_url(schema, host, port, path)); \ + CHECK_EQ(m_expected_schema, schema); \ + CHECK_EQ(m_expected_host, host); \ + CHECK_EQ(m_expected_path, path); \ + CHECK_EQ(m_expected_port, port); \ + } while (false) + + // Valid URLs. + CHECK_URL("https://godotengine.org", "https://", "godotengine.org", 0, "", Error::OK); + CHECK_URL("https://godotengine.org/", "https://", "godotengine.org", 0, "/", Error::OK); + CHECK_URL("godotengine.org/", "", "godotengine.org", 0, "/", Error::OK); + CHECK_URL("HTTPS://godotengine.org/", "https://", "godotengine.org", 0, "/", Error::OK); + CHECK_URL("https://GODOTENGINE.ORG/", "https://", "godotengine.org", 0, "/", Error::OK); + CHECK_URL("http://godotengine.org", "http://", "godotengine.org", 0, "", Error::OK); + CHECK_URL("https://godotengine.org:8080", "https://", "godotengine.org", 8080, "", Error::OK); + CHECK_URL("https://godotengine.org/blog", "https://", "godotengine.org", 0, "/blog", Error::OK); + CHECK_URL("https://godotengine.org/blog/", "https://", "godotengine.org", 0, "/blog/", Error::OK); + CHECK_URL("https://godotengine.org/download/macos", "https://", "godotengine.org", 0, "/download/macos", Error::OK); + CHECK_URL("https://me:secret@godotengine.org", "https://", "godotengine.org", 0, "", Error::OK); + CHECK_URL("https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/ipv6", "https://", "fedc:ba98:7654:3210:fedc:ba98:7654:3210", 0, "/ipv6", Error::OK); + + // Invalid URLs. + + // Multiple ports. + CHECK_URL("https://godotengine.org:8080:433", "https://", "", 0, "", Error::ERR_INVALID_PARAMETER); + // Missing ] on literal IPv6. + CHECK_URL("https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/ipv6", "https://", "", 0, "/ipv6", Error::ERR_INVALID_PARAMETER); + // Missing host. + CHECK_URL("https:///blog", "https://", "", 0, "/blog", Error::ERR_INVALID_PARAMETER); + // Invalid ports. + CHECK_URL("https://godotengine.org:notaport", "https://", "godotengine.org", 0, "", Error::ERR_INVALID_PARAMETER); + CHECK_URL("https://godotengine.org:-8080", "https://", "godotengine.org", -8080, "", Error::ERR_INVALID_PARAMETER); + CHECK_URL("https://godotengine.org:88888", "https://", "godotengine.org", 88888, "", Error::ERR_INVALID_PARAMETER); +#undef CHECK_URL +} + TEST_CASE("[Stress][String] Empty via ' == String()'") { for (int i = 0; i < 100000; ++i) { String str = "Hello World!";