Improve content type
We have a number of specialized content-type functions for varying
levels of degree, and most of them rely on quite a few strings. This
commit changes them to consolidate on two APIs.
isContentTypeSupported, which as the name implies, takes a single
content type, and returns a bool about whether or not that content type
is allowed.
getPreferedContentType, which takes an array of multiple options, and
fine the first one in the list that matches the clients expected string.
These two functions makes these functions more able to be reused in the
future, and don't require specialized entries for each possible type or
combination of types that we need to check for.
Tested: Unit tests passing. Pretty good coverage.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I8b976d0cefec5f24e62fbbfae33d12cc803cb373
diff --git a/include/ut/http_utility_test.cpp b/include/ut/http_utility_test.cpp
index 1a33fd8..d1df6d1 100644
--- a/include/ut/http_utility_test.cpp
+++ b/include/ut/http_utility_test.cpp
@@ -11,37 +11,72 @@
namespace
{
-TEST(RequestPrefersHtml, ContainsHtmlReturnsTrue)
+TEST(isContentTypeAllowed, PositiveTest)
{
- EXPECT_TRUE(requestPrefersHtml("text/html, application/json"));
+ EXPECT_TRUE(isContentTypeAllowed("*/*, application/octet-stream",
+ ContentType::OctetStream));
+ EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
+ ContentType::OctetStream));
+ EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML));
+ EXPECT_TRUE(isContentTypeAllowed("application/json", ContentType::JSON));
+ EXPECT_TRUE(isContentTypeAllowed("application/cbor", ContentType::CBOR));
+ EXPECT_TRUE(
+ isContentTypeAllowed("application/json, text/html", ContentType::HTML));
}
-TEST(RequestPrefersHtml, NoHtmlReturnsFalse)
+TEST(isContentTypeAllowed, NegativeTest)
{
- EXPECT_FALSE(requestPrefersHtml("*/*, application/octet-stream"));
- EXPECT_FALSE(requestPrefersHtml("application/json"));
+ EXPECT_FALSE(
+ isContentTypeAllowed("application/octet-stream", ContentType::HTML));
+ EXPECT_FALSE(isContentTypeAllowed("application/html", ContentType::JSON));
+ EXPECT_FALSE(isContentTypeAllowed("application/json", ContentType::CBOR));
+ EXPECT_FALSE(isContentTypeAllowed("application/cbor", ContentType::HTML));
+ EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
+ ContentType::OctetStream));
}
-TEST(IsOctetAccepted, ContainsOctetReturnsTrue)
+TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
{
- EXPECT_TRUE(isOctetAccepted("*/*, application/octet-stream"));
+ EXPECT_TRUE(
+ isContentTypeAllowed("text/html, */*", ContentType::OctetStream));
}
-TEST(IsOctetAccepted, ContainsAnyMimeTypeReturnsTrue)
+TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
{
- EXPECT_TRUE(http_helpers::isOctetAccepted("text/html, */*"));
+ EXPECT_TRUE(
+ isContentTypeAllowed("text/html, */*;q=0.8", ContentType::OctetStream));
}
-TEST(IsOctetAccepted, ContainsQFactorWeightingReturnsTrue)
+TEST(getPreferedContentType, PositiveTest)
{
- EXPECT_TRUE(http_helpers::isOctetAccepted("text/html, */*;q=0.8"));
+ std::array<ContentType, 1> contentType{ContentType::HTML};
+ EXPECT_EQ(
+ getPreferedContentType("text/html, application/json", contentType),
+ ContentType::HTML);
+
+ std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
+ EXPECT_EQ(getPreferedContentType("text/html, application/json", htmlJson),
+ ContentType::HTML);
+
+ std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
+ EXPECT_EQ(getPreferedContentType("text/html, application/json", jsonHtml),
+ ContentType::HTML);
+
+ std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
+ EXPECT_EQ(
+ getPreferedContentType("application/cbor, application::json", cborJson),
+ ContentType::CBOR);
+
+ EXPECT_EQ(getPreferedContentType("application/json", cborJson),
+ ContentType::JSON);
}
-TEST(IsOctetAccepted, NoOctetReturnsFalse)
+TEST(getPreferedContentType, NegativeTest)
{
- EXPECT_FALSE(isOctetAccepted("text/html, application/json"));
- EXPECT_FALSE(isOctetAccepted("application/json"));
+ std::array<ContentType, 1> contentType{ContentType::CBOR};
+ EXPECT_EQ(
+ getPreferedContentType("text/html, application/json", contentType),
+ ContentType::NoMatch);
}
-
} // namespace
} // namespace http_helpers