【问题标题】:Wrapper for testing Poco HTTPServer handleRequest not compiling.用于测试 Poco HTTPServer handleRequest 的包装器未编译。
【发布时间】:2016-05-07 01:46:28
【问题描述】:

我正在尝试编写一个包装器来测试使用 Poco 实现的 http 服务器。服务器使用 HTTPRequestHandler 的子类实现,并实现了 handleRequest 方法。 handleRequest 方法接受两个参数 HTTPServerRequest 和 HTTPServerResponse。

handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)

这些都是抽象类。在我的包装器中,我需要直接调用 handleRequest 方法。像这样的:

req_handler->handleRequest(&req, &res);

我想要填充请求的方式是使用从 HTTPRequest 继承的 read() 方法,HTTPRequest 是 HTTPServerRequest 的父类。像这样:

HTTPServerRequest req = new HTTPServerRequest();
char* buffer //populate this from std::cin 
std::istringstream iss(buffer);
try {
      req->read(iss);
}

但问题是我不能直接为 HTTPServerRequest / HTTPServerResponse 调用“新”,因为它们是抽象类。我尝试使用 HTTPServerRequestImpl 但它给出了纯虚函数未实现的编译错误,并且 HTTPServerRequestImpl 也没有空构造函数。我试过这个:

HTTPServerRequest req  = new HTTPServerRequestImpl()

这也会产生预期的编译器错误。那么编写这样一个包装器的正确方法是什么?在 Poco 中编写服务器时如何理想地完成。 ?我只想要一个简约的包装器,它使用从标准输入填充的请求调用 handleRequest。

【问题讨论】:

    标签: c++ poco poco-libraries


    【解决方案1】:

    不需要直接使用 HTTPServerRequest。使用HTTPClientSessionHTTPRequest

    HTTPClientSession s("host", port);
    HTTPRequest request(HTTPRequest::HTTP_POST, "/");
    std::string body("hello world\r\n0\r\n");
    request.setContentLength((int) body.length());
    s.sendRequest(request) << body;
    HTTPResponse response;
    std::istream& rs = s.receiveResponse(response);
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多