Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1438)

Side by Side Diff: net/http/http_stream_parser_unittest.cc

Issue 9242018: Factor out chunk encoding logic into HttpStreamParser::EncodeChunk(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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.
4
5 #include "net/http/http_stream_parser.h"
6
7 #include "base/string_piece.h"
8 #include "base/stringprintf.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12
13 const size_t kOutputSize = 1024; // Just large enough for this test.
14 // The number of bytes that can fit in a buffer of kOutputSize.
15 const size_t kNumMaxPayloadBytes =
wtc 2012/01/18 22:49:24 Nit: kNumMaxPayloadBytes => kMaxPayloadSize ?
satorux1 2012/01/19 00:07:41 Done.
16 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
17
18 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
wtc 2012/01/18 22:49:24 Please add a comment to point out this is also how
satorux1 2012/01/19 00:07:41 Done.
19 char output[kOutputSize];
20
21 const base::StringPiece kPayload = "";
22 const base::StringPiece kExpected = "0\r\n\r\n";
23 const int num_bytes_written =
24 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
25 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
26 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
27 }
28
29 TEST(HttpStreamParser, EncodeChunk_ShortPayload) {
30 char output[kOutputSize];
31
32 const std::string kPayload("foo\x00\x11\x22", 6);
33 // 11 = payload size + sizeof("6") + CRLF x 2.
34 const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11);
35 const int num_bytes_written =
36 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
37 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
38 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
39 }
40
41 TEST(HttpStreamParser, EncodeChunk_LargePayload) {
42 char output[kOutputSize];
43
44 const std::string kPayload(1000, '\xff'); // '\xff' x 1000.
45 // 3E8 = 1000 in hex.
46 const std::string kExpected = "3E8\r\n" + kPayload + "\r\n";
47 const int num_bytes_written =
48 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
49 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
50 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
51 }
52
53 TEST(HttpStreamParser, EncodeChunk_FullPayload) {
54 char output[kOutputSize];
55
56 const std::string kPayload(kNumMaxPayloadBytes, '\xff');
57 // 3F4 = 1012 in hex.
58 const std::string kExpected = "3F4\r\n" + kPayload + "\r\n";
59 const int num_bytes_written =
60 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
61 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
62 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
63 }
64
65 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) {
66 char output[kOutputSize];
67
68 // The payload is one byte larger the output buffer size.
69 const std::string kPayload(kNumMaxPayloadBytes + 1, '\xff');
70 const int num_bytes_written =
71 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
72 ASSERT_EQ(-1, num_bytes_written);
73 }
74
75 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698