-
Notifications
You must be signed in to change notification settings - Fork 4
/
winhttp_request_test.cpp
424 lines (368 loc) · 15.2 KB
/
winhttp_request_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// Copyright (c) 2022 Youyuan Wu
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// #define BOOST_TEST_MODULE http_client2
#include <boost/test/unit_test.hpp>
#include "boost/asio.hpp"
#include "boost/winasio/winhttp/winhttp.hpp"
#include "boost/winasio/winhttp/client.hpp"
#include "boost/winasio/winhttp/winhttp_stream.hpp"
// use beast server for testing
#include "beast_test_server.hpp"
#include <boost/log/trivial.hpp>
#include <latch>
namespace net = boost::asio; // from <boost/asio.hpp>
namespace winnet = boost::winasio;
class beast_server {
public:
beast_server() : lch_(1), ioc_(1), th_() {
my_program_state::set_request_count(0);
th_ = std::jthread([this]() {
// start beast server
unsigned short port = static_cast<unsigned short>(12345);
myacceptor acceptor{ioc_, {tcp::v4(), port}};
mysocket socket{ioc_};
http_server(acceptor, socket);
lch_.count_down();
ioc_.run();
});
// wait for server to run;
lch_.wait();
}
~beast_server() { ioc_.stop(); }
private:
std::latch lch_;
net::io_context ioc_;
std::jthread th_;
};
BOOST_AUTO_TEST_SUITE(test_winhttp_request)
BOOST_AUTO_TEST_CASE(Basic) {
beast_server bs;
boost::system::error_code ec;
net::io_context io_context;
winnet::winhttp::basic_winhttp_session_handle<net::io_context::executor_type>
h_session(io_context);
h_session.open(ec); // by default async session is created
BOOST_REQUIRE(!ec.failed());
winnet::winhttp::url_component url;
url.crack(L"http://localhost:12345/count", ec);
BOOST_REQUIRE(!ec.failed());
winnet::winhttp::basic_winhttp_connect_handle<net::io_context::executor_type>
h_connect(io_context);
h_connect.connect(h_session.native_handle(), url.get_hostname().c_str(),
url.get_port(), ec);
BOOST_REQUIRE(!ec.failed());
winnet::winhttp::payload pl;
pl.method = L"GET";
pl.path = url.get_path();
pl.header = std::nullopt;
pl.accept = std::nullopt;
pl.body = std::nullopt;
pl.secure = false;
// test callback style
winnet::winhttp::basic_winhttp_request_asio_handle<
net::io_context::executor_type>
h_request(io_context.get_executor());
std::vector<BYTE> body_buff;
auto buff = net::dynamic_buffer(body_buff);
winnet::winhttp::async_exec(
pl, h_connect, h_request, buff,
[&h_request, &buff](boost::system::error_code ec, std::size_t) {
BOOST_LOG_TRIVIAL(debug) << "async_exec handler";
BOOST_REQUIRE(!ec.failed());
// print result
std::wstring headers;
winnet::winhttp::header::get_all_raw_crlf(h_request, ec, headers);
BOOST_REQUIRE(!ec.failed());
BOOST_LOG_TRIVIAL(debug) << headers;
BOOST_LOG_TRIVIAL(debug) << winnet::winhttp::buff_to_string(buff);
// more tests
// check status
DWORD dwStatusCode;
winnet::winhttp::header::get_status_code(h_request, ec, dwStatusCode);
BOOST_REQUIRE_EQUAL(boost::system::errc::success, ec);
// sometimes the api is 403, but this does not impact this test.
BOOST_REQUIRE(200 == dwStatusCode);
std::wstring version;
winnet::winhttp::header::get_version(h_request, ec, version);
BOOST_CHECK_EQUAL(boost::system::errc::success, ec);
BOOST_CHECK(L"HTTP/1.1" == version);
std::wstring content_type;
winnet::winhttp::header::get_content_type(h_request, ec, content_type);
BOOST_REQUIRE_EQUAL(boost::system::errc::success, ec);
BOOST_CHECK(L"text/html" == content_type);
});
io_context.run();
}
BOOST_AUTO_TEST_CASE(ObjectHandleAlreadySet) {
// Tests when event is already set, executor runs the task immediately.
net::io_context io_context;
net::windows::object_handle oh(io_context);
HANDLE ev = CreateEvent(NULL, // default security attributes
TRUE, // manual-reset event
FALSE, // initial state is nonsignaled
NULL // object name
);
BOOST_REQUIRE(ev != nullptr);
oh.assign(ev);
bool ok = SetEvent(ev);
BOOST_REQUIRE(ok);
bool flag = false;
oh.async_wait([&flag](boost::system::error_code ec) {
BOOST_CHECK(!ec);
flag = true;
});
io_context.run();
BOOST_CHECK(flag);
io_context.reset();
oh.async_wait([&flag](boost::system::error_code ec) {
BOOST_CHECK(!ec);
flag = false;
});
io_context.run();
BOOST_CHECK(!flag);
}
BOOST_AUTO_TEST_CASE(Coroutine) {
beast_server bs;
boost::system::error_code ec;
net::io_context io_context;
winnet::winhttp::basic_winhttp_session_handle<net::io_context::executor_type>
h_session(io_context);
h_session.open(ec); // by default async session is created
BOOST_REQUIRE(!ec.failed());
winnet::winhttp::url_component url;
url.crack(L"http://localhost:12345/count", ec);
BOOST_REQUIRE(!ec.failed());
winnet::winhttp::basic_winhttp_connect_handle<net::io_context::executor_type>
h_connect(io_context);
h_connect.connect(h_session.native_handle(), url.get_hostname().c_str(),
url.get_port(), ec);
BOOST_REQUIRE(!ec.failed());
// test GET request
winnet::winhttp::basic_winhttp_request_asio_handle<
net::io_context::executor_type>
h_request1(io_context.get_executor());
{
std::wstring path = url.get_path();
h_request1.managed_open(h_connect.native_handle(), L"GET", path.c_str(),
NULL, // http 1.1
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE(!ec);
auto f = [&h_request = h_request1, &url]() -> net::awaitable<void> {
auto executor = co_await net::this_coro::executor;
boost::system::error_code ec;
co_await h_request.async_send(
NULL, // headers
0, // header len
WINHTTP_NO_REQUEST_DATA, // optional
0, // optional len
0, // total len
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::vector<BYTE> body_buff;
auto dybuff = net::dynamic_buffer(body_buff);
co_await h_request.async_recieve_response(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::size_t len = 0;
do {
len = co_await h_request.async_query_data_available(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
auto buff = dybuff.prepare(len + 1);
std::size_t read_len = co_await h_request.async_read_data(
(LPVOID)buff.data(), static_cast<DWORD>(len),
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
BOOST_REQUIRE_EQUAL(read_len, len);
dybuff.commit(len);
} while (len > 0);
BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(winnet::winhttp::buff_to_string(dybuff));
};
net::co_spawn(io_context, f, net::detached);
}
// test POST request
winnet::winhttp::basic_winhttp_request_asio_handle<
net::io_context::executor_type>
h_request2(io_context.get_executor());
{
std::wstring path = url.get_path();
h_request2.managed_open(h_connect.native_handle(), L"POST", path.c_str(),
NULL, // http 1.1
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
auto f = [&h_request2, &url]() -> net::awaitable<void> {
auto executor = co_await net::this_coro::executor;
boost::system::error_code ec;
std::string req_body = "set_count=100";
const DWORD req_len = static_cast<DWORD>(req_body.size());
co_await h_request2.async_send(
NULL, // headers
0, // header len
NULL, // optional
0, // optional len
req_len, // total len
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::size_t total_write_len = {};
std::size_t index = {};
std::size_t remain_len = req_len;
while (total_write_len < req_len) {
char *c = req_body.data() + index;
std::size_t write_len = co_await h_request2.async_write_data(
(LPCVOID)c, static_cast<DWORD>(remain_len),
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
total_write_len += write_len;
index += write_len;
remain_len -= write_len;
// This is not necessary the case since winhttp might not gurantee
// that write len equals provided buffer len. If test fail here, we
// can remove this check.
BOOST_REQUIRE_EQUAL(req_len, write_len);
}
BOOST_REQUIRE_EQUAL(req_len, total_write_len);
std::vector<BYTE> body_buff;
auto dybuff = net::dynamic_buffer(body_buff);
co_await h_request2.async_recieve_response(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::size_t len = 0;
do {
len = co_await h_request2.async_query_data_available(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
auto buff = dybuff.prepare(len + 1);
std::size_t read_len = co_await h_request2.async_read_data(
(LPVOID)buff.data(), static_cast<DWORD>(len),
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
BOOST_REQUIRE_EQUAL(read_len, len);
dybuff.commit(len);
} while (len > 0);
BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(winnet::winhttp::buff_to_string(dybuff));
};
net::co_spawn(io_context, f, net::detached);
}
// stress test async by read and write one byte at a time.
winnet::winhttp::basic_winhttp_request_asio_handle<
net::io_context::executor_type>
h_request3(io_context.get_executor());
{
std::wstring path = url.get_path();
h_request3.managed_open(h_connect.native_handle(), L"POST", path.c_str(),
NULL, // http 1.1
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
auto f = [&h_request = h_request3, &url]() -> net::awaitable<void> {
auto executor = co_await net::this_coro::executor;
boost::system::error_code ec;
std::string req_body = "set_count=111";
const DWORD req_len = static_cast<DWORD>(req_body.size());
co_await h_request.async_send(
NULL, // headers
0, // header len
NULL, // optional
0, // optional len
req_len, // total len
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
// write data byte by byte to stress test
std::size_t total_write_len = {};
std::size_t index = {};
const DWORD step_size = 1;
while (total_write_len < req_len) {
char *c = req_body.data() + index;
std::size_t write_len = co_await h_request.async_write_data(
(LPCVOID)c, step_size, net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
total_write_len += write_len;
index += write_len;
}
BOOST_REQUIRE_EQUAL(req_len, total_write_len);
co_await h_request.async_recieve_response(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
DWORD dwStatusCode = {};
winnet::winhttp::header::get_status_code(h_request, ec, dwStatusCode);
BOOST_CHECK_EQUAL(boost::system::errc::success, ec);
BOOST_CHECK_EQUAL(static_cast<DWORD>(201), dwStatusCode);
// read bytes one by one
std::size_t total_read = {};
std::string body = {};
while (true) {
std::size_t len = co_await h_request.async_query_data_available(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
if (len == 0) {
break;
}
char c = {};
std::size_t read_len = co_await h_request.async_read_data(
(LPVOID)&c, 1, net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
total_read += read_len;
body += c;
}
BOOST_CHECK_EQUAL(total_read, body.size());
BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(body);
};
net::co_spawn(io_context, f, net::detached);
}
// test using stream
winnet::winhttp::basic_winhttp_request_stream_handle<
net::io_context::executor_type>
h_stream(io_context.get_executor());
{
std::wstring path = url.get_path();
h_stream.managed_open(h_connect.native_handle(), L"POST", path.c_str(),
NULL, // http 1.1
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
auto f = [&h_request = h_stream, &url]() -> net::awaitable<void> {
auto executor = co_await net::this_coro::executor;
boost::system::error_code ec;
std::string req_body = "set_count=200";
DWORD req_len = static_cast<DWORD>(req_body.size());
co_await h_request.async_send(
NULL, // headers
0, // header len
NULL, // optional
0, // optional len
static_cast<DWORD>(req_body.size()), // total len
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::size_t write_len = co_await net::async_write(
h_request, net::buffer(req_body),
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
BOOST_REQUIRE_EQUAL(req_len, write_len);
co_await h_request.async_recieve_response(
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
std::vector<BYTE> body_buff;
co_await net::async_read(h_request, net::dynamic_buffer(body_buff),
net::redirect_error(net::use_awaitable, ec));
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);
BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(std::string(body_buff.begin(), body_buff.end()));
};
net::co_spawn(io_context, f, net::detached);
}
io_context.run();
}
BOOST_AUTO_TEST_SUITE_END()