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