147 lines
3.8 KiB
C++
147 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <boost/asio.hpp>
|
|
#include <boost/beast.hpp>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
namespace Echoion::Test
|
|
{
|
|
|
|
// Tiny synchronous HTTP/1.1 server for source-router tests. Register
|
|
// canned responses per path (query string included); unknown paths get
|
|
// 404. Runs its accept loop on one background thread and handles one
|
|
// request per connection, which is all the NetworkClient needs.
|
|
class StubHttpServer
|
|
{
|
|
public:
|
|
|
|
struct Response {
|
|
int status = 200;
|
|
std::string contentType = "application/json";
|
|
std::string body;
|
|
};
|
|
|
|
StubHttpServer() :
|
|
acceptor_(io_, {boost::asio::ip::make_address("127.0.0.1"), 0})
|
|
{
|
|
port_ = acceptor_.local_endpoint().port();
|
|
thread_ = std::thread([this] { run(); });
|
|
}
|
|
|
|
~StubHttpServer()
|
|
{
|
|
stopping_ = true;
|
|
// A synchronous accept(2) is NOT interrupted by closing the
|
|
// fd from another thread on Linux; poke it with a throwaway
|
|
// connection so the loop observes stopping_.
|
|
try {
|
|
boost::asio::io_context io;
|
|
boost::asio::ip::tcp::socket s(io);
|
|
s.connect({boost::asio::ip::make_address("127.0.0.1"),
|
|
port_});
|
|
} catch (...) {
|
|
// Server may already be gone; nothing to unblock.
|
|
}
|
|
boost::system::error_code ec;
|
|
acceptor_.close(ec);
|
|
if (thread_.joinable()) {
|
|
thread_.join();
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] uint16_t port() const { return port_; }
|
|
|
|
[[nodiscard]] std::string baseUrl() const
|
|
{
|
|
return "http://127.0.0.1:" + std::to_string(port_);
|
|
}
|
|
|
|
// Exact-match route ("/api/v1/stats") or prefix route when the
|
|
// registered path ends with '*' ("/api/v1/videos/*").
|
|
void route(std::string path, Response response)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
routes_[std::move(path)] = std::move(response);
|
|
}
|
|
|
|
[[nodiscard]] std::size_t requestCount() const
|
|
{
|
|
return requestCount_.load();
|
|
}
|
|
|
|
private:
|
|
|
|
void run()
|
|
{
|
|
while (!stopping_) {
|
|
boost::system::error_code ec;
|
|
boost::asio::ip::tcp::socket socket(io_);
|
|
acceptor_.accept(socket, ec);
|
|
if (ec) {
|
|
break; // closed by destructor
|
|
}
|
|
handle(socket);
|
|
}
|
|
}
|
|
|
|
void handle(boost::asio::ip::tcp::socket& socket)
|
|
{
|
|
namespace http = boost::beast::http;
|
|
try {
|
|
boost::beast::flat_buffer buffer;
|
|
http::request<http::string_body> req;
|
|
http::read(socket, buffer, req);
|
|
++requestCount_;
|
|
|
|
const Response canned = lookup(std::string(req.target()));
|
|
|
|
http::response<http::string_body> res{
|
|
static_cast<http::status>(canned.status),
|
|
req.version()};
|
|
res.set(http::field::content_type, canned.contentType);
|
|
res.body() = canned.body;
|
|
res.prepare_payload();
|
|
http::write(socket, res);
|
|
|
|
boost::system::error_code ec;
|
|
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send,
|
|
ec);
|
|
} catch (const std::exception&) {
|
|
// Connection torn down mid-request; irrelevant for tests.
|
|
}
|
|
}
|
|
|
|
Response lookup(const std::string& target)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
if (auto it = routes_.find(target); it != routes_.end()) {
|
|
return it->second;
|
|
}
|
|
for (const auto& [path, response] : routes_) {
|
|
if (path.ends_with('*') &&
|
|
target.starts_with(
|
|
std::string_view(path).substr(0, path.size() - 1))) {
|
|
return response;
|
|
}
|
|
}
|
|
return Response{.status = 404,
|
|
.contentType = "text/plain",
|
|
.body = "not found"};
|
|
}
|
|
|
|
boost::asio::io_context io_;
|
|
boost::asio::ip::tcp::acceptor acceptor_;
|
|
uint16_t port_ = 0;
|
|
std::thread thread_;
|
|
std::atomic<bool> stopping_{false};
|
|
std::atomic<std::size_t> requestCount_{0};
|
|
std::mutex mutex_;
|
|
std::map<std::string, Response> routes_;
|
|
};
|
|
|
|
} // namespace Echoion::Test
|