proxygen
StructuredHeadersDecoderTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
11 #include <string>
13 
14 namespace proxygen{
15 
17 };
18 
20  std::string input = "645643";
21  StructuredHeadersDecoder shd(input);
22 
24  shd.decodeItem(item);
25 
26  EXPECT_EQ(item.tag, StructuredHeaderItem::Type::INT64);
27  EXPECT_EQ(item, int64_t(645643));
28 }
29 
31  std::string input = "\"cookies\", 3.1415 , 74657";
32  StructuredHeadersDecoder shd(input);
33 
34  std::vector<StructuredHeaderItem> v;
35  shd.decodeList(v);
36  EXPECT_EQ(v.size(), 3);
37 
39  EXPECT_EQ(v[1].tag, StructuredHeaderItem::Type::DOUBLE);
40  EXPECT_EQ(v[2].tag, StructuredHeaderItem::Type::INT64);
41 
42  EXPECT_EQ(v[0], std::string("cookies"));
43  EXPECT_EQ(v[1], 3.1415);
44  EXPECT_EQ(v[2], int64_t(74657));
45 }
46 
47 TEST_F(StructuredHeadersDecoderTest, TestListBeginningWhitespace) {
48  std::string input = " 19 , 95";
49  StructuredHeadersDecoder shd(input);
50 
51  std::vector<StructuredHeaderItem> v;
52  shd.decodeList(v);
53  EXPECT_EQ(v.size(), 2);
54 
55  EXPECT_EQ(v[0].tag, StructuredHeaderItem::Type::INT64);
56  EXPECT_EQ(v[1].tag, StructuredHeaderItem::Type::INT64);
57 
58  EXPECT_EQ(v[0], int64_t(19));
59  EXPECT_EQ(v[1], int64_t(95));
60 }
61 
62 TEST_F(StructuredHeadersDecoderTest, TestListEndingWhitespace) {
63  std::string input = "19 , 95 ";
64  StructuredHeadersDecoder shd(input);
65 
66  std::vector<StructuredHeaderItem> v;
67  shd.decodeList(v);
68  EXPECT_EQ(v.size(), 2);
69 
70  EXPECT_EQ(v[0].tag, StructuredHeaderItem::Type::INT64);
71  EXPECT_EQ(v[1].tag, StructuredHeaderItem::Type::INT64);
72 
73  EXPECT_EQ(v[0], int64_t(19));
74  EXPECT_EQ(v[1], int64_t(95));
75 }
76 
77 TEST_F(StructuredHeadersDecoderTest, TestListNoWhitespace) {
78  std::string input = "19,95";
79  StructuredHeadersDecoder shd(input);
80 
81  std::vector<StructuredHeaderItem> v;
82  shd.decodeList(v);
83  EXPECT_EQ(v.size(), 2);
84 
85  EXPECT_EQ(v[0].tag, StructuredHeaderItem::Type::INT64);
86  EXPECT_EQ(v[1].tag, StructuredHeaderItem::Type::INT64);
87 
88  EXPECT_EQ(v[0], int64_t(19));
89  EXPECT_EQ(v[1], int64_t(95));
90 }
91 
93  std::string input = "*Zm9vZA==*";
94  StructuredHeadersDecoder shd(input);
95 
96  std::vector<StructuredHeaderItem> v;
97  shd.decodeList(v);
98  EXPECT_EQ(v.size(), 1);
99 
100  EXPECT_EQ(v[0].tag, StructuredHeaderItem::Type::BINARYCONTENT);
101 
102  EXPECT_EQ(v[0], std::string("food"));
103 }
104 
105 TEST_F(StructuredHeadersDecoderTest, TestDictionaryManyElts) {
106  std::string input = "age=87 , weight=150.8 , name=\"John Doe\"";
107  StructuredHeadersDecoder shd(input);
108 
109  std::unordered_map<std::string, StructuredHeaderItem> m;
110  shd.decodeDictionary(m);
111  EXPECT_EQ(m.size(), 3);
112 
113  EXPECT_EQ(m["age"].tag, StructuredHeaderItem::Type::INT64);
114  EXPECT_EQ(m["weight"].tag, StructuredHeaderItem::Type::DOUBLE);
116 
117  EXPECT_EQ(m["age"], int64_t(87));
118  EXPECT_EQ(m["weight"], 150.8);
119  EXPECT_EQ(m["name"], std::string("John Doe"));
120 }
121 
122 TEST_F(StructuredHeadersDecoderTest, TestDictionaryOneElt) {
123  std::string input = "bagel=*YXZvY2Fkbw==*";
124  StructuredHeadersDecoder shd(input);
125 
126  std::unordered_map<std::string, StructuredHeaderItem> m;
127  shd.decodeDictionary(m);
128  EXPECT_EQ(m.size(), 1);
129 
130  EXPECT_EQ(m["bagel"].tag, StructuredHeaderItem::Type::BINARYCONTENT);
131  EXPECT_EQ(m["bagel"], std::string("avocado"));
132 }
133 
134 TEST_F(StructuredHeadersDecoderTest, TestParamListOneElt) {
135  std::string input = "abc_123;a=1;b=2";
136  StructuredHeadersDecoder shd(input);
137 
139  shd.decodeParameterisedList(pl);
140  EXPECT_EQ(pl.size(), 1);
141  EXPECT_EQ(pl[0].identifier, "abc_123");
142  EXPECT_EQ(pl[0].parameterMap.size(), 2);
143  EXPECT_EQ(pl[0].parameterMap["a"], int64_t(1));
144  EXPECT_EQ(pl[0].parameterMap["b"], int64_t(2));
145 }
146 
147 TEST_F(StructuredHeadersDecoderTest, TestParamListManyElts) {
148  std::string input = "a_13;a=1;b=2; c_4, ghi;q=\"9\";r=*bWF4IGlzIGF3ZXNvbWU=*";
149  StructuredHeadersDecoder shd(input);
150 
152  shd.decodeParameterisedList(pl);
153  EXPECT_EQ(pl.size(), 2);
154 
155  EXPECT_EQ(pl[0].identifier, "a_13");
156  EXPECT_EQ(pl[0].parameterMap.size(), 3);
157  EXPECT_EQ(pl[0].parameterMap["a"].tag, StructuredHeaderItem::Type::INT64);
158  EXPECT_EQ(pl[0].parameterMap["b"].tag, StructuredHeaderItem::Type::INT64);
159  EXPECT_EQ(pl[0].parameterMap["c_4"].tag, StructuredHeaderItem::Type::NONE);
160  EXPECT_EQ(pl[0].parameterMap["a"], int64_t(1));
161  EXPECT_EQ(pl[0].parameterMap["b"], int64_t(2));
162 
163  EXPECT_EQ(pl[1].identifier, "ghi");
164  EXPECT_EQ(pl[1].parameterMap.size(), 2);
165  EXPECT_EQ(pl[1].parameterMap["q"].tag, StructuredHeaderItem::Type::STRING);
166  EXPECT_EQ(pl[1].parameterMap["r"].tag,
167  StructuredHeaderItem::Type::BINARYCONTENT);
168  EXPECT_EQ(pl[1].parameterMap["q"], std::string("9"));
169  EXPECT_EQ(pl[1].parameterMap["r"], std::string("max is awesome"));
170 }
171 
172 TEST_F(StructuredHeadersDecoderTest, TestParamListNoParams) {
173  std::string input = "apple12, cat14, dog22";
174  StructuredHeadersDecoder shd(input);
175 
177  shd.decodeParameterisedList(pl);
178  EXPECT_EQ(pl.size(), 3);
179  EXPECT_EQ(pl[0].identifier, "apple12");
180  EXPECT_EQ(pl[0].parameterMap.size(), 0);
181 
182  EXPECT_EQ(pl[1].identifier, "cat14");
183  EXPECT_EQ(pl[1].parameterMap.size(), 0);
184 
185  EXPECT_EQ(pl[2].identifier, "dog22");
186  EXPECT_EQ(pl[2].parameterMap.size(), 0);
187 }
188 
189 TEST_F(StructuredHeadersDecoderTest, TestParamListWhitespace) {
190  std::string input = "am_95 ; abc=11.8 , foo ";
191  StructuredHeadersDecoder shd(input);
192 
194  shd.decodeParameterisedList(pl);
195  EXPECT_EQ(pl.size(), 2);
196 
197  EXPECT_EQ(pl[0].identifier, "am_95");
198  EXPECT_EQ(pl[0].parameterMap.size(), 1);
199  EXPECT_EQ(pl[0].parameterMap["abc"].tag, StructuredHeaderItem::Type::DOUBLE);
200  EXPECT_EQ(pl[0].parameterMap["abc"], 11.8);
201 
202  EXPECT_EQ(pl[1].identifier, "foo");
203  EXPECT_EQ(pl[1].parameterMap.size(), 0);
204 }
205 
206 TEST_F(StructuredHeadersDecoderTest, TestParamListNullValues) {
207  std::string input = "beverages;water;juice, food;pizza;burger";
208  StructuredHeadersDecoder shd(input);
209 
211  shd.decodeParameterisedList(pl);
212  EXPECT_EQ(pl.size(), 2);
213 
214  EXPECT_EQ(pl[0].identifier, "beverages");
215  EXPECT_EQ(pl[0].parameterMap.size(), 2);
216  EXPECT_EQ(pl[0].parameterMap["water"].tag, StructuredHeaderItem::Type::NONE);
217  EXPECT_EQ(pl[0].parameterMap["juice"].tag, StructuredHeaderItem::Type::NONE);
218 
219  EXPECT_EQ(pl[1].identifier, "food");
220  EXPECT_EQ(pl[1].parameterMap.size(), 2);
221  EXPECT_EQ(pl[1].parameterMap["pizza"].tag, StructuredHeaderItem::Type::NONE);
222  EXPECT_EQ(pl[1].parameterMap["burger"].tag, StructuredHeaderItem::Type::NONE);
223 }
224 
225 }
auto v
std::vector< StructuredHeaders::ParameterisedIdentifier > ParameterisedList
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
StructuredHeaders::DecodeError decodeParameterisedList(ParameterisedList &result)
#define STRING
StructuredHeaders::DecodeError decodeDictionary(Dictionary &result)
static map< string, int > m
StructuredHeaders::DecodeError decodeList(std::vector< StructuredHeaderItem > &result)
const char * string
Definition: Conv.cpp:212
StructuredHeaders::DecodeError decodeItem(StructuredHeaderItem &result)
TEST_F(HeaderTableTests, IndexTranslation)