1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
#include "app.hpp"
#include "async_resp.hpp"
#include "http_request.hpp"
#include "redfish.hpp"
#include "sub_request.hpp"
#include "verb.hpp"
#include <boost/beast/http/verb.hpp>
#include <nlohmann/json.hpp>
#include <memory>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <gtest/gtest.h>
namespace redfish
{
namespace
{
TEST(OemRouter, FragmentRoutes)
{
std::error_code ec;
App app;
RedfishService service(app);
// Callback handler that does nothing
bool oemCalled = false;
auto oemCallback = [&oemCalled](const SubRequest&,
const std::shared_ptr<bmcweb::AsyncResp>&,
const std::string& bar) {
oemCalled = true;
EXPECT_EQ(bar, "bar");
};
bool standardCalled = false;
auto standardCallback =
[&standardCalled,
&service](const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& bar) {
service.handleSubRoute(req, asyncResp);
standardCalled = true;
EXPECT_EQ(bar, "bar");
};
// Need the normal route registered for OEM to work
BMCWEB_ROUTE(app, "/foo/<str>/")
.methods(boost::beast::http::verb::get)(standardCallback);
REDFISH_SUB_ROUTE<"/foo/<str>/#/Oem">(service, HttpVerb::Get)(oemCallback);
app.validate();
service.validate();
{
constexpr std::string_view reqUrl = "/foo/bar";
std::shared_ptr<crow::Request> req = std::make_shared<crow::Request>(
crow::Request::Body{boost::beast::http::verb::get, reqUrl, 11}, ec);
std::shared_ptr<bmcweb::AsyncResp> asyncResp =
std::make_shared<bmcweb::AsyncResp>();
app.handle(req, asyncResp);
}
EXPECT_TRUE(oemCalled);
EXPECT_TRUE(standardCalled);
}
TEST(OemRouter, PatchHandlerWithJsonObject)
{
std::error_code ec;
App app;
RedfishService service(app);
// Callback handlers
bool callback1Called = false;
auto patchCallback1 =
[&callback1Called](
const SubRequest& req,
[[maybe_unused]] const std::shared_ptr<bmcweb::AsyncResp>&
asyncResp,
[[maybe_unused]] const std::string& param) {
callback1Called = true;
const nlohmann::json::object_t& payload = req.payload();
auto oemFooIt = payload.find("OemFoo");
ASSERT_NE(oemFooIt, payload.end());
ASSERT_TRUE(oemFooIt->second.is_object());
auto keyIt = oemFooIt->second.find("OemFooKey");
ASSERT_NE(keyIt, oemFooIt->second.end());
EXPECT_EQ(keyIt.value(), "fooValue");
};
bool callback2Called = false;
auto patchCallback2 =
[&callback2Called](
const SubRequest& req,
[[maybe_unused]] const std::shared_ptr<bmcweb::AsyncResp>&
asyncResp,
[[maybe_unused]] const std::string& param) {
callback2Called = true;
const nlohmann::json::object_t& payload = req.payload();
auto oemBarIt = payload.find("OemBar");
ASSERT_NE(oemBarIt, payload.end());
ASSERT_TRUE(oemBarIt->second.is_object());
auto keyIt = oemBarIt->second.find("OemBarKey");
ASSERT_NE(keyIt, oemBarIt->second.end());
EXPECT_EQ(keyIt.value(), "barValue");
};
bool standardCalled = false;
auto standardCallback =
[&standardCalled,
&service](const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& bar) {
service.handleSubRoute(req, asyncResp);
standardCalled = true;
EXPECT_EQ(bar, "bar");
};
// Need the normal route registered for OEM to work
BMCWEB_ROUTE(app, "/foo/<str>/")
.methods(boost::beast::http::verb::patch)(standardCallback);
REDFISH_SUB_ROUTE<"/foo/<str>/#/Oem/OemFoo">(service, HttpVerb::Patch)(
patchCallback1);
REDFISH_SUB_ROUTE<"/foo/<str>/#/Oem/OemBar">(service, HttpVerb::Patch)(
patchCallback2);
app.validate();
service.validate();
{
constexpr std::string_view reqUrl = "/foo/bar";
// OEM payload
nlohmann::json reqBody = {{"Oem",
{{"OemFoo", {{"OemFooKey", "fooValue"}}},
{"OemBar", {{"OemBarKey", "barValue"}}}}}};
// Create request with the body string
std::shared_ptr<crow::Request> req = std::make_shared<crow::Request>(
crow::Request::Body{
boost::beast::http::verb::patch, reqUrl, 11,
reqBody.dump() // Pass body string directly in the constructor
},
ec);
req->addHeader("Content-Type", "application/json");
std::shared_ptr<bmcweb::AsyncResp> asyncResp =
std::make_shared<bmcweb::AsyncResp>();
app.handle(req, asyncResp);
}
EXPECT_TRUE(standardCalled);
EXPECT_TRUE(callback1Called);
EXPECT_TRUE(callback2Called);
}
} // namespace
} // namespace redfish
|