1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3c33a039bSNan Zhou #include "http_utility.hpp"
4c33a039bSNan Zhou
5f0b59af4SEd Tanous #include <array>
6f0b59af4SEd Tanous
7478b7adfSEd Tanous #include <gtest/gtest.h>
8c33a039bSNan Zhou
9c33a039bSNan Zhou namespace http_helpers
10c33a039bSNan Zhou {
11c33a039bSNan Zhou namespace
12c33a039bSNan Zhou {
13c33a039bSNan Zhou
TEST(isContentTypeAllowed,PositiveTest)14c33a039bSNan Zhou TEST(isContentTypeAllowed, PositiveTest)
15c33a039bSNan Zhou {
164a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
17c33a039bSNan Zhou EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
184a0e1a0cSEd Tanous ContentType::OctetStream, false));
194a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
20c33a039bSNan Zhou EXPECT_TRUE(
214a0e1a0cSEd Tanous isContentTypeAllowed("application/json", ContentType::JSON, false));
224a0e1a0cSEd Tanous EXPECT_TRUE(
234a0e1a0cSEd Tanous isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
244a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
254a0e1a0cSEd Tanous ContentType::HTML, false));
26c33a039bSNan Zhou }
27c33a039bSNan Zhou
TEST(isContentTypeAllowed,NegativeTest)28c33a039bSNan Zhou TEST(isContentTypeAllowed, NegativeTest)
29c33a039bSNan Zhou {
304a0e1a0cSEd Tanous EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
314a0e1a0cSEd Tanous ContentType::HTML, false));
32c33a039bSNan Zhou EXPECT_FALSE(
334a0e1a0cSEd Tanous isContentTypeAllowed("application/html", ContentType::JSON, false));
344a0e1a0cSEd Tanous EXPECT_FALSE(
354a0e1a0cSEd Tanous isContentTypeAllowed("application/json", ContentType::CBOR, false));
364a0e1a0cSEd Tanous EXPECT_FALSE(
374a0e1a0cSEd Tanous isContentTypeAllowed("application/cbor", ContentType::HTML, false));
38c33a039bSNan Zhou EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
394a0e1a0cSEd Tanous ContentType::OctetStream, false));
40c33a039bSNan Zhou }
41c33a039bSNan Zhou
TEST(isContentTypeAllowed,ContainsAnyMimeTypeReturnsTrue)42c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
43c33a039bSNan Zhou {
44c33a039bSNan Zhou EXPECT_TRUE(
454a0e1a0cSEd Tanous isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
46c33a039bSNan Zhou }
47c33a039bSNan Zhou
TEST(isContentTypeAllowed,ContainsQFactorWeightingReturnsTrue)48c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
49c33a039bSNan Zhou {
504a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
514a0e1a0cSEd Tanous ContentType::OctetStream, true));
52c33a039bSNan Zhou }
53c33a039bSNan Zhou
TEST(getPreferredContentType,PositiveTest)548ece0e45SEd Tanous TEST(getPreferredContentType, PositiveTest)
55c33a039bSNan Zhou {
56c33a039bSNan Zhou std::array<ContentType, 1> contentType{ContentType::HTML};
57c33a039bSNan Zhou EXPECT_EQ(
588ece0e45SEd Tanous getPreferredContentType("text/html, application/json", contentType),
59c33a039bSNan Zhou ContentType::HTML);
60c33a039bSNan Zhou
61c33a039bSNan Zhou std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
628ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
63c33a039bSNan Zhou ContentType::HTML);
64c33a039bSNan Zhou
65463b2934SEd Tanous // String the chrome gives
66463b2934SEd Tanous EXPECT_EQ(getPreferredContentType(
67463b2934SEd Tanous "text/html,"
68463b2934SEd Tanous "application/xhtml+xml,"
69463b2934SEd Tanous "application/xml;q=0.9,"
70463b2934SEd Tanous "image/avif,"
71463b2934SEd Tanous "image/webp,"
72463b2934SEd Tanous "image/apng,*/*;q=0.8,"
73463b2934SEd Tanous "application/signed-exchange;v=b3;q=0.7",
74463b2934SEd Tanous htmlJson),
75463b2934SEd Tanous ContentType::HTML);
76463b2934SEd Tanous
77c33a039bSNan Zhou std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
788ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
79c33a039bSNan Zhou ContentType::HTML);
80c33a039bSNan Zhou
81c33a039bSNan Zhou std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
828ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
838ece0e45SEd Tanous cborJson),
84c33a039bSNan Zhou ContentType::CBOR);
85c33a039bSNan Zhou
8680e6e25eSEd Tanous EXPECT_EQ(
8780e6e25eSEd Tanous getPreferredContentType("application/json;charset=UTF-8", htmlJson),
8880e6e25eSEd Tanous ContentType::JSON);
8980e6e25eSEd Tanous
9080e6e25eSEd Tanous std::array<ContentType, 1> eventStream{ContentType::EventStream};
9180e6e25eSEd Tanous EXPECT_EQ(
9280e6e25eSEd Tanous getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
9380e6e25eSEd Tanous ContentType::EventStream);
9480e6e25eSEd Tanous
958ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("application/json", cborJson),
96c33a039bSNan Zhou ContentType::JSON);
978ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
98463b2934SEd Tanous
99463b2934SEd Tanous // Application types with odd characters
100463b2934SEd Tanous EXPECT_EQ(getPreferredContentType(
101463b2934SEd Tanous "application/prs.nprend, application/json", cborJson),
102463b2934SEd Tanous ContentType::JSON);
103463b2934SEd Tanous
104463b2934SEd Tanous EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
105463b2934SEd Tanous cborJson),
106463b2934SEd Tanous ContentType::JSON);
107463b2934SEd Tanous
108463b2934SEd Tanous // Q values are ignored, but should parse
109463b2934SEd Tanous EXPECT_EQ(getPreferredContentType(
110463b2934SEd Tanous "application/rdf+xml;q=0.9, application/json", cborJson),
111463b2934SEd Tanous ContentType::JSON);
112463b2934SEd Tanous EXPECT_EQ(getPreferredContentType(
113463b2934SEd Tanous "application/rdf+xml;q=1, application/json", cborJson),
114463b2934SEd Tanous ContentType::JSON);
115463b2934SEd Tanous EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
116463b2934SEd Tanous ContentType::JSON);
117463b2934SEd Tanous EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
118463b2934SEd Tanous ContentType::JSON);
119c33a039bSNan Zhou }
120c33a039bSNan Zhou
TEST(getPreferredContentType,NegativeTest)1218ece0e45SEd Tanous TEST(getPreferredContentType, NegativeTest)
122c33a039bSNan Zhou {
123c33a039bSNan Zhou std::array<ContentType, 1> contentType{ContentType::CBOR};
124c33a039bSNan Zhou EXPECT_EQ(
1258ece0e45SEd Tanous getPreferredContentType("text/html, application/json", contentType),
126c33a039bSNan Zhou ContentType::NoMatch);
127c33a039bSNan Zhou }
128276ede55SEd Tanous
TEST(getPreferredEncoding,PositiveTest)129276ede55SEd Tanous TEST(getPreferredEncoding, PositiveTest)
130276ede55SEd Tanous {
131276ede55SEd Tanous std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
132276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
133276ede55SEd Tanous
134276ede55SEd Tanous std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
135276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
136276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
137276ede55SEd Tanous
138276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
139276ede55SEd Tanous
140276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
141276ede55SEd Tanous Encoding::ZSTD);
142276ede55SEd Tanous }
143276ede55SEd Tanous
TEST(getPreferredEncoding,NegativeTest)144276ede55SEd Tanous TEST(getPreferredEncoding, NegativeTest)
145276ede55SEd Tanous {
146276ede55SEd Tanous std::array<Encoding, 2> contentType{Encoding::GZIP,
147276ede55SEd Tanous Encoding::UnencodedBytes};
148276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("noexist", contentType),
149276ede55SEd Tanous Encoding::UnencodedBytes);
150276ede55SEd Tanous
151276ede55SEd Tanous std::array<Encoding, 1> contentType2{Encoding::GZIP};
152276ede55SEd Tanous EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
153276ede55SEd Tanous }
154276ede55SEd Tanous
155c33a039bSNan Zhou } // namespace
156c33a039bSNan Zhou } // namespace http_helpers
157