From 14624e39aff9239d5f016af1c0553483c856555b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 3 Jan 2023 20:05:44 +0100 Subject: HTTP Get test (WIP) --- common.mk | 2 +- tests/test-webserver.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++-- 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 #include +#include +#include +#include +#include +#include #include #include #include #include +#include #include #include #include @@ -34,8 +40,7 @@ public: const fs::path testConfigFilename{"./webserver.conf"}; WebserverProcess(): m_pid{} { - File::setFile(testConfigFilename, R"CONFIG( - + File::setFile(testConfigFilename, R"CONFIG( www-data www-data 10 @@ -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 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 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"); } -- cgit v1.2.3