mirror of
https://github.com/LizardByte/Sunshine.git
synced 2025-08-10 00:52:16 +00:00
tests: add httpcommon tests and add new file_handler methods (#2712)
Co-authored-by: Mariotaku <mariotaku.lee@gmail.com>
This commit is contained in:
@@ -6,6 +6,47 @@
|
||||
|
||||
#include <tests/conftest.cpp>
|
||||
|
||||
class FileHandlerParentDirectoryTest: public ::testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||
|
||||
TEST_P(FileHandlerParentDirectoryTest, Run) {
|
||||
auto [input, expected] = GetParam();
|
||||
EXPECT_EQ(file_handler::get_parent_directory(input), expected);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
FileHandlerTests,
|
||||
FileHandlerParentDirectoryTest,
|
||||
::testing::Values(
|
||||
std::make_tuple("/path/to/file.txt", "/path/to"),
|
||||
std::make_tuple("/path/to/directory", "/path/to"),
|
||||
std::make_tuple("/path/to/directory/", "/path/to")));
|
||||
|
||||
class FileHandlerMakeDirectoryTest: public ::testing::TestWithParam<std::tuple<std::string, bool, bool>> {};
|
||||
|
||||
TEST_P(FileHandlerMakeDirectoryTest, Run) {
|
||||
auto [input, expected, remove] = GetParam();
|
||||
const std::string test_dir = platf::appdata().string() + "/tests/path/";
|
||||
input = test_dir + input;
|
||||
|
||||
EXPECT_EQ(file_handler::make_directory(input), expected);
|
||||
EXPECT_TRUE(std::filesystem::exists(input));
|
||||
|
||||
// remove test directory
|
||||
if (remove) {
|
||||
std::filesystem::remove_all(test_dir);
|
||||
EXPECT_FALSE(std::filesystem::exists(test_dir));
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
FileHandlerTests,
|
||||
FileHandlerMakeDirectoryTest,
|
||||
::testing::Values(
|
||||
std::make_tuple("dir_123", true, false),
|
||||
std::make_tuple("dir_123", true, true),
|
||||
std::make_tuple("dir_123/abc", true, false),
|
||||
std::make_tuple("dir_123/abc", true, true)));
|
||||
|
||||
class FileHandlerTests: public virtual BaseTest, public ::testing::WithParamInterface<std::tuple<int, std::string>> {
|
||||
protected:
|
||||
void
|
||||
|
||||
53
tests/unit/test_httpcommon.cpp
Normal file
53
tests/unit/test_httpcommon.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @file tests/test_httpcommon.cpp
|
||||
* @brief Test src/httpcommon.*.
|
||||
*/
|
||||
#include <src/httpcommon.h>
|
||||
|
||||
#include <tests/conftest.cpp>
|
||||
|
||||
class UrlEscapeTest: public ::testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||
|
||||
TEST_P(UrlEscapeTest, Run) {
|
||||
auto [input, expected] = GetParam();
|
||||
ASSERT_EQ(http::url_escape(input), expected);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
UrlEscapeTests,
|
||||
UrlEscapeTest,
|
||||
::testing::Values(
|
||||
std::make_tuple("igdb_0123456789", "igdb_0123456789"),
|
||||
std::make_tuple("../../../", "..%2F..%2F..%2F"),
|
||||
std::make_tuple("..*\\", "..%2A%5C")));
|
||||
|
||||
class UrlGetHostTest: public ::testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||
|
||||
TEST_P(UrlGetHostTest, Run) {
|
||||
auto [input, expected] = GetParam();
|
||||
ASSERT_EQ(http::url_get_host(input), expected);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
UrlGetHostTests,
|
||||
UrlGetHostTest,
|
||||
::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!!}{::", "")));
|
||||
|
||||
class DownloadFileTest: public ::testing::TestWithParam<std::tuple<std::string, std::string>> {};
|
||||
|
||||
TEST_P(DownloadFileTest, Run) {
|
||||
auto [url, filename] = GetParam();
|
||||
const std::string test_dir = platf::appdata().string() + "/tests/";
|
||||
std::basic_string path = test_dir + filename;
|
||||
ASSERT_TRUE(http::download_file(url, path));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
DownloadFileTests,
|
||||
DownloadFileTest,
|
||||
::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")));
|
||||
Reference in New Issue
Block a user