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