1c33a039bSNan Zhou #include "http_utility.hpp"
2c33a039bSNan Zhou 
3*f0b59af4SEd Tanous #include <array>
4*f0b59af4SEd Tanous 
5c33a039bSNan Zhou #include <gtest/gtest.h> // IWYU pragma: keep
6c33a039bSNan Zhou 
7c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-message.h>
8c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-test-part.h>
9c33a039bSNan Zhou // IWYU pragma: no_include "gtest/gtest_pred_impl.h"
10c33a039bSNan Zhou 
11c33a039bSNan Zhou namespace http_helpers
12c33a039bSNan Zhou {
13c33a039bSNan Zhou namespace
14c33a039bSNan Zhou {
15c33a039bSNan Zhou 
16c33a039bSNan Zhou TEST(isContentTypeAllowed, PositiveTest)
17c33a039bSNan Zhou {
184a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
19c33a039bSNan Zhou     EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
204a0e1a0cSEd Tanous                                      ContentType::OctetStream, false));
214a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
22c33a039bSNan Zhou     EXPECT_TRUE(
234a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::JSON, false));
244a0e1a0cSEd Tanous     EXPECT_TRUE(
254a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
264a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
274a0e1a0cSEd Tanous                                      ContentType::HTML, false));
28c33a039bSNan Zhou }
29c33a039bSNan Zhou 
30c33a039bSNan Zhou TEST(isContentTypeAllowed, NegativeTest)
31c33a039bSNan Zhou {
324a0e1a0cSEd Tanous     EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
334a0e1a0cSEd Tanous                                       ContentType::HTML, false));
34c33a039bSNan Zhou     EXPECT_FALSE(
354a0e1a0cSEd Tanous         isContentTypeAllowed("application/html", ContentType::JSON, false));
364a0e1a0cSEd Tanous     EXPECT_FALSE(
374a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::CBOR, false));
384a0e1a0cSEd Tanous     EXPECT_FALSE(
394a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::HTML, false));
40c33a039bSNan Zhou     EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
414a0e1a0cSEd Tanous                                       ContentType::OctetStream, false));
42c33a039bSNan Zhou }
43c33a039bSNan Zhou 
44c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
45c33a039bSNan Zhou {
46c33a039bSNan Zhou     EXPECT_TRUE(
474a0e1a0cSEd Tanous         isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
48c33a039bSNan Zhou }
49c33a039bSNan Zhou 
50c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
51c33a039bSNan Zhou {
524a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
534a0e1a0cSEd Tanous                                      ContentType::OctetStream, true));
54c33a039bSNan Zhou }
55c33a039bSNan Zhou 
568ece0e45SEd Tanous TEST(getPreferredContentType, PositiveTest)
57c33a039bSNan Zhou {
58c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::HTML};
59c33a039bSNan Zhou     EXPECT_EQ(
608ece0e45SEd Tanous         getPreferredContentType("text/html, application/json", contentType),
61c33a039bSNan Zhou         ContentType::HTML);
62c33a039bSNan Zhou 
63c33a039bSNan Zhou     std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
648ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
65c33a039bSNan Zhou               ContentType::HTML);
66c33a039bSNan Zhou 
67c33a039bSNan Zhou     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
688ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
69c33a039bSNan Zhou               ContentType::HTML);
70c33a039bSNan Zhou 
71c33a039bSNan Zhou     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
728ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
738ece0e45SEd Tanous                                       cborJson),
74c33a039bSNan Zhou               ContentType::CBOR);
75c33a039bSNan Zhou 
768ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("application/json", cborJson),
77c33a039bSNan Zhou               ContentType::JSON);
788ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
79c33a039bSNan Zhou }
80c33a039bSNan Zhou 
818ece0e45SEd Tanous TEST(getPreferredContentType, NegativeTest)
82c33a039bSNan Zhou {
83c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::CBOR};
84c33a039bSNan Zhou     EXPECT_EQ(
858ece0e45SEd Tanous         getPreferredContentType("text/html, application/json", contentType),
86c33a039bSNan Zhou         ContentType::NoMatch);
87c33a039bSNan Zhou }
88c33a039bSNan Zhou } // namespace
89c33a039bSNan Zhou } // namespace http_helpers
90