Repos
C++
1.6 KB61 lines Opens in site
Directory
Raw
Download
#pragma once
// Spec: SPEC.md NFR-TEST-01. A real aiohttpcpp web server on 127.0.0.1 with a random port, running on its own loop
// thread. One catch-all route per method records each request and answers with the Reply the test handler returns.
#include <chrono>
#include <condition_variable>
#include <memory>
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
namespace dp::test {
struct SeenRequest {
std::string method;
std::string path;
std::string query;
std::string authorization;
std::string accept;
std::string contentType;
std::string body;
};
struct Reply {
int status = 200;
std::string body;
std::string contentType = "application/json";
double delaySeconds = 0.0;
};
class TestServer {
public:
using Handler = std::function<Reply(const SeenRequest&)>;
explicit TestServer(Handler handler);
~TestServer();
TestServer(const TestServer&) = delete;
TestServer& operator=(const TestServer&) = delete;
int port() const { return port_; }
std::string baseUrl() const { return "http://127.0.0.1:" + std::to_string(port_) + "/"; }
std::vector<SeenRequest> seen() const;
void clearSeen();
SeenRequest last() const;
// Websocket route GET /messages/ws: sends `greeting` frames after the handshake, then (optionally) closes at once.
void setWsGreeting(std::vector<std::string> frames, bool closeAfterGreeting);
int wsConnections() const;
std::vector<std::string> wsReceived() const;
std::string wsLastQuery() const;
struct Impl;
private:
std::unique_ptr<Impl> impl_;
int port_ = 0;
};
} // namespace dp::test