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

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

Issue 2467833002: Implement redirect handling on MojoAsyncResourceHandler (Closed)
Patch Set: rebase Created 4 years, 1 month 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 10
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } 239 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; }
240 240
241 void set_begin_write_expectation(MojoResult begin_write_expectation) { 241 void set_begin_write_expectation(MojoResult begin_write_expectation) {
242 is_begin_write_expectation_set_ = true; 242 is_begin_write_expectation_set_ = true;
243 begin_write_expectation_ = begin_write_expectation; 243 begin_write_expectation_ = begin_write_expectation;
244 } 244 }
245 void set_end_write_expectation(MojoResult end_write_expectation) { 245 void set_end_write_expectation(MojoResult end_write_expectation) {
246 is_end_write_expectation_set_ = true; 246 is_end_write_expectation_set_ = true;
247 end_write_expectation_ = end_write_expectation; 247 end_write_expectation_ = end_write_expectation;
248 } 248 }
249 bool has_received_bad_message() const { return has_received_bad_message_; }
249 250
250 private: 251 private:
251 MojoResult BeginWrite(void** data, uint32_t* available) override { 252 MojoResult BeginWrite(void** data, uint32_t* available) override {
252 if (is_begin_write_expectation_set_) 253 if (is_begin_write_expectation_set_)
253 return begin_write_expectation_; 254 return begin_write_expectation_;
254 return MojoAsyncResourceHandler::BeginWrite(data, available); 255 return MojoAsyncResourceHandler::BeginWrite(data, available);
255 } 256 }
256 MojoResult EndWrite(uint32_t written) override { 257 MojoResult EndWrite(uint32_t written) override {
257 if (is_end_write_expectation_set_) 258 if (is_end_write_expectation_set_)
258 return end_write_expectation_; 259 return end_write_expectation_;
259 return MojoAsyncResourceHandler::EndWrite(written); 260 return MojoAsyncResourceHandler::EndWrite(written);
260 } 261 }
262 void ReportBadMessage(const std::string& error) override {
263 has_received_bad_message_ = true;
264 }
261 265
262 bool is_begin_write_expectation_set_ = false; 266 bool is_begin_write_expectation_set_ = false;
263 bool is_end_write_expectation_set_ = false; 267 bool is_end_write_expectation_set_ = false;
268 bool has_received_bad_message_ = false;
264 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 269 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
265 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 270 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
266 271
267 DISALLOW_COPY_AND_ASSIGN( 272 DISALLOW_COPY_AND_ASSIGN(
268 MojoAsyncResourceHandlerWithCustomDataPipeOperations); 273 MojoAsyncResourceHandlerWithCustomDataPipeOperations);
269 }; 274 };
270 275
271 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { 276 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
272 public: 277 public:
273 TestURLLoaderFactory() {} 278 TestURLLoaderFactory() {}
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 written += io_buffer_size; 837 written += io_buffer_size;
833 bool defer = false; 838 bool defer = false;
834 ASSERT_TRUE(handler_->OnReadCompleted(io_buffer_size, &defer)); 839 ASSERT_TRUE(handler_->OnReadCompleted(io_buffer_size, &defer));
835 if (defer) 840 if (defer)
836 break; 841 break;
837 } 842 }
838 843
839 url_loader_client_.RunUntilResponseBodyArrived(); 844 url_loader_client_.RunUntilResponseBodyArrived();
840 ASSERT_TRUE(url_loader_client_.response_body().is_valid()); 845 ASSERT_TRUE(url_loader_client_.response_body().is_valid());
841 handler_->ResetBeginWriteExpectation(); 846 handler_->ResetBeginWriteExpectation();
842 handler_->ResumeForTesting(); 847 handler_->OnWritableForTesting();
843 848
844 std::string actual; 849 std::string actual;
845 while (actual.size() < written) { 850 while (actual.size() < written) {
846 char buf[16]; 851 char buf[16];
847 uint32_t read_size = sizeof(buf); 852 uint32_t read_size = sizeof(buf);
848 MojoResult result = 853 MojoResult result =
849 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size, 854 mojo::ReadDataRaw(url_loader_client_.response_body(), buf, &read_size,
850 MOJO_READ_DATA_FLAG_NONE); 855 MOJO_READ_DATA_FLAG_NONE);
851 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT); 856 ASSERT_TRUE(result == MOJO_RESULT_OK || result == MOJO_RESULT_SHOULD_WAIT);
852 if (result == MOJO_RESULT_OK) 857 if (result == MOJO_RESULT_OK)
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 if (result == MOJO_RESULT_FAILED_PRECONDITION) 1076 if (result == MOJO_RESULT_FAILED_PRECONDITION)
1072 break; 1077 break;
1073 base::RunLoop().RunUntilIdle(); 1078 base::RunLoop().RunUntilIdle();
1074 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); 1079 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK);
1075 } 1080 }
1076 1081
1077 base::RunLoop().RunUntilIdle(); 1082 base::RunLoop().RunUntilIdle();
1078 EXPECT_EQ(0, resource_controller_.num_resume_calls()); 1083 EXPECT_EQ(0, resource_controller_.num_resume_calls());
1079 } 1084 }
1080 1085
1086 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, RedirectHandling) {
1087 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
1088 bool defer = false;
1089
1090 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer));
1091 ASSERT_FALSE(defer);
1092 scoped_refptr<ResourceResponse> response = new ResourceResponse();
1093 net::RedirectInfo redirect_info;
1094 redirect_info.status_code = 301;
1095 ASSERT_TRUE(
1096 handler_->OnRequestRedirected(redirect_info, response.get(), &defer));
1097 ASSERT_TRUE(defer);
1098
1099 ASSERT_FALSE(url_loader_client_.has_received_response());
1100 ASSERT_FALSE(url_loader_client_.has_received_redirect());
1101 url_loader_client_.RunUntilRedirectReceived();
1102
1103 ASSERT_FALSE(url_loader_client_.has_received_response());
1104 ASSERT_TRUE(url_loader_client_.has_received_redirect());
1105 EXPECT_EQ(301, url_loader_client_.redirect_info().status_code);
1106
1107 EXPECT_EQ(0, resource_controller_.num_resume_calls());
1108 handler_->FollowRedirect();
1109 EXPECT_EQ(1, resource_controller_.num_resume_calls());
1110
1111 url_loader_client_.ClearHasReceivedRedirect();
1112 // Redirect once more.
1113 defer = false;
1114 redirect_info.status_code = 302;
1115 ASSERT_TRUE(
1116 handler_->OnRequestRedirected(redirect_info, response.get(), &defer));
1117 ASSERT_TRUE(defer);
1118
1119 ASSERT_FALSE(url_loader_client_.has_received_response());
1120 ASSERT_FALSE(url_loader_client_.has_received_redirect());
1121 url_loader_client_.RunUntilRedirectReceived();
1122
1123 ASSERT_FALSE(url_loader_client_.has_received_response());
1124 ASSERT_TRUE(url_loader_client_.has_received_redirect());
1125 EXPECT_EQ(302, url_loader_client_.redirect_info().status_code);
1126
1127 EXPECT_EQ(1, resource_controller_.num_resume_calls());
1128 handler_->FollowRedirect();
1129 EXPECT_EQ(2, resource_controller_.num_resume_calls());
1130
1131 // Give the final response.
1132 defer = false;
1133 ASSERT_TRUE(handler_->OnResponseStarted(response.get(), &defer));
1134 ASSERT_FALSE(defer);
1135
1136 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, net::OK);
1137 handler_->OnResponseCompleted(status, &defer);
1138 ASSERT_FALSE(defer);
1139
1140 ASSERT_FALSE(url_loader_client_.has_received_completion());
1141 url_loader_client_.RunUntilComplete();
1142
1143 ASSERT_TRUE(url_loader_client_.has_received_response());
1144 ASSERT_TRUE(url_loader_client_.has_received_completion());
1145 EXPECT_EQ(net::OK, url_loader_client_.completion_status().error_code);
1146 }
1147
1148 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1149 MalformedFollowRedirectRequest) {
1150 handler_->FollowRedirect();
1151
1152 EXPECT_TRUE(handler_->has_received_bad_message());
1153 EXPECT_EQ(0, resource_controller_.num_resume_calls());
1154 }
1155
1081 // Typically ResourceHandler methods are called in this order. 1156 // Typically ResourceHandler methods are called in this order.
1082 TEST_P( 1157 TEST_P(
1083 MojoAsyncResourceHandlerWithAllocationSizeTest, 1158 MojoAsyncResourceHandlerWithAllocationSizeTest,
1084 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo nseCompleted) { 1159 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo nseCompleted) {
1085 rdh_delegate_.set_num_on_response_started_calls_expectation(1); 1160 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
1086 bool defer = false; 1161 bool defer = false;
1087 1162
1088 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer)); 1163 ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer));
1089 ASSERT_FALSE(defer); 1164 ASSERT_FALSE(defer);
1090 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 1165 scoped_refptr<ResourceResponse> response = new ResourceResponse();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1195 } 1270 }
1196 } 1271 }
1197 EXPECT_EQ("B", body); 1272 EXPECT_EQ("B", body);
1198 } 1273 }
1199 1274
1200 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1275 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1201 MojoAsyncResourceHandlerWithAllocationSizeTest, 1276 MojoAsyncResourceHandlerWithAllocationSizeTest,
1202 ::testing::Values(8, 32 * 2014)); 1277 ::testing::Values(8, 32 * 2014));
1203 } // namespace 1278 } // namespace
1204 } // namespace content 1279 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.cc ('k') | content/browser/loader/test_url_loader_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698