diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-06 19:25:51 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-06 19:25:51 +0100 |
commit | 1e6cca9ca8c4771b05f419a23cd3bb1bbc1f0e38 (patch) | |
tree | 4fdeacee76c24a9704d4741910020311bd95cdfb | |
parent | ccbfd4b39162a6a320ed400635ebae2992cecd61 (diff) |
More HTTP tests
-rw-r--r-- | response.cpp | 4 | ||||
-rw-r--r-- | 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<std::istream> m_is; }; -std::pair<std::string,std::string> HTTPGet(const std::string& target, bool ipv6 = true, bool HTTP11 = true) +std::pair<std::string,std::string> 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<std::string,std::string> HTTPGet(const std::string& target, bool ipv6 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, ipv6 && host == "::1"s ? "["s + host + "]"s : host); + boost::beast::http::request<boost::beast::http::string_body> 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<std::string,std::string> HTTPSGet(const std::string& target, bool ipv6 = true, bool HTTP11 = true) +std::pair<std::string,std::string> 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<std::string,std::string> 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<boost::beast::http::string_body> 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<boost::beast::http::string_body> 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<std::string,std::string> response{https ? HTTPSGet("/webserver.conf") : HTTPGet("/webserver.conf")}; + std::pair<std::string,std::string> 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<std::string,std::string> 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"); } + |