mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-12-03 02:24:22 +00:00
111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
/**
|
|
* @file tests/unit/test_httpcommon.cpp
|
|
* @brief Test src/httpcommon.*.
|
|
*/
|
|
// test includes
|
|
#include "../tests_common.h"
|
|
#include "../tests_log_checker.h"
|
|
|
|
// standard includes
|
|
#include <format>
|
|
#include <memory>
|
|
#include <thread>
|
|
|
|
// lib includes
|
|
#include <curl/curl.h>
|
|
#include <Simple-Web-Server/server_http.hpp>
|
|
|
|
// local includes
|
|
#include <src/httpcommon.h>
|
|
#include <src/utility.h>
|
|
|
|
struct UrlEscapeTest: BaseTest, testing::WithParamInterface<std::tuple<std::string, std::string>> {};
|
|
|
|
TEST_P(UrlEscapeTest, Run) {
|
|
const 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")
|
|
)
|
|
);
|
|
|
|
struct UrlGetHostTest: BaseTest, testing::WithParamInterface<std::tuple<std::string, std::string>> {};
|
|
|
|
TEST_P(UrlGetHostTest, Run) {
|
|
const 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!!}{::", "")
|
|
)
|
|
);
|
|
|
|
struct DownloadFileTest: BaseTest, testing::WithParamInterface<std::tuple<std::string, std::string>> {};
|
|
|
|
TEST_P(DownloadFileTest, Run) {
|
|
const auto &[url, filename] = GetParam();
|
|
const std::string test_dir = platf::appdata().string() + "/tests/";
|
|
std::string path = test_dir + filename;
|
|
ASSERT_TRUE(http::download_file(url, path, CURL_SSLVERSION_TLSv1_0));
|
|
}
|
|
|
|
#ifdef SUNSHINE_BUILD_FLATPAK
|
|
// requires running `npm run serve` prior to running the tests
|
|
constexpr const char *URL_1 = "http://0.0.0.0:3000/hello.txt";
|
|
constexpr const char *URL_2 = "http://0.0.0.0:3000/hello-redirect.txt";
|
|
#else
|
|
constexpr const char *URL_1 = "https://httpbin.org/base64/aGVsbG8h";
|
|
constexpr const char *URL_2 = "https://httpbin.org/redirect-to?url=/base64/aGVsbG8h";
|
|
#endif
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
DownloadFileTests,
|
|
DownloadFileTest,
|
|
testing::Values(
|
|
std::make_tuple(URL_1, "hello.txt"),
|
|
std::make_tuple(URL_2, "hello-redirect.txt")
|
|
)
|
|
);
|
|
|
|
TEST(DownloadFileErrorTest, FailsOnHttpErrorStatus) {
|
|
SimpleWeb::Server<SimpleWeb::HTTP> server;
|
|
server.io_service = std::make_shared<SimpleWeb::io_context>();
|
|
server.config.address = "127.0.0.1";
|
|
server.config.port = 0;
|
|
server.resource["^/missing$"]["GET"] = [](const auto &response, const auto &) {
|
|
response->write(SimpleWeb::StatusCode::client_error_not_found, "Could not find image");
|
|
};
|
|
|
|
unsigned short port = 0;
|
|
server.start([&port](unsigned short assigned_port) {
|
|
port = assigned_port;
|
|
});
|
|
server.io_service->poll();
|
|
ASSERT_NE(port, 0);
|
|
std::jthread server_thread([&server] {
|
|
server.io_service->run();
|
|
});
|
|
auto stop_server = util::fail_guard([&server] {
|
|
server.io_service->stop();
|
|
});
|
|
|
|
const auto url = std::format("http://127.0.0.1:{}/missing", port); // NOSONAR(cpp:S5332): Loopback HTTP/1.1 fixture.
|
|
const auto path = platf::appdata().string() + "/tests/missing.txt";
|
|
EXPECT_FALSE(http::download_file(url, path, CURL_SSLVERSION_TLSv1_0));
|
|
const auto expected = std::format("Couldn't download [{}, code:{}]", url, static_cast<int>(CURLE_HTTP_RETURNED_ERROR));
|
|
EXPECT_TRUE(log_checker::line_contains("test_sunshine.log", expected));
|
|
}
|