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

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

Issue 2954853002: Use Independent URLLoader
Patch Set: . Created 3 years, 6 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/url_loader_factory_impl.h" 5 #include "content/browser/loader/url_loader_factory_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 scoped_refptr<ResourceMessageFilter> resource_message_filter_; 142 scoped_refptr<ResourceMessageFilter> resource_message_filter_;
143 mojom::URLLoaderFactoryPtr factory_; 143 mojom::URLLoaderFactoryPtr factory_;
144 144
145 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest); 145 DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryImplTest);
146 }; 146 };
147 147
148 TEST_P(URLLoaderFactoryImplTest, GetResponse) { 148 TEST_P(URLLoaderFactoryImplTest, GetResponse) {
149 constexpr int32_t kRoutingId = 81; 149 constexpr int32_t kRoutingId = 81;
150 constexpr int32_t kRequestId = 28; 150 constexpr int32_t kRequestId = 28;
151 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 151 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
152 mojom::URLLoaderAssociatedPtr loader; 152 mojom::URLLoaderPtr loader;
153 base::FilePath root; 153 base::FilePath root;
154 PathService::Get(DIR_TEST_DATA, &root); 154 PathService::Get(DIR_TEST_DATA, &root);
155 net::URLRequestMockHTTPJob::AddUrlHandlers(root); 155 net::URLRequestMockHTTPJob::AddUrlHandlers(root);
156 ResourceRequest request; 156 ResourceRequest request;
157 TestURLLoaderClient client; 157 TestURLLoaderClient client;
158 // Assume the file contents is small enough to be stored in the data pipe. 158 // Assume the file contents is small enough to be stored in the data pipe.
159 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); 159 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
160 request.method = "GET"; 160 request.method = "GET";
161 // |resource_type| can't be a frame type. It is because when PlzNavigate is 161 // |resource_type| can't be a frame type. It is because when PlzNavigate is
162 // enabled, the url scheme of frame type requests from the renderer process 162 // enabled, the url scheme of frame type requests from the renderer process
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 EXPECT_EQ(static_cast<int64_t>(expected.size()), 221 EXPECT_EQ(static_cast<int64_t>(expected.size()),
222 client.completion_status().encoded_body_length); 222 client.completion_status().encoded_body_length);
223 // OnTransferSizeUpdated is not dispatched as report_raw_headers is not set. 223 // OnTransferSizeUpdated is not dispatched as report_raw_headers is not set.
224 EXPECT_EQ(0, client.body_transfer_size()); 224 EXPECT_EQ(0, client.body_transfer_size());
225 EXPECT_GT(client.response_head().encoded_data_length, 0); 225 EXPECT_GT(client.response_head().encoded_data_length, 0);
226 EXPECT_GT(client.completion_status().encoded_data_length, 0); 226 EXPECT_GT(client.completion_status().encoded_data_length, 0);
227 } 227 }
228 228
229 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) { 229 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse) {
230 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 230 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
231 mojom::URLLoaderAssociatedPtr loader; 231 mojom::URLLoaderPtr loader;
232 ResourceRequest request; 232 ResourceRequest request;
233 TestURLLoaderClient client; 233 TestURLLoaderClient client;
234 net::URLRequestFailedJob::AddUrlHandler(); 234 net::URLRequestFailedJob::AddUrlHandler();
235 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 235 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
236 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT); 236 net::URLRequestFailedJob::START, net::ERR_TIMED_OUT);
237 request.method = "GET"; 237 request.method = "GET";
238 // |resource_type| can't be a frame type. It is because when PlzNavigate is 238 // |resource_type| can't be a frame type. It is because when PlzNavigate is
239 // enabled, the url scheme of frame type requests from the renderer process 239 // enabled, the url scheme of frame type requests from the renderer process
240 // must be blob scheme. 240 // must be blob scheme.
241 request.resource_type = RESOURCE_TYPE_XHR; 241 request.resource_type = RESOURCE_TYPE_XHR;
242 // Need to set |request_initiator| for non main frame type request. 242 // Need to set |request_initiator| for non main frame type request.
243 request.request_initiator = url::Origin(); 243 request.request_initiator = url::Origin();
244 factory_->CreateLoaderAndStart( 244 factory_->CreateLoaderAndStart(
245 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request, 245 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request,
246 client.CreateInterfacePtr(), 246 client.CreateInterfacePtr(),
247 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); 247 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS));
248 248
249 client.RunUntilComplete(); 249 client.RunUntilComplete();
250 ASSERT_FALSE(client.has_received_response()); 250 ASSERT_FALSE(client.has_received_response());
251 ASSERT_FALSE(client.response_body().is_valid()); 251 ASSERT_FALSE(client.response_body().is_valid());
252 252
253 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); 253 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
254 EXPECT_EQ(0, client.completion_status().encoded_data_length); 254 EXPECT_EQ(0, client.completion_status().encoded_data_length);
255 EXPECT_EQ(0, client.completion_status().encoded_body_length); 255 EXPECT_EQ(0, client.completion_status().encoded_body_length);
256 } 256 }
257 257
258 // In this case, the loading fails after receiving a response. 258 // In this case, the loading fails after receiving a response.
259 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) { 259 TEST_P(URLLoaderFactoryImplTest, GetFailedResponse2) {
260 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 260 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
261 mojom::URLLoaderAssociatedPtr loader; 261 mojom::URLLoaderPtr loader;
262 ResourceRequest request; 262 ResourceRequest request;
263 TestURLLoaderClient client; 263 TestURLLoaderClient client;
264 net::URLRequestFailedJob::AddUrlHandler(); 264 net::URLRequestFailedJob::AddUrlHandler();
265 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 265 request.url = net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
266 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT); 266 net::URLRequestFailedJob::READ_ASYNC, net::ERR_TIMED_OUT);
267 request.method = "GET"; 267 request.method = "GET";
268 // |resource_type| can't be a frame type. It is because when PlzNavigate is 268 // |resource_type| can't be a frame type. It is because when PlzNavigate is
269 // enabled, the url scheme of frame type requests from the renderer process 269 // enabled, the url scheme of frame type requests from the renderer process
270 // must be blob scheme. 270 // must be blob scheme.
271 request.resource_type = RESOURCE_TYPE_XHR; 271 request.resource_type = RESOURCE_TYPE_XHR;
272 // Need to set |request_initiator| for non main frame type request. 272 // Need to set |request_initiator| for non main frame type request.
273 request.request_initiator = url::Origin(); 273 request.request_initiator = url::Origin();
274 factory_->CreateLoaderAndStart( 274 factory_->CreateLoaderAndStart(
275 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request, 275 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request,
276 client.CreateInterfacePtr(), 276 client.CreateInterfacePtr(),
277 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); 277 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS));
278 278
279 client.RunUntilComplete(); 279 client.RunUntilComplete();
280 ASSERT_FALSE(client.has_received_response()); 280 ASSERT_FALSE(client.has_received_response());
281 ASSERT_FALSE(client.response_body().is_valid()); 281 ASSERT_FALSE(client.response_body().is_valid());
282 282
283 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code); 283 EXPECT_EQ(net::ERR_TIMED_OUT, client.completion_status().error_code);
284 EXPECT_GT(client.completion_status().encoded_data_length, 0); 284 EXPECT_GT(client.completion_status().encoded_data_length, 0);
285 EXPECT_EQ(0, client.completion_status().encoded_body_length); 285 EXPECT_EQ(0, client.completion_status().encoded_body_length);
286 } 286 }
287 287
288 // This test tests a case where resource loading is cancelled before started. 288 // This test tests a case where resource loading is cancelled before started.
289 TEST_P(URLLoaderFactoryImplTest, InvalidURL) { 289 TEST_P(URLLoaderFactoryImplTest, InvalidURL) {
290 mojom::URLLoaderAssociatedPtr loader; 290 mojom::URLLoaderPtr loader;
291 ResourceRequest request; 291 ResourceRequest request;
292 TestURLLoaderClient client; 292 TestURLLoaderClient client;
293 request.url = GURL(); 293 request.url = GURL();
294 request.method = "GET"; 294 request.method = "GET";
295 // |resource_type| can't be a frame type. It is because when PlzNavigate is 295 // |resource_type| can't be a frame type. It is because when PlzNavigate is
296 // enabled, the url scheme of frame type requests from the renderer process 296 // enabled, the url scheme of frame type requests from the renderer process
297 // must be blob scheme. 297 // must be blob scheme.
298 request.resource_type = RESOURCE_TYPE_XHR; 298 request.resource_type = RESOURCE_TYPE_XHR;
299 // Need to set |request_initiator| for non main frame type request. 299 // Need to set |request_initiator| for non main frame type request.
300 request.request_initiator = url::Origin(); 300 request.request_initiator = url::Origin();
301 ASSERT_FALSE(request.url.is_valid()); 301 ASSERT_FALSE(request.url.is_valid());
302 factory_->CreateLoaderAndStart( 302 factory_->CreateLoaderAndStart(
303 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request, 303 mojo::MakeRequest(&loader), 2, 1, mojom::kURLLoadOptionNone, request,
304 client.CreateInterfacePtr(), 304 client.CreateInterfacePtr(),
305 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); 305 net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS));
306 306
307 client.RunUntilComplete(); 307 client.RunUntilComplete();
308 ASSERT_FALSE(client.has_received_response()); 308 ASSERT_FALSE(client.has_received_response());
309 ASSERT_FALSE(client.response_body().is_valid()); 309 ASSERT_FALSE(client.response_body().is_valid());
310 310
311 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 311 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
312 } 312 }
313 313
314 // This test tests a case where resource loading is cancelled before started. 314 // This test tests a case where resource loading is cancelled before started.
315 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) { 315 TEST_P(URLLoaderFactoryImplTest, ShouldNotRequestURL) {
316 mojom::URLLoaderAssociatedPtr loader; 316 mojom::URLLoaderPtr loader;
317 RejectingResourceDispatcherHostDelegate rdh_delegate; 317 RejectingResourceDispatcherHostDelegate rdh_delegate;
318 rdh_.SetDelegate(&rdh_delegate); 318 rdh_.SetDelegate(&rdh_delegate);
319 ResourceRequest request; 319 ResourceRequest request;
320 TestURLLoaderClient client; 320 TestURLLoaderClient client;
321 request.url = GURL("http://localhost/"); 321 request.url = GURL("http://localhost/");
322 request.method = "GET"; 322 request.method = "GET";
323 // |resource_type| can't be a frame type. It is because when PlzNavigate is 323 // |resource_type| can't be a frame type. It is because when PlzNavigate is
324 // enabled, the url scheme of frame type requests from the renderer process 324 // enabled, the url scheme of frame type requests from the renderer process
325 // must be blob scheme. 325 // must be blob scheme.
326 request.resource_type = RESOURCE_TYPE_XHR; 326 request.resource_type = RESOURCE_TYPE_XHR;
(...skipping 10 matching lines...) Expand all
337 ASSERT_FALSE(client.has_received_response()); 337 ASSERT_FALSE(client.has_received_response());
338 ASSERT_FALSE(client.response_body().is_valid()); 338 ASSERT_FALSE(client.response_body().is_valid());
339 339
340 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 340 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
341 } 341 }
342 342
343 TEST_P(URLLoaderFactoryImplTest, DownloadToFile) { 343 TEST_P(URLLoaderFactoryImplTest, DownloadToFile) {
344 constexpr int32_t kRoutingId = 1; 344 constexpr int32_t kRoutingId = 1;
345 constexpr int32_t kRequestId = 2; 345 constexpr int32_t kRequestId = 2;
346 346
347 mojom::URLLoaderAssociatedPtr loader; 347 mojom::URLLoaderPtr loader;
348 base::FilePath root; 348 base::FilePath root;
349 PathService::Get(DIR_TEST_DATA, &root); 349 PathService::Get(DIR_TEST_DATA, &root);
350 net::URLRequestMockHTTPJob::AddUrlHandlers(root); 350 net::URLRequestMockHTTPJob::AddUrlHandlers(root);
351 351
352 ResourceRequest request; 352 ResourceRequest request;
353 TestURLLoaderClient client; 353 TestURLLoaderClient client;
354 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html"); 354 request.url = net::URLRequestMockHTTPJob::GetMockUrl("hello.html");
355 request.method = "GET"; 355 request.method = "GET";
356 request.resource_type = RESOURCE_TYPE_XHR; 356 request.resource_type = RESOURCE_TYPE_XHR;
357 request.download_to_file = true; 357 request.download_to_file = true;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 client.response_head().encoded_data_length, 405 client.response_head().encoded_data_length,
406 client.completion_status().encoded_data_length); 406 client.completion_status().encoded_data_length);
407 EXPECT_EQ(static_cast<int64_t>(expected.size()), 407 EXPECT_EQ(static_cast<int64_t>(expected.size()),
408 client.completion_status().encoded_body_length); 408 client.completion_status().encoded_body_length);
409 } 409 }
410 410
411 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) { 411 TEST_P(URLLoaderFactoryImplTest, DownloadToFileFailure) {
412 constexpr int32_t kRoutingId = 1; 412 constexpr int32_t kRoutingId = 1;
413 constexpr int32_t kRequestId = 2; 413 constexpr int32_t kRequestId = 2;
414 414
415 mojom::URLLoaderAssociatedPtr loader; 415 mojom::URLLoaderPtr loader;
416 base::FilePath root; 416 base::FilePath root;
417 PathService::Get(DIR_TEST_DATA, &root); 417 PathService::Get(DIR_TEST_DATA, &root);
418 net::URLRequestSlowDownloadJob::AddUrlHandler(); 418 net::URLRequestSlowDownloadJob::AddUrlHandler();
419 419
420 ResourceRequest request; 420 ResourceRequest request;
421 TestURLLoaderClient client; 421 TestURLLoaderClient client;
422 request.url = GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl); 422 request.url = GURL(net::URLRequestSlowDownloadJob::kKnownSizeUrl);
423 request.method = "GET"; 423 request.method = "GET";
424 request.resource_type = RESOURCE_TYPE_XHR; 424 request.resource_type = RESOURCE_TYPE_XHR;
425 request.download_to_file = true; 425 request.download_to_file = true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 ASSERT_TRUE(client.has_received_completion()); 464 ASSERT_TRUE(client.has_received_completion());
465 465
466 EXPECT_EQ(200, client.response_head().headers->response_code()); 466 EXPECT_EQ(200, client.response_head().headers->response_code());
467 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code); 467 EXPECT_EQ(net::ERR_ABORTED, client.completion_status().error_code);
468 } 468 }
469 469
470 TEST_P(URLLoaderFactoryImplTest, OnTransferSizeUpdated) { 470 TEST_P(URLLoaderFactoryImplTest, OnTransferSizeUpdated) {
471 constexpr int32_t kRoutingId = 81; 471 constexpr int32_t kRoutingId = 81;
472 constexpr int32_t kRequestId = 28; 472 constexpr int32_t kRequestId = 28;
473 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 473 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
474 mojom::URLLoaderAssociatedPtr loader; 474 mojom::URLLoaderPtr loader;
475 base::FilePath root; 475 base::FilePath root;
476 PathService::Get(DIR_TEST_DATA, &root); 476 PathService::Get(DIR_TEST_DATA, &root);
477 net::URLRequestMockHTTPJob::AddUrlHandlers(root); 477 net::URLRequestMockHTTPJob::AddUrlHandlers(root);
478 ResourceRequest request; 478 ResourceRequest request;
479 TestURLLoaderClient client; 479 TestURLLoaderClient client;
480 // Assume the file contents is small enough to be stored in the data pipe. 480 // Assume the file contents is small enough to be stored in the data pipe.
481 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz"); 481 request.url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.svgz");
482 request.method = "GET"; 482 request.method = "GET";
483 // |resource_type| can't be a frame type. It is because when PlzNavigate is 483 // |resource_type| can't be a frame type. It is because when PlzNavigate is
484 // enabled, the url scheme of frame type requests from the renderer process 484 // enabled, the url scheme of frame type requests from the renderer process
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 EXPECT_EQ(client.body_transfer_size(), 525 EXPECT_EQ(client.body_transfer_size(),
526 client.completion_status().encoded_body_length); 526 client.completion_status().encoded_body_length);
527 EXPECT_EQ(contents, "Hello World!\n"); 527 EXPECT_EQ(contents, "Hello World!\n");
528 } 528 }
529 529
530 // Removing the loader in the remote side will cancel the request. 530 // Removing the loader in the remote side will cancel the request.
531 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) { 531 TEST_P(URLLoaderFactoryImplTest, CancelFromRenderer) {
532 constexpr int32_t kRoutingId = 81; 532 constexpr int32_t kRoutingId = 81;
533 constexpr int32_t kRequestId = 28; 533 constexpr int32_t kRequestId = 28;
534 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true); 534 NavigationResourceThrottle::set_ui_checks_always_succeed_for_testing(true);
535 mojom::URLLoaderAssociatedPtr loader; 535 mojom::URLLoaderPtr loader;
536 base::FilePath root; 536 base::FilePath root;
537 PathService::Get(DIR_TEST_DATA, &root); 537 PathService::Get(DIR_TEST_DATA, &root);
538 net::URLRequestFailedJob::AddUrlHandler(); 538 net::URLRequestFailedJob::AddUrlHandler();
539 ResourceRequest request; 539 ResourceRequest request;
540 TestURLLoaderClient client; 540 TestURLLoaderClient client;
541 // Assume the file contents is small enough to be stored in the data pipe. 541 // Assume the file contents is small enough to be stored in the data pipe.
542 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING); 542 request.url = net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_IO_PENDING);
543 request.method = "GET"; 543 request.method = "GET";
544 request.is_main_frame = true; 544 request.is_main_frame = true;
545 // |resource_type| can't be a frame type. It is because when PlzNavigate is 545 // |resource_type| can't be a frame type. It is because when PlzNavigate is
(...skipping 18 matching lines...) Expand all
564 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId))); 564 ASSERT_FALSE(rdh_.GetURLRequest(GlobalRequestID(kChildId, kRequestId)));
565 } 565 }
566 566
567 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest, 567 INSTANTIATE_TEST_CASE_P(URLLoaderFactoryImplTest,
568 URLLoaderFactoryImplTest, 568 URLLoaderFactoryImplTest,
569 ::testing::Values(128, 32 * 1024)); 569 ::testing::Values(128, 32 * 1024));
570 570
571 } // namespace 571 } // namespace
572 572
573 } // namespace content 573 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/url_loader_factory_impl.cc ('k') | content/browser/service_worker/service_worker_fetch_dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698