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