Skip to content

Commit

Permalink
improve winhttp coro test
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu committed Nov 9, 2023
1 parent 7228ddb commit 65abb58
Showing 1 changed file with 172 additions and 165 deletions.
337 changes: 172 additions & 165 deletions tests/winhttp/winhttp_request_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,36 +188,38 @@ BOOST_AUTO_TEST_CASE(Coroutine) {
BOOST_REQUIRE(!ec);

auto f = [&h_request = h_request1, &url]() -> net::awaitable<void> {
try {
auto executor = co_await net::this_coro::executor;

co_await h_request.async_send(NULL, // headers
0, // header len
WINHTTP_NO_REQUEST_DATA, // optional
0, // optional len
0, // total len
net::use_awaitable);

std::vector<BYTE> body_buff;
auto dybuff = net::dynamic_buffer(body_buff);

co_await h_request.async_recieve_response(net::use_awaitable);
std::size_t len = 0;
do {
len =
co_await h_request.async_query_data_available(net::use_awaitable);
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::use_awaitable);
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));
} catch (const std::exception &e) {
BOOST_REQUIRE_MESSAGE(false,
std::string("coro has exception ") + e.what());
}
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);
}
Expand All @@ -233,58 +235,62 @@ BOOST_AUTO_TEST_CASE(Coroutine) {
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE(!ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);

auto f = [&h_request2, &url]() -> net::awaitable<void> {
try {
auto executor = co_await net::this_coro::executor;

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::use_awaitable);

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::use_awaitable);
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::use_awaitable);
std::size_t len = 0;
do {
len = co_await h_request2.async_query_data_available(
net::use_awaitable);
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::use_awaitable);
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));
} catch (const std::exception &e) {
BOOST_REQUIRE_MESSAGE(false,
std::string("coro has exception ") + e.what());
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);
}
Expand All @@ -300,66 +306,66 @@ BOOST_AUTO_TEST_CASE(Coroutine) {
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE(!ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);

auto f = [&h_request = h_request3, &url]() -> net::awaitable<void> {
try {
auto executor = co_await net::this_coro::executor;

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::use_awaitable);

// 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::use_awaitable);
total_write_len += write_len;
index += write_len;
}
BOOST_REQUIRE_EQUAL(req_len, total_write_len);

co_await h_request.async_recieve_response(net::use_awaitable);

DWORD dwStatusCode = {};
boost::system::error_code ec = {};
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::use_awaitable);
if (len == 0) {
break;
}
char c = {};
std::size_t read_len = co_await h_request.async_read_data(
(LPVOID)&c, 1, net::use_awaitable);
total_read += read_len;
body += c;
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;
}
BOOST_CHECK_EQUAL(total_read, body.size());

BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(body);

} catch (const std::exception &e) {
BOOST_REQUIRE_MESSAGE(false,
std::string("coro has exception ") + e.what());
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);
}
Expand All @@ -375,38 +381,39 @@ BOOST_AUTO_TEST_CASE(Coroutine) {
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
NULL, // no ssl WINHTTP_FLAG_SECURE,
ec);
BOOST_REQUIRE(!ec);
BOOST_REQUIRE_EQUAL(ec, boost::system::errc::success);

auto f = [&h_request = h_stream, &url]() -> net::awaitable<void> {
try {
auto executor = co_await net::this_coro::executor;

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::use_awaitable);

std::size_t write_len = co_await net::async_write(
h_request, net::buffer(req_body), net::use_awaitable);

BOOST_REQUIRE_EQUAL(req_len, write_len);

co_await h_request.async_recieve_response(net::use_awaitable);

std::vector<BYTE> body_buff;
co_await net::async_read(h_request, net::dynamic_buffer(body_buff),
net::use_awaitable);
BOOST_TEST_MESSAGE("request body:");
BOOST_TEST_MESSAGE(std::string(body_buff.begin(), body_buff.end()));
} catch (const std::exception &e) {
BOOST_REQUIRE_MESSAGE(false,
std::string("coro has exception ") + e.what());
}
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);
}
Expand Down

0 comments on commit 65abb58

Please sign in to comment.