xref: /openbmc/bmcweb/test/include/json_html_serializer.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "json_html_serializer.hpp"
4 
5 #include <nlohmann/json.hpp>
6 
7 #include <string>
8 
9 #include <gtest/gtest.h>
10 
11 namespace json_html_util
12 {
13 namespace
14 {
15 
16 const std::string boilerplateStart =
17     "<html>\n"
18     "<head>\n"
19     "<title>Redfish API</title>\n"
20     "<link href=\"/styles/redfish.css\" rel=\"stylesheet\">\n"
21     "</head>\n"
22     "<body>\n"
23     "<div class=\"container\">\n"
24     "<img src=\"/images/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" height=\"406px\" width=\"576px\">\n";
25 
26 const std::string boilerplateEnd =
27     "</div>\n"
28     "</body>\n"
29     "</html>\n";
30 
TEST(JsonHtmlSerializer,dumpHtmlLink)31 TEST(JsonHtmlSerializer, dumpHtmlLink)
32 {
33     std::string out;
34     nlohmann::json j;
35     j["@odata.id"] = "/redfish/v1";
36     dumpHtml(out, j);
37     EXPECT_EQ(
38         out,
39         boilerplateStart +
40             "<div class=\"content\">\n"
41             "{<div class=tab>&quot@odata.id&quot: <a href=\"/redfish/v1\">\"/redfish/v1\"</a><br></div>}</div>\n" +
42             boilerplateEnd);
43 }
44 
TEST(JsonHtmlSerializer,dumpint)45 TEST(JsonHtmlSerializer, dumpint)
46 {
47     std::string out;
48     nlohmann::json j = 42;
49     dumpHtml(out, j);
50     EXPECT_EQ(out, boilerplateStart + "<div class=\"content\">\n42</div>\n" +
51                        boilerplateEnd);
52 }
53 
TEST(JsonHtmlSerializer,dumpstring)54 TEST(JsonHtmlSerializer, dumpstring)
55 {
56     std::string out;
57     nlohmann::json j = "foobar";
58     dumpHtml(out, j);
59     EXPECT_EQ(out,
60               boilerplateStart + "<div class=\"content\">\n\"foobar\"</div>\n" +
61                   boilerplateEnd);
62 }
63 } // namespace
64 } // namespace json_html_util
65