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 <memory> | 10 #include <memory> |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 } | 234 } |
235 | 235 |
236 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { | 236 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
237 // Shouldn't be merged if upload data is non-existent. | 237 // Shouldn't be merged if upload data is non-existent. |
238 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 238 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
239 "some header", NULL)); | 239 "some header", NULL)); |
240 } | 240 } |
241 | 241 |
242 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { | 242 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
243 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 243 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
244 std::unique_ptr<UploadDataStream> body(base::WrapUnique( | 244 std::unique_ptr<UploadDataStream> body( |
245 new ElementsUploadDataStream(std::move(element_readers), 0))); | 245 base::MakeUnique<ElementsUploadDataStream>(std::move(element_readers), |
| 246 0)); |
246 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); | 247 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); |
247 // Shouldn't be merged if upload data is empty. | 248 // Shouldn't be merged if upload data is empty. |
248 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 249 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
249 "some header", body.get())); | 250 "some header", body.get())); |
250 } | 251 } |
251 | 252 |
252 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { | 253 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
253 const std::string payload = "123"; | 254 const std::string payload = "123"; |
254 std::unique_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); | 255 std::unique_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); |
255 body->AppendData(payload.data(), payload.size(), true); | 256 body->AppendData(payload.data(), payload.size(), true); |
256 ASSERT_THAT(body->Init(TestCompletionCallback().callback(), BoundNetLog()), | 257 ASSERT_THAT(body->Init(TestCompletionCallback().callback(), BoundNetLog()), |
257 IsOk()); | 258 IsOk()); |
258 // Shouldn't be merged if upload data carries chunked data. | 259 // Shouldn't be merged if upload data carries chunked data. |
259 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 260 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
260 "some header", body.get())); | 261 "some header", body.get())); |
261 } | 262 } |
262 | 263 |
263 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { | 264 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
264 // Create an empty temporary file. | 265 // Create an empty temporary file. |
265 base::ScopedTempDir temp_dir; | 266 base::ScopedTempDir temp_dir; |
266 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 267 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
267 base::FilePath temp_file_path; | 268 base::FilePath temp_file_path; |
268 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); | 269 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); |
269 | 270 |
270 { | 271 { |
271 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 272 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
272 | 273 |
273 element_readers.push_back(base::WrapUnique( | 274 element_readers.push_back(base::MakeUnique<UploadFileElementReader>( |
274 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), | 275 base::ThreadTaskRunnerHandle::Get().get(), temp_file_path, 0, 0, |
275 temp_file_path, 0, 0, base::Time()))); | 276 base::Time())); |
276 | 277 |
277 std::unique_ptr<UploadDataStream> body( | 278 std::unique_ptr<UploadDataStream> body( |
278 new ElementsUploadDataStream(std::move(element_readers), 0)); | 279 new ElementsUploadDataStream(std::move(element_readers), 0)); |
279 TestCompletionCallback callback; | 280 TestCompletionCallback callback; |
280 ASSERT_THAT(body->Init(callback.callback(), BoundNetLog()), | 281 ASSERT_THAT(body->Init(callback.callback(), BoundNetLog()), |
281 IsError(ERR_IO_PENDING)); | 282 IsError(ERR_IO_PENDING)); |
282 ASSERT_THAT(callback.WaitForResult(), IsOk()); | 283 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
283 // Shouldn't be merged if upload data carries a file, as it's not in-memory. | 284 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
284 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 285 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
285 "some header", body.get())); | 286 "some header", body.get())); |
286 } | 287 } |
287 | 288 |
288 // UploadFileElementReaders may post clean-up tasks on destruction. | 289 // UploadFileElementReaders may post clean-up tasks on destruction. |
289 base::RunLoop().RunUntilIdle(); | 290 base::RunLoop().RunUntilIdle(); |
290 } | 291 } |
291 | 292 |
292 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { | 293 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
293 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 294 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
294 const std::string payload = "123"; | 295 const std::string payload = "123"; |
295 element_readers.push_back(base::WrapUnique( | 296 element_readers.push_back(base::MakeUnique<UploadBytesElementReader>( |
296 new UploadBytesElementReader(payload.data(), payload.size()))); | 297 payload.data(), payload.size())); |
297 | 298 |
298 std::unique_ptr<UploadDataStream> body( | 299 std::unique_ptr<UploadDataStream> body( |
299 new ElementsUploadDataStream(std::move(element_readers), 0)); | 300 new ElementsUploadDataStream(std::move(element_readers), 0)); |
300 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); | 301 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); |
301 // Yes, should be merged if the in-memory body is small here. | 302 // Yes, should be merged if the in-memory body is small here. |
302 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 303 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
303 "some header", body.get())); | 304 "some header", body.get())); |
304 } | 305 } |
305 | 306 |
306 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { | 307 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
307 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 308 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
308 const std::string payload(10000, 'a'); // 'a' x 10000. | 309 const std::string payload(10000, 'a'); // 'a' x 10000. |
309 element_readers.push_back(base::WrapUnique( | 310 element_readers.push_back(base::MakeUnique<UploadBytesElementReader>( |
310 new UploadBytesElementReader(payload.data(), payload.size()))); | 311 payload.data(), payload.size())); |
311 | 312 |
312 std::unique_ptr<UploadDataStream> body( | 313 std::unique_ptr<UploadDataStream> body( |
313 new ElementsUploadDataStream(std::move(element_readers), 0)); | 314 new ElementsUploadDataStream(std::move(element_readers), 0)); |
314 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); | 315 ASSERT_THAT(body->Init(CompletionCallback(), BoundNetLog()), IsOk()); |
315 // Shouldn't be merged if the in-memory body is large here. | 316 // Shouldn't be merged if the in-memory body is large here. |
316 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 317 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
317 "some header", body.get())); | 318 "some header", body.get())); |
318 } | 319 } |
319 | 320 |
320 TEST(HttpStreamParser, SentBytesNoHeaders) { | 321 TEST(HttpStreamParser, SentBytesNoHeaders) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), | 445 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), |
445 MockWrite(SYNCHRONOUS, 2, "hello world!"), | 446 MockWrite(SYNCHRONOUS, 2, "hello world!"), |
446 }; | 447 }; |
447 | 448 |
448 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 449 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
449 std::unique_ptr<ClientSocketHandle> socket_handle = | 450 std::unique_ptr<ClientSocketHandle> socket_handle = |
450 CreateConnectedSocketHandle(&data); | 451 CreateConnectedSocketHandle(&data); |
451 | 452 |
452 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 453 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
453 element_readers.push_back( | 454 element_readers.push_back( |
454 base::WrapUnique(new UploadBytesElementReader("hello world!", 12))); | 455 base::MakeUnique<UploadBytesElementReader>("hello world!", 12)); |
455 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 456 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
456 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback(), | 457 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback(), |
457 BoundNetLog()), | 458 BoundNetLog()), |
458 IsOk()); | 459 IsOk()); |
459 | 460 |
460 HttpRequestInfo request; | 461 HttpRequestInfo request; |
461 request.method = "POST"; | 462 request.method = "POST"; |
462 request.url = GURL("http://localhost"); | 463 request.url = GURL("http://localhost"); |
463 request.upload_data_stream = &upload_data_stream; | 464 request.upload_data_stream = &upload_data_stream; |
464 | 465 |
(...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1505 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1506 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
1506 body_buffer.get(), kBodySize, callback.callback())); | 1507 body_buffer.get(), kBodySize, callback.callback())); |
1507 | 1508 |
1508 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1509 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
1509 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1510 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
1510 } | 1511 } |
1511 | 1512 |
1512 } // namespace | 1513 } // namespace |
1513 | 1514 |
1514 } // namespace net | 1515 } // namespace net |
OLD | NEW |