Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: content/browser/loader/mojo_async_resource_handler_unittest.cc

Issue 2743723003: Add buffering to MimeSniffingResourceHandler.
Patch Set: Remove unused 'first_call' variable. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/loader/mojo_async_resource_handler.h" 5 #include "content/browser/loader/mojo_async_resource_handler.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 if (result == MOJO_RESULT_SHOULD_WAIT) { 547 if (result == MOJO_RESULT_SHOULD_WAIT) {
548 base::RunLoop().RunUntilIdle(); 548 base::RunLoop().RunUntilIdle();
549 continue; 549 continue;
550 } 550 }
551 contents.append(buffer, read_size); 551 contents.append(buffer, read_size);
552 } 552 }
553 EXPECT_EQ("AB", contents); 553 EXPECT_EQ("AB", contents);
554 } 554 }
555 555
556 TEST_F(MojoAsyncResourceHandlerTest, 556 TEST_F(MojoAsyncResourceHandlerTest,
557 OnWillReadAndOnReadCompletedWithInsufficientInitialCapacity) {
558 MojoAsyncResourceHandler::SetAllocationSizeForTesting(2);
559
560 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
561 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
562 // The buffer size that the mime sniffer requires implicitly.
563 ASSERT_GE(mock_loader_->io_buffer_size(),
564 kSizeMimeSnifferRequiresForFirstOnWillRead);
565
566 const std::string data("abcdefgh");
567 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING,
568 mock_loader_->OnReadCompleted(data));
569
570 url_loader_client_.RunUntilResponseBodyArrived();
571 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
572
573 std::string contents;
574 while (contents.size() < data.size()) {
575 // This is needed for Resume to be called.
576 base::RunLoop().RunUntilIdle();
577 char buffer[16];
578 uint32_t read_size = sizeof(buffer);
579 MojoResult result =
580 mojo::ReadDataRaw(url_loader_client_.response_body(), buffer,
581 &read_size, MOJO_READ_DATA_FLAG_NONE);
582 if (result == MOJO_RESULT_SHOULD_WAIT)
583 continue;
584 ASSERT_EQ(MOJO_RESULT_OK, result);
585 contents.append(buffer, read_size);
586 }
587 EXPECT_EQ(data, contents);
588 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->status());
589 }
590
591 TEST_F(MojoAsyncResourceHandlerTest,
592 IOBufferFromOnWillReadShouldRemainValidEvenIfHandlerIsGone) { 557 IOBufferFromOnWillReadShouldRemainValidEvenIfHandlerIsGone) {
593 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 558 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
594 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 559 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
595 // The io_buffer size that the mime sniffer requires implicitly. 560 // The io_buffer size that the mime sniffer requires implicitly.
596 ASSERT_GE(mock_loader_->io_buffer_size(), 561 ASSERT_GE(mock_loader_->io_buffer_size(),
597 kSizeMimeSnifferRequiresForFirstOnWillRead); 562 kSizeMimeSnifferRequiresForFirstOnWillRead);
598 563
599 handler_ = nullptr; 564 handler_ = nullptr;
600 url_loader_client_.Unbind(); 565 url_loader_client_.Unbind();
601 base::RunLoop().RunUntilIdle(); 566 base::RunLoop().RunUntilIdle();
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 mock_loader_->OnResponseCompleted( 841 mock_loader_->OnResponseCompleted(
877 net::URLRequestStatus::FromError(net::OK))); 842 net::URLRequestStatus::FromError(net::OK)));
878 843
879 url_loader_client_.RunUntilComplete(); 844 url_loader_client_.RunUntilComplete();
880 EXPECT_TRUE(url_loader_client_.has_received_completion()); 845 EXPECT_TRUE(url_loader_client_.has_received_completion());
881 846
882 handler_.reset(); 847 handler_.reset();
883 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 848 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
884 } 849 }
885 850
886 TEST_F(MojoAsyncResourceHandlerTest,
887 EndWriteFailsOnWillReadWithInsufficientInitialCapacity) {
888 MojoAsyncResourceHandler::SetAllocationSizeForTesting(2);
889 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
890 handler_->set_end_write_expectation(MOJO_RESULT_UNKNOWN);
891 ASSERT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->OnWillRead());
892 }
893
894 TEST_F(MojoAsyncResourceHandlerTest, EndWriteFailsOnReadCompleted) { 851 TEST_F(MojoAsyncResourceHandlerTest, EndWriteFailsOnReadCompleted) {
895 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 852 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
896 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); 853 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
897 854
898 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT); 855 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
899 ASSERT_EQ(MockResourceLoader::Status::CANCELED, 856 ASSERT_EQ(MockResourceLoader::Status::CANCELED,
900 mock_loader_->OnReadCompleted( 857 mock_loader_->OnReadCompleted(
901 std::string(mock_loader_->io_buffer_size(), 'w'))); 858 std::string(mock_loader_->io_buffer_size(), 'w')));
902 } 859 }
903 860
904 TEST_F(MojoAsyncResourceHandlerTest,
905 EndWriteFailsOnReadCompletedWithInsufficientInitialCapacity) {
906 MojoAsyncResourceHandler::SetAllocationSizeForTesting(2);
907 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
908 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
909
910 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
911 ASSERT_EQ(MockResourceLoader::Status::CANCELED,
912 mock_loader_->OnReadCompleted(
913 std::string(mock_loader_->io_buffer_size(), 'w')));
914 }
915
916 TEST_F(MojoAsyncResourceHandlerTest,
917 EndWriteFailsOnResumeWithInsufficientInitialCapacity) {
918 MojoAsyncResourceHandler::SetAllocationSizeForTesting(8);
919 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
920 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
921
922 while (true) {
923 MockResourceLoader::Status result = mock_loader_->OnReadCompleted(
924 std::string(mock_loader_->io_buffer_size(), 'A'));
925 if (result == MockResourceLoader::Status::CALLBACK_PENDING)
926 break;
927 ASSERT_EQ(MockResourceLoader::Status::IDLE, result);
928
929 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead());
930 }
931
932 url_loader_client_.RunUntilResponseBodyArrived();
933 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
934
935 while (true) {
936 char buf[16];
937 uint32_t read_size = sizeof(buf);
938 MojoResult result =
939 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size,
940 MOJO_READ_DATA_FLAG_NONE);
941 if (result == MOJO_RESULT_SHOULD_WAIT)
942 break;
943 ASSERT_EQ(MOJO_RESULT_OK, result);
944 }
945
946 handler_->set_end_write_expectation(MOJO_RESULT_SHOULD_WAIT);
947 mock_loader_->WaitUntilIdleOrCanceled();
948 EXPECT_FALSE(url_loader_client_.has_received_completion());
949 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader_->status());
950 EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES, mock_loader_->error_code());
951 }
952
953 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) { 861 TEST_F(MojoAsyncResourceHandlerUploadTest, UploadProgressHandling) {
954 ASSERT_TRUE(CallOnWillStart()); 862 ASSERT_TRUE(CallOnWillStart());
955 863
956 // Expect no report for no progress. 864 // Expect no report for no progress.
957 set_upload_progress(net::UploadProgress(0, 1000)); 865 set_upload_progress(net::UploadProgress(0, 1000));
958 handler_->PollUploadProgress(); 866 handler_->PollUploadProgress();
959 EXPECT_FALSE(url_loader_client_.has_received_upload_progress()); 867 EXPECT_FALSE(url_loader_client_.has_received_upload_progress());
960 EXPECT_EQ(0, url_loader_client_.current_upload_position()); 868 EXPECT_EQ(0, url_loader_client_.current_upload_position());
961 EXPECT_EQ(0, url_loader_client_.total_upload_size()); 869 EXPECT_EQ(0, url_loader_client_.total_upload_size());
962 870
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
1354 } 1262 }
1355 } 1263 }
1356 EXPECT_EQ("B", body); 1264 EXPECT_EQ("B", body);
1357 } 1265 }
1358 1266
1359 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1267 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1360 MojoAsyncResourceHandlerWithAllocationSizeTest, 1268 MojoAsyncResourceHandlerWithAllocationSizeTest,
1361 ::testing::Values(8, 32 * 2014)); 1269 ::testing::Values(8, 32 * 2014));
1362 } // namespace 1270 } // namespace
1363 } // namespace content 1271 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.cc ('k') | content/browser/loader/redirect_to_file_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698