From f0e0f0a92b41f61224805870183a943e4f5cc22d Mon Sep 17 00:00:00 2001 From: jr0me Date: Wed, 7 Aug 2024 20:30:07 +0200 Subject: [PATCH] feat: add tests for CreateHttpRequest --- src/agent/communicator/tests/CMakeLists.txt | 4 ++ .../communicator/tests/http_client_test.cpp | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/agent/communicator/tests/http_client_test.cpp diff --git a/src/agent/communicator/tests/CMakeLists.txt b/src/agent/communicator/tests/CMakeLists.txt index 3dff9d5be1..ff482a8bbd 100644 --- a/src/agent/communicator/tests/CMakeLists.txt +++ b/src/agent/communicator/tests/CMakeLists.txt @@ -3,3 +3,7 @@ find_package(GTest CONFIG REQUIRED) add_executable(communicator_test communicator_test.cpp) target_link_libraries(communicator_test PUBLIC communicator GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) add_test(NAME CommunicatorTest COMMAND communicator_test) + +add_executable(http_client_test http_client_test.cpp) +target_link_libraries(http_client_test PUBLIC communicator GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main) +add_test(NAME HttpClientTest COMMAND http_client_test) diff --git a/src/agent/communicator/tests/http_client_test.cpp b/src/agent/communicator/tests/http_client_test.cpp new file mode 100644 index 0000000000..c363fc8f43 --- /dev/null +++ b/src/agent/communicator/tests/http_client_test.cpp @@ -0,0 +1,60 @@ +#include + +#include + +#include +#include + +#include + +TEST(CreateHttpRequestTest, BasicGetRequest) +{ + const auto req = http_client::CreateHttpRequest(boost::beast::http::verb::get, "/test", "localhost", "", "", ""); + + EXPECT_EQ(req.method(), boost::beast::http::verb::get); + EXPECT_EQ(req.target(), "/test"); + EXPECT_EQ(req.version(), 11); + EXPECT_EQ(req[boost::beast::http::field::host], "localhost"); + EXPECT_EQ(req[boost::beast::http::field::user_agent], BOOST_BEAST_VERSION_STRING); + EXPECT_EQ(req[boost::beast::http::field::accept], "application/json"); +} + +TEST(CreateHttpRequestTest, PostRequestWithBody) +{ + const std::string body = R"({"key": "value"})"; + const auto req = + http_client::CreateHttpRequest(boost::beast::http::verb::post, "/submit", "localhost", "", body, ""); + + EXPECT_EQ(req.method(), boost::beast::http::verb::post); + EXPECT_EQ(req.target(), "/submit"); + EXPECT_EQ(req.version(), 11); + EXPECT_EQ(req[boost::beast::http::field::host], "localhost"); + EXPECT_EQ(req[boost::beast::http::field::user_agent], BOOST_BEAST_VERSION_STRING); + EXPECT_EQ(req[boost::beast::http::field::accept], "application/json"); + EXPECT_EQ(req[boost::beast::http::field::content_type], "application/json"); + EXPECT_EQ(req.body(), body); +} + +TEST(CreateHttpRequestTest, AuthorizationBearerToken) +{ + const std::string token = "dummy_token"; + const auto req = + http_client::CreateHttpRequest(boost::beast::http::verb::get, "/secure", "localhost", token, "", ""); + + EXPECT_EQ(req[boost::beast::http::field::authorization], "Bearer dummy_token"); +} + +TEST(CreateHttpRequestTest, AuthorizationBasic) +{ + const std::string user_pass = "username:password"; + const auto req = + http_client::CreateHttpRequest(boost::beast::http::verb::get, "/secure", "localhost", "", "", user_pass); + + EXPECT_EQ(req[boost::beast::http::field::authorization], "Basic username:password"); +} + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}