1c33a039bSNan Zhou #include "http_utility.hpp" 2c33a039bSNan Zhou 3c33a039bSNan Zhou #include <gtest/gtest.h> // IWYU pragma: keep 4c33a039bSNan Zhou 5c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-message.h> 6c33a039bSNan Zhou // IWYU pragma: no_include <gtest/gtest-test-part.h> 7c33a039bSNan Zhou // IWYU pragma: no_include "gtest/gtest_pred_impl.h" 8c33a039bSNan Zhou 9c33a039bSNan Zhou namespace http_helpers 10c33a039bSNan Zhou { 11c33a039bSNan Zhou namespace 12c33a039bSNan Zhou { 13c33a039bSNan Zhou 14c33a039bSNan Zhou TEST(isContentTypeAllowed, PositiveTest) 15c33a039bSNan Zhou { 164a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true)); 17c33a039bSNan Zhou EXPECT_TRUE(isContentTypeAllowed("application/octet-stream", 184a0e1a0cSEd Tanous ContentType::OctetStream, false)); 194a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false)); 20c33a039bSNan Zhou EXPECT_TRUE( 214a0e1a0cSEd Tanous isContentTypeAllowed("application/json", ContentType::JSON, false)); 224a0e1a0cSEd Tanous EXPECT_TRUE( 234a0e1a0cSEd Tanous isContentTypeAllowed("application/cbor", ContentType::CBOR, false)); 244a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("application/json, text/html", 254a0e1a0cSEd Tanous ContentType::HTML, false)); 26c33a039bSNan Zhou } 27c33a039bSNan Zhou 28c33a039bSNan Zhou TEST(isContentTypeAllowed, NegativeTest) 29c33a039bSNan Zhou { 304a0e1a0cSEd Tanous EXPECT_FALSE(isContentTypeAllowed("application/octet-stream", 314a0e1a0cSEd Tanous ContentType::HTML, false)); 32c33a039bSNan Zhou EXPECT_FALSE( 334a0e1a0cSEd Tanous isContentTypeAllowed("application/html", ContentType::JSON, false)); 344a0e1a0cSEd Tanous EXPECT_FALSE( 354a0e1a0cSEd Tanous isContentTypeAllowed("application/json", ContentType::CBOR, false)); 364a0e1a0cSEd Tanous EXPECT_FALSE( 374a0e1a0cSEd Tanous isContentTypeAllowed("application/cbor", ContentType::HTML, false)); 38c33a039bSNan Zhou EXPECT_FALSE(isContentTypeAllowed("application/json, text/html", 394a0e1a0cSEd Tanous ContentType::OctetStream, false)); 40c33a039bSNan Zhou } 41c33a039bSNan Zhou 42c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue) 43c33a039bSNan Zhou { 44c33a039bSNan Zhou EXPECT_TRUE( 454a0e1a0cSEd Tanous isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true)); 46c33a039bSNan Zhou } 47c33a039bSNan Zhou 48c33a039bSNan Zhou TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue) 49c33a039bSNan Zhou { 504a0e1a0cSEd Tanous EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8", 514a0e1a0cSEd Tanous ContentType::OctetStream, true)); 52c33a039bSNan Zhou } 53c33a039bSNan Zhou 54*8ece0e45SEd Tanous TEST(getPreferredContentType, PositiveTest) 55c33a039bSNan Zhou { 56c33a039bSNan Zhou std::array<ContentType, 1> contentType{ContentType::HTML}; 57c33a039bSNan Zhou EXPECT_EQ( 58*8ece0e45SEd Tanous getPreferredContentType("text/html, application/json", contentType), 59c33a039bSNan Zhou ContentType::HTML); 60c33a039bSNan Zhou 61c33a039bSNan Zhou std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON}; 62*8ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson), 63c33a039bSNan Zhou ContentType::HTML); 64c33a039bSNan Zhou 65c33a039bSNan Zhou std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML}; 66*8ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml), 67c33a039bSNan Zhou ContentType::HTML); 68c33a039bSNan Zhou 69c33a039bSNan Zhou std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON}; 70*8ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("application/cbor, application::json", 71*8ece0e45SEd Tanous cborJson), 72c33a039bSNan Zhou ContentType::CBOR); 73c33a039bSNan Zhou 74*8ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("application/json", cborJson), 75c33a039bSNan Zhou ContentType::JSON); 76*8ece0e45SEd Tanous EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY); 77c33a039bSNan Zhou } 78c33a039bSNan Zhou 79*8ece0e45SEd Tanous TEST(getPreferredContentType, NegativeTest) 80c33a039bSNan Zhou { 81c33a039bSNan Zhou std::array<ContentType, 1> contentType{ContentType::CBOR}; 82c33a039bSNan Zhou EXPECT_EQ( 83*8ece0e45SEd Tanous getPreferredContentType("text/html, application/json", contentType), 84c33a039bSNan Zhou ContentType::NoMatch); 85c33a039bSNan Zhou } 86c33a039bSNan Zhou } // namespace 87c33a039bSNan Zhou } // namespace http_helpers 88