Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为网络客户端编写单元测试?_C++_Unit Testing_C++11_Googletest_Gmock - Fatal编程技术网

C++ 如何为网络客户端编写单元测试?

C++ 如何为网络客户端编写单元测试?,c++,unit-testing,c++11,googletest,gmock,C++,Unit Testing,C++11,Googletest,Gmock,我需要编写简单的http客户端。为我的班级进行单元测试可能会很棒。但我不知道如何编写适当的可测试类 例如,我有这样一个客户: class HTTPClient { public: HTTPCLient(const std::string& host, const std::string& port): session(host, port) {} void send() { session.sendRequest(someRequ

我需要编写简单的http客户端。为我的班级进行单元测试可能会很棒。但我不知道如何编写适当的可测试类

例如,我有这样一个客户:

class HTTPClient
{
public:
     HTTPCLient(const std::string& host, const std::string& port): session(host, port) {}

     void send()
     {
         session.sendRequest(someRequest);
         Response response = session.receiveResponse();
         // ...
     }

private:
    SomeLibrary::ClientSession session;
};
如何测试
send
方法(我真的发送了我想要的东西)?我不能嘲笑它。我可以在构造函数中编写
HTTPClient
接收
SomeLibrary::ClientSession
对象(在测试中,我会通过mock),但它的设计好吗?我认为会话等的实现方式应该隐藏在我的课堂中


你知道吗?

前几天我碰巧写了一个HTTP客户端库

为了测试HTTP客户端库,我只编写了简单的测试代码,启动了一个
std::thread
localhost
上的某个随机端口上侦听。然后我告诉客户机使用
主机
端口
参数发出测试请求,就像在您的例子中一样,指向我的线程正在侦听的端口。线程的代码被编程为接受一个连接,读取一个HTTP请求,保存它,然后用一个固定的HTTP响应进行响应

这就是我测试客户端库的方式,验证客户端发送的实际请求,以及客户端如何处理固定的HTTP响应。后来,我开发了这个单元测试代码来发送各种HTTP错误和格式错误的HTTP响应,以便测试和验证客户机代码如何处理这些情况

而且,作为一个很好的衡量标准,整个过程都是由一个
alarm()
调用来保护的,因此,如果某个东西陷入无限循环中,整个过程最终都会自杀


同样,这也是测试自己代码的方法。

在构造函数中插入抽象客户端会话实例。在单元测试中模拟它,并在实际运行时传递一个实际实例

你说你不能在一句话中嘲弄它,而在下一句话中你说你可以-你是什么意思?如果您的意思是会话类不是“您的”,或者它不能通过这种方式被模拟,那么您是否尝试将它包装到您的类中,以便可以模拟它

另外,你说“我认为会话等的实现方式应该隐藏在我的课堂中。”


你在这个假设中的谬误是你的类,我猜你的意思是
HTTPClient
,与它无关-它是会话类,应该隐藏自己的实现,如果你在构造函数中作为实例传递它,它也可以做,这也增加了无限的灵活性。

您可以使用套接字模拟简单的HTTP服务器。 以下psuedo代码可能会有所帮助:

1) set up a string to send from the client, take its length in before hand
2) open a new thread with a socket in it
3) bind the socket into some port , listen and accept new connection
4) send the string you have setted with your http client
5) in the socket side, read until the length you saved has reached, save that string for comparison
6) send some pre-defined http response
7) close the socket 
7) close the thread
8) continue testing, you have the string which the server got, and the string which the client got, and the original strings which these was originated from
模拟分块传输和重定向很容易,但模拟SSL可能相当困难。您可以使用openSSL或Boost SSL流提供的一些SSL流包装套接字

另一种选择是在本地主机上使用已经写入的HTTP服务器。用Python或Node.js编写一个(仅用于测试)非常简单,并且适合于该测试任务。在启动测试之前,激活服务器脚本(使用Node.js脚本,这与
系统(“Node myServer.js”)
一样简单),测试完成后,杀死该服务器