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