Adds a new operator to SpdyHeaderBlock::ValueProxy, allowing it to be compared with SpdyStringPiece.
gfe-relnote: n/a (new code, only used in tests)
PiperOrigin-RevId: 285846905
Change-Id: I5273fbe19b4cb9c7778f98fd53e2fb7d003881d5
diff --git a/spdy/core/spdy_header_block.cc b/spdy/core/spdy_header_block.cc
index 609f6d7..c87bfff 100644
--- a/spdy/core/spdy_header_block.cc
+++ b/spdy/core/spdy_header_block.cc
@@ -140,7 +140,7 @@
}
SpdyHeaderBlock::ValueProxy& SpdyHeaderBlock::ValueProxy::operator=(
- const SpdyStringPiece value) {
+ SpdyStringPiece value) {
*spdy_header_block_value_size_ += value.size();
SpdyHeaderStorage* storage = &block_->storage_;
if (lookup_result_ == block_->map_.end()) {
@@ -158,6 +158,14 @@
return *this;
}
+bool SpdyHeaderBlock::ValueProxy::operator==(SpdyStringPiece value) const {
+ if (lookup_result_ == block_->map_.end()) {
+ return false;
+ } else {
+ return value == lookup_result_->second.value();
+ }
+}
+
std::string SpdyHeaderBlock::ValueProxy::as_string() const {
if (lookup_result_ == block_->map_.end()) {
return "";
diff --git a/spdy/core/spdy_header_block.h b/spdy/core/spdy_header_block.h
index bd575f4..b02284f 100644
--- a/spdy/core/spdy_header_block.h
+++ b/spdy/core/spdy_header_block.h
@@ -194,7 +194,10 @@
ValueProxy& operator=(const ValueProxy& other) = delete;
// Assignment modifies the underlying SpdyHeaderBlock.
- ValueProxy& operator=(const SpdyStringPiece other);
+ ValueProxy& operator=(SpdyStringPiece value);
+
+ // Provides easy comparison against SpdyStringPiece.
+ bool operator==(SpdyStringPiece value) const;
std::string as_string() const;
diff --git a/spdy/core/spdy_header_block_test.cc b/spdy/core/spdy_header_block_test.cc
index 774854b..7695aad 100644
--- a/spdy/core/spdy_header_block_test.cc
+++ b/spdy/core/spdy_header_block_test.cc
@@ -189,6 +189,27 @@
EXPECT_EQ("singleton", block["h4"]);
}
+TEST(SpdyHeaderBlockTest, CompareValueToSpdyStringPiece) {
+ SpdyHeaderBlock block;
+ block["foo"] = "foo";
+ block.AppendValueOrAddHeader("foo", "bar");
+ const auto& val = block["foo"];
+ const char expected[] = "foo\0bar";
+ EXPECT_TRUE(SpdyStringPiece(expected, 7) == val);
+ EXPECT_TRUE(val == SpdyStringPiece(expected, 7));
+ EXPECT_FALSE(SpdyStringPiece(expected, 3) == val);
+ EXPECT_FALSE(val == SpdyStringPiece(expected, 3));
+ const char not_expected[] = "foo\0barextra";
+ EXPECT_FALSE(SpdyStringPiece(not_expected, 12) == val);
+ EXPECT_FALSE(val == SpdyStringPiece(not_expected, 12));
+
+ const auto& val2 = block["foo2"];
+ EXPECT_FALSE(SpdyStringPiece(expected, 7) == val2);
+ EXPECT_FALSE(val2 == SpdyStringPiece(expected, 7));
+ EXPECT_FALSE(SpdyStringPiece("") == val2);
+ EXPECT_FALSE(val2 == SpdyStringPiece(""));
+}
+
// This test demonstrates that the SpdyHeaderBlock data structure does not place
// any limitations on the characters present in the header names.
TEST(SpdyHeaderBlockTest, UpperCaseNames) {
diff --git a/spdy/core/spdy_test_utils.h b/spdy/core/spdy_test_utils.h
index e2c3b47..ff08a50 100644
--- a/spdy/core/spdy_test_utils.h
+++ b/spdy/core/spdy_test_utils.h
@@ -19,7 +19,7 @@
inline bool operator==(SpdyStringPiece x,
const SpdyHeaderBlock::ValueProxy& y) {
- return x == y.as_string();
+ return y.operator==(x);
}
namespace test {