diff --git a/tests/unit/test_httpcommon.cpp b/tests/unit/test_httpcommon.cpp new file mode 100644 index 00000000000..0365c1dd5a4 --- /dev/null +++ b/tests/unit/test_httpcommon.cpp @@ -0,0 +1,53 @@ +/** + * @file tests/test_httpcommon.cpp + * @brief Test src/httpcommon.*. + */ +#include + +#include + +class HttpCommonTest : public ::testing::TestWithParam> {}; + +TEST_P(HttpCommonTest, UrlEscape) { + auto [input, expected] = GetParam(); + ASSERT_EQ(http::url_escape(input), expected); +} + +TEST_P(HttpCommonTest, UrlGetHost) { + auto [input, expected] = GetParam(); + ASSERT_EQ(http::url_get_host(input), expected); +} + +TEST_P(HttpCommonTest, DownloadFile) { + auto [url, filename] = GetParam(); + ASSERT_TRUE(http::download_file(url, filename)); +} + +INSTANTIATE_TEST_SUITE_P( + UrlEscapeTests, + HttpCommonTest, + ::testing::Values( + std::make_tuple("igdb_0123456789", "igdb_0123456789"), + std::make_tuple("../../../", "..%2F..%2F..%2F"), + std::make_tuple("..*\\", "..%2A%5C") + ) +); + +INSTANTIATE_TEST_SUITE_P( + UrlGetHostTests, + HttpCommonTest, + ::testing::Values( + std::make_tuple("https://images.igdb.com/example.txt", "images.igdb.com"), + std::make_tuple("http://localhost:8080", "localhost"), + std::make_tuple("nonsense!!}{::", "") + ) +); + +INSTANTIATE_TEST_SUITE_P( + DownloadFileTests, + HttpCommonTest, + ::testing::Values( + std::make_tuple("https://httpbin.org/base64/aGVsbG8h", "hello.txt"), + std::make_tuple("https://httpbin.org/redirect-to?url=/base64/aGVsbG8h", "hello-redirect.txt") + ) +);