Skip to content

Commit d004c57

Browse files
author
pfeatherstone
committed
more unit tests
1 parent 1701e5f commit d004c57

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ add_executable(tests
4747
unit_tests/main.cpp
4848
unit_tests/base64.cpp
4949
unit_tests/sha1.cpp
50-
unit_tests/message.cpp)
50+
unit_tests/message.cpp
51+
unit_tests/async.cpp)
5152
target_link_options(tests PRIVATE $<$<CONFIG:Release>:-s>)
5253
target_include_directories(tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/extra)
5354
target_link_libraries(tests PRIVATE http)

examples/unit_tests/async.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <string_view>
2+
#include <vector>
3+
#include <boost/asio/io_context.hpp>
4+
#include <boost/asio/ip/basic_resolver.hpp>
5+
#include <boost/asio/ip/tcp.hpp>
6+
#include <boost/asio/connect.hpp>
7+
#include <http_async.h>
8+
#include "doctest.h"
9+
10+
using boost::asio::ip::tcp;
11+
using boost::asio::ip::make_address_v4;
12+
using tcp_acceptor = boost::asio::basic_socket_acceptor<tcp, boost::asio::io_context::executor_type>;
13+
using tcp_socket = boost::asio::basic_stream_socket<tcp, boost::asio::io_context::executor_type>;
14+
using tcp_resolver = boost::asio::ip::basic_resolver<tcp, boost::asio::io_context::executor_type>;
15+
using tcp_endpoint = boost::asio::ip::tcp::endpoint;
16+
17+
TEST_SUITE("[ASYNC]")
18+
{
19+
TEST_CASE("HTTP GET")
20+
{
21+
boost::asio::io_context ioc{1};
22+
tcp_acceptor acceptor(ioc, {tcp::v4(), 6666});
23+
tcp_socket peer(ioc);
24+
tcp_socket client(ioc);
25+
tcp_resolver resolver(ioc);
26+
bool exception_thrown{false};
27+
28+
http::request req_client, req_peer;
29+
http::response resp_client, resp_peer;
30+
std::string buf_client;
31+
std::string buf_peer;
32+
33+
req_client.verb = http::GET;
34+
req_client.uri = "/data?name=bane&peace=lie";
35+
req_client.add_header(http::host, "hello there!");
36+
req_client.add_header(http::user_agent, "Boost::asio " + std::to_string(BOOST_ASIO_VERSION)); // optional header
37+
38+
resp_peer.status = http::ok;
39+
resp_peer.content_str = "There is only passion";
40+
41+
try
42+
{
43+
acceptor.async_accept(peer, [&](boost::system::error_code ec) {
44+
REQUIRE(!bool(ec));
45+
http::async_http_read(peer, req_peer, buf_peer, [&](boost::system::error_code ec, size_t){
46+
REQUIRE(!bool(ec));
47+
http::async_http_write(peer, resp_peer, buf_peer, [&](boost::system::error_code ec, size_t){
48+
REQUIRE(!bool(ec));
49+
});
50+
});
51+
});
52+
53+
resolver.async_resolve("localhost", "6666", [&](boost::system::error_code ec, const auto& endpoints) {
54+
REQUIRE(!bool(ec));
55+
boost::asio::async_connect(client, endpoints, [&](boost::system::error_code ec, auto endpoint) {
56+
REQUIRE(!bool(ec));
57+
http::async_http_write(client, req_client, buf_client, [&](boost::system::error_code ec, size_t) {
58+
REQUIRE(!bool(ec));
59+
http::async_http_read(client, resp_client, buf_client, [&](boost::system::error_code ec, size_t) {
60+
REQUIRE(!bool(ec));
61+
});
62+
});
63+
});
64+
});
65+
66+
ioc.run();
67+
}
68+
catch(const std::exception& e)
69+
{
70+
exception_thrown = true;
71+
}
72+
73+
REQUIRE(!exception_thrown);
74+
REQUIRE(req_peer.verb == http::GET);
75+
REQUIRE(req_peer.uri == "/data");
76+
REQUIRE(req_peer.params.size() == 2);
77+
REQUIRE(req_peer.params[0].key == "name");
78+
REQUIRE(req_peer.params[0].val == "bane");
79+
REQUIRE(req_peer.params[1].key == "peace");
80+
REQUIRE(req_peer.params[1].val == "lie");
81+
const auto it = req_peer.find(http::host);
82+
REQUIRE(it != req_peer.headers.end());
83+
REQUIRE(it->value == "hello there!");
84+
85+
REQUIRE(resp_client.status == http::ok);
86+
REQUIRE(resp_client.content_str == "There is only passion");
87+
}
88+
}

0 commit comments

Comments
 (0)