#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include class MockRequestHandler : public Poco::Net::HTTPRequestHandler { Poco::Net::MessageHeader & last_request_header; public: explicit MockRequestHandler(Poco::Net::MessageHeader & last_request_header_) : last_request_header(last_request_header_) { } void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response) override { response.setStatus(Poco::Net::HTTPResponse::HTTP_OK); last_request_header = request; response.send(); } }; class HTTPRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { Poco::Net::MessageHeader & last_request_header; Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest &) override { return new MockRequestHandler(last_request_header); } public: explicit HTTPRequestHandlerFactory(Poco::Net::MessageHeader & last_request_header_) : last_request_header(last_request_header_) { } ~HTTPRequestHandlerFactory() override = default; }; class TestPocoHTTPServer { std::unique_ptr server_socket; Poco::SharedPtr handler_factory; Poco::AutoPtr server_params; std::unique_ptr server; // Stores the last request header handled. It's obviously not thread-safe to share the same // reference across request handlers, but it's good enough for this the purposes of this test. Poco::Net::MessageHeader last_request_header; public: TestPocoHTTPServer(): server_socket(std::make_unique(0)), handler_factory(new HTTPRequestHandlerFactory(last_request_header)), server_params(new Poco::Net::HTTPServerParams()), server(std::make_unique(handler_factory, *server_socket, server_params)) { server->start(); } std::string getUrl() { return "http://" + server_socket->address().toString(); } const Poco::Net::MessageHeader & getLastRequestHeader() const { return last_request_header; } };