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     std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
64     EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
65               ContentType::HTML);
66 
67     std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
68     EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
69                                       cborJson),
70               ContentType::CBOR);
71 
72     EXPECT_EQ(getPreferredContentType("application/json", cborJson),
73               ContentType::JSON);
74     EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
75 }
76 
77 TEST(getPreferredContentType, NegativeTest)
78 {
79     std::array<ContentType, 1> contentType{ContentType::CBOR};
80     EXPECT_EQ(
81         getPreferredContentType("text/html, application/json", contentType),
82         ContentType::NoMatch);
83 }
84 } // namespace
85 } // namespace http_helpers
86