| OLD | NEW |
| 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 | 10 |
| (...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 written += io_buffer_size; | 832 written += io_buffer_size; |
| 833 bool defer = false; | 833 bool defer = false; |
| 834 ASSERT_TRUE(handler_->OnReadCompleted(io_buffer_size, &defer)); | 834 ASSERT_TRUE(handler_->OnReadCompleted(io_buffer_size, &defer)); |
| 835 if (defer) | 835 if (defer) |
| 836 break; | 836 break; |
| 837 } | 837 } |
| 838 | 838 |
| 839 url_loader_client_.RunUntilResponseBodyArrived(); | 839 url_loader_client_.RunUntilResponseBodyArrived(); |
| 840 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); | 840 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); |
| 841 handler_->ResetBeginWriteExpectation(); | 841 handler_->ResetBeginWriteExpectation(); |
| 842 handler_->ResumeForTesting(); | 842 handler_->OnWritableForTesting(); |
| 843 | 843 |
| 844 std::string actual; | 844 std::string actual; |
| 845 while (actual.size() < written) { | 845 while (actual.size() < written) { |
| 846 char buf[16]; | 846 char buf[16]; |
| 847 uint32_t read_size = sizeof(buf); | 847 uint32_t read_size = sizeof(buf); |
| 848 MojoResult result = | 848 MojoResult result = |
| 849 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size, | 849 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size, |
| 850 MOJO_READ_DATA_FLAG_NONE); | 850 MOJO_READ_DATA_FLAG_NONE); |
| 851 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT); | 851 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT); |
| 852 if (result == MOJO_RESULT_OK) | 852 if (result == MOJO_RESULT_OK) |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 if (result == MOJO_RESULT_FAILED_PRECONDITION) | 1071 if (result == MOJO_RESULT_FAILED_PRECONDITION) |
| 1072 break; | 1072 break; |
| 1073 base::RunLoop().RunUntilIdle(); | 1073 base::RunLoop().RunUntilIdle(); |
| 1074 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); | 1074 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); |
| 1075 } | 1075 } |
| 1076 | 1076 |
| 1077 base::RunLoop().RunUntilIdle(); | 1077 base::RunLoop().RunUntilIdle(); |
| 1078 EXPECT_EQ(0, resource_controller_.num_resume_calls()); | 1078 EXPECT_EQ(0, resource_controller_.num_resume_calls()); |
| 1079 } | 1079 } |
| 1080 | 1080 |
| 1081 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, RedirectHandling) { |
| 1082 rdh_delegate_.set_num_on_response_started_calls_expectation(1); |
| 1083 bool defer = false; |
| 1084 |
| 1085 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer)); |
| 1086 ASSERT_FALSE(defer); |
| 1087 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
| 1088 net::RedirectInfo redirect_info; |
| 1089 redirect_info.status_code = 301; |
| 1090 ASSERT_TRUE( |
| 1091 handler_->OnRequestRedirected(redirect_info, response.get(), &defer)); |
| 1092 ASSERT_TRUE(defer); |
| 1093 |
| 1094 ASSERT_FALSE(url_loader_client_.has_received_response()); |
| 1095 ASSERT_FALSE(url_loader_client_.has_received_redirect()); |
| 1096 url_loader_client_.RunUntilRedirectReceived(); |
| 1097 |
| 1098 ASSERT_FALSE(url_loader_client_.has_received_response()); |
| 1099 ASSERT_TRUE(url_loader_client_.has_received_redirect()); |
| 1100 EXPECT_EQ(301, url_loader_client_.redirect_info().status_code); |
| 1101 |
| 1102 EXPECT_EQ(0, resource_controller_.num_resume_calls()); |
| 1103 handler_->FollowRedirect(); |
| 1104 EXPECT_EQ(1, resource_controller_.num_resume_calls()); |
| 1105 |
| 1106 url_loader_client_.ClearHasReceivedRedirect(); |
| 1107 // Redirect once more. |
| 1108 defer = false; |
| 1109 redirect_info.status_code = 302; |
| 1110 ASSERT_TRUE( |
| 1111 handler_->OnRequestRedirected(redirect_info, response.get(), &defer)); |
| 1112 ASSERT_TRUE(defer); |
| 1113 |
| 1114 ASSERT_FALSE(url_loader_client_.has_received_response()); |
| 1115 ASSERT_FALSE(url_loader_client_.has_received_redirect()); |
| 1116 url_loader_client_.RunUntilRedirectReceived(); |
| 1117 |
| 1118 ASSERT_FALSE(url_loader_client_.has_received_response()); |
| 1119 ASSERT_TRUE(url_loader_client_.has_received_redirect()); |
| 1120 EXPECT_EQ(302, url_loader_client_.redirect_info().status_code); |
| 1121 |
| 1122 EXPECT_EQ(1, resource_controller_.num_resume_calls()); |
| 1123 handler_->FollowRedirect(); |
| 1124 EXPECT_EQ(2, resource_controller_.num_resume_calls()); |
| 1125 |
| 1126 // Give the final response. |
| 1127 defer = false; |
| 1128 ASSERT_TRUE(handler_->OnResponseStarted(response.get(), &defer)); |
| 1129 ASSERT_FALSE(defer); |
| 1130 |
| 1131 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, net::OK); |
| 1132 handler_->OnResponseCompleted(status, &defer); |
| 1133 ASSERT_FALSE(defer); |
| 1134 |
| 1135 ASSERT_FALSE(url_loader_client_.has_received_completion()); |
| 1136 url_loader_client_.RunUntilComplete(); |
| 1137 |
| 1138 ASSERT_TRUE(url_loader_client_.has_received_response()); |
| 1139 ASSERT_TRUE(url_loader_client_.has_received_completion()); |
| 1140 EXPECT_EQ(net::OK, url_loader_client_.completion_status().error_code); |
| 1141 } |
| 1142 |
| 1143 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1144 MalformedFollowRedirectRequest) { |
| 1145 handler_->FollowRedirect(); |
| 1146 |
| 1147 EXPECT_EQ(0, resource_controller_.num_resume_calls()); |
| 1148 EXPECT_TRUE(resource_controller_.is_cancel_with_error_called()); |
| 1149 EXPECT_EQ(net::ERR_ABORTED, resource_controller_.error()); |
| 1150 } |
| 1151 |
| 1081 // Typically ResourceHandler methods are called in this order. | 1152 // Typically ResourceHandler methods are called in this order. |
| 1082 TEST_P( | 1153 TEST_P( |
| 1083 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1154 MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1084 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo
nseCompleted) { | 1155 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo
nseCompleted) { |
| 1085 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | 1156 rdh_delegate_.set_num_on_response_started_calls_expectation(1); |
| 1086 bool defer = false; | 1157 bool defer = false; |
| 1087 | 1158 |
| 1088 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer)); | 1159 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer)); |
| 1089 ASSERT_FALSE(defer); | 1160 ASSERT_FALSE(defer); |
| 1090 scoped_refptr<ResourceResponse> response = new ResourceResponse(); | 1161 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1195 } | 1266 } |
| 1196 } | 1267 } |
| 1197 EXPECT_EQ("B", body); | 1268 EXPECT_EQ("B", body); |
| 1198 } | 1269 } |
| 1199 | 1270 |
| 1200 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, | 1271 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1201 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1272 MojoAsyncResourceHandlerWithAllocationSizeTest, |
| 1202 ::testing::Values(8, 32 * 2014)); | 1273 ::testing::Values(8, 32 * 2014)); |
| 1203 } // namespace | 1274 } // namespace |
| 1204 } // namespace content | 1275 } // namespace content |
| OLD | NEW |