Chromium Code Reviews| Index: net/http/http_stream_parser_unittest.cc |
| diff --git a/net/http/http_stream_parser_unittest.cc b/net/http/http_stream_parser_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c96bb82ddfb7efc8427598b4b4cf80241bdcf16 |
| --- /dev/null |
| +++ b/net/http/http_stream_parser_unittest.cc |
| @@ -0,0 +1,75 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/http/http_stream_parser.h" |
| + |
| +#include "base/string_piece.h" |
| +#include "base/stringprintf.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +const size_t kOutputSize = 1024; // Just large enough for this test. |
| +// The number of bytes that can fit in a buffer of kOutputSize. |
| +const size_t kNumMaxPayloadBytes = |
|
wtc
2012/01/18 22:49:24
Nit: kNumMaxPayloadBytes => kMaxPayloadSize ?
satorux1
2012/01/19 00:07:41
Done.
|
| + kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| + |
| +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.
|
| + char output[kOutputSize]; |
| + |
| + const base::StringPiece kPayload = ""; |
| + const base::StringPiece kExpected = "0\r\n\r\n"; |
| + const int num_bytes_written = |
| + HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| + ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| + EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| +} |
| + |
| +TEST(HttpStreamParser, EncodeChunk_ShortPayload) { |
| + char output[kOutputSize]; |
| + |
| + const std::string kPayload("foo\x00\x11\x22", 6); |
| + // 11 = payload size + sizeof("6") + CRLF x 2. |
| + const std::string kExpected("6\r\nfoo\x00\x11\x22\r\n", 11); |
| + const int num_bytes_written = |
| + HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| + ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| + EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| +} |
| + |
| +TEST(HttpStreamParser, EncodeChunk_LargePayload) { |
| + char output[kOutputSize]; |
| + |
| + const std::string kPayload(1000, '\xff'); // '\xff' x 1000. |
| + // 3E8 = 1000 in hex. |
| + const std::string kExpected = "3E8\r\n" + kPayload + "\r\n"; |
| + const int num_bytes_written = |
| + HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| + ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| + EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| +} |
| + |
| +TEST(HttpStreamParser, EncodeChunk_FullPayload) { |
| + char output[kOutputSize]; |
| + |
| + const std::string kPayload(kNumMaxPayloadBytes, '\xff'); |
| + // 3F4 = 1012 in hex. |
| + const std::string kExpected = "3F4\r\n" + kPayload + "\r\n"; |
| + const int num_bytes_written = |
| + HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| + ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| + EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| +} |
| + |
| +TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { |
| + char output[kOutputSize]; |
| + |
| + // The payload is one byte larger the output buffer size. |
| + const std::string kPayload(kNumMaxPayloadBytes + 1, '\xff'); |
| + const int num_bytes_written = |
| + HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| + ASSERT_EQ(-1, num_bytes_written); |
| +} |
| + |
| +} // namespace net |