1#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
10#include <nlohmann/json.hpp>
17 std::array<char, 128> buffer;
21 std::unique_ptr<FILE, std::function<void(FILE *)>> pipe(
22 popen(cmd.c_str(),
"r"), [](FILE *f) { pclose(f); });
25 throw std::runtime_error(
"popen() failed");
27 while (fgets(buffer.data(), buffer.size(), pipe.get()) !=
nullptr) {
28 result += buffer.data();
39 throw std::runtime_error(
"Failed to init curl");
44 curl, CURLOPT_WRITEFUNCTION,
45 +[](
char *ptr,
size_t size,
size_t nmemb,
void *userdata) ->
size_t {
46 std::string *resp =
static_cast<std::string *
>(userdata);
47 resp->append(ptr, size * nmemb);
55 throw std::runtime_error(
"CURL request failed: " +
64 for (
int i = 0; i < max_retries; ++i) {
66 std::string res =
http_get(
"http://localhost:1234/wycats");
69 std::this_thread::sleep_for(std::chrono::milliseconds(300));
76 int ret = system(
"pkill -f 'github_user_fetcher --server'");
78 std::cerr <<
"Warning: Failed to stop server process\n";
86 std::cout <<
"Output for default user fetch:\n" <<
output << std::endl;
89 CHECK(
output.find(
"Login: izelnakri") != std::string::npos);
90 CHECK(
output.find(
"Name: Izel Nakri") != std::string::npos);
91 CHECK(
output.find(
"Company: Ruby, JavaScript") != std::string::npos);
92 CHECK(
output.find(
"Location: Madrid") != std::string::npos);
98 std::cout <<
"Output for custom user fetch (wycats):\n"
101 CHECK(
output.find(
"Login: wycats") != std::string::npos);
106 std::thread server_thread([]() {
107 int ret = system(
"./github_user_fetcher --server");
109 std::cerr <<
"Server exited with code: " << ret << std::endl;
118 std::string url =
"http://localhost:1234/izelnakri";
119 std::string json_response =
http_get(url);
122 auto json = nlohmann::json::parse(json_response);
125 CHECK(json[
"login"] ==
"izelnakri");
126 CHECK(json[
"name"] ==
"Izel Nakri | izelnakri.eth");
127 CHECK(json[
"company"] ==
128 "Ruby, JavaScript, TS, elixir, rust, k8s, lua, nix, pkl, android");
129 CHECK(json[
"location"] ==
"Madrid | Amsterdam");
131 json_response =
http_get(
"http://localhost:1234/wycats");
132 json = nlohmann::json::parse(json_response);
135 CHECK(json[
"login"] ==
"wycats");
136 }
catch (
const std::exception &e) {
139 std::cerr <<
"Test failed: " << e.what() << std::endl;
141 MESSAGE(
"Test failed: ", e.what());
148 if (server_thread.joinable())
149 server_thread.detach();
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
CURL_EXTERN CURL * curl_easy_init(void)
static const char * output
std::string run_command(const std::string &cmd)
bool wait_for_server(int max_retries=10)
std::string http_get(const std::string &url)
#define curl_easy_setopt(handle, option, value)