xref: /openbmc/bmcweb/test/include/http_utility_test.cpp (revision 56431b29998d58c43b101b5f55401e505c85be5e)
1 #include "http_utility.hpp"
2 
3 #include <array>
4 
5 #include <gtest/gtest.h>
6 
7 namespace http_helpers
8 {
9 namespace
10 {
11 
12 TEST(isContentTypeAllowed, PositiveTest)
13 {
14     EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
15     EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
16                                      ContentType::OctetStream, false));
17     EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
18     EXPECT_TRUE(
19         isContentTypeAllowed("application/json", ContentType::JSON, false));
20     EXPECT_TRUE(
21         isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
22     EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
23                                      ContentType::HTML, false));
24 }
25 
26 TEST(isContentTypeAllowed, NegativeTest)
27 {
28     EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
29                                       ContentType::HTML, false));
30     EXPECT_FALSE(
31         isContentTypeAllowed("application/html", ContentType::JSON, false));
32     EXPECT_FALSE(
33         isContentTypeAllowed("application/json", ContentType::CBOR, false));
34     EXPECT_FALSE(
35         isContentTypeAllowed("application/cbor", ContentType::HTML, false));
36     EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
37                                       ContentType::OctetStream, false));
38 }
39 
40 TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
41 {
42     EXPECT_TRUE(
43         isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
44 }
45 
46 TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
47 {
48     EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
49                                      ContentType::OctetStream, true));
50 }
51 
52 TEST(getPreferredContentType, PositiveTest)
53 {
54     std::array<ContentType, 1> contentType{ContentType::HTML};
55     EXPECT_EQ(
56         getPreferredContentType("text/html, application/json", contentType),
57         ContentType::HTML);
58 
59     std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
60     EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
61               ContentType::HTML);
62 
63     // String the chrome gives
64     EXPECT_EQ(getPreferredContentType(
65                   "text/html,"
66                   "application/xhtml+xml,"
67                   "application/xml;q=0.9,"
68                   "image/avif,"
69                   "image/webp,"
70                   "image/apng,*/*;q=0.8,"
71                   "application/signed-exchange;v=b3;q=0.7",
72                   htmlJson),
73               ContentType::HTML);
74 
75     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
76     EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
77               ContentType::HTML);
78 
79     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
80     EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
81                                       cborJson),
82               ContentType::CBOR);
83 
84     EXPECT_EQ(
85         getPreferredContentType("application/json;charset=UTF-8", htmlJson),
86         ContentType::JSON);
87 
88     std::array<ContentType, 1> eventStream{ContentType::EventStream};
89     EXPECT_EQ(
90         getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
91         ContentType::EventStream);
92 
93     EXPECT_EQ(getPreferredContentType("application/json", cborJson),
94               ContentType::JSON);
95     EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
96 
97     // Application types with odd characters
98     EXPECT_EQ(getPreferredContentType(
99                   "application/prs.nprend, application/json", cborJson),
100               ContentType::JSON);
101 
102     EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
103                                       cborJson),
104               ContentType::JSON);
105 
106     // Q values are ignored, but should parse
107     EXPECT_EQ(getPreferredContentType(
108                   "application/rdf+xml;q=0.9, application/json", cborJson),
109               ContentType::JSON);
110     EXPECT_EQ(getPreferredContentType(
111                   "application/rdf+xml;q=1, application/json", cborJson),
112               ContentType::JSON);
113     EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
114               ContentType::JSON);
115     EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
116               ContentType::JSON);
117 }
118 
119 TEST(getPreferredContentType, NegativeTest)
120 {
121     std::array<ContentType, 1> contentType{ContentType::CBOR};
122     EXPECT_EQ(
123         getPreferredContentType("text/html, application/json", contentType),
124         ContentType::NoMatch);
125 }
126 
127 TEST(getPreferredEncoding, PositiveTest)
128 {
129     std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
130     EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
131 
132     std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
133     EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
134     EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
135 
136     EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
137 
138     EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
139               Encoding::ZSTD);
140 }
141 
142 TEST(getPreferredEncoding, NegativeTest)
143 {
144     std::array<Encoding, 2> contentType{Encoding::GZIP,
145                                         Encoding::UnencodedBytes};
146     EXPECT_EQ(getPreferredEncoding("noexist", contentType),
147               Encoding::UnencodedBytes);
148 
149     std::array<Encoding, 1> contentType2{Encoding::GZIP};
150     EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
151 }
152 
153 } // namespace
154 } // namespace http_helpers
155