| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 7 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
| 8 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 9 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/upload_data.h" |
| 14 #include "net/base/upload_data_stream.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 16 |
| 12 namespace net { | 17 namespace net { |
| 13 | 18 |
| 14 const size_t kOutputSize = 1024; // Just large enough for this test. | 19 const size_t kOutputSize = 1024; // Just large enough for this test. |
| 15 // The number of bytes that can fit in a buffer of kOutputSize. | 20 // The number of bytes that can fit in a buffer of kOutputSize. |
| 16 const size_t kMaxPayloadSize = | 21 const size_t kMaxPayloadSize = |
| 17 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; | 22 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| 18 | 23 |
| 19 // The empty payload is how the last chunk is encoded. | 24 // The empty payload is how the last chunk is encoded. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { | 72 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { |
| 68 char output[kOutputSize]; | 73 char output[kOutputSize]; |
| 69 | 74 |
| 70 // The payload is one byte larger the output buffer size. | 75 // The payload is one byte larger the output buffer size. |
| 71 const std::string kPayload(kMaxPayloadSize + 1, '\xff'); | 76 const std::string kPayload(kMaxPayloadSize + 1, '\xff'); |
| 72 const int num_bytes_written = | 77 const int num_bytes_written = |
| 73 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); | 78 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 74 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); | 79 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); |
| 75 } | 80 } |
| 76 | 81 |
| 82 TEST(HttpStreamParser, ShouldMerge_NoBody) { |
| 83 // Shouldn't be merged if upload data is non-existent. |
| 84 ASSERT_FALSE(HttpStreamParser::ShouldMerge("some header", NULL)); |
| 85 } |
| 86 |
| 87 TEST(HttpStreamParser, ShouldMerge_EmptyBody) { |
| 88 scoped_refptr<UploadData> upload_data = new UploadData; |
| 89 scoped_ptr<UploadDataStream> body( |
| 90 UploadDataStream::Create(upload_data.get(), NULL)); |
| 91 // Shouldn't be merged if upload data is empty. |
| 92 ASSERT_FALSE(HttpStreamParser::ShouldMerge("some header", body.get())); |
| 93 } |
| 94 |
| 95 TEST(HttpStreamParser, ShouldMerge_ChunkedBody) { |
| 96 scoped_refptr<UploadData> upload_data = new UploadData; |
| 97 upload_data->set_is_chunked(true); |
| 98 const std::string payload = "123"; |
| 99 upload_data->AppendChunk(payload.data(), payload.size(), true); |
| 100 |
| 101 scoped_ptr<UploadDataStream> body( |
| 102 UploadDataStream::Create(upload_data.get(), NULL)); |
| 103 // Shouldn't be merged if upload data carries chunked data. |
| 104 ASSERT_FALSE(HttpStreamParser::ShouldMerge("some header", body.get())); |
| 105 } |
| 106 |
| 107 TEST(HttpStreamParser, ShouldMerge_FileBody) { |
| 108 scoped_refptr<UploadData> upload_data = new UploadData; |
| 109 |
| 110 // Create an empty temporary file. |
| 111 ScopedTempDir temp_dir; |
| 112 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 113 FilePath temp_file_path; |
| 114 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), |
| 115 &temp_file_path)); |
| 116 |
| 117 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time()); |
| 118 |
| 119 scoped_ptr<UploadDataStream> body( |
| 120 UploadDataStream::Create(upload_data.get(), NULL)); |
| 121 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
| 122 ASSERT_FALSE(HttpStreamParser::ShouldMerge("some header", body.get())); |
| 123 } |
| 124 |
| 125 TEST(HttpStreamParser, ShouldMerge_SmallBodyInMemory) { |
| 126 scoped_refptr<UploadData> upload_data = new UploadData; |
| 127 const std::string payload = "123"; |
| 128 upload_data->AppendBytes(payload.data(), payload.size()); |
| 129 |
| 130 scoped_ptr<UploadDataStream> body( |
| 131 UploadDataStream::Create(upload_data.get(), NULL)); |
| 132 // Yes, should be merged if the in-memory body is small here. |
| 133 ASSERT_TRUE(HttpStreamParser::ShouldMerge("some header", body.get())); |
| 134 } |
| 135 |
| 136 TEST(HttpStreamParser, ShouldMerge_LargeBodyInMemory) { |
| 137 scoped_refptr<UploadData> upload_data = new UploadData; |
| 138 const std::string payload(10000, 'a'); // 'a' x 10000. |
| 139 upload_data->AppendBytes(payload.data(), payload.size()); |
| 140 |
| 141 scoped_ptr<UploadDataStream> body( |
| 142 UploadDataStream::Create(upload_data.get(), NULL)); |
| 143 // Shouldn't be merged if the in-memory body is large here. |
| 144 ASSERT_FALSE(HttpStreamParser::ShouldMerge("some header", body.get())); |
| 145 } |
| 146 |
| 77 } // namespace net | 147 } // namespace net |
| OLD | NEW |