|
| 1 | +#include "HttpClient.h" |
| 2 | +#include <catch2/catch_test_macros.hpp> |
| 3 | +#include <httplib.h> |
| 4 | +#include <uvw/loop.h> |
| 5 | +#include <thread> |
| 6 | + |
| 7 | +using namespace visor::http; |
| 8 | + |
| 9 | +// Spin a throwaway httplib server on an ephemeral port; returns port. |
| 10 | +static int start_test_server(httplib::Server &svr, std::thread &t) |
| 11 | +{ |
| 12 | + svr.Get("/ok", [](const httplib::Request &, httplib::Response &res) { res.set_content("hi", "text/plain"); }); |
| 13 | + svr.Get("/notfound", [](const httplib::Request &, httplib::Response &res) { res.status = 404; }); |
| 14 | + svr.Get("/error", [](const httplib::Request &, httplib::Response &res) { res.status = 500; }); |
| 15 | + svr.Get("/slow", [](const httplib::Request &, httplib::Response &res) { |
| 16 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 17 | + res.set_content("late", "text/plain"); |
| 18 | + }); |
| 19 | + svr.Post("/echo", [](const httplib::Request &req, httplib::Response &res) { |
| 20 | + res.set_content(req.body, "application/octet-stream"); |
| 21 | + }); |
| 22 | + int port = svr.bind_to_any_port("127.0.0.1"); |
| 23 | + REQUIRE(port > 0); |
| 24 | + t = std::thread([&svr] { svr.listen_after_bind(); }); |
| 25 | + // Wait until the server is actually accepting connections before the test |
| 26 | + // connects — avoids a connect-before-listen race on slow/loaded CI machines. |
| 27 | + svr.wait_until_ready(); |
| 28 | + return port; |
| 29 | +} |
| 30 | + |
| 31 | +// Arm a watchdog timer that calls loop->stop() after timeout_ms. |
| 32 | +// The handle is unreferenced so loop->run() returns as soon as the real work |
| 33 | +// handles close — the watchdog fires only if the loop would otherwise stall |
| 34 | +// forever (hang-guard). The caller must close the returned handle after |
| 35 | +// loop->run() completes. |
| 36 | +static std::shared_ptr<uvw::timer_handle> arm_watchdog(std::shared_ptr<uvw::loop> loop, uint64_t timeout_ms) |
| 37 | +{ |
| 38 | + auto wd = loop->resource<uvw::timer_handle>(); |
| 39 | + wd->on<uvw::timer_event>([loop](const auto &, auto &) { loop->stop(); }); |
| 40 | + wd->start(uvw::timer_handle::time{timeout_ms}, uvw::timer_handle::time{0}); |
| 41 | + // Unreference: the watchdog must not prevent the loop from exiting when all |
| 42 | + // real work handles (curl poll + timer) have closed. uv_unref makes the |
| 43 | + // handle "idle" w.r.t. loop liveness — the loop exits when only unreferenced |
| 44 | + // handles remain active, then we close the watchdog in disarm_watchdog. |
| 45 | + wd->unreference(); |
| 46 | + return wd; |
| 47 | +} |
| 48 | + |
| 49 | +// Disarm the watchdog and drain its close on the loop. |
| 50 | +static void disarm_watchdog(std::shared_ptr<uvw::loop> loop, std::shared_ptr<uvw::timer_handle> wd) |
| 51 | +{ |
| 52 | + if (wd && !wd->closing()) { |
| 53 | + wd->stop(); |
| 54 | + wd->close(); |
| 55 | + loop->run(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +TEST_CASE("HttpClient basic results", "[http][client]") |
| 60 | +{ |
| 61 | + httplib::Server svr; |
| 62 | + std::thread server_thread; |
| 63 | + int port = start_test_server(svr, server_thread); |
| 64 | + |
| 65 | + auto loop = uvw::loop::create(); |
| 66 | + HttpClient client(loop); |
| 67 | + std::string base = "http://127.0.0.1:" + std::to_string(port); |
| 68 | + |
| 69 | + std::vector<HttpResult> results; |
| 70 | + auto on_done = [&](const HttpResult &r) { results.push_back(r); }; |
| 71 | + // Named init (not positional aggregate) so adding HttpRequest fields doesn't warn. |
| 72 | + auto get_req = [](std::string url, uint64_t timeout_ms) { |
| 73 | + HttpRequest r; |
| 74 | + r.url = std::move(url); |
| 75 | + r.timeout_ms = timeout_ms; |
| 76 | + return r; |
| 77 | + }; |
| 78 | + |
| 79 | + SECTION("200 OK") |
| 80 | + { |
| 81 | + client.request(get_req(base + "/ok", 2000), on_done); |
| 82 | + auto wd = arm_watchdog(loop, 5000); |
| 83 | + loop->run(); |
| 84 | + disarm_watchdog(loop, wd); |
| 85 | + REQUIRE(results.size() == 1); |
| 86 | + CHECK(results[0].transport_ok); |
| 87 | + CHECK(results[0].status_code == 200); |
| 88 | + const auto &t = results[0].timings; |
| 89 | + CHECK(t.total_us > 0); |
| 90 | + // Per-phase delta sanity (plain HTTP, no TLS): the TLS phase must be 0, and every |
| 91 | + // phase delta must stay within the total — guards the delta arithmetic against a |
| 92 | + // wrong base or an unsigned underflow producing a huge bogus value. |
| 93 | + CHECK(t.tls_us == 0); |
| 94 | + CHECK(t.dns_us <= t.total_us); |
| 95 | + CHECK(t.connect_us <= t.total_us); |
| 96 | + CHECK(t.ttfb_us <= t.total_us); |
| 97 | + } |
| 98 | + SECTION("404") |
| 99 | + { |
| 100 | + client.request(get_req(base + "/notfound", 2000), on_done); |
| 101 | + auto wd = arm_watchdog(loop, 5000); |
| 102 | + loop->run(); |
| 103 | + disarm_watchdog(loop, wd); |
| 104 | + REQUIRE(results.size() == 1); |
| 105 | + CHECK(results[0].transport_ok); |
| 106 | + CHECK(results[0].status_code == 404); |
| 107 | + } |
| 108 | + SECTION("connection refused") |
| 109 | + { |
| 110 | + client.request(get_req("http://127.0.0.1:1/x", 2000), on_done); |
| 111 | + auto wd = arm_watchdog(loop, 5000); |
| 112 | + loop->run(); |
| 113 | + disarm_watchdog(loop, wd); |
| 114 | + REQUIRE(results.size() == 1); |
| 115 | + CHECK_FALSE(results[0].transport_ok); |
| 116 | + } |
| 117 | + SECTION("timeout") |
| 118 | + { |
| 119 | + client.request(get_req(base + "/slow", 100), on_done); |
| 120 | + auto wd = arm_watchdog(loop, 5000); |
| 121 | + loop->run(); |
| 122 | + disarm_watchdog(loop, wd); |
| 123 | + REQUIRE(results.size() == 1); |
| 124 | + CHECK_FALSE(results[0].transport_ok); |
| 125 | + CHECK(results[0].curl_code == CURLE_OPERATION_TIMEDOUT); |
| 126 | + } |
| 127 | + SECTION("close while in flight") |
| 128 | + { |
| 129 | + // Issue the slow request but then immediately close() before loop->run() completes it. |
| 130 | + // The process must not crash and results must stay empty (interrupted = no metric recorded). |
| 131 | + client.request(get_req(base + "/slow", 5000), on_done); |
| 132 | + client.close(); |
| 133 | + auto wd = arm_watchdog(loop, 3000); |
| 134 | + loop->run(); |
| 135 | + disarm_watchdog(loop, wd); |
| 136 | + CHECK(results.empty()); |
| 137 | + } |
| 138 | + |
| 139 | + // close() is idempotent — this is a no-op for the "close while in flight" section. |
| 140 | + client.close(); |
| 141 | + loop->run(); // drain handle closes |
| 142 | + svr.stop(); |
| 143 | + if (server_thread.joinable()) server_thread.join(); |
| 144 | +} |
| 145 | + |
| 146 | +TEST_CASE("HttpClient POST body + headers + response capture", "[http][client]") |
| 147 | +{ |
| 148 | + httplib::Server svr; |
| 149 | + std::thread server_thread; |
| 150 | + int port = start_test_server(svr, server_thread); |
| 151 | + auto loop = uvw::loop::create(); |
| 152 | + HttpClient client(loop); |
| 153 | + std::string base = "http://127.0.0.1:" + std::to_string(port); |
| 154 | + |
| 155 | + std::vector<HttpResult> results; |
| 156 | + auto on_done = [&](const HttpResult &r) { results.push_back(r); }; |
| 157 | + |
| 158 | + SECTION("POST echoes body when capture_response") |
| 159 | + { |
| 160 | + HttpRequest req; |
| 161 | + req.url = base + "/echo"; |
| 162 | + req.method = "POST"; |
| 163 | + req.body = std::string("\x00\x01hello", 7); // binary-safe |
| 164 | + req.headers = {"Content-Type: application/octet-stream"}; |
| 165 | + req.capture_response = true; |
| 166 | + req.timeout_ms = 2000; |
| 167 | + client.request(req, on_done); |
| 168 | + auto wd = arm_watchdog(loop, 5000); |
| 169 | + loop->run(); |
| 170 | + disarm_watchdog(loop, wd); |
| 171 | + REQUIRE(results.size() == 1); |
| 172 | + CHECK(results[0].transport_ok); |
| 173 | + CHECK(results[0].status_code == 200); |
| 174 | + CHECK(results[0].response_body == std::string("\x00\x01hello", 7)); |
| 175 | + } |
| 176 | + SECTION("body NOT captured by default") |
| 177 | + { |
| 178 | + HttpRequest req; |
| 179 | + req.url = base + "/ok"; |
| 180 | + req.capture_response = false; |
| 181 | + req.timeout_ms = 2000; |
| 182 | + client.request(req, on_done); |
| 183 | + auto wd = arm_watchdog(loop, 5000); |
| 184 | + loop->run(); |
| 185 | + disarm_watchdog(loop, wd); |
| 186 | + REQUIRE(results.size() == 1); |
| 187 | + CHECK(results[0].response_body.empty()); |
| 188 | + } |
| 189 | + |
| 190 | + client.close(); |
| 191 | + loop->run(); |
| 192 | + svr.stop(); |
| 193 | + if (server_thread.joinable()) server_thread.join(); |
| 194 | +} |
| 195 | + |
| 196 | +TEST_CASE("HttpClient request() issued from within a completion callback is safe", "[http][client]") |
| 197 | +{ |
| 198 | + httplib::Server svr; |
| 199 | + std::thread server_thread; |
| 200 | + int port = start_test_server(svr, server_thread); |
| 201 | + |
| 202 | + auto loop = uvw::loop::create(); |
| 203 | + HttpClient client(loop); |
| 204 | + std::string base = "http://127.0.0.1:" + std::to_string(port); |
| 205 | + |
| 206 | + std::vector<long> statuses; |
| 207 | + // In the first request's completion callback, issue a SECOND request (reentrant). The |
| 208 | + // _processing guard must keep this safe and let both complete. |
| 209 | + auto second = [&](const HttpResult &r) { statuses.push_back(r.status_code); }; |
| 210 | + auto first = [&](const HttpResult &r) { |
| 211 | + statuses.push_back(r.status_code); |
| 212 | + HttpRequest r2; |
| 213 | + r2.url = base + "/notfound"; |
| 214 | + r2.timeout_ms = 2000; |
| 215 | + client.request(r2, second); // reentrant: called from inside check_multi_info()'s callback fire |
| 216 | + }; |
| 217 | + HttpRequest r1; |
| 218 | + r1.url = base + "/ok"; |
| 219 | + r1.timeout_ms = 2000; |
| 220 | + client.request(r1, first); |
| 221 | + |
| 222 | + auto wd = arm_watchdog(loop, 5000); |
| 223 | + loop->run(); |
| 224 | + disarm_watchdog(loop, wd); |
| 225 | + |
| 226 | + REQUIRE(statuses.size() == 2); |
| 227 | + CHECK(statuses[0] == 200); |
| 228 | + CHECK(statuses[1] == 404); |
| 229 | + |
| 230 | + client.close(); |
| 231 | + loop->run(); |
| 232 | + svr.stop(); |
| 233 | + if (server_thread.joinable()) server_thread.join(); |
| 234 | +} |
0 commit comments