diff options
author | Ed Tanous <edtanous@google.com> | 2023-09-02 00:20:50 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2023-12-05 21:54:28 +0300 |
commit | 4fa45dffd1ece21a468ed32850428b3b41bc8093 (patch) | |
tree | da14af66150c4b652c8aeb5c42d1e5789ac04cdd /test | |
parent | 7164bc62dd26ec92b01985aaae97ecc48276dea5 (diff) | |
download | bmcweb-4fa45dffd1ece21a468ed32850428b3b41bc8093.tar.xz |
Unit test Connection
Boost asio provides a test stream object that we can use to begin unit
testing the connection object. This patchset uses it to re-enable
some simple http1.1 tests. There's some features that have snuck into
the connection class that aren't compatible with a stream (like ip
address getting), so unfortunately we do need the connection class to
be aware if it's in test mode, but that tradeoff seems worthwhile.
Tested: Unit test pass.
Change-Id: Id8b1f8866582b58502dbafe6139f841bf64b8ef3
Signed-off-by: Ed Tanous <edtanous@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/http/http_connection_test.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/test/http/http_connection_test.cpp b/test/http/http_connection_test.cpp new file mode 100644 index 0000000000..c4252e1ee7 --- /dev/null +++ b/test/http/http_connection_test.cpp @@ -0,0 +1,85 @@ +#include "http/http_connection.hpp" +#include "http/http_request.hpp" +#include "http/http_response.hpp" + +#include <boost/asio/steady_timer.hpp> +#include <boost/beast/_experimental/test/stream.hpp> + +#include <filesystem> +#include <fstream> +#include <functional> +#include <memory> +#include <string> + +#include "gtest/gtest.h" +namespace crow +{ + +struct FakeHandler +{ + static void + handleUpgrade(Request& /*req*/, + const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, + boost::beast::test::stream&& /*adaptor*/) + { + // Handle Upgrade should never be called + EXPECT_FALSE(true); + } + + void handle(Request& req, + const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/) + { + EXPECT_EQ(req.method(), boost::beast::http::verb::get); + EXPECT_EQ(req.target(), "/"); + called = true; + } + bool called = false; +}; + +static std::string getDateStr() +{ + return "TestTime"; +} + +TEST(http_connection, RequestPropogates) +{ + boost::asio::io_context io; + boost::beast::test::stream stream(io); + boost::beast::test::stream out(io); + stream.connect(out); + + out.write_some(boost::asio::buffer( + "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n")); + FakeHandler handler; + boost::asio::steady_timer timer(io); + std::function<std::string()> date(&getDateStr); + std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>> + conn = std::make_shared< + crow::Connection<boost::beast::test::stream, FakeHandler>>( + &handler, std::move(timer), date, std::move(stream)); + conn->start(); + io.run_for(std::chrono::seconds(1000)); + EXPECT_TRUE(handler.called); + std::string outStr = out.str(); + + std::string expected = + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n" + "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n" + "X-Frame-Options: DENY\r\n" + "Pragma: no-cache\r\n" + "Cache-Control: no-store, max-age=0\r\n" + "X-Content-Type-Options: nosniff\r\n" + "Referrer-Policy: no-referrer\r\n" + "Permissions-Policy: accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-get=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wak-lock=(),web-share=(),xr-spatial-tracking=()\r\n" + "X-Permitted-Cross-Domain-Policies: none\r\n" + "Cross-Origin-Embedder-Policy: require-corp\r\n" + "Cross-Origin-Opener-Policy: same-origin\r\n" + "Cross-Origin-Resource-Policy: same-origin\r\n" + "Content-Security-Policy: default-src 'none'; img-src 'self' data:; font-src 'self'; style-src 'self'; script-src 'self'; connect-src 'self' wss:; form-action 'none'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'\r\n" + "Content-Length: 0\r\n" + "\r\n"; + EXPECT_EQ(outStr, expected); +} + +} // namespace crow |