Skip to content

Commit

Permalink
feat: add tests for CreateHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
jr0me committed Aug 7, 2024
1 parent bc27112 commit f0e0f0a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/agent/communicator/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
60 changes: 60 additions & 0 deletions src/agent/communicator/tests/http_client_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <gtest/gtest.h>

#include <http_client.hpp>

#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>

#include <string>

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();
}

0 comments on commit f0e0f0a

Please sign in to comment.