1c33a039bSNan Zhou #include "http_utility.hpp"
2c33a039bSNan Zhou 
3f0b59af4SEd Tanous #include <array>
4f0b59af4SEd Tanous 
5*478b7adfSEd Tanous #include <gtest/gtest.h>
6c33a039bSNan Zhou 
7c33a039bSNan Zhou namespace http_helpers
8c33a039bSNan Zhou {
9c33a039bSNan Zhou namespace
10c33a039bSNan Zhou {
11c33a039bSNan Zhou 
TEST(isContentTypeAllowed,PositiveTest)12c33a039bSNan Zhou TEST(isContentTypeAllowed, PositiveTest)
13c33a039bSNan Zhou {
144a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
15c33a039bSNan Zhou     EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
164a0e1a0cSEd Tanous                                      ContentType::OctetStream, false));
174a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
18c33a039bSNan Zhou     EXPECT_TRUE(
194a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::JSON, false));
204a0e1a0cSEd Tanous     EXPECT_TRUE(
214a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
224a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
234a0e1a0cSEd Tanous                                      ContentType::HTML, false));
24c33a039bSNan Zhou }
25c33a039bSNan Zhou 
TEST(isContentTypeAllowed,NegativeTest)26c33a039bSNan Zhou TEST(isContentTypeAllowed, NegativeTest)
27c33a039bSNan Zhou {
284a0e1a0cSEd Tanous     EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
294a0e1a0cSEd Tanous                                       ContentType::HTML, false));
30c33a039bSNan Zhou     EXPECT_FALSE(
314a0e1a0cSEd Tanous         isContentTypeAllowed("application/html", ContentType::JSON, false));
324a0e1a0cSEd Tanous     EXPECT_FALSE(
334a0e1a0cSEd Tanous         isContentTypeAllowed("application/json", ContentType::CBOR, false));
344a0e1a0cSEd Tanous     EXPECT_FALSE(
354a0e1a0cSEd Tanous         isContentTypeAllowed("application/cbor", ContentType::HTML, false));
36c33a039bSNan Zhou     EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
374a0e1a0cSEd Tanous                                       ContentType::OctetStream, false));
38c33a039bSNan Zhou }
39c33a039bSNan Zhou 
TEST(isContentTypeAllowed,ContainsAnyMimeTypeReturnsTrue)40c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
41c33a039bSNan Zhou {
42c33a039bSNan Zhou     EXPECT_TRUE(
434a0e1a0cSEd Tanous         isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
44c33a039bSNan Zhou }
45c33a039bSNan Zhou 
TEST(isContentTypeAllowed,ContainsQFactorWeightingReturnsTrue)46c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
47c33a039bSNan Zhou {
484a0e1a0cSEd Tanous     EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
494a0e1a0cSEd Tanous                                      ContentType::OctetStream, true));
50c33a039bSNan Zhou }
51c33a039bSNan Zhou 
TEST(getPreferredContentType,PositiveTest)528ece0e45SEd Tanous TEST(getPreferredContentType, PositiveTest)
53c33a039bSNan Zhou {
54c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::HTML};
55c33a039bSNan Zhou     EXPECT_EQ(
568ece0e45SEd Tanous         getPreferredContentType("text/html, application/json", contentType),
57c33a039bSNan Zhou         ContentType::HTML);
58c33a039bSNan Zhou 
59c33a039bSNan Zhou     std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
608ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
61c33a039bSNan Zhou               ContentType::HTML);
62c33a039bSNan Zhou 
63c33a039bSNan Zhou     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
648ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
65c33a039bSNan Zhou               ContentType::HTML);
66c33a039bSNan Zhou 
67c33a039bSNan Zhou     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
688ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
698ece0e45SEd Tanous                                       cborJson),
70c33a039bSNan Zhou               ContentType::CBOR);
71c33a039bSNan Zhou 
728ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("application/json", cborJson),
73c33a039bSNan Zhou               ContentType::JSON);
748ece0e45SEd Tanous     EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
75c33a039bSNan Zhou }
76c33a039bSNan Zhou 
TEST(getPreferredContentType,NegativeTest)778ece0e45SEd Tanous TEST(getPreferredContentType, NegativeTest)
78c33a039bSNan Zhou {
79c33a039bSNan Zhou     std::array<ContentType, 1> contentType{ContentType::CBOR};
80c33a039bSNan Zhou     EXPECT_EQ(
818ece0e45SEd Tanous         getPreferredContentType("text/html, application/json", contentType),
82c33a039bSNan Zhou         ContentType::NoMatch);
83c33a039bSNan Zhou }
84c33a039bSNan Zhou } // namespace
85c33a039bSNan Zhou } // namespace http_helpers
86