From 1e6cca9ca8c4771b05f419a23cd3bb1bbc1f0e38 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 6 Jan 2023 19:25:51 +0100 Subject: More HTTP tests --- response.cpp | 4 ++-- tests/test-webserver.cpp | 45 +++++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/response.cpp b/response.cpp index 17d3e48..29176af 100644 --- a/response.cpp +++ b/response.cpp @@ -319,11 +319,11 @@ response_type generate_response(request_type& req, Server& server) std::string res_data { plugin->generate_page(GetServerParamFunction, GetRequestParamFunction, SetResponseHeaderFunction)}; if (req.method() == http::verb::head) { - res.content_length(res_data.size()); + res.body() = std::string{}; } else { res.body() = res_data; - res.prepare_payload(); } + res.prepare_payload(); return HttpStatusAndStats("200", "OK", req_ctx, res); } catch(const std::out_of_range& ex) { diff --git a/tests/test-webserver.cpp b/tests/test-webserver.cpp index e9dba3a..feb4f2a 100644 --- a/tests/test-webserver.cpp +++ b/tests/test-webserver.cpp @@ -199,7 +199,7 @@ private: std::shared_ptr m_is; }; -std::pair HTTPGet(const std::string& target, bool ipv6 = true, bool HTTP11 = true) +std::pair HTTP(const std::string& target, bool ipv6 = true, bool HTTP11 = true, boost::beast::http::verb method = boost::beast::http::verb::get) { auto const host = ipv6 ? "::1" : "127.0.0.1"; auto const port = "8080"; @@ -219,8 +219,11 @@ std::pair HTTPGet(const std::string& target, bool ipv6 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, ipv6 && host == "::1"s ? "["s + host + "]"s : host); + boost::beast::http::request req; + req.method(method); + req.target(target); + req.version(version); + req.set(boost::beast::http::field::host, host == "::1"s ? "["s + host + "]"s : host); req.set(boost::beast::http::field::user_agent, "Webserver Testsuite"); // Send the HTTP request to the remote host @@ -260,7 +263,7 @@ void load_root_certificates(boost::asio::ssl::context& ctx) ctx.add_certificate_authority(boost::asio::buffer(cert_chain.data(), cert_chain.size())); } -std::pair HTTPSGet(const std::string& target, bool ipv6 = true, bool HTTP11 = true) +std::pair HTTPS(const std::string& target, bool ipv6 = true, bool HTTP11 = true, boost::beast::http::verb method = boost::beast::http::verb::get) { auto const host = ipv6 ? "::1" : "127.0.0.1"; auto const port = "8081"; @@ -305,8 +308,11 @@ std::pair HTTPSGet(const std::string& target, bool ipv6 stream.handshake(boost::asio::ssl::stream_base::client); // 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, ipv6 && host == "::1"s ? "["s + host + "]"s : host); + boost::beast::http::request req; + req.method(method); + req.target(target); + req.version(version); + req.set(boost::beast::http::field::host, host == "::1"s ? "["s + host + "]"s : host); req.set(boost::beast::http::field::user_agent, "Webserver Testsuite"); // Send the HTTP request to the remote host @@ -349,25 +355,36 @@ public: ~Fixture(){} }; -BOOST_DATA_TEST_CASE_F(Fixture, http_get, data::make({false, true}) * data::make({false, true}) * data::make({false, true}), ipv6, http11, https) +BOOST_DATA_TEST_CASE_F(Fixture, http_get, data::make({false, true}) * data::make({false, true}) * data::make({false, true}) * data::make({boost::beast::http::verb::head, boost::beast::http::verb::get}), ipv6, http11, https, method) { + std::cout << "DEBUG: " << ipv6 << " " << http11 << " " << https << " " << method << std::endl; WebserverProcess serverProcess; BOOST_REQUIRE(serverProcess.isRunning()); - std::pair response{https ? HTTPSGet("/webserver.conf") : HTTPGet("/webserver.conf")}; + std::pair response{https ? HTTPS("/webserver.conf", ipv6, http11, method) : HTTP("/webserver.conf", ipv6, http11, method)}; BOOST_REQUIRE(serverProcess.isRunning()); - BOOST_REQUIRE_EQUAL(response.first, "HTTP/1.1 200 OK\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: application/text\r\nContent-Length: 1021\r\n\r\n"); - BOOST_REQUIRE_EQUAL(response.second, File::getFile(serverProcess.testConfigFilename)); + BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 200 OK\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: application/text\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : 1021)); + BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? ""s : File::getFile(serverProcess.testConfigFilename)); +#if 0 + for (int i = 0; i < 10; i++) { + std::pair response{https ? HTTPS("/webserver.conf", ipv6, http11, method) : HTTP("/webserver.conf", ipv6, http11, method)}; + BOOST_REQUIRE(serverProcess.isRunning()); + BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 200 OK\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: application/text\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : 1021)); + BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? ""s : File::getFile(serverProcess.testConfigFilename)); + } +#endif } -BOOST_DATA_TEST_CASE_F(Fixture, http_get_file_not_found, data::make({false, true}) * data::make({false, true}) * data::make({false, true}), ipv6, http11, https) +BOOST_DATA_TEST_CASE_F(Fixture, http_get_file_not_found, data::make({false, true}) * data::make({false, true}) * data::make({false, true}) * data::make({boost::beast::http::verb::head, boost::beast::http::verb::get}), ipv6, http11, https, method) { + std::cout << "DEBUGe: " << ipv6 << " " << http11 << " " << https << " " << method << std::endl; WebserverProcess serverProcess; BOOST_REQUIRE(serverProcess.isRunning()); BOOST_REQUIRE(!fs::exists("./webserver.confSUFFIX")); - auto response{(https ? HTTPSGet("/webserver.confSUFFIX") : HTTPGet("/webserver.confSUFFIX"))}; + auto response{(https ? HTTPS("/webserver.confSUFFIX", ipv6, http11, method) : HTTP("/webserver.confSUFFIX", ipv6, http11, method))}; BOOST_REQUIRE(serverProcess.isRunning()); - BOOST_REQUIRE_EQUAL(response.first, "HTTP/1.1 404 Not Found\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: text/html\r\nContent-Length: 36\r\n\r\n"); - BOOST_REQUIRE_EQUAL(response.second, "404 Not found: /webserver.confSUFFIX"); + BOOST_REQUIRE_EQUAL(response.first, fmt::format("HTTP/{} 404 Not Found\r\nServer: Reichwein.IT Webserver " VERSION "\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n", http11 ? "1.1" : "1.0", method == boost::beast::http::verb::head ? 0 : 36)); + BOOST_REQUIRE_EQUAL(response.second, method == boost::beast::http::verb::head ? "" : "404 Not found: /webserver.confSUFFIX"); } + -- cgit v1.2.3