Commit 5a78076c authored by Rodney Ding's avatar Rodney Ding Committed by Commit Bot

Update boolean param parsing/serialization in SH.

Boolean params without a value is now defaulted to True.

Bug: 1048757
Change-Id: I2c3ed2562671ae64cf9eceeaa5ba5cc0d69501e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2085697
Commit-Queue: Rodney Ding <rodneyding@google.com>
Reviewed-by: default avatarIan Clelland <iclelland@chromium.org>
Reviewed-by: default avatarAsanka Herath <asanka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748288}
parent a5ef8671
...@@ -278,7 +278,7 @@ class StructuredHeaderParser { ...@@ -278,7 +278,7 @@ class StructuredHeaderParser {
return base::nullopt; return base::nullopt;
} }
Item value; Item value{true};
if (ConsumeChar('=')) { if (ConsumeChar('=')) {
auto item = ReadBareItem(); auto item = ReadBareItem();
if (!item) if (!item)
...@@ -710,6 +710,8 @@ class StructuredHeaderSerializer { ...@@ -710,6 +710,8 @@ class StructuredHeaderSerializer {
if (!WriteKey(param_name)) if (!WriteKey(param_name))
return false; return false;
if (!param_value.is_null()) { if (!param_value.is_null()) {
if (param_value.is_boolean() && param_value.GetBoolean())
continue;
output_ << "="; output_ << "=";
if (!WriteBareItem(param_value)) if (!WriteBareItem(param_value))
return false; return false;
......
...@@ -25,10 +25,15 @@ Item Integer(int64_t value) { ...@@ -25,10 +25,15 @@ Item Integer(int64_t value) {
return Item(value); return Item(value);
} }
std::pair<std::string, Item> Param(std::string key) { // Parameter with null value, only used in Structured Headers Draft 09
std::pair<std::string, Item> NullParam(std::string key) {
return std::make_pair(key, Item()); return std::make_pair(key, Item());
} }
std::pair<std::string, Item> BooleanParam(std::string key, bool value) {
return std::make_pair(key, Item(value));
}
std::pair<std::string, Item> DoubleParam(std::string key, double value) { std::pair<std::string, Item> DoubleParam(std::string key, double value) {
return std::make_pair(key, Item(value)); return std::make_pair(key, Item(value));
} }
...@@ -445,10 +450,10 @@ const struct ParameterizedItemTestCase { ...@@ -445,10 +450,10 @@ const struct ParameterizedItemTestCase {
{{Token("text/html"), {DoubleParam("q", 1)}}}}, {{Token("text/html"), {DoubleParam("q", 1)}}}},
{"missing parameter value item", {"missing parameter value item",
"text/html;a;q=1.0", "text/html;a;q=1.0",
{{Token("text/html"), {Param("a"), DoubleParam("q", 1)}}}}, {{Token("text/html"), {BooleanParam("a", true), DoubleParam("q", 1)}}}},
{"missing terminal parameter value item", {"missing terminal parameter value item",
"text/html;q=1.0;a", "text/html;q=1.0;a",
{{Token("text/html"), {DoubleParam("q", 1), Param("a")}}}}, {{Token("text/html"), {DoubleParam("q", 1), BooleanParam("a", true)}}}},
{"whitespace before = parameterised item", "text/html, text/plain;q =0.5", {"whitespace before = parameterised item", "text/html, text/plain;q =0.5",
base::nullopt}, base::nullopt},
{"whitespace after = parameterised item", "text/html, text/plain;q= 0.5", {"whitespace after = parameterised item", "text/html, text/plain;q= 0.5",
...@@ -511,7 +516,8 @@ const struct ListTestCase { ...@@ -511,7 +516,8 @@ const struct ListTestCase {
// Parameterized Lists // Parameterized Lists
{"basic parameterised list", {"basic parameterised list",
"abc_123;a=1;b=2; cdef_456, ghi;q=\"9\";r=\"+w\"", "abc_123;a=1;b=2; cdef_456, ghi;q=\"9\";r=\"+w\"",
{{{Token("abc_123"), {Param("a", 1), Param("b", 2), Param("cdef_456")}}, {{{Token("abc_123"),
{Param("a", 1), Param("b", 2), BooleanParam("cdef_456", true)}},
{Token("ghi"), {Param("q", "9"), Param("r", "+w")}}}}, {Token("ghi"), {Param("q", "9"), Param("r", "+w")}}}},
"abc_123;a=1;b=2;cdef_456, ghi;q=\"9\";r=\"+w\""}, "abc_123;a=1;b=2;cdef_456, ghi;q=\"9\";r=\"+w\""},
{"single item parameterised list", {"single item parameterised list",
...@@ -519,10 +525,10 @@ const struct ListTestCase { ...@@ -519,10 +525,10 @@ const struct ListTestCase {
{{{Token("text/html"), {DoubleParam("q", 1)}}}}}, {{{Token("text/html"), {DoubleParam("q", 1)}}}}},
{"missing parameter value parameterised list", {"missing parameter value parameterised list",
"text/html;a;q=1.0", "text/html;a;q=1.0",
{{{Token("text/html"), {Param("a"), DoubleParam("q", 1)}}}}}, {{{Token("text/html"), {BooleanParam("a", true), DoubleParam("q", 1)}}}}},
{"missing terminal parameter value parameterised list", {"missing terminal parameter value parameterised list",
"text/html;q=1.0;a", "text/html;q=1.0;a",
{{{Token("text/html"), {DoubleParam("q", 1), Param("a")}}}}}, {{{Token("text/html"), {DoubleParam("q", 1), BooleanParam("a", true)}}}}},
{"no whitespace parameterised list", {"no whitespace parameterised list",
"text/html,text/plain;q=0.5", "text/html,text/plain;q=0.5",
{{{Token("text/html"), {}}, {{{Token("text/html"), {}},
...@@ -640,10 +646,12 @@ const struct DictionaryTestCase { ...@@ -640,10 +646,12 @@ const struct DictionaryTestCase {
"a=(1 2);q=1.0"}, "a=(1 2);q=1.0"},
{"missing parameter value parameterised dict", {"missing parameter value parameterised dict",
"a=3;c;d=5", "a=3;c;d=5",
{Dictionary{{{"a", {Integer(3), {Param("c"), Param("d", 5)}}}}}}}, {Dictionary{
{{"a", {Integer(3), {BooleanParam("c", true), Param("d", 5)}}}}}}},
{"terminal missing parameter value parameterised dict", {"terminal missing parameter value parameterised dict",
"a=3;c=5;d", "a=3;c=5;d",
{Dictionary{{{"a", {Integer(3), {Param("c", 5), Param("d")}}}}}}}, {Dictionary{
{{"a", {Integer(3), {Param("c", 5), BooleanParam("d", true)}}}}}}},
{"no whitespace parameterised dict", {"no whitespace parameterised dict",
"a=b;c=1,d=e;f=2", "a=b;c=1,d=e;f=2",
{Dictionary{{{"a", {Token("b"), {Param("c", 1)}}}, {Dictionary{{{"a", {Token("b"), {Param("c", 1)}}},
...@@ -774,7 +782,7 @@ TEST(StructuredHeaderTest, ParseParameterisedList) { ...@@ -774,7 +782,7 @@ TEST(StructuredHeaderTest, ParseParameterisedList) {
"abc_123;a=1;b=2; cdef_456, ghi;q=\"9\";r=\"w\"", "abc_123;a=1;b=2; cdef_456, ghi;q=\"9\";r=\"w\"",
{ {
{Token("abc_123"), {Token("abc_123"),
{Param("a", 1), Param("b", 2), Param("cdef_456")}}, {Param("a", 1), Param("b", 2), NullParam("cdef_456")}},
{Token("ghi"), {Param("q", "9"), Param("r", "w")}}, {Token("ghi"), {Param("q", "9"), Param("r", "w")}},
}}, }},
{"empty param-list", "", {}}, {"empty param-list", "", {}},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment