| 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" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { | 82 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
| 83 // Shouldn't be merged if upload data is non-existent. | 83 // Shouldn't be merged if upload data is non-existent. |
| 84 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 84 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 85 "some header", NULL)); | 85 "some header", NULL)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { | 88 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
| 89 scoped_refptr<UploadData> upload_data = new UploadData; | 89 scoped_refptr<UploadData> upload_data = new UploadData; |
| 90 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 90 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 91 ASSERT_EQ(OK, body->Init()); | 91 ASSERT_EQ(OK, body->InitSync()); |
| 92 // Shouldn't be merged if upload data is empty. | 92 // Shouldn't be merged if upload data is empty. |
| 93 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 93 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 94 "some header", body.get())); | 94 "some header", body.get())); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { | 97 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
| 98 scoped_refptr<UploadData> upload_data = new UploadData; | 98 scoped_refptr<UploadData> upload_data = new UploadData; |
| 99 upload_data->set_is_chunked(true); | 99 upload_data->set_is_chunked(true); |
| 100 const std::string payload = "123"; | 100 const std::string payload = "123"; |
| 101 upload_data->AppendChunk(payload.data(), payload.size(), true); | 101 upload_data->AppendChunk(payload.data(), payload.size(), true); |
| 102 | 102 |
| 103 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 103 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 104 ASSERT_EQ(OK, body->Init()); | 104 ASSERT_EQ(OK, body->InitSync()); |
| 105 // Shouldn't be merged if upload data carries chunked data. | 105 // Shouldn't be merged if upload data carries chunked data. |
| 106 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 106 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 107 "some header", body.get())); | 107 "some header", body.get())); |
| 108 } | 108 } |
| 109 | 109 |
| 110 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { | 110 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
| 111 scoped_refptr<UploadData> upload_data = new UploadData; | 111 scoped_refptr<UploadData> upload_data = new UploadData; |
| 112 | 112 |
| 113 // Create an empty temporary file. | 113 // Create an empty temporary file. |
| 114 ScopedTempDir temp_dir; | 114 ScopedTempDir temp_dir; |
| 115 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 115 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 116 FilePath temp_file_path; | 116 FilePath temp_file_path; |
| 117 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), | 117 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), |
| 118 &temp_file_path)); | 118 &temp_file_path)); |
| 119 | 119 |
| 120 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time()); | 120 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time()); |
| 121 | 121 |
| 122 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 122 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 123 ASSERT_EQ(OK, body->Init()); | 123 ASSERT_EQ(OK, body->InitSync()); |
| 124 // Shouldn't be merged if upload data carries a file, as it's not in-memory. | 124 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
| 125 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 125 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 126 "some header", body.get())); | 126 "some header", body.get())); |
| 127 } | 127 } |
| 128 | 128 |
| 129 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { | 129 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
| 130 scoped_refptr<UploadData> upload_data = new UploadData; | 130 scoped_refptr<UploadData> upload_data = new UploadData; |
| 131 const std::string payload = "123"; | 131 const std::string payload = "123"; |
| 132 upload_data->AppendBytes(payload.data(), payload.size()); | 132 upload_data->AppendBytes(payload.data(), payload.size()); |
| 133 | 133 |
| 134 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 134 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 135 ASSERT_EQ(OK, body->Init()); | 135 ASSERT_EQ(OK, body->InitSync()); |
| 136 // Yes, should be merged if the in-memory body is small here. | 136 // Yes, should be merged if the in-memory body is small here. |
| 137 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 137 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 138 "some header", body.get())); | 138 "some header", body.get())); |
| 139 } | 139 } |
| 140 | 140 |
| 141 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { | 141 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
| 142 scoped_refptr<UploadData> upload_data = new UploadData; | 142 scoped_refptr<UploadData> upload_data = new UploadData; |
| 143 const std::string payload(10000, 'a'); // 'a' x 10000. | 143 const std::string payload(10000, 'a'); // 'a' x 10000. |
| 144 upload_data->AppendBytes(payload.data(), payload.size()); | 144 upload_data->AppendBytes(payload.data(), payload.size()); |
| 145 | 145 |
| 146 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 146 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 147 ASSERT_EQ(OK, body->Init()); | 147 ASSERT_EQ(OK, body->InitSync()); |
| 148 // Shouldn't be merged if the in-memory body is large here. | 148 // Shouldn't be merged if the in-memory body is large here. |
| 149 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 149 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 150 "some header", body.get())); | 150 "some header", body.get())); |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace net | 153 } // namespace net |
| OLD | NEW |