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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
21 #include "net/base/chunked_upload_data_stream.h" | 22 #include "net/base/chunked_upload_data_stream.h" |
22 #include "net/base/elements_upload_data_stream.h" | 23 #include "net/base/elements_upload_data_stream.h" |
23 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/base/test_completion_callback.h" | 26 #include "net/base/test_completion_callback.h" |
26 #include "net/base/upload_bytes_element_reader.h" | 27 #include "net/base/upload_bytes_element_reader.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); | 120 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); |
120 } | 121 } |
121 | 122 |
122 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { | 123 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
123 // Shouldn't be merged if upload data is non-existent. | 124 // Shouldn't be merged if upload data is non-existent. |
124 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 125 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
125 "some header", NULL)); | 126 "some header", NULL)); |
126 } | 127 } |
127 | 128 |
128 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { | 129 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
129 ScopedVector<UploadElementReader> element_readers; | 130 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
130 scoped_ptr<UploadDataStream> body( | 131 scoped_ptr<UploadDataStream> body(make_scoped_ptr( |
131 new ElementsUploadDataStream(element_readers.Pass(), 0)); | 132 new ElementsUploadDataStream(std::move(element_readers), 0))); |
132 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 133 ASSERT_EQ(OK, body->Init(CompletionCallback())); |
133 // Shouldn't be merged if upload data is empty. | 134 // Shouldn't be merged if upload data is empty. |
134 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 135 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
135 "some header", body.get())); | 136 "some header", body.get())); |
136 } | 137 } |
137 | 138 |
138 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { | 139 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
139 const std::string payload = "123"; | 140 const std::string payload = "123"; |
140 scoped_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); | 141 scoped_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); |
141 body->AppendData(payload.data(), payload.size(), true); | 142 body->AppendData(payload.data(), payload.size(), true); |
142 ASSERT_EQ(OK, body->Init(TestCompletionCallback().callback())); | 143 ASSERT_EQ(OK, body->Init(TestCompletionCallback().callback())); |
143 // Shouldn't be merged if upload data carries chunked data. | 144 // Shouldn't be merged if upload data carries chunked data. |
144 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 145 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
145 "some header", body.get())); | 146 "some header", body.get())); |
146 } | 147 } |
147 | 148 |
148 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { | 149 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
149 // Create an empty temporary file. | 150 // Create an empty temporary file. |
150 base::ScopedTempDir temp_dir; | 151 base::ScopedTempDir temp_dir; |
151 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
152 base::FilePath temp_file_path; | 153 base::FilePath temp_file_path; |
153 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); | 154 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); |
154 | 155 |
155 { | 156 { |
156 ScopedVector<UploadElementReader> element_readers; | 157 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
157 | 158 |
158 element_readers.push_back( | 159 element_readers.push_back(make_scoped_ptr( |
159 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), | 160 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), |
160 temp_file_path, 0, 0, base::Time())); | 161 temp_file_path, 0, 0, base::Time()))); |
161 | 162 |
162 scoped_ptr<UploadDataStream> body( | 163 scoped_ptr<UploadDataStream> body( |
163 new ElementsUploadDataStream(element_readers.Pass(), 0)); | 164 new ElementsUploadDataStream(std::move(element_readers), 0)); |
164 TestCompletionCallback callback; | 165 TestCompletionCallback callback; |
165 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback())); | 166 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback())); |
166 ASSERT_EQ(OK, callback.WaitForResult()); | 167 ASSERT_EQ(OK, callback.WaitForResult()); |
167 // Shouldn't be merged if upload data carries a file, as it's not in-memory. | 168 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
168 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 169 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
169 "some header", body.get())); | 170 "some header", body.get())); |
170 } | 171 } |
171 | 172 |
172 // UploadFileElementReaders may post clean-up tasks on destruction. | 173 // UploadFileElementReaders may post clean-up tasks on destruction. |
173 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
174 } | 175 } |
175 | 176 |
176 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { | 177 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
177 ScopedVector<UploadElementReader> element_readers; | 178 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
178 const std::string payload = "123"; | 179 const std::string payload = "123"; |
179 element_readers.push_back(new UploadBytesElementReader( | 180 element_readers.push_back(make_scoped_ptr( |
180 payload.data(), payload.size())); | 181 new UploadBytesElementReader(payload.data(), payload.size()))); |
181 | 182 |
182 scoped_ptr<UploadDataStream> body( | 183 scoped_ptr<UploadDataStream> body( |
183 new ElementsUploadDataStream(element_readers.Pass(), 0)); | 184 new ElementsUploadDataStream(std::move(element_readers), 0)); |
184 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 185 ASSERT_EQ(OK, body->Init(CompletionCallback())); |
185 // Yes, should be merged if the in-memory body is small here. | 186 // Yes, should be merged if the in-memory body is small here. |
186 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 187 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
187 "some header", body.get())); | 188 "some header", body.get())); |
188 } | 189 } |
189 | 190 |
190 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { | 191 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
191 ScopedVector<UploadElementReader> element_readers; | 192 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
192 const std::string payload(10000, 'a'); // 'a' x 10000. | 193 const std::string payload(10000, 'a'); // 'a' x 10000. |
193 element_readers.push_back(new UploadBytesElementReader( | 194 element_readers.push_back(make_scoped_ptr( |
194 payload.data(), payload.size())); | 195 new UploadBytesElementReader(payload.data(), payload.size()))); |
195 | 196 |
196 scoped_ptr<UploadDataStream> body( | 197 scoped_ptr<UploadDataStream> body( |
197 new ElementsUploadDataStream(element_readers.Pass(), 0)); | 198 new ElementsUploadDataStream(std::move(element_readers), 0)); |
198 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 199 ASSERT_EQ(OK, body->Init(CompletionCallback())); |
199 // Shouldn't be merged if the in-memory body is large here. | 200 // Shouldn't be merged if the in-memory body is large here. |
200 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 201 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
201 "some header", body.get())); | 202 "some header", body.get())); |
202 } | 203 } |
203 | 204 |
204 TEST(HttpStreamParser, SentBytesNoHeaders) { | 205 TEST(HttpStreamParser, SentBytesNoHeaders) { |
205 MockWrite writes[] = { | 206 MockWrite writes[] = { |
206 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), | 207 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), |
207 }; | 208 }; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 MockWrite writes[] = { | 327 MockWrite writes[] = { |
327 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), | 328 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), |
328 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), | 329 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), |
329 MockWrite(SYNCHRONOUS, 2, "hello world!"), | 330 MockWrite(SYNCHRONOUS, 2, "hello world!"), |
330 }; | 331 }; |
331 | 332 |
332 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 333 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
333 scoped_ptr<ClientSocketHandle> socket_handle = | 334 scoped_ptr<ClientSocketHandle> socket_handle = |
334 CreateConnectedSocketHandle(&data); | 335 CreateConnectedSocketHandle(&data); |
335 | 336 |
336 ScopedVector<UploadElementReader> element_readers; | 337 std::vector<scoped_ptr<UploadElementReader>> element_readers; |
337 element_readers.push_back(new UploadBytesElementReader("hello world!", 12)); | 338 element_readers.push_back( |
338 ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | 339 make_scoped_ptr(new UploadBytesElementReader("hello world!", 12))); |
| 340 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
339 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | 341 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); |
340 | 342 |
341 HttpRequestInfo request; | 343 HttpRequestInfo request; |
342 request.method = "POST"; | 344 request.method = "POST"; |
343 request.url = GURL("http://localhost"); | 345 request.url = GURL("http://localhost"); |
344 request.upload_data_stream = &upload_data_stream; | 346 request.upload_data_stream = &upload_data_stream; |
345 | 347 |
346 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 348 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
347 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | 349 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
348 BoundNetLog()); | 350 BoundNetLog()); |
(...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1283 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
1282 body_buffer.get(), kBodySize, callback.callback())); | 1284 body_buffer.get(), kBodySize, callback.callback())); |
1283 | 1285 |
1284 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1286 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
1285 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1287 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
1286 } | 1288 } |
1287 | 1289 |
1288 } // namespace | 1290 } // namespace |
1289 | 1291 |
1290 } // namespace net | 1292 } // namespace net |
OLD | NEW |