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"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 using base::StringPiece
;
17 using testing::ElementsAre
;
21 class HpackEncoderPeer
{
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
);
34 HpackEncoder
* encoder_
;
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
65 TEST(HpackEncoderTest
, Basic
) {
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
) {
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
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
));