1 #include "http_utility.hpp"
2 
3 #include <array>
4 
5 #include <gtest/gtest.h>
6 
7 namespace http_helpers
8 {
9 namespace
10 {
11 
12 TEST(isContentTypeAllowed, PositiveTest)
13 {
14     EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
15     EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
16                                      ContentType::OctetStream, false));
17     EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
18     EXPECT_TRUE(
19         isContentTypeAllowed("application/json", ContentType::JSON, false));
20     EXPECT_TRUE(
21         isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
22     EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
23                                      ContentType::HTML, false));
24 }
25 
26 TEST(isContentTypeAllowed, NegativeTest)
27 {
28     EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
29                                       ContentType::HTML, false));
30     EXPECT_FALSE(
31         isContentTypeAllowed("application/html", ContentType::JSON, false));
32     EXPECT_FALSE(
33         isContentTypeAllowed("application/json", ContentType::CBOR, false));
34     EXPECT_FALSE(
35         isContentTypeAllowed("application/cbor", ContentType::HTML, false));
36     EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
37                                       ContentType::OctetStream, false));
38 }
39 
40 TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
41 {
42     EXPECT_TRUE(
43         isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
44 }
45 
46 TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
47 {
48     EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
49                                      ContentType::OctetStream, true));
50 }
51 
52 TEST(getPreferredContentType, PositiveTest)
53 {
54     std::array<ContentType, 1> contentType{ContentType::HTML};
55     EXPECT_EQ(
56         getPreferredContentType("text/html, application/json", contentType),
57         ContentType::HTML);
58 
59     std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
60     EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
61               ContentType::HTML);
62 
63     // String the chrome gives
64     EXPECT_EQ(getPreferredContentType(
65                   "text/html,"
66                   "application/xhtml+xml,"
67                   "application/xml;q=0.9,"
68                   "image/avif,"
69                   "image/webp,"
70                   "image/apng,*/*;q=0.8,"
71                   "application/signed-exchange;v=b3;q=0.7",
72                   htmlJson),
73               ContentType::HTML);
74 
75     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
76     EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
77               ContentType::HTML);
78 
79     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
80     EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
81                                       cborJson),
82               ContentType::CBOR);
83 
84     EXPECT_EQ(getPreferredContentType("application/json", cborJson),
85               ContentType::JSON);
86     EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
87 
88     // Application types with odd characters
89     EXPECT_EQ(getPreferredContentType(
90                   "application/prs.nprend, application/json", cborJson),
91               ContentType::JSON);
92 
93     EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
94                                       cborJson),
95               ContentType::JSON);
96 
97     // Q values are ignored, but should parse
98     EXPECT_EQ(getPreferredContentType(
99                   "application/rdf+xml;q=0.9, application/json", cborJson),
100               ContentType::JSON);
101     EXPECT_EQ(getPreferredContentType(
102                   "application/rdf+xml;q=1, application/json", cborJson),
103               ContentType::JSON);
104     EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
105               ContentType::JSON);
106     EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
107               ContentType::JSON);
108 }
109 
110 TEST(getPreferredContentType, NegativeTest)
111 {
112     std::array<ContentType, 1> contentType{ContentType::CBOR};
113     EXPECT_EQ(
114         getPreferredContentType("text/html, application/json", contentType),
115         ContentType::NoMatch);
116 }
117 
118 TEST(getPreferredEncoding, PositiveTest)
119 {
120     std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
121     EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
122 
123     std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
124     EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
125     EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
126 
127     EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
128 
129     EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
130               Encoding::ZSTD);
131 }
132 
133 TEST(getPreferredEncoding, NegativeTest)
134 {
135     std::array<Encoding, 2> contentType{Encoding::GZIP,
136                                         Encoding::UnencodedBytes};
137     EXPECT_EQ(getPreferredEncoding("noexist", contentType),
138               Encoding::UnencodedBytes);
139 
140     std::array<Encoding, 1> contentType2{Encoding::GZIP};
141     EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
142 }
143 
144 } // namespace
145 } // namespace http_helpers
146