diff options
Diffstat (limited to 'tests/test-webserver.cpp')
-rw-r--r-- | tests/test-webserver.cpp | 66 |
1 files changed, 64 insertions, 2 deletions
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"); } |