1c33a039bSNan Zhou #include "http_utility.hpp"
2c33a039bSNan Zhou 
3c33a039bSNan Zhou #include <gtest/gtest.h> // IWYU pragma: keep
4c33a039bSNan Zhou 
5c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-message.h>
6c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-test-part.h>
7c33a039bSNan Zhou // IWYU pragma: no_include "gtest/gtest_pred_impl.h"
8c33a039bSNan Zhou 
9c33a039bSNan Zhou namespace http_helpers
10c33a039bSNan Zhou {
11c33a039bSNan Zhou namespace
12c33a039bSNan Zhou {
13c33a039bSNan Zhou 
14c33a039bSNan Zhou TEST(isContentTypeAllowed, PositiveTest)
15c33a039bSNan Zhou {
16*4a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
17c33a039bSNan Zhou     EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
18*4a0e1a0cSEd Tanous                                      ContentType::OctetStream, false));
19*4a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
20c33a039bSNan Zhou     EXPECT_TRUE(
21*4a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::JSON, false));
22*4a0e1a0cSEd Tanous     EXPECT_TRUE(
23*4a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
24*4a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
25*4a0e1a0cSEd Tanous                                      ContentType::HTML, false));
26c33a039bSNan Zhou }
27c33a039bSNan Zhou 
28c33a039bSNan Zhou TEST(isContentTypeAllowed, NegativeTest)
29c33a039bSNan Zhou {
30*4a0e1a0cSEd Tanous     EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
31*4a0e1a0cSEd Tanous                                       ContentType::HTML, false));
32c33a039bSNan Zhou     EXPECT_FALSE(
33*4a0e1a0cSEd Tanous         isContentTypeAllowed("application/html", ContentType::JSON, false));
34*4a0e1a0cSEd Tanous     EXPECT_FALSE(
35*4a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::CBOR, false));
36*4a0e1a0cSEd Tanous     EXPECT_FALSE(
37*4a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::HTML, false));
38c33a039bSNan Zhou     EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
39*4a0e1a0cSEd Tanous                                       ContentType::OctetStream, false));
40c33a039bSNan Zhou }
41c33a039bSNan Zhou 
42c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
43c33a039bSNan Zhou {
44c33a039bSNan Zhou     EXPECT_TRUE(
45*4a0e1a0cSEd Tanous         isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
46c33a039bSNan Zhou }
47c33a039bSNan Zhou 
48c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
49c33a039bSNan Zhou {
50*4a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
51*4a0e1a0cSEd Tanous                                      ContentType::OctetStream, true));
52c33a039bSNan Zhou }
53c33a039bSNan Zhou 
54c33a039bSNan Zhou TEST(getPreferedContentType, PositiveTest)
55c33a039bSNan Zhou {
56c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::HTML};
57c33a039bSNan Zhou     EXPECT_EQ(
58c33a039bSNan Zhou         getPreferedContentType("text/html, application/json", contentType),
59c33a039bSNan Zhou         ContentType::HTML);
60c33a039bSNan Zhou 
61c33a039bSNan Zhou     std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
62c33a039bSNan Zhou     EXPECT_EQ(getPreferedContentType("text/html, application/json", htmlJson),
63c33a039bSNan Zhou               ContentType::HTML);
64c33a039bSNan Zhou 
65c33a039bSNan Zhou     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
66c33a039bSNan Zhou     EXPECT_EQ(getPreferedContentType("text/html, application/json", jsonHtml),
67c33a039bSNan Zhou               ContentType::HTML);
68c33a039bSNan Zhou 
69c33a039bSNan Zhou     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
70c33a039bSNan Zhou     EXPECT_EQ(
71c33a039bSNan Zhou         getPreferedContentType("application/cbor, application::json", cborJson),
72c33a039bSNan Zhou         ContentType::CBOR);
73c33a039bSNan Zhou 
74c33a039bSNan Zhou     EXPECT_EQ(getPreferedContentType("application/json", cborJson),
75c33a039bSNan Zhou               ContentType::JSON);
76*4a0e1a0cSEd Tanous     EXPECT_EQ(getPreferedContentType("*/*", cborJson), ContentType::ANY);
77c33a039bSNan Zhou }
78c33a039bSNan Zhou 
79c33a039bSNan Zhou TEST(getPreferedContentType, NegativeTest)
80c33a039bSNan Zhou {
81c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::CBOR};
82c33a039bSNan Zhou     EXPECT_EQ(
83c33a039bSNan Zhou         getPreferedContentType("text/html, application/json", contentType),
84c33a039bSNan Zhou         ContentType::NoMatch);
85c33a039bSNan Zhou }
86c33a039bSNan Zhou } // namespace
87c33a039bSNan Zhou } // namespace http_helpers
88