Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / net / spdy / hpack_encoder_test.cc
blob316952edd0dcbd4fe4e91f628b5e3b0cb59ffc46
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/spdy/hpack_encoder.h"
7 #include <map>
8 #include <string>
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace net {
15 using base::StringPiece;
16 using std::string;
17 using testing::ElementsAre;
19 namespace test {
21 class HpackEncoderPeer {
22 public:
23 explicit HpackEncoderPeer(HpackEncoder* encoder)
24 : encoder_(encoder) {}
26 void set_max_string_literal_size(uint32 size) {
27 encoder_->max_string_literal_size_ = size;
29 static void CookieToCrumbs(StringPiece cookie,
30 std::vector<StringPiece>* out) {
31 HpackEncoder::CookieToCrumbs(cookie, out);
33 private:
34 HpackEncoder* encoder_;
37 } // namespace test
39 namespace {
41 TEST(HpackEncoderTest, CookieToCrumbs) {
42 test::HpackEncoderPeer peer(NULL);
43 std::vector<StringPiece> out;
45 // A space after ';' is consumed. All other spaces remain.
46 // ';' at begin and end of string produce empty crumbs.
47 peer.CookieToCrumbs(" foo=1;bar=2 ; baz=3; bing=4;", &out);
48 EXPECT_THAT(out, ElementsAre(" foo=1", "bar=2 ", "baz=3", " bing=4", ""));
50 peer.CookieToCrumbs(";;foo = bar ;; ;baz =bing", &out);
51 EXPECT_THAT(out, ElementsAre("", "", "foo = bar ", "", "", "baz =bing"));
53 peer.CookieToCrumbs("foo=bar; baz=bing", &out);
54 EXPECT_THAT(out, ElementsAre("foo=bar", "baz=bing"));
56 peer.CookieToCrumbs("baz=bing", &out);
57 EXPECT_THAT(out, ElementsAre("baz=bing"));
59 peer.CookieToCrumbs("", &out);
60 EXPECT_THAT(out, ElementsAre(""));
63 // Test that EncoderHeaderSet() simply encodes everything as literals
64 // without indexing.
65 TEST(HpackEncoderTest, Basic) {
66 HpackEncoder encoder;
68 std::map<string, string> header_set1;
69 header_set1["name1"] = "value1";
70 header_set1["name2"] = "value2";
72 string encoded_header_set1;
73 EXPECT_TRUE(encoder.EncodeHeaderSet(header_set1, &encoded_header_set1));
74 EXPECT_EQ("\x40\x05name1\x06value1"
75 "\x40\x05name2\x06value2", encoded_header_set1);
77 std::map<string, string> header_set2;
78 header_set2["name2"] = "different-value";
79 header_set2["name3"] = "value3";
81 string encoded_header_set2;
82 EXPECT_TRUE(encoder.EncodeHeaderSet(header_set2, &encoded_header_set2));
83 EXPECT_EQ("\x40\x05name2\x0f" "different-value"
84 "\x40\x05name3\x06value3", encoded_header_set2);
87 TEST(HpackEncoderTest, CookieCrumbling) {
88 HpackEncoder encoder;
90 std::map<string, string> header_set;
91 header_set["Cookie"] = "key1=value1; key2=value2";
93 string encoded_header_set;
94 EXPECT_TRUE(encoder.EncodeHeaderSet(header_set, &encoded_header_set));
95 EXPECT_EQ("\x40\x06""Cookie\x0bkey1=value1"
96 "\x40\x06""Cookie\x0bkey2=value2", encoded_header_set);
99 // Test that trying to encode a header set with a too-long header
100 // field will fail.
101 TEST(HpackEncoderTest, HeaderTooLarge) {
102 HpackEncoder encoder;
103 test::HpackEncoderPeer(&encoder).set_max_string_literal_size(10);
105 std::map<string, string> header_set;
106 header_set["name1"] = "too-long value";
107 header_set["name2"] = "value2";
109 // TODO(akalin): Verify that the encoder did not attempt to encode
110 // the second header field.
111 string encoded_header_set;
112 EXPECT_FALSE(encoder.EncodeHeaderSet(header_set, &encoded_header_set));
115 } // namespace
117 } // namespace net