| 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/quic/chromium/quic_chromium_client_stream.h" | 5 #include "net/quic/chromium/quic_chromium_client_stream.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 358 |
| 359 stream_->OnStreamHeaders(uncompressed_trailers); | 359 stream_->OnStreamHeaders(uncompressed_trailers); |
| 360 stream_->OnStreamHeadersComplete(true, uncompressed_trailers.length()); | 360 stream_->OnStreamHeadersComplete(true, uncompressed_trailers.length()); |
| 361 | 361 |
| 362 base::RunLoop run_loop; | 362 base::RunLoop run_loop; |
| 363 EXPECT_CALL(delegate_, | 363 EXPECT_CALL(delegate_, |
| 364 OnHeadersAvailableMock(_, uncompressed_trailers.length())) | 364 OnHeadersAvailableMock(_, uncompressed_trailers.length())) |
| 365 .WillOnce(testing::InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); | 365 .WillOnce(testing::InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); |
| 366 | 366 |
| 367 run_loop.Run(); | 367 run_loop.Run(); |
| 368 |
| 369 // OnDataAvailable callback should follow trailers notification. |
| 370 base::RunLoop run_loop3; |
| 371 EXPECT_CALL(delegate_, OnDataAvailable()) |
| 372 .Times(1) |
| 373 .WillOnce(testing::DoAll( |
| 374 testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| 375 base::Unretained(this), StringPiece())), |
| 376 testing::InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }))); |
| 377 run_loop3.Run(); |
| 378 |
| 368 // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. | 379 // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. |
| 369 trailers.erase(kFinalOffsetHeaderKey); | 380 trailers.erase(kFinalOffsetHeaderKey); |
| 370 EXPECT_EQ(trailers, delegate_.headers_); | 381 EXPECT_EQ(trailers, delegate_.headers_); |
| 371 base::RunLoop().RunUntilIdle(); | 382 base::RunLoop().RunUntilIdle(); |
| 372 EXPECT_CALL(delegate_, OnClose()); | 383 EXPECT_CALL(delegate_, OnClose()); |
| 373 } | 384 } |
| 374 | 385 |
| 375 // Tests that trailers are marked as consumed only before delegate is to be | 386 // Tests that trailers are marked as consumed only before delegate is to be |
| 376 // immediately notified about trailers. | 387 // immediately notified about trailers. |
| 377 TEST_P(QuicChromiumClientStreamTest, MarkTrailersConsumedWhenNotifyDelegate) { | 388 TEST_P(QuicChromiumClientStreamTest, MarkTrailersConsumedWhenNotifyDelegate) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 EXPECT_FALSE(stream_->IsDoneReading()); | 429 EXPECT_FALSE(stream_->IsDoneReading()); |
| 419 | 430 |
| 420 base::RunLoop run_loop2; | 431 base::RunLoop run_loop2; |
| 421 EXPECT_CALL(delegate_, | 432 EXPECT_CALL(delegate_, |
| 422 OnHeadersAvailableMock(_, uncompressed_trailers.length())) | 433 OnHeadersAvailableMock(_, uncompressed_trailers.length())) |
| 423 .WillOnce( | 434 .WillOnce( |
| 424 testing::InvokeWithoutArgs([&run_loop2]() { run_loop2.Quit(); })); | 435 testing::InvokeWithoutArgs([&run_loop2]() { run_loop2.Quit(); })); |
| 425 | 436 |
| 426 run_loop2.Run(); | 437 run_loop2.Run(); |
| 427 | 438 |
| 439 // OnDataAvailable callback should follow trailers notification. |
| 440 base::RunLoop run_loop3; |
| 441 EXPECT_CALL(delegate_, OnDataAvailable()) |
| 442 .Times(1) |
| 443 .WillOnce(testing::DoAll( |
| 444 testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| 445 base::Unretained(this), StringPiece())), |
| 446 testing::InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }))); |
| 447 run_loop3.Run(); |
| 448 |
| 428 // Make sure the stream is properly closed since trailers and data are all | 449 // Make sure the stream is properly closed since trailers and data are all |
| 429 // consumed. | 450 // consumed. |
| 430 EXPECT_TRUE(stream_->IsDoneReading()); | 451 EXPECT_TRUE(stream_->IsDoneReading()); |
| 431 // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. | 452 // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. |
| 432 trailers.erase(kFinalOffsetHeaderKey); | 453 trailers.erase(kFinalOffsetHeaderKey); |
| 433 EXPECT_EQ(trailers, delegate_.headers_); | 454 EXPECT_EQ(trailers, delegate_.headers_); |
| 434 | 455 |
| 435 base::RunLoop().RunUntilIdle(); | 456 base::RunLoop().RunUntilIdle(); |
| 436 EXPECT_CALL(delegate_, OnClose()); | 457 EXPECT_CALL(delegate_, OnClose()); |
| 437 } | 458 } |
| 438 | 459 |
| 460 // Test that if Read() is called after response body is read and after trailers |
| 461 // are received but not yet delivered, Read() will return ERR_IO_PENDING instead |
| 462 // of 0 (EOF). |
| 463 TEST_P(QuicChromiumClientStreamTest, ReadAfterTrailersReceivedButNotDelivered) { |
| 464 InitializeHeaders(); |
| 465 std::string uncompressed_headers = |
| 466 SpdyUtils::SerializeUncompressedHeaders(headers_); |
| 467 stream_->OnStreamHeaders(uncompressed_headers); |
| 468 stream_->OnStreamHeadersComplete(false, uncompressed_headers.length()); |
| 469 |
| 470 EXPECT_CALL(delegate_, |
| 471 OnHeadersAvailableMock(_, uncompressed_headers.length())); |
| 472 base::RunLoop().RunUntilIdle(); |
| 473 EXPECT_EQ(headers_, delegate_.headers_); |
| 474 EXPECT_TRUE(stream_->decompressed_headers().empty()); |
| 475 |
| 476 const char data[] = "hello world!"; |
| 477 stream_->OnStreamFrame(QuicStreamFrame(kTestStreamId, /*fin=*/false, |
| 478 /*offset=*/0, data)); |
| 479 |
| 480 base::RunLoop run_loop; |
| 481 EXPECT_CALL(delegate_, OnDataAvailable()) |
| 482 .Times(1) |
| 483 .WillOnce(testing::DoAll( |
| 484 testing::Invoke(CreateFunctor( |
| 485 &QuicChromiumClientStreamTest::ReadData, base::Unretained(this), |
| 486 StringPiece(data, arraysize(data) - 1))), |
| 487 testing::Invoke([&run_loop]() { run_loop.Quit(); }))); |
| 488 |
| 489 // Wait for the read to complete. |
| 490 run_loop.Run(); |
| 491 |
| 492 // Deliver trailers. Delegate notification is posted asynchronously. |
| 493 SpdyHeaderBlock trailers; |
| 494 trailers["bar"] = "foo"; |
| 495 trailers[kFinalOffsetHeaderKey] = base::IntToString(strlen(data)); |
| 496 std::string uncompressed_trailers = |
| 497 SpdyUtils::SerializeUncompressedHeaders(trailers); |
| 498 |
| 499 stream_->OnStreamHeaders(uncompressed_trailers); |
| 500 stream_->OnStreamHeadersComplete(true, uncompressed_trailers.length()); |
| 501 |
| 502 // Read again, it return ERR_IO_PENDING. |
| 503 scoped_refptr<IOBuffer> buffer(new IOBuffer(1)); |
| 504 EXPECT_THAT(stream_->Read(buffer.get(), 1), ERR_IO_PENDING); |
| 505 |
| 506 // Trailers are not delivered |
| 507 EXPECT_FALSE(stream_->IsDoneReading()); |
| 508 |
| 509 base::RunLoop run_loop2; |
| 510 EXPECT_CALL(delegate_, |
| 511 OnHeadersAvailableMock(_, uncompressed_trailers.length())) |
| 512 .WillOnce( |
| 513 testing::InvokeWithoutArgs([&run_loop2]() { run_loop2.Quit(); })); |
| 514 |
| 515 run_loop2.Run(); |
| 516 |
| 517 base::RunLoop run_loop3; |
| 518 // OnDataAvailable() should follow right after and Read() will return 0. |
| 519 EXPECT_CALL(delegate_, OnDataAvailable()) |
| 520 .WillOnce(testing::DoAll( |
| 521 testing::Invoke(CreateFunctor(&QuicChromiumClientStreamTest::ReadData, |
| 522 base::Unretained(this), StringPiece())), |
| 523 testing::Invoke([&run_loop3]() { run_loop3.Quit(); }))); |
| 524 run_loop3.Run(); |
| 525 |
| 526 // Make sure the stream is properly closed since trailers and data are all |
| 527 // consumed. |
| 528 EXPECT_TRUE(stream_->IsDoneReading()); |
| 529 |
| 530 // Make sure kFinalOffsetHeaderKey is gone from the delivered actual trailers. |
| 531 trailers.erase(kFinalOffsetHeaderKey); |
| 532 EXPECT_EQ(trailers, delegate_.headers_); |
| 533 |
| 534 base::RunLoop().RunUntilIdle(); |
| 535 EXPECT_CALL(delegate_, OnClose()); |
| 536 } |
| 537 |
| 439 TEST_P(QuicChromiumClientStreamTest, WriteStreamData) { | 538 TEST_P(QuicChromiumClientStreamTest, WriteStreamData) { |
| 440 EXPECT_CALL(delegate_, OnClose()); | 539 EXPECT_CALL(delegate_, OnClose()); |
| 441 | 540 |
| 442 const char kData1[] = "hello world"; | 541 const char kData1[] = "hello world"; |
| 443 const size_t kDataLen = arraysize(kData1); | 542 const size_t kDataLen = arraysize(kData1); |
| 444 | 543 |
| 445 // All data written. | 544 // All data written. |
| 446 EXPECT_CALL(session_, WritevData(stream_, stream_->id(), _, _, _, _)) | 545 EXPECT_CALL(session_, WritevData(stream_, stream_->id(), _, _, _, _)) |
| 447 .WillOnce(Return(QuicConsumedData(kDataLen, true))); | 546 .WillOnce(Return(QuicConsumedData(kDataLen, true))); |
| 448 TestCompletionCallback callback; | 547 TestCompletionCallback callback; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 base::RunLoop().RunUntilIdle(); | 639 base::RunLoop().RunUntilIdle(); |
| 541 EXPECT_EQ(headers_, delegate_.headers_); | 640 EXPECT_EQ(headers_, delegate_.headers_); |
| 542 | 641 |
| 543 // Times(2) because OnClose will be called for stream and stream_. | 642 // Times(2) because OnClose will be called for stream and stream_. |
| 544 EXPECT_CALL(delegate_, OnClose()).Times(2); | 643 EXPECT_CALL(delegate_, OnClose()).Times(2); |
| 545 } | 644 } |
| 546 | 645 |
| 547 } // namespace | 646 } // namespace |
| 548 } // namespace test | 647 } // namespace test |
| 549 } // namespace net | 648 } // namespace net |
| OLD | NEW |