Roll src/third_party/WebKit fe3d0ed:840e60d (svn 202350:202351)
[chromium-blink-merge.git] / net / tools / balsa / balsa_headers_test.cc
blob3dc8eb33d2af2da7cc7270aae36f6241887c930a
1 // Copyright 2013 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/tools/balsa/balsa_headers.h"
7 #include <iterator>
8 #include <string>
9 #include <vector>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/tools/balsa/balsa_enums.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
18 using ::base::StringPiece;
20 class BalsaBufferTest : public ::testing::Test {
21 public:
22 void SetUp() override {
23 buffer_.reset(new BalsaBuffer);
24 anotherBuffer_.reset(new BalsaBuffer);
27 protected:
28 scoped_ptr<BalsaBuffer> buffer_;
29 scoped_ptr<BalsaBuffer> anotherBuffer_;
32 namespace {
34 class BalsaHeadersTest: public ::testing::Test {
35 public:
36 void SetUp() override { headers_.reset(new BalsaHeaders); }
38 protected:
39 scoped_ptr<BalsaHeaders> headers_;
42 class StringBuffer {
43 public:
44 void Write(const char* p, size_t size) {
45 string_ += std::string(p, size);
47 const std::string& string() {return string_;}
49 private:
50 std::string string_;
53 TEST_F(BalsaBufferTest, EmptyBuffer) {
54 ASSERT_EQ(1u, buffer_->num_blocks());
57 TEST_F(BalsaBufferTest, Write) {
58 size_t index1, index2;
59 StringPiece sp1 = buffer_->Write(StringPiece("hello"), &index1);
60 StringPiece sp2 = buffer_->Write(StringPiece(", world"), &index2);
62 ASSERT_EQ(2u, buffer_->num_blocks());
63 ASSERT_EQ("hello", sp1);
64 ASSERT_EQ(", world", sp2);
65 ASSERT_EQ(1u, index1);
66 ASSERT_EQ(1u, index2);
67 ASSERT_EQ("hello, world",
68 StringPiece(buffer_->GetPtr(1), buffer_->bytes_used(1)));
71 TEST_F(BalsaBufferTest, WriteLongData) {
72 size_t index1, index2, index3;
73 std::string as(2, 'a');
74 std::string bs(BalsaBuffer::kDefaultBlocksize + 1, 'b');
75 std::string cs(4, 'c');
77 StringPiece sp1 = buffer_->Write(as, &index1);
78 StringPiece sp2 = buffer_->Write(bs, &index2);
79 StringPiece sp3 = buffer_->Write(cs, &index3);
81 ASSERT_EQ(3u, buffer_->num_blocks());
82 ASSERT_EQ(as, sp1);
83 ASSERT_EQ(bs, sp2);
84 ASSERT_EQ(cs, sp3);
85 ASSERT_EQ(1u, index1);
86 ASSERT_EQ(2u, index2);
87 ASSERT_EQ(1u, index3);
88 ASSERT_EQ("aacccc", StringPiece(buffer_->GetPtr(1), buffer_->bytes_used(1)));
89 ASSERT_EQ(sp2, StringPiece(buffer_->GetPtr(2), buffer_->bytes_used(2)));
92 TEST_F(BalsaBufferTest, WriteToContiguousBuffer) {
93 std::string as(2, 'a');
94 std::string bs(BalsaBuffer::kDefaultBlocksize + 1, 'b');
95 std::string cs(4, 'c');
97 buffer_->WriteToContiguousBuffer(as);
98 buffer_->WriteToContiguousBuffer(bs);
99 buffer_->WriteToContiguousBuffer(cs);
101 ASSERT_EQ(1u, buffer_->num_blocks());
102 ASSERT_EQ(as + bs + cs,
103 StringPiece(buffer_->GetPtr(0), buffer_->bytes_used(0)));
106 TEST_F(BalsaBufferTest, NoMoreWriteToContiguousBuffer) {
107 size_t index1, index2;
108 StringPiece sp1 = buffer_->Write(StringPiece("hello"), &index1);
109 buffer_->NoMoreWriteToContiguousBuffer();
110 StringPiece sp2 = buffer_->Write(StringPiece(", world"), &index2);
112 ASSERT_EQ(2u, buffer_->num_blocks());
113 ASSERT_EQ("hello", sp1);
114 ASSERT_EQ(", world", sp2);
115 ASSERT_EQ(1u, index1);
116 ASSERT_EQ(0u, index2);
117 ASSERT_EQ(sp1, StringPiece(buffer_->GetPtr(1), buffer_->bytes_used(1)));
118 ASSERT_EQ(sp2, StringPiece(buffer_->GetPtr(0), buffer_->bytes_used(0)));
121 TEST_F(BalsaBufferTest, Clear) {
122 buffer_->Write("hello", NULL);
123 ASSERT_EQ(2u, buffer_->num_blocks());
124 buffer_->Clear();
125 ASSERT_EQ(1u, buffer_->num_blocks());
128 TEST_F(BalsaBufferTest, Swap) {
129 buffer_->Write("hello", NULL);
131 ASSERT_EQ(2u, buffer_->num_blocks());
132 ASSERT_EQ(1u, anotherBuffer_->num_blocks());
134 buffer_->Swap(anotherBuffer_.get());
136 ASSERT_EQ(1u, buffer_->num_blocks());
137 ASSERT_EQ(2u, anotherBuffer_->num_blocks());
138 ASSERT_EQ("hello",
139 StringPiece(anotherBuffer_->GetPtr(1),
140 anotherBuffer_->bytes_used(1)));
143 TEST_F(BalsaBufferTest, CopyFrom) {
144 buffer_->Write("hello", NULL);
146 ASSERT_EQ(2u, buffer_->num_blocks());
147 ASSERT_EQ(1u, anotherBuffer_->num_blocks());
149 anotherBuffer_->CopyFrom(*buffer_);
151 ASSERT_EQ(2u, buffer_->num_blocks());
152 ASSERT_EQ(2u, anotherBuffer_->num_blocks());
153 ASSERT_EQ("hello", StringPiece(buffer_->GetPtr(1), buffer_->bytes_used(1)));
154 ASSERT_EQ("hello",
155 StringPiece(anotherBuffer_->GetPtr(1),
156 anotherBuffer_->bytes_used(1)));
159 TEST_F(BalsaHeadersTest, AppendHeader) {
160 headers_->AppendHeader("key1", "value1");
161 headers_->AppendHeader("key2", "value2");
162 headers_->AppendHeader("key3", "value3");
163 headers_->AppendHeader("key3", "value3.1");
164 headers_->AppendHeader("key3", "value3.2");
166 ASSERT_EQ(5, std::distance(headers_->header_lines_begin(),
167 headers_->header_lines_end()));
168 ASSERT_EQ("value1", headers_->GetHeader("key1"));
169 ASSERT_EQ("value2", headers_->GetHeader("key2"));
170 ASSERT_EQ("value3", headers_->GetHeader("key3"));
172 std::vector<base::StringPiece> v1, v2, v3;
173 std::string s1, s2, s3;
174 headers_->GetAllOfHeader("key1", &v1);
175 headers_->GetAllOfHeader("key2", &v2);
176 headers_->GetAllOfHeader("key3", &v3);
177 headers_->GetAllOfHeaderAsString("key1", &s1);
178 headers_->GetAllOfHeaderAsString("key2", &s2);
179 headers_->GetAllOfHeaderAsString("key3", &s3);
181 ASSERT_EQ(1u, v1.size());
182 ASSERT_EQ(1u, v2.size());
183 ASSERT_EQ(3u, v3.size());
184 ASSERT_EQ("value1", v1[0]);
185 ASSERT_EQ("value2", v2[0]);
186 ASSERT_EQ("value3", v3[0]);
187 ASSERT_EQ("value3.1", v3[1]);
188 ASSERT_EQ("value3.2", v3[2]);
189 ASSERT_EQ("value1", s1);
190 ASSERT_EQ("value2", s2);
191 ASSERT_EQ("value3,value3.1,value3.2", s3);
194 TEST_F(BalsaHeadersTest, ReplaceOrAppendHeader) {
195 headers_->ReplaceOrAppendHeader("key1", "value1");
196 headers_->ReplaceOrAppendHeader("key1", "value2");
198 ASSERT_EQ(1, std::distance(headers_->header_lines_begin(),
199 headers_->header_lines_end()));
200 ASSERT_EQ("value2", headers_->GetHeader("key1"));
202 std::vector<base::StringPiece> v;
203 headers_->GetAllOfHeader("key1", &v);
205 ASSERT_EQ(1u, v.size());
206 ASSERT_EQ("value2", v[0]);
209 TEST_F(BalsaHeadersTest, AppendToHeader) {
210 headers_->AppendToHeader("key1", "value1");
211 headers_->AppendToHeader("keY1", "value2");
213 ASSERT_EQ(1, std::distance(headers_->header_lines_begin(),
214 headers_->header_lines_end()));
215 ASSERT_EQ("value1,value2", headers_->GetHeader("key1"));
217 std::vector<base::StringPiece> v;
218 std::string s;
219 headers_->GetAllOfHeader("key1", &v);
220 headers_->GetAllOfHeaderAsString("keY1", &s);
222 ASSERT_EQ(1u, v.size());
223 ASSERT_EQ("value1,value2", v[0]);
224 ASSERT_EQ("value1,value2", s);
227 TEST_F(BalsaHeadersTest, PrepentToHeader) {
228 headers_->PrependToHeader("key1", "value1");
229 headers_->PrependToHeader("key1", "value2");
231 ASSERT_EQ(1, std::distance(headers_->header_lines_begin(),
232 headers_->header_lines_end()));
233 ASSERT_EQ("value2,value1", headers_->GetHeader("key1"));
235 std::vector<base::StringPiece> v;
236 std::string s;
237 headers_->GetAllOfHeader("key1", &v);
238 headers_->GetAllOfHeaderAsString("key1", &s);
240 ASSERT_EQ(1u, v.size());
241 ASSERT_EQ("value2,value1", v[0]);
242 ASSERT_EQ("value2,value1", s);
245 TEST_F(BalsaHeadersTest, HasHeader) {
246 headers_->AppendHeader("key1", "value1");
248 ASSERT_TRUE(headers_->HasHeader("key1"));
249 ASSERT_FALSE(headers_->HasHeader("value1"));
250 ASSERT_FALSE(headers_->HasHeader("key2"));
253 TEST_F(BalsaHeadersTest, HasNonEmptyHeader) {
254 headers_->AppendHeader("key1", "value1");
255 headers_->AppendHeader("key2", "");
257 ASSERT_TRUE(headers_->HasNonEmptyHeader("key1"));
258 ASSERT_FALSE(headers_->HasNonEmptyHeader("key2"));
259 ASSERT_FALSE(headers_->HasNonEmptyHeader("key3"));
262 TEST_F(BalsaHeadersTest, GetHeaderPosition) {
263 headers_->AppendHeader("key1", "value1");
264 headers_->AppendHeader("key2", "value2");
265 headers_->AppendHeader("key3", "value3");
267 BalsaHeaders::const_header_lines_iterator i =
268 headers_->GetHeaderPosition("key2");
270 ASSERT_EQ(headers_->header_lines_end(),
271 headers_->GetHeaderPosition("foobar"));
272 ASSERT_EQ(headers_->header_lines_begin(),
273 headers_->GetHeaderPosition("key1"));
274 ASSERT_NE(headers_->header_lines_end(), i);
275 ASSERT_EQ("key2", i->first);
276 ASSERT_EQ("value2", i->second);
277 ++i;
278 ASSERT_EQ("key3", i->first);
279 ASSERT_EQ("value3", i->second);
280 ++i;
281 ASSERT_EQ(headers_->header_lines_end(), i);
284 TEST_F(BalsaHeadersTest, GetIteratorForKey) {
285 headers_->AppendHeader("key1", "value1");
286 headers_->AppendHeader("key2", "value2");
287 headers_->AppendHeader("key1", "value1.1");
288 headers_->AppendHeader("key3", "value3");
289 headers_->AppendHeader("KEY1", "value1.2");
291 BalsaHeaders::const_header_lines_key_iterator i =
292 headers_->GetIteratorForKey("key1");
294 ASSERT_EQ(headers_->header_lines_key_end(),
295 headers_->GetIteratorForKey("foobar"));
296 ASSERT_NE(headers_->header_lines_key_end(), i);
297 ASSERT_EQ("key1", i->first);
298 ASSERT_EQ("value1", i->second);
299 ++i;
300 ASSERT_EQ("key1", i->first);
301 ASSERT_EQ("value1.1", i->second);
302 ++i;
303 ASSERT_EQ("KEY1", i->first);
304 ASSERT_EQ("value1.2", i->second);
305 ++i;
306 ASSERT_EQ(headers_->header_lines_key_end(), i);
309 TEST_F(BalsaHeadersTest, RemoveAllOfHeader) {
310 headers_->AppendHeader("key1", "value1");
311 headers_->AppendHeader("key2", "value2");
312 headers_->AppendHeader("key1", "value1.1");
313 headers_->AppendHeader("key3", "value3");
314 headers_->AppendHeader("key1", "value1.2");
315 headers_->AppendHeader("kEY1", "value1.3");
317 ASSERT_EQ(6, std::distance(headers_->header_lines_begin(),
318 headers_->header_lines_end()));
319 headers_->RemoveAllOfHeader("key1");
320 ASSERT_EQ(2, std::distance(headers_->header_lines_begin(),
321 headers_->header_lines_end()));
324 TEST_F(BalsaHeadersTest, RemoveAllHeadersWithPrefix) {
325 headers_->AppendHeader("1key", "value1");
326 headers_->AppendHeader("2key", "value2");
327 headers_->AppendHeader("1kEz", "value1.1");
328 headers_->AppendHeader("key3", "value3");
329 headers_->AppendHeader("1KEEjkladf", "value1.2");
331 ASSERT_EQ(5, std::distance(headers_->header_lines_begin(),
332 headers_->header_lines_end()));
333 headers_->RemoveAllHeadersWithPrefix("1ke");
334 ASSERT_EQ(2, std::distance(headers_->header_lines_begin(),
335 headers_->header_lines_end()));
338 TEST_F(BalsaHeadersTest, WriteRequestHeaderAndEndingToBuffer) {
339 headers_->AppendHeader("key1", "value1");
340 headers_->AppendHeader("key2", "value2");
341 headers_->AppendHeader("key1", "value1.1");
343 headers_->SetRequestFirstlineFromStringPieces("GET", "/", "HTTP/1.0");
345 std::string expected = "GET / HTTP/1.0\r\n"
346 "key1: value1\r\n"
347 "key2: value2\r\n"
348 "key1: value1.1\r\n\r\n";
349 StringBuffer buffer;
350 headers_->WriteHeaderAndEndingToBuffer(&buffer);
351 ASSERT_EQ(expected, buffer.string());
354 TEST_F(BalsaHeadersTest, WriteResponseHeaderAndEndingToBuffer) {
355 headers_->AppendHeader("key1", "value1");
356 headers_->AppendHeader("key2", "value2");
357 headers_->AppendHeader("key1", "value1.1");
359 headers_->SetResponseFirstlineFromStringPieces("HTTP/1.0", "200", "OK");
361 std::string expected = "HTTP/1.0 200 OK\r\n"
362 "key1: value1\r\n"
363 "key2: value2\r\n"
364 "key1: value1.1\r\n\r\n";
365 StringBuffer buffer;
366 headers_->WriteHeaderAndEndingToBuffer(&buffer);
367 ASSERT_EQ(expected, buffer.string());
370 TEST_F(BalsaHeadersTest, RequestFirstLine) {
371 headers_->SetRequestFirstlineFromStringPieces("HEAD", "/path", "HTTP/1.1");
373 ASSERT_EQ("HEAD /path HTTP/1.1", headers_->first_line());
374 ASSERT_EQ("HEAD", headers_->request_method());
375 ASSERT_EQ("/path", headers_->request_uri());
376 ASSERT_EQ("HTTP/1.1", headers_->request_version());
379 TEST_F(BalsaHeadersTest, ResponseFirstLine) {
380 headers_->SetRequestFirstlineFromStringPieces("HTTP/1.0", "403", "FORBIDDEN");
382 ASSERT_EQ("HTTP/1.0 403 FORBIDDEN", headers_->first_line());
383 ASSERT_EQ("HTTP/1.0", headers_->response_version());
384 ASSERT_EQ("403", headers_->response_code());
385 ASSERT_EQ("FORBIDDEN", headers_->response_reason_phrase());
388 } // namespace
390 } // namespace net