编程语言
首页 > 编程语言> > 一个自己写的C++版RestClient测试用例

一个自己写的C++版RestClient测试用例

作者:互联网

C++版RestClient链接:https://github.com/mrtazz/restclient-cpp

一、这里简单说一下搭建环境的坑:

  1. 他的环境第一个推荐的是Linux的,第二个是Windows的。我用的是第二个。
  2. 由于在Windows环境下最便捷的安装方式是通过Vcpkg,这里推荐:https://www.cnblogs.com/vcpkg/p/14550478.html。这是我目前找到的所有版本里各种错误概括最全的。Ps:一定要网好,需要从github上下载不少的东西。

二、上代码:

#include <iostream>
#include<ctime>
#include "restclient-cpp/connection.h"
#include "restclient-cpp/restclient.h"
using namespace std;
int main()
{
    cout << "start:\n";
    //std::cout << "Hello World!\n";
    clock_t startTime, endTime;
    for (int i = 0; i < 10; i++) {
        RestClient::init();
        startTime = clock();
        //获得一个coonecton对象
        RestClient::Connection* conn = new RestClient::Connection("http://10.10.10.159:5000/api/");
        //设置超时
        conn->SetTimeout(5);
        //设置自定义用户代理
        conn->SetUserAgent("foo/cool");
        //启用重定向跟踪(默认关闭)
        conn->FollowRedirects(true);
        //并限制重定向次数(默认为 -1,无限制)
        conn->FollowRedirects(true, 3);
        // set headers
        RestClient::HeaderFields headers;
        headers["Accept"] = "application/json";
        conn->SetHeaders(headers);
        // append additional headers
        conn->AppendHeader("X-MY-HEADER", "foo");
        // if using a non-standard Certificate Authority (CA) trust file
        conn->SetCAInfoFilePath("/etc/custom-ca.crt");
        RestClient::Response r = conn->get("/get");
        r = conn->head("/get");
        r = conn->del("/delete");
        r = conn->options("/options");
        // set different content header for POST, PUT and PATCH
        conn->AppendHeader("Content-Type", "application/json");
        // set different content header for POST, PUT and PATCH
        conn->SetCAInfoFilePath("EpicEye/RunMode");
        r = conn->post("/post", "{\"foo\": \"bla\"}");
        r = conn->put("/put", "application/json");
        r = conn->patch("/patch", "text/plain");
        cout <<"上次运行总时间:" << conn->GetInfo().lastRequest.totalTime << endl;
        cout << "DNS查找时间:" << conn->GetInfo().lastRequest.nameLookupTime << endl;
        cout << "从开始到远程主机或代理所用的时间:" << conn->GetInfo().lastRequest.connectTime << endl;
        cout << "从开始到 SSL/SSH 握手完成的时间:" << conn->GetInfo().lastRequest.appConnectTime << endl;
        cout << "从开始到传输开始之前的时间:" << conn->GetInfo().lastRequest.preTransferTime << endl;
        cout << "从开始到接收到第一个字节的时间:" << conn->GetInfo().lastRequest.startTransferTime << endl;
        cout << "在最终传输之前所有重定向步骤所花费的时间:" << conn->GetInfo().lastRequest.redirectTime << endl;
        cout << "重定向次数:" << conn->GetInfo().lastRequest.redirectCount << endl;
        RestClient::disable();
        endTime = clock();
        cout << "响应时间: " << float(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    }
}

三、结果图:

 

标签:lastRequest,RestClient,GetInfo,C++,headers,测试用例,include,conn,restclient
来源: https://blog.csdn.net/m0_56584302/article/details/117787774