forked from ithewei/libhv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client_test.cpp
187 lines (166 loc) · 5.1 KB
/
http_client_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* @build make examples
*
* @server bin/http_server_test 8080
*
* @client bin/http_client_test
*
*/
#include "requests.h"
#include "axios.h"
using namespace hv;
#include "hthread.h" // import hv_gettid
static void test_http_async_client(HttpClient* cli, int* resp_cnt) {
printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
HttpRequestPtr req(new HttpRequest);
req->method = HTTP_POST;
req->url = "http://127.0.0.1:8080/echo";
req->headers["Connection"] = "keep-alive";
req->body = "This is an async request.";
req->timeout = 10;
cli->sendAsync(req, [resp_cnt](const HttpResponsePtr& resp) {
printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
*resp_cnt += 1;
});
}
static void test_http_sync_client(HttpClient* cli) {
HttpRequest req;
req.method = HTTP_POST;
req.url = "http://127.0.0.1:8080/echo";
req.headers["Connection"] = "keep-alive";
req.body = "This is a sync request.";
req.timeout = 10;
HttpResponse resp;
int ret = cli->send(&req, &resp);
if (ret != 0) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp.status_code, resp.status_message());
printf("%s\n", resp.body.c_str());
}
}
static void test_requests() {
// auto resp = requests::get("http://www.example.com");
//
// make clean && make WITH_OPENSSL=yes
// auto resp = requests::get("https://www.baidu.com");
auto resp = requests::get("http://127.0.0.1:8080/ping");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
// Content-Type: application/json
hv::Json jroot;
jroot["user"] = "admin";
jroot["pswd"] = "123456";
http_headers headers;
headers["Content-Type"] = "application/json";
resp = requests::post("http://127.0.0.1:8080/echo", jroot.dump(), headers);
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
// Content-Type: multipart/form-data
std::map<std::string, std::string> params;
params["user"] = "admin";
params["pswd"] = "123456";
resp = requests::uploadFormFile("http://127.0.0.1:8080/echo", "avatar", "avatar.jpg", params);
if (resp == NULL) {
printf("uploadFormFile failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
/*
size_t filesize = requests::downloadFile("http://www.example.com/index.html", "index.html");
if (filesize == 0) {
printf("downloadFile failed!\n");
} else {
printf("downloadFile success!\n");
}
*/
// async
/*
// Request req(new HttpRequest);
req->url = "http://127.0.0.1:8080/echo";
req->method = HTTP_POST;
req->body = "This is an async request.";
req->timeout = 10;
requests::async(req, [](const HttpResponsePtr& resp) {
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
});
*/
}
static void test_axios() {
const char* strReq = R"(
{
"method": "POST",
"url": "http://127.0.0.1:8080/echo",
"timeout": 10,
"params": {
"page_no": "1",
"page_size": "10"
},
"headers": {
"Content-Type": "application/json"
},
"body": {
"app_id": "123456",
"app_secret": "abcdefg"
}
}
)";
// sync
auto resp = axios::axios(strReq);
// auto resp = axios::post("http://127.0.0.1:8080/echo", strReq);
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
// async
/*
axios::axios(strReq, [](const HttpResponsePtr& resp) {
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
});
*/
}
int main(int argc, char* argv[]) {
int req_cnt = 0;
if (argc > 1) req_cnt = atoi(argv[1]);
if (req_cnt == 0) req_cnt = 1;
HttpClient sync_client;
HttpClient async_client;
int resp_cnt = 0;
for (int i = 0; i < req_cnt; ++i) {
test_http_async_client(&async_client, &resp_cnt);
test_http_sync_client(&sync_client);
test_requests();
test_axios();
}
// demo wait async finished
while (resp_cnt < req_cnt) hv_delay(100);
printf("finished!\n");
return 0;
}