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/spdy/spdy_http_stream.h" | 5 #include "net/spdy/spdy_http_stream.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 EXPECT_EQ(static_cast<int64_t>(req->size() + body->size()), | 383 EXPECT_EQ(static_cast<int64_t>(req->size() + body->size()), |
384 http_stream.GetTotalSentBytes()); | 384 http_stream.GetTotalSentBytes()); |
385 EXPECT_EQ(static_cast<int64_t>(resp->size() + body->size()), | 385 EXPECT_EQ(static_cast<int64_t>(resp->size() + body->size()), |
386 http_stream.GetTotalReceivedBytes()); | 386 http_stream.GetTotalReceivedBytes()); |
387 | 387 |
388 // Because the server closed the connection, we there shouldn't be a session | 388 // Because the server closed the connection, we there shouldn't be a session |
389 // in the pool anymore. | 389 // in the pool anymore. |
390 EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 390 EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
391 } | 391 } |
392 | 392 |
| 393 TEST_P(SpdyHttpStreamTest, SendChunkedPostLastEmpty) { |
| 394 const char kUploadData1[] = "12345678"; |
| 395 const int kUploadData1Size = arraysize(kUploadData1) - 1; |
| 396 |
| 397 std::unique_ptr<SpdySerializedFrame> req( |
| 398 spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
| 399 std::unique_ptr<SpdySerializedFrame> chunk1( |
| 400 spdy_util_.ConstructSpdyBodyFrame(1, false)); |
| 401 std::unique_ptr<SpdySerializedFrame> chunk2(spdy_util_.ConstructSpdyBodyFrame( |
| 402 1, kUploadData1, kUploadData1Size, false)); |
| 403 std::unique_ptr<SpdySerializedFrame> chunk3( |
| 404 spdy_util_.ConstructSpdyBodyFrame(1, "", 0, true)); |
| 405 MockWrite writes[] = { |
| 406 CreateMockWrite(*req, 0), // request |
| 407 CreateMockWrite(*chunk1, 1), CreateMockWrite(*chunk2, 2), |
| 408 CreateMockWrite(*chunk3, 3), |
| 409 }; |
| 410 |
| 411 std::unique_ptr<SpdySerializedFrame> resp( |
| 412 spdy_util_.ConstructSpdyPostSynReply(NULL, 0)); |
| 413 MockRead reads[] = { |
| 414 CreateMockRead(*resp, 4), CreateMockRead(*chunk1, 5), |
| 415 CreateMockRead(*chunk2, 6), CreateMockRead(*chunk3, 7), |
| 416 MockRead(SYNCHRONOUS, 0, 8) // EOF |
| 417 }; |
| 418 |
| 419 HostPortPair host_port_pair("www.example.org", 80); |
| 420 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
| 421 PRIVACY_MODE_DISABLED); |
| 422 InitSession(reads, arraysize(reads), writes, arraysize(writes), key); |
| 423 EXPECT_EQ(spdy_util_.spdy_version(), session_->GetProtocolVersion()); |
| 424 |
| 425 ChunkedUploadDataStream upload_stream(0); |
| 426 |
| 427 HttpRequestInfo request; |
| 428 request.method = "POST"; |
| 429 request.url = GURL("http://www.example.org/"); |
| 430 request.upload_data_stream = &upload_stream; |
| 431 |
| 432 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); |
| 433 upload_stream.AppendData(kUploadData, kUploadDataSize, false); |
| 434 |
| 435 TestCompletionCallback callback; |
| 436 HttpResponseInfo response; |
| 437 HttpRequestHeaders headers; |
| 438 BoundNetLog net_log; |
| 439 SpdyHttpStream http_stream(session_, true); |
| 440 ASSERT_EQ(OK, http_stream.InitializeStream(&request, DEFAULT_PRIORITY, |
| 441 net_log, CompletionCallback())); |
| 442 |
| 443 EXPECT_EQ(ERR_IO_PENDING, |
| 444 http_stream.SendRequest(headers, &response, callback.callback())); |
| 445 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
| 446 |
| 447 base::RunLoop().RunUntilIdle(); |
| 448 |
| 449 upload_stream.AppendData(kUploadData1, kUploadData1Size, false); |
| 450 |
| 451 base::RunLoop().RunUntilIdle(); |
| 452 |
| 453 upload_stream.AppendData(nullptr, 0, true); |
| 454 |
| 455 base::RunLoop().RunUntilIdle(); |
| 456 EXPECT_EQ(OK, callback.WaitForResult()); |
| 457 |
| 458 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size() + chunk2->size() + |
| 459 chunk3->size()), |
| 460 http_stream.GetTotalSentBytes()); |
| 461 EXPECT_EQ(static_cast<int64_t>(resp->size() + chunk1->size() + |
| 462 chunk2->size() + chunk3->size()), |
| 463 http_stream.GetTotalReceivedBytes()); |
| 464 |
| 465 // Because the server closed the connection, we there shouldn't be a session |
| 466 // in the pool anymore. |
| 467 EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
| 468 } |
| 469 |
393 TEST_P(SpdyHttpStreamTest, ConnectionClosedDuringChunkedPost) { | 470 TEST_P(SpdyHttpStreamTest, ConnectionClosedDuringChunkedPost) { |
394 BufferedSpdyFramer framer(spdy_util_.spdy_version()); | 471 BufferedSpdyFramer framer(spdy_util_.spdy_version()); |
395 | 472 |
396 std::unique_ptr<SpdySerializedFrame> req( | 473 std::unique_ptr<SpdySerializedFrame> req( |
397 spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); | 474 spdy_util_.ConstructChunkedSpdyPost(NULL, 0)); |
398 std::unique_ptr<SpdySerializedFrame> body( | 475 std::unique_ptr<SpdySerializedFrame> body( |
399 framer.CreateDataFrame(1, kUploadData, kUploadDataSize, DATA_FLAG_NONE)); | 476 framer.CreateDataFrame(1, kUploadData, kUploadDataSize, DATA_FLAG_NONE)); |
400 MockWrite writes[] = { | 477 MockWrite writes[] = { |
401 CreateMockWrite(*req, 0), // Request | 478 CreateMockWrite(*req, 0), // Request |
402 CreateMockWrite(*body, 1) // First POST upload frame | 479 CreateMockWrite(*body, 1) // First POST upload frame |
(...skipping 27 matching lines...) Expand all Loading... |
430 HttpRequestHeaders headers; | 507 HttpRequestHeaders headers; |
431 BoundNetLog net_log; | 508 BoundNetLog net_log; |
432 SpdyHttpStream http_stream(session_, true); | 509 SpdyHttpStream http_stream(session_, true); |
433 ASSERT_EQ(OK, http_stream.InitializeStream(&request, DEFAULT_PRIORITY, | 510 ASSERT_EQ(OK, http_stream.InitializeStream(&request, DEFAULT_PRIORITY, |
434 net_log, CompletionCallback())); | 511 net_log, CompletionCallback())); |
435 | 512 |
436 EXPECT_EQ(ERR_IO_PENDING, | 513 EXPECT_EQ(ERR_IO_PENDING, |
437 http_stream.SendRequest(headers, &response, callback.callback())); | 514 http_stream.SendRequest(headers, &response, callback.callback())); |
438 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 515 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
439 | 516 |
440 EXPECT_EQ(OK, callback.WaitForResult()); | 517 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
441 | 518 |
442 EXPECT_EQ(static_cast<int64_t>(req->size() + body->size()), | 519 EXPECT_EQ(static_cast<int64_t>(req->size() + body->size()), |
443 http_stream.GetTotalSentBytes()); | 520 http_stream.GetTotalSentBytes()); |
444 EXPECT_EQ(0, http_stream.GetTotalReceivedBytes()); | 521 EXPECT_EQ(0, http_stream.GetTotalReceivedBytes()); |
445 | 522 |
446 // Because the server closed the connection, we there shouldn't be a session | 523 // Because the server closed the connection, we there shouldn't be a session |
447 // in the pool anymore. | 524 // in the pool anymore. |
448 EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 525 EXPECT_FALSE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
449 | 526 |
450 // Appending a second chunk now should not result in a crash. | 527 // Appending a second chunk now should not result in a crash. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 HttpRequestHeaders headers; | 590 HttpRequestHeaders headers; |
514 HttpResponseInfo response; | 591 HttpResponseInfo response; |
515 // This will attempt to Write() the initial request and headers, which will | 592 // This will attempt to Write() the initial request and headers, which will |
516 // complete asynchronously. | 593 // complete asynchronously. |
517 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, | 594 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, |
518 callback.callback())); | 595 callback.callback())); |
519 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 596 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
520 | 597 |
521 // Complete the initial request write and the first chunk. | 598 // Complete the initial request write and the first chunk. |
522 base::RunLoop().RunUntilIdle(); | 599 base::RunLoop().RunUntilIdle(); |
523 ASSERT_TRUE(callback.have_result()); | 600 ASSERT_FALSE(callback.have_result()); |
524 EXPECT_EQ(OK, callback.WaitForResult()); | |
525 | 601 |
526 // Now append the final two chunks which will enqueue two more writes. | 602 // Now append the final two chunks which will enqueue two more writes. |
527 upload_stream.AppendData(kUploadData1, kUploadData1Size, false); | 603 upload_stream.AppendData(kUploadData1, kUploadData1Size, false); |
528 upload_stream.AppendData(kUploadData, kUploadDataSize, true); | 604 upload_stream.AppendData(kUploadData, kUploadDataSize, true); |
529 | 605 |
530 // Finish writing all the chunks and do all reads. | 606 // Finish writing all the chunks and do all reads. |
531 base::RunLoop().RunUntilIdle(); | 607 base::RunLoop().RunUntilIdle(); |
| 608 ASSERT_TRUE(callback.have_result()); |
| 609 EXPECT_EQ(OK, callback.WaitForResult()); |
532 | 610 |
533 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size() + chunk2->size() + | 611 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size() + chunk2->size() + |
534 chunk3->size()), | 612 chunk3->size()), |
535 http_stream->GetTotalSentBytes()); | 613 http_stream->GetTotalSentBytes()); |
536 EXPECT_EQ(static_cast<int64_t>(resp->size() + chunk1->size() + | 614 EXPECT_EQ(static_cast<int64_t>(resp->size() + chunk1->size() + |
537 chunk2->size() + chunk3->size()), | 615 chunk2->size() + chunk3->size()), |
538 http_stream->GetTotalReceivedBytes()); | 616 http_stream->GetTotalReceivedBytes()); |
539 | 617 |
540 // Check response headers. | 618 // Check response headers. |
541 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); | 619 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 HttpRequestHeaders headers; | 691 HttpRequestHeaders headers; |
614 HttpResponseInfo response; | 692 HttpResponseInfo response; |
615 // This will attempt to Write() the initial request and headers, which will | 693 // This will attempt to Write() the initial request and headers, which will |
616 // complete asynchronously. | 694 // complete asynchronously. |
617 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, | 695 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, |
618 callback.callback())); | 696 callback.callback())); |
619 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 697 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
620 | 698 |
621 // Complete the initial request write and the first chunk. | 699 // Complete the initial request write and the first chunk. |
622 base::RunLoop().RunUntilIdle(); | 700 base::RunLoop().RunUntilIdle(); |
623 ASSERT_TRUE(callback.have_result()); | 701 ASSERT_FALSE(callback.have_result()); |
624 EXPECT_EQ(OK, callback.WaitForResult()); | |
625 | 702 |
626 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size()), | 703 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size()), |
627 http_stream->GetTotalSentBytes()); | 704 http_stream->GetTotalSentBytes()); |
628 EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); | 705 EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); |
629 | 706 |
630 // Now end the stream with an empty data frame and the FIN set. | 707 // Now end the stream with an empty data frame and the FIN set. |
631 upload_stream.AppendData(NULL, 0, true); | 708 upload_stream.AppendData(NULL, 0, true); |
632 | 709 |
633 // Finish writing the final frame, and perform all reads. | 710 // Finish writing the final frame, and perform all reads. |
634 base::RunLoop().RunUntilIdle(); | 711 base::RunLoop().RunUntilIdle(); |
| 712 ASSERT_TRUE(callback.have_result()); |
| 713 EXPECT_EQ(OK, callback.WaitForResult()); |
635 | 714 |
636 // Check response headers. | 715 // Check response headers. |
637 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); | 716 ASSERT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
638 | 717 |
639 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size() + chunk2->size()), | 718 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size() + chunk2->size()), |
640 http_stream->GetTotalSentBytes()); | 719 http_stream->GetTotalSentBytes()); |
641 EXPECT_EQ( | 720 EXPECT_EQ( |
642 static_cast<int64_t>(resp->size() + chunk1->size() + chunk2->size()), | 721 static_cast<int64_t>(resp->size() + chunk1->size() + chunk2->size()), |
643 http_stream->GetTotalReceivedBytes()); | 722 http_stream->GetTotalReceivedBytes()); |
644 | 723 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 HttpResponseInfo response; | 907 HttpResponseInfo response; |
829 // This will attempt to Write() the initial request and headers, which will | 908 // This will attempt to Write() the initial request and headers, which will |
830 // complete asynchronously. | 909 // complete asynchronously. |
831 TestCompletionCallback callback; | 910 TestCompletionCallback callback; |
832 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, | 911 EXPECT_EQ(ERR_IO_PENDING, http_stream->SendRequest(headers, &response, |
833 callback.callback())); | 912 callback.callback())); |
834 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); | 913 EXPECT_TRUE(HasSpdySession(http_session_->spdy_session_pool(), key)); |
835 | 914 |
836 // Complete the initial request write and first chunk. | 915 // Complete the initial request write and first chunk. |
837 base::RunLoop().RunUntilIdle(); | 916 base::RunLoop().RunUntilIdle(); |
838 ASSERT_TRUE(callback.have_result()); | 917 ASSERT_FALSE(callback.have_result()); |
839 EXPECT_EQ(OK, callback.WaitForResult()); | |
840 | 918 |
841 EXPECT_EQ(static_cast<int64_t>(req->size()), | 919 EXPECT_EQ(static_cast<int64_t>(req->size()), |
842 http_stream->GetTotalSentBytes()); | 920 http_stream->GetTotalSentBytes()); |
843 EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); | 921 EXPECT_EQ(0, http_stream->GetTotalReceivedBytes()); |
844 | 922 |
845 upload_stream.AppendData(kUploadData, kUploadDataSize, true); | 923 upload_stream.AppendData(kUploadData, kUploadDataSize, true); |
846 | 924 |
| 925 ASSERT_TRUE(callback.have_result()); |
| 926 EXPECT_EQ(OK, callback.WaitForResult()); |
| 927 |
847 // Verify that the window size has decreased. | 928 // Verify that the window size has decreased. |
848 ASSERT_TRUE(http_stream->stream() != NULL); | 929 ASSERT_TRUE(http_stream->stream() != NULL); |
849 EXPECT_NE(static_cast<int>( | 930 EXPECT_NE(static_cast<int>( |
850 SpdySession::GetDefaultInitialWindowSize(session_->protocol())), | 931 SpdySession::GetDefaultInitialWindowSize(session_->protocol())), |
851 http_stream->stream()->send_window_size()); | 932 http_stream->stream()->send_window_size()); |
852 | 933 |
853 // Read window update. | 934 // Read window update. |
854 base::RunLoop().RunUntilIdle(); | 935 base::RunLoop().RunUntilIdle(); |
855 | 936 |
856 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size()), | 937 EXPECT_EQ(static_cast<int64_t>(req->size() + chunk1->size()), |
(...skipping 27 matching lines...) Expand all Loading... |
884 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); | 965 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
885 | 966 |
886 ASSERT_TRUE(response.headers.get()); | 967 ASSERT_TRUE(response.headers.get()); |
887 ASSERT_EQ(200, response.headers->response_code()); | 968 ASSERT_EQ(200, response.headers->response_code()); |
888 } | 969 } |
889 | 970 |
890 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 971 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
891 // methods. | 972 // methods. |
892 | 973 |
893 } // namespace net | 974 } // namespace net |
OLD | NEW |