summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-03 20:05:44 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-03 20:05:44 +0100
commit14624e39aff9239d5f016af1c0553483c856555b (patch)
treeed15856c28f70caf21be37aa358f4d59ebcf0457
parent3642434a0b1b3dd223e7c2666eb00231858ee1b7 (diff)
HTTP Get test (WIP)
-rw-r--r--common.mk2
-rw-r--r--tests/test-webserver.cpp66
2 files changed, 65 insertions, 3 deletions
diff --git a/common.mk b/common.mk
index 9f1d074..5770d7d 100644
--- a/common.mk
+++ b/common.mk
@@ -79,7 +79,7 @@ CXXTYPE=clang++
else ifeq ($(CXX),clang++-14)
LIBS+= \
-fuse-ld=lld-14 \
--lstdc++
+-lc++ -lc++abi
LLVMPROFDATA=llvm-profdata-14
LLVMCOV=llvm-cov-14
CXXTYPE=clang++
diff --git a/tests/test-webserver.cpp b/tests/test-webserver.cpp
index 473e67d..4ac550e 100644
--- a/tests/test-webserver.cpp
+++ b/tests/test-webserver.cpp
@@ -7,11 +7,17 @@
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>
+#include <boost/beast/core.hpp>
+#include <boost/beast/http.hpp>
+#include <boost/beast/version.hpp>
+#include <boost/asio/connect.hpp>
+#include <boost/asio/ip/tcp.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <chrono>
#include <filesystem>
+#include <iostream>
#include <sstream>
#include <string>
#include <thread>
@@ -34,8 +40,7 @@ public:
const fs::path testConfigFilename{"./webserver.conf"};
WebserverProcess(): m_pid{}
{
- File::setFile(testConfigFilename, R"CONFIG(
-<webserver>
+ File::setFile(testConfigFilename, R"CONFIG(<webserver>
<user>www-data</user>
<group>www-data</group>
<threads>10</threads>
@@ -117,6 +122,61 @@ private:
pid_t m_pid;
};
+std::string HTTPGet(const std::string& target)
+{
+ auto const host = "127.0.0.1"; //"::1";
+ auto const port = "8080";
+ int version = 11; // or 10
+
+ // The io_context is required for all I/O
+ boost::asio::io_context ioc;
+
+ // These objects perform our I/O
+ boost::asio::ip::tcp::resolver resolver(ioc);
+ boost::beast::tcp_stream stream(ioc);
+
+ // Look up the domain name
+ auto const results = resolver.resolve(host, port);
+
+ // Make the connection on the IP address we get from a lookup
+ stream.connect(results);
+
+ // Set up an HTTP GET request message
+ boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::get, target, version};
+ req.set(boost::beast::http::field::host, host);
+ req.set(boost::beast::http::field::user_agent, "Webserver Testsuite");
+
+ // Send the HTTP request to the remote host
+ boost::beast::http::write(stream, req);
+
+ // This buffer is used for reading and must be persisted
+ boost::beast::flat_buffer buffer;
+
+ // Declare a container to hold the response
+ boost::beast::http::response<boost::beast::http::dynamic_body> res;
+
+ // Receive the HTTP response
+ boost::beast::http::read(stream, buffer, res);
+
+ // Write the message to standard out
+ std::ostringstream header_stream;
+ header_stream << res.base();
+ //std::ostringstream body_stream;
+ header_stream << boost::beast::buffers_to_string(res.body().data());
+
+ // Gracefully close the socket
+ boost::beast::error_code ec;
+ stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
+
+ // not_connected happens sometimes
+ // so don't bother reporting it.
+ //
+ if(ec && ec != boost::beast::errc::not_connected)
+ throw boost::beast::system_error{ec};
+
+ return header_stream.str();
+}
+
class Fixture
{
public:
@@ -127,5 +187,7 @@ public:
BOOST_FIXTURE_TEST_CASE(http_download, Fixture)
{
WebserverProcess serverProcess;
+
+ std::cout << HTTPGet("/webserver.conf");
}