Make accepts a real parser
We somewhat copped out a little with regards to this originally, because
writing parsers is hard, and we don't have to implement the full field
of what the Accepts header allows.
We should aim to be correct where we can, so implement a real parser
that parses values, including the floats.
Tested: Unit tests pass, good coverage.
Change-Id: I1b4232929367d230641be9f41f5af6e6dbcea037
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/test/include/http_utility_test.cpp b/test/include/http_utility_test.cpp
index d4823da..72c4759 100644
--- a/test/include/http_utility_test.cpp
+++ b/test/include/http_utility_test.cpp
@@ -60,6 +60,18 @@
EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
ContentType::HTML);
+ // String the chrome gives
+ EXPECT_EQ(getPreferredContentType(
+ "text/html,"
+ "application/xhtml+xml,"
+ "application/xml;q=0.9,"
+ "image/avif,"
+ "image/webp,"
+ "image/apng,*/*;q=0.8,"
+ "application/signed-exchange;v=b3;q=0.7",
+ htmlJson),
+ ContentType::HTML);
+
std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
ContentType::HTML);
@@ -72,6 +84,27 @@
EXPECT_EQ(getPreferredContentType("application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
+
+ // Application types with odd characters
+ EXPECT_EQ(getPreferredContentType(
+ "application/prs.nprend, application/json", cborJson),
+ ContentType::JSON);
+
+ EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
+ cborJson),
+ ContentType::JSON);
+
+ // Q values are ignored, but should parse
+ EXPECT_EQ(getPreferredContentType(
+ "application/rdf+xml;q=0.9, application/json", cborJson),
+ ContentType::JSON);
+ EXPECT_EQ(getPreferredContentType(
+ "application/rdf+xml;q=1, application/json", cborJson),
+ ContentType::JSON);
+ EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
+ ContentType::JSON);
+ EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
+ ContentType::JSON);
}
TEST(getPreferredContentType, NegativeTest)