#include #include #include #include #include #include #include #include #include namespace { // Minimal one-shot HTTP/1.1 server on a chosen loopback port. Accepts one // connection, reads the request (ignored beyond headers), and replies with // a fixed 200 body. Runs on a detached helper thread; the test joins it. struct OneShotServer { int fd = -1; std::uint16_t port = 0; std::thread worker; std::string last_request; std::atomic served{false}; explicit OneShotServer(std::string body) { fd = ::socket(AF_INET, SOCK_STREAM, 0); int yes = 1; ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = 0; // ephemeral ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); socklen_t len = sizeof(addr); ::getsockname(fd, reinterpret_cast(&addr), &len); port = ntohs(addr.sin_port); ::listen(fd, 1); worker = std::thread([this, body = std::move(body)] { int conn = ::accept(fd, nullptr, nullptr); if (conn < 0) return; char buf[4096]; const auto n = ::read(conn, buf, sizeof(buf)); if (n > 0) last_request.assign(buf, buf + n); const std::string resp = "HTTP/1.1 200 OK\r\nContent-Length: " + std::to_string(body.size()) + "\r\nConnection: close\r\n\r\n" + body; [[maybe_unused]] auto w = ::write(conn, resp.data(), resp.size()); ::close(conn); served = true; }); } ~OneShotServer() { if (worker.joinable()) worker.join(); if (fd >= 0) ::close(fd); } }; } // namespace Void::Testing::Test http_client_get_localhost_round_trip() { OneShotServer server{"hello from void"}; const std::string url = "http://127.0.0.1:" + std::to_string(server.port) + "/ping"; Void::Net::HttpClient client; const auto resp = client.get(url); Void::Testing::expect(resp.has_value()); Void::Testing::expect_eq(resp->status, static_cast(200)); Void::Testing::expect_eq(resp->body, std::string{"hello from void"}); Void::Testing::expect(resp->ok()); // Request line reached the server with the right target. Void::Testing::expect(server.last_request.find("GET /ping HTTP/1.1") != std::string::npos); } Void::Testing::Test http_client_post_sends_body() { OneShotServer server{"ok"}; const std::string url = "http://127.0.0.1:" + std::to_string(server.port) + "/submit"; Void::Net::HttpClient client; const auto resp = client.post(url, "payload=42"); Void::Testing::expect(resp.has_value()); Void::Testing::expect_eq(resp->status, static_cast(200)); Void::Testing::expect(server.last_request.find("POST /submit") != std::string::npos); Void::Testing::expect(server.last_request.find("payload=42") != std::string::npos); } Void::Testing::Test http_client_invalid_url_is_error() { Void::Net::HttpClient client; const auto resp = client.get("not a url"); Void::Testing::expect(!resp.has_value()); } Void::Testing::Test http_client_connection_refused_is_error() { Void::Net::HttpClient client; // Port 1 on loopback: nothing listening. const auto resp = client.get("http://127.0.0.1:1/"); Void::Testing::expect(!resp.has_value()); } Void::Testing::Test http_client_timeout_bounds_a_stalled_server() { // A server that accepts and then never responds. Without an enforced // Request::timeout the sync read blocks forever (Asio's sync emulation // poll()s on would-block with no deadline). struct StallServer { int fd = -1; std::uint16_t port = 0; int conn = -1; std::thread worker; StallServer() { fd = ::socket(AF_INET, SOCK_STREAM, 0); int yes = 1; ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); addr.sin_port = 0; ::bind(fd, reinterpret_cast(&addr), sizeof(addr)); socklen_t len = sizeof(addr); ::getsockname(fd, reinterpret_cast(&addr), &len); port = ntohs(addr.sin_port); ::listen(fd, 1); worker = std::thread([this] { conn = ::accept(fd, nullptr, nullptr); // Never write a byte; hold the connection open. }); } ~StallServer() { if (conn >= 0) ::close(conn); if (worker.joinable()) worker.join(); if (fd >= 0) ::close(fd); } }; StallServer server; Void::Net::HttpClient client; Void::Net::Request req; req.url = "http://127.0.0.1:" + std::to_string(server.port) + "/stall"; req.timeout = std::chrono::seconds{1}; const auto start = std::chrono::steady_clock::now(); const auto resp = client.send(req); const auto elapsed = std::chrono::steady_clock::now() - start; Void::Testing::expect(!resp.has_value()); // Well under the old "forever": the deadline tripped around 1s. Void::Testing::expect(elapsed < std::chrono::seconds{5}); }