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 #include <vector> | 10 #include <vector> |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 base::TimeTicks current_time_; | 92 base::TimeTicks current_time_; |
93 | 93 |
94 private: | 94 private: |
95 DISALLOW_COPY_AND_ASSIGN(FakeUploadProgressTracker); | 95 DISALLOW_COPY_AND_ASSIGN(FakeUploadProgressTracker); |
96 }; | 96 }; |
97 | 97 |
98 class TestResourceDispatcherHostDelegate final | 98 class TestResourceDispatcherHostDelegate final |
99 : public ResourceDispatcherHostDelegate { | 99 : public ResourceDispatcherHostDelegate { |
100 public: | 100 public: |
101 TestResourceDispatcherHostDelegate() = default; | 101 TestResourceDispatcherHostDelegate() = default; |
102 ~TestResourceDispatcherHostDelegate() override { | 102 ~TestResourceDispatcherHostDelegate() override = default; |
103 EXPECT_EQ(num_on_response_started_calls_expectation_, | |
104 num_on_response_started_calls_); | |
105 } | |
106 | 103 |
107 bool ShouldBeginRequest(const std::string& method, | 104 bool ShouldBeginRequest(const std::string& method, |
108 const GURL& url, | 105 const GURL& url, |
109 ResourceType resource_type, | 106 ResourceType resource_type, |
110 ResourceContext* resource_context) override { | 107 ResourceContext* resource_context) override { |
111 ADD_FAILURE() << "ShouldBeginRequest should not be called."; | 108 ADD_FAILURE() << "ShouldBeginRequest should not be called."; |
112 return false; | 109 return false; |
113 } | 110 } |
114 | 111 |
115 void RequestBeginning( | 112 void RequestBeginning( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 } | 158 } |
162 | 159 |
163 void OnStreamCreated(net::URLRequest* request, | 160 void OnStreamCreated(net::URLRequest* request, |
164 std::unique_ptr<content::StreamInfo> stream) override { | 161 std::unique_ptr<content::StreamInfo> stream) override { |
165 ADD_FAILURE() << "OnStreamCreated should not be called."; | 162 ADD_FAILURE() << "OnStreamCreated should not be called."; |
166 } | 163 } |
167 | 164 |
168 void OnResponseStarted(net::URLRequest* request, | 165 void OnResponseStarted(net::URLRequest* request, |
169 ResourceContext* resource_context, | 166 ResourceContext* resource_context, |
170 ResourceResponse* response) override { | 167 ResourceResponse* response) override { |
171 ++num_on_response_started_calls_; | |
172 } | 168 } |
173 | 169 |
174 void OnRequestRedirected(const GURL& redirect_url, | 170 void OnRequestRedirected(const GURL& redirect_url, |
175 net::URLRequest* request, | 171 net::URLRequest* request, |
176 ResourceContext* resource_context, | 172 ResourceContext* resource_context, |
177 ResourceResponse* response) override { | 173 ResourceResponse* response) override { |
178 ADD_FAILURE() << "OnRequestRedirected should not be called."; | 174 ADD_FAILURE() << "OnRequestRedirected should not be called."; |
179 } | 175 } |
180 | 176 |
181 void RequestComplete(net::URLRequest* url_request) override { | 177 void RequestComplete(net::URLRequest* url_request) override { |
(...skipping 11 matching lines...) Expand all Loading... |
193 ADD_FAILURE() << "GetNavigationData should not be called."; | 189 ADD_FAILURE() << "GetNavigationData should not be called."; |
194 return nullptr; | 190 return nullptr; |
195 } | 191 } |
196 | 192 |
197 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( | 193 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( |
198 ResourceContext* resource_context) override { | 194 ResourceContext* resource_context) override { |
199 ADD_FAILURE() << "CreateClientCertStore should not be called."; | 195 ADD_FAILURE() << "CreateClientCertStore should not be called."; |
200 return nullptr; | 196 return nullptr; |
201 } | 197 } |
202 | 198 |
203 int num_on_response_started_calls() const { | |
204 return num_on_response_started_calls_; | |
205 } | |
206 void set_num_on_response_started_calls_expectation(int expectation) { | |
207 num_on_response_started_calls_expectation_ = expectation; | |
208 } | |
209 | |
210 private: | 199 private: |
211 int num_on_response_started_calls_ = 0; | |
212 int num_on_response_started_calls_expectation_ = 0; | |
213 | |
214 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); | 200 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); |
215 }; | 201 }; |
216 | 202 |
217 class MojoAsyncResourceHandlerWithStubOperations | 203 class MojoAsyncResourceHandlerWithStubOperations |
218 : public MojoAsyncResourceHandler { | 204 : public MojoAsyncResourceHandler { |
219 public: | 205 public: |
220 MojoAsyncResourceHandlerWithStubOperations( | 206 MojoAsyncResourceHandlerWithStubOperations( |
221 net::URLRequest* request, | 207 net::URLRequest* request, |
222 ResourceDispatcherHostImpl* rdh, | 208 ResourceDispatcherHostImpl* rdh, |
223 mojom::URLLoaderAssociatedRequest mojo_request, | 209 mojom::URLLoaderAssociatedRequest mojo_request, |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 // Returns false if something bad happens. | 377 // Returns false if something bad happens. |
392 bool CallOnWillStart() { | 378 bool CallOnWillStart() { |
393 MockResourceLoader::Status result = | 379 MockResourceLoader::Status result = |
394 mock_loader_->OnWillStart(request_->url()); | 380 mock_loader_->OnWillStart(request_->url()); |
395 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); | 381 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); |
396 return result == MockResourceLoader::Status::IDLE; | 382 return result == MockResourceLoader::Status::IDLE; |
397 } | 383 } |
398 | 384 |
399 // Returns false if something bad happens. | 385 // Returns false if something bad happens. |
400 bool CallOnResponseStarted() { | 386 bool CallOnResponseStarted() { |
401 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | |
402 MockResourceLoader::Status result = mock_loader_->OnResponseStarted( | 387 MockResourceLoader::Status result = mock_loader_->OnResponseStarted( |
403 make_scoped_refptr(new ResourceResponse())); | 388 make_scoped_refptr(new ResourceResponse())); |
404 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); | 389 EXPECT_EQ(MockResourceLoader::Status::IDLE, result); |
405 if (result != MockResourceLoader::Status::IDLE) | 390 if (result != MockResourceLoader::Status::IDLE) |
406 return false; | 391 return false; |
407 | 392 |
408 if (url_loader_client_.has_received_response()) { | 393 if (url_loader_client_.has_received_response()) { |
409 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; | 394 ADD_FAILURE() << "URLLoaderClient unexpectedly gets a response."; |
410 return false; | 395 return false; |
411 } | 396 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 handler_ = nullptr; | 461 handler_ = nullptr; |
477 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); | 462 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); |
478 } | 463 } |
479 | 464 |
480 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { | 465 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { |
481 EXPECT_EQ(MockResourceLoader::Status::IDLE, | 466 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
482 mock_loader_->OnWillStart(request_->url())); | 467 mock_loader_->OnWillStart(request_->url())); |
483 } | 468 } |
484 | 469 |
485 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { | 470 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { |
486 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | |
487 scoped_refptr<net::IOBufferWithSize> metadata = new net::IOBufferWithSize(5); | 471 scoped_refptr<net::IOBufferWithSize> metadata = new net::IOBufferWithSize(5); |
488 memcpy(metadata->data(), "hello", 5); | 472 memcpy(metadata->data(), "hello", 5); |
489 handler_->SetMetadata(metadata); | 473 handler_->SetMetadata(metadata); |
490 | 474 |
491 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 475 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
492 mock_loader_->OnWillStart(request_->url())); | 476 mock_loader_->OnWillStart(request_->url())); |
493 | 477 |
494 scoped_refptr<ResourceResponse> response = new ResourceResponse(); | 478 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
495 response->head.content_length = 99; | 479 response->head.content_length = 99; |
496 response->head.request_start = | 480 response->head.request_start = |
497 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); | 481 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); |
498 response->head.response_start = | 482 response->head.response_start = |
499 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); | 483 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); |
500 | 484 |
501 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls()); | |
502 base::TimeTicks now1 = base::TimeTicks::Now(); | 485 base::TimeTicks now1 = base::TimeTicks::Now(); |
503 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 486 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
504 mock_loader_->OnResponseStarted(response)); | 487 mock_loader_->OnResponseStarted(response)); |
505 base::TimeTicks now2 = base::TimeTicks::Now(); | 488 base::TimeTicks now2 = base::TimeTicks::Now(); |
506 | 489 |
507 EXPECT_EQ(request_->creation_time(), response->head.request_start); | 490 EXPECT_EQ(request_->creation_time(), response->head.request_start); |
508 EXPECT_LE(now1, response->head.response_start); | 491 EXPECT_LE(now1, response->head.response_start); |
509 EXPECT_LE(response->head.response_start, now2); | 492 EXPECT_LE(response->head.response_start, now2); |
510 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls()); | |
511 | 493 |
512 url_loader_client_.RunUntilResponseReceived(); | 494 url_loader_client_.RunUntilResponseReceived(); |
513 EXPECT_EQ(response->head.request_start, | 495 EXPECT_EQ(response->head.request_start, |
514 url_loader_client_.response_head().request_start); | 496 url_loader_client_.response_head().request_start); |
515 EXPECT_EQ(response->head.response_start, | 497 EXPECT_EQ(response->head.response_start, |
516 url_loader_client_.response_head().response_start); | 498 url_loader_client_.response_head().response_start); |
517 EXPECT_EQ(99, url_loader_client_.response_head().content_length); | 499 EXPECT_EQ(99, url_loader_client_.response_head().content_length); |
518 | 500 |
519 url_loader_client_.RunUntilCachedMetadataReceived(); | 501 url_loader_client_.RunUntilCachedMetadataReceived(); |
520 EXPECT_EQ("hello", url_loader_client_.cached_metadata()); | 502 EXPECT_EQ("hello", url_loader_client_.cached_metadata()); |
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 if (result == MOJO_RESULT_FAILED_PRECONDITION) | 1079 if (result == MOJO_RESULT_FAILED_PRECONDITION) |
1098 break; | 1080 break; |
1099 base::RunLoop().RunUntilIdle(); | 1081 base::RunLoop().RunUntilIdle(); |
1100 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); | 1082 DCHECK(result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_OK); |
1101 } | 1083 } |
1102 | 1084 |
1103 base::RunLoop().RunUntilIdle(); | 1085 base::RunLoop().RunUntilIdle(); |
1104 } | 1086 } |
1105 | 1087 |
1106 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, RedirectHandling) { | 1088 TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, RedirectHandling) { |
1107 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | |
1108 | |
1109 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 1089 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
1110 mock_loader_->OnWillStart(request_->url())); | 1090 mock_loader_->OnWillStart(request_->url())); |
1111 | 1091 |
1112 net::RedirectInfo redirect_info; | 1092 net::RedirectInfo redirect_info; |
1113 redirect_info.status_code = 301; | 1093 redirect_info.status_code = 301; |
1114 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, | 1094 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
1115 mock_loader_->OnRequestRedirected( | 1095 mock_loader_->OnRequestRedirected( |
1116 redirect_info, make_scoped_refptr(new ResourceResponse()))); | 1096 redirect_info, make_scoped_refptr(new ResourceResponse()))); |
1117 | 1097 |
1118 ASSERT_FALSE(url_loader_client_.has_received_response()); | 1098 ASSERT_FALSE(url_loader_client_.has_received_response()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1171 MalformedFollowRedirectRequest) { | 1151 MalformedFollowRedirectRequest) { |
1172 handler_->FollowRedirect(); | 1152 handler_->FollowRedirect(); |
1173 | 1153 |
1174 EXPECT_TRUE(handler_->has_received_bad_message()); | 1154 EXPECT_TRUE(handler_->has_received_bad_message()); |
1175 } | 1155 } |
1176 | 1156 |
1177 // Typically ResourceHandler methods are called in this order. | 1157 // Typically ResourceHandler methods are called in this order. |
1178 TEST_P( | 1158 TEST_P( |
1179 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1159 MojoAsyncResourceHandlerWithAllocationSizeTest, |
1180 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo
nseCompleted) { | 1160 OnWillStartThenOnResponseStartedThenOnWillReadThenOnReadCompletedThenOnRespo
nseCompleted) { |
1181 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | |
1182 | 1161 |
1183 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 1162 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
1184 mock_loader_->OnWillStart(request_->url())); | 1163 mock_loader_->OnWillStart(request_->url())); |
1185 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 1164 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
1186 mock_loader_->OnResponseStarted( | 1165 mock_loader_->OnResponseStarted( |
1187 make_scoped_refptr(new ResourceResponse()))); | 1166 make_scoped_refptr(new ResourceResponse()))); |
1188 | 1167 |
1189 ASSERT_FALSE(url_loader_client_.has_received_response()); | 1168 ASSERT_FALSE(url_loader_client_.has_received_response()); |
1190 url_loader_client_.RunUntilResponseReceived(); | 1169 url_loader_client_.RunUntilResponseReceived(); |
1191 | 1170 |
(...skipping 30 matching lines...) Expand all Loading... |
1222 body.append(buffer, read_size); | 1201 body.append(buffer, read_size); |
1223 } | 1202 } |
1224 } | 1203 } |
1225 EXPECT_EQ("A", body); | 1204 EXPECT_EQ("A", body); |
1226 } | 1205 } |
1227 | 1206 |
1228 // MimeResourceHandler calls delegated ResourceHandler's methods in this order. | 1207 // MimeResourceHandler calls delegated ResourceHandler's methods in this order. |
1229 TEST_P( | 1208 TEST_P( |
1230 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1209 MojoAsyncResourceHandlerWithAllocationSizeTest, |
1231 OnWillStartThenOnWillReadThenOnResponseStartedThenOnReadCompletedThenOnRespo
nseCompleted) { | 1210 OnWillStartThenOnWillReadThenOnResponseStartedThenOnReadCompletedThenOnRespo
nseCompleted) { |
1232 rdh_delegate_.set_num_on_response_started_calls_expectation(1); | |
1233 | 1211 |
1234 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 1212 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
1235 mock_loader_->OnWillStart(request_->url())); | 1213 mock_loader_->OnWillStart(request_->url())); |
1236 | 1214 |
1237 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); | 1215 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead()); |
1238 | 1216 |
1239 ASSERT_EQ(MockResourceLoader::Status::IDLE, | 1217 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
1240 mock_loader_->OnResponseStarted( | 1218 mock_loader_->OnResponseStarted( |
1241 make_scoped_refptr(new ResourceResponse()))); | 1219 make_scoped_refptr(new ResourceResponse()))); |
1242 | 1220 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1275 } | 1253 } |
1276 } | 1254 } |
1277 EXPECT_EQ("B", body); | 1255 EXPECT_EQ("B", body); |
1278 } | 1256 } |
1279 | 1257 |
1280 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, | 1258 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, |
1281 MojoAsyncResourceHandlerWithAllocationSizeTest, | 1259 MojoAsyncResourceHandlerWithAllocationSizeTest, |
1282 ::testing::Values(8, 32 * 2014)); | 1260 ::testing::Values(8, 32 * 2014)); |
1283 } // namespace | 1261 } // namespace |
1284 } // namespace content | 1262 } // namespace content |
OLD | NEW |