Skip to content

Commit 03b2e26

Browse files
Add unit and component test
1 parent 7142a19 commit 03b2e26

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

src/UNIXSocketRequest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void UNIXSocketRequest::download(std::variant<TRequestParameters<std::string>,
4242
GetRequest::builder(FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))
4343
.url(arg.url.url(), arg.secureCommunication)
4444
.unixSocketPath(arg.url.unixSocketPath())
45+
.appendHeaders(arg.httpHeaders)
4546
.timeout(timeout)
4647
.userAgent(userAgent)
4748
.outputFile(outputFile)
@@ -102,6 +103,7 @@ void UNIXSocketRequest::post(std::variant<TRequestParameters<std::string>,
102103
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
103104
req.url(arg.url.url(), arg.secureCommunication)
104105
.unixSocketPath(arg.url.unixSocketPath())
106+
.appendHeaders(arg.httpHeaders)
105107
.timeout(timeout)
106108
.userAgent(userAgent)
107109
.template postData<const std::string&>(arg.data)
@@ -128,6 +130,7 @@ void UNIXSocketRequest::post(std::variant<TRequestParameters<std::string>,
128130
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
129131
req.url(arg.url.url(), arg.secureCommunication)
130132
.unixSocketPath(arg.url.unixSocketPath())
133+
.appendHeaders(arg.httpHeaders)
131134
.timeout(timeout)
132135
.userAgent(userAgent)
133136
.template postData<std::string_view>(arg.data)
@@ -156,6 +159,7 @@ void UNIXSocketRequest::post(std::variant<TRequestParameters<std::string>,
156159
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
157160
req.url(arg.url.url(), arg.secureCommunication)
158161
.unixSocketPath(arg.url.unixSocketPath())
162+
.appendHeaders(arg.httpHeaders)
159163
.timeout(timeout)
160164
.userAgent(userAgent)
161165
.template postData<const std::string&>(data)
@@ -234,6 +238,7 @@ void UNIXSocketRequest::get(std::variant<TRequestParameters<std::string>,
234238
GetRequest::builder(FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
235239
req.url(arg.url.url(), arg.secureCommunication)
236240
.unixSocketPath(arg.url.unixSocketPath())
241+
.appendHeaders(arg.httpHeaders)
237242
.timeout(timeout)
238243
.userAgent(userAgent)
239244
.outputFile(outputFile)
@@ -309,6 +314,7 @@ void UNIXSocketRequest::put(std::variant<TRequestParameters<std::string>,
309314
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
310315
req.url(arg.url.url(), arg.secureCommunication)
311316
.unixSocketPath(arg.url.unixSocketPath())
317+
.appendHeaders(arg.httpHeaders)
312318
.timeout(timeout)
313319
.userAgent(userAgent)
314320
.template postData<const std::string&>(arg.data)
@@ -336,6 +342,7 @@ void UNIXSocketRequest::put(std::variant<TRequestParameters<std::string>,
336342
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
337343
req.url(arg.url.url(), arg.secureCommunication)
338344
.unixSocketPath(arg.url.unixSocketPath())
345+
.appendHeaders(arg.httpHeaders)
339346
.timeout(timeout)
340347
.userAgent(userAgent)
341348
.template postData<std::string_view>(arg.data)
@@ -364,6 +371,7 @@ void UNIXSocketRequest::put(std::variant<TRequestParameters<std::string>,
364371
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
365372
req.url(arg.url.url(), arg.secureCommunication)
366373
.template postData<const std::string&>(data)
374+
.unixSocketPath(arg.url.unixSocketPath())
367375
.appendHeaders(arg.httpHeaders)
368376
.timeout(timeout)
369377
.userAgent(userAgent)
@@ -445,6 +453,7 @@ void UNIXSocketRequest::patch(std::variant<TRequestParameters<std::string>,
445453
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
446454
req.url(arg.url.url(), arg.secureCommunication)
447455
.unixSocketPath(arg.url.unixSocketPath())
456+
.appendHeaders(arg.httpHeaders)
448457
.timeout(timeout)
449458
.userAgent(userAgent)
450459
.template postData<const std::string&>(arg.data)
@@ -472,6 +481,7 @@ void UNIXSocketRequest::patch(std::variant<TRequestParameters<std::string>,
472481
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
473482
req.url(arg.url.url(), arg.secureCommunication)
474483
.unixSocketPath(arg.url.unixSocketPath())
484+
.appendHeaders(arg.httpHeaders)
475485
.timeout(timeout)
476486
.userAgent(userAgent)
477487
.template postData<std::string_view>(arg.data)
@@ -500,6 +510,7 @@ void UNIXSocketRequest::patch(std::variant<TRequestParameters<std::string>,
500510
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
501511
req.url(arg.url.url(), arg.secureCommunication)
502512
.unixSocketPath(arg.url.unixSocketPath())
513+
.appendHeaders(arg.httpHeaders)
503514
.timeout(timeout)
504515
.userAgent(userAgent)
505516
.template postData<const std::string&>(data)
@@ -581,6 +592,7 @@ void UNIXSocketRequest::delete_(std::variant<TRequestParameters<std::string>,
581592
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
582593
req.url(arg.url.url(), arg.secureCommunication)
583594
.unixSocketPath(arg.url.unixSocketPath())
595+
.appendHeaders(arg.httpHeaders)
584596
.timeout(timeout)
585597
.userAgent(userAgent)
586598
.template postData<const std::string&>(arg.data)
@@ -608,6 +620,7 @@ void UNIXSocketRequest::delete_(std::variant<TRequestParameters<std::string>,
608620
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
609621
req.url(arg.url.url(), arg.secureCommunication)
610622
.unixSocketPath(arg.url.unixSocketPath())
623+
.appendHeaders(arg.httpHeaders)
611624
.timeout(timeout)
612625
.userAgent(userAgent)
613626
.template postData<std::string_view>(arg.data)
@@ -635,6 +648,7 @@ void UNIXSocketRequest::delete_(std::variant<TRequestParameters<std::string>,
635648
auto req {DeleteRequest::builder(
636649
FactoryRequestWrapper<wrapperType>::create(response, handlerType, shouldRun))};
637650
req.url(arg.url.url(), arg.secureCommunication)
651+
.unixSocketPath(arg.url.unixSocketPath())
638652
.template postData<const std::string&>(data)
639653
.appendHeaders(arg.httpHeaders)
640654
.timeout(timeout)

test/component/component_test.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,3 +1619,107 @@ TEST_F(ComponentTestInterface, Post100MbsStringView)
16191619

16201620
EXPECT_LE(postPost, prePost + SERVER_RSS_USAGE);
16211621
}
1622+
1623+
/**
1624+
* @brief Test the delete request sending a JSON payload.
1625+
*/
1626+
TEST_F(ComponentTestInterface, DeleteHelloWorldWithPayloadJson)
1627+
{
1628+
bool errorOccurred = false;
1629+
1630+
HTTPRequest::instance().delete_(
1631+
RequestParametersJson {.url = HttpURL("http://localhost:44441/"), .data = R"({"hello":"world"})"_json},
1632+
PostRequestParameters {.onSuccess =
1633+
[&](const std::string& result)
1634+
{
1635+
EXPECT_EQ(result, R"({"hello":"world"})");
1636+
m_callbackComplete = true;
1637+
},
1638+
.onError =
1639+
[&](const std::string& error, const long, const std::string&)
1640+
{
1641+
errorOccurred = true;
1642+
GTEST_SKIP() << "Test server not available on localhost:44441 - " << error;
1643+
}});
1644+
1645+
if (!errorOccurred)
1646+
{
1647+
EXPECT_TRUE(m_callbackComplete);
1648+
}
1649+
else
1650+
{
1651+
SUCCEED();
1652+
}
1653+
}
1654+
1655+
/**
1656+
* @brief Test the delete request sending a raw payload (echo body).
1657+
*/
1658+
TEST_F(ComponentTestInterface, DeleteHelloWorldWithPayloadRaw)
1659+
{
1660+
bool errorOccurred = false;
1661+
1662+
HTTPRequest::instance().delete_(
1663+
RequestParameters {.url = HttpURL("http://localhost:44441/"), .data = "hello world"},
1664+
PostRequestParameters {.onSuccess =
1665+
[&](const std::string& result)
1666+
{
1667+
EXPECT_EQ(result, "hello world");
1668+
m_callbackComplete = true;
1669+
},
1670+
.onError =
1671+
[&](const std::string& error, const long, const std::string&)
1672+
{
1673+
errorOccurred = true;
1674+
GTEST_SKIP() << "Test server not available on localhost:44441 - " << error;
1675+
}});
1676+
1677+
if (!errorOccurred)
1678+
{
1679+
EXPECT_TRUE(m_callbackComplete);
1680+
}
1681+
else
1682+
{
1683+
SUCCEED();
1684+
}
1685+
}
1686+
1687+
/**
1688+
* @brief Test the DELETE request appending a custom header and sending payload.
1689+
*/
1690+
TEST_F(ComponentTestInterface, DeleteWithCustomHeaderAndPayload)
1691+
{
1692+
const std::string headerKey {"Custom-Key"};
1693+
const std::string headerValue {"Custom-Value"};
1694+
bool errorOccurred = false;
1695+
1696+
HTTPRequest::instance().delete_(
1697+
RequestParametersJson {.url = HttpURL("http://localhost:44441/check-headers-and-body"),
1698+
.data = R"({"hello":"world"})"_json,
1699+
.httpHeaders = {headerKey + ":" + headerValue}},
1700+
PostRequestParameters {.onSuccess =
1701+
[&](const std::string& result)
1702+
{
1703+
const auto response = nlohmann::json::parse(result);
1704+
1705+
ASSERT_EQ(response.at("headers").at(headerKey), headerValue);
1706+
ASSERT_EQ(response.at("body"), R"({"hello":"world"})");
1707+
1708+
m_callbackComplete = true;
1709+
},
1710+
.onError =
1711+
[&](const std::string& error, const long, const std::string&)
1712+
{
1713+
errorOccurred = true;
1714+
GTEST_SKIP() << "Test server not available on localhost:44441 - " << error;
1715+
}});
1716+
1717+
if (!errorOccurred)
1718+
{
1719+
EXPECT_TRUE(m_callbackComplete);
1720+
}
1721+
else
1722+
{
1723+
SUCCEED();
1724+
}
1725+
}

test/component/component_test.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ class FakeServer final
236236
[&getHttpHeaders](const httplib::Request& req, httplib::Response& res)
237237
{ res.set_content(getHttpHeaders(req).dump(), "text/json"); });
238238

239+
server.Delete("/check-headers-and-body",
240+
[&getHttpHeaders](const httplib::Request& req, httplib::Response& res)
241+
{
242+
nlohmann::json out;
243+
out["headers"] = getHttpHeaders(req);
244+
out["body"] = req.body;
245+
res.set_content(out.dump(), "text/json");
246+
});
247+
248+
server.Delete("/",
249+
[](const httplib::Request& req, httplib::Response& res)
250+
{ res.set_content(req.body, "text/json"); });
251+
239252
server.set_keep_alive_max_count(1);
240253
server.listen("localhost", 44441);
241254
}

test/unit/unit_test.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,74 @@ TEST_F(UrlRequestUnitTest, DeleteApiRequest)
298298
.execute();
299299
}
300300

301+
/**
302+
* @brief This test checks the API DELETE request with post field.
303+
*/
304+
TEST_F(UrlRequestUnitTest, DeleteApiRequestWithPostFields)
305+
{
306+
auto request {std::make_shared<RequestWrapper>()};
307+
308+
constexpr auto payload = R"({"id":"123"})";
309+
310+
EXPECT_CALL(*request, setOptionString(optUrl, "http://www.wazuh.com/")).Times(1);
311+
EXPECT_CALL(*request, setOptionString(optCustomRequest, "DELETE")).Times(1);
312+
EXPECT_CALL(*request, appendHeader("Content-Type: Application/json")).Times(1);
313+
EXPECT_CALL(*request, setOptionString(optUserAgent, "Wazuh-Agent/1.0")).Times(1);
314+
EXPECT_CALL(*request, setOptionString(optCainfo, "cert.ca")).Times(1);
315+
EXPECT_CALL(*request, setOptionLong(optTimeout, 10)).Times(1);
316+
317+
// Payload
318+
EXPECT_CALL(*request, setOptionString(optPostFields, payload)).Times(1);
319+
EXPECT_CALL(*request, setOptionLong(optPostFieldSize, std::string(payload).length())).Times(1);
320+
321+
EXPECT_CALL(*request, setOptionLong(optVerifyPeer, 1L)).Times(1);
322+
EXPECT_CALL(*request, execute()).Times(1);
323+
324+
DeleteRequest::builder(request)
325+
.url("http://www.wazuh.com/")
326+
.appendHeader("Content-Type: Application/json")
327+
.userAgent("Wazuh-Agent/1.0")
328+
.certificate("cert.ca")
329+
.timeout(10)
330+
.template postData<std::string>(payload)
331+
.execute();
332+
}
333+
334+
/**
335+
* @brief This test checks the API DELETE request with post field and unix socket.
336+
*/
337+
TEST_F(UrlRequestUnitTest, DeleteApiRequestWithPostFieldsAndUnixSocket)
338+
{
339+
auto request {std::make_shared<RequestWrapper>()};
340+
341+
constexpr auto payload = R"({"id":"123"})";
342+
343+
EXPECT_CALL(*request, setOptionString(optUnixSocketPath, "/tmp/wazuh-agent.sock")).Times(1);
344+
EXPECT_CALL(*request, setOptionString(optUrl, "http://www.wazuh.com/")).Times(1);
345+
EXPECT_CALL(*request, setOptionString(optCustomRequest, "DELETE")).Times(1);
346+
EXPECT_CALL(*request, appendHeader("Content-Type: Application/json")).Times(1);
347+
EXPECT_CALL(*request, setOptionString(optUserAgent, "Wazuh-Agent/1.0")).Times(1);
348+
EXPECT_CALL(*request, setOptionString(optCainfo, "cert.ca")).Times(1);
349+
EXPECT_CALL(*request, setOptionLong(optTimeout, 10)).Times(1);
350+
351+
// Payload
352+
EXPECT_CALL(*request, setOptionString(optPostFields, payload)).Times(1);
353+
EXPECT_CALL(*request, setOptionLong(optPostFieldSize, std::string(payload).length())).Times(1);
354+
355+
EXPECT_CALL(*request, setOptionLong(optVerifyPeer, 1L)).Times(1);
356+
EXPECT_CALL(*request, execute()).Times(1);
357+
358+
DeleteRequest::builder(request)
359+
.url("http://www.wazuh.com/")
360+
.appendHeader("Content-Type: Application/json")
361+
.userAgent("Wazuh-Agent/1.0")
362+
.certificate("cert.ca")
363+
.timeout(10)
364+
.template postData<std::string>(payload)
365+
.unixSocketPath("/tmp/wazuh-agent.sock")
366+
.execute();
367+
}
368+
301369
/**
302370
* @brief This test checks the malformed API DELETE request.
303371
*/

0 commit comments

Comments
 (0)