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

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

Issue 2543633004: Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: Fix cert tests, add out-of-band cancel tests Created 4 years 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/files/file.h" 13 #include "base/files/file.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
18 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
19 #include "base/run_loop.h" 19 #include "base/run_loop.h"
20 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/threading/thread_task_runner_handle.h" 21 #include "base/threading/thread_task_runner_handle.h"
22 #include "content/browser/browser_thread_impl.h" 22 #include "content/browser/browser_thread_impl.h"
23 #include "content/browser/loader/redirect_to_file_resource_handler.h" 23 #include "content/browser/loader/redirect_to_file_resource_handler.h"
24 #include "content/browser/loader/resource_loader_delegate.h" 24 #include "content/browser/loader/resource_loader_delegate.h"
25 #include "content/browser/loader/test_resource_handler.h"
25 #include "content/public/browser/client_certificate_delegate.h" 26 #include "content/public/browser/client_certificate_delegate.h"
26 #include "content/public/browser/resource_request_info.h" 27 #include "content/public/browser/resource_request_info.h"
27 #include "content/public/common/content_paths.h" 28 #include "content/public/common/content_paths.h"
28 #include "content/public/common/resource_response.h" 29 #include "content/public/common/resource_response.h"
29 #include "content/public/common/resource_type.h" 30 #include "content/public/common/resource_type.h"
30 #include "content/public/test/mock_resource_context.h" 31 #include "content/public/test/mock_resource_context.h"
31 #include "content/public/test/test_browser_context.h" 32 #include "content/public/test/test_browser_context.h"
32 #include "content/public/test/test_browser_thread_bundle.h" 33 #include "content/public/test/test_browser_thread_bundle.h"
33 #include "content/public/test/test_renderer_host.h" 34 #include "content/public/test/test_renderer_host.h"
34 #include "content/test/test_content_browser_client.h" 35 #include "content/test/test_content_browser_client.h"
35 #include "content/test/test_web_contents.h" 36 #include "content/test/test_web_contents.h"
36 #include "ipc/ipc_message.h" 37 #include "ipc/ipc_message.h"
37 #include "net/base/chunked_upload_data_stream.h" 38 #include "net/base/chunked_upload_data_stream.h"
38 #include "net/base/io_buffer.h" 39 #include "net/base/io_buffer.h"
39 #include "net/base/mock_file_stream.h" 40 #include "net/base/mock_file_stream.h"
40 #include "net/base/net_errors.h" 41 #include "net/base/net_errors.h"
41 #include "net/base/request_priority.h" 42 #include "net/base/request_priority.h"
42 #include "net/base/upload_bytes_element_reader.h" 43 #include "net/base/upload_bytes_element_reader.h"
43 #include "net/cert/x509_certificate.h" 44 #include "net/cert/x509_certificate.h"
44 #include "net/nqe/effective_connection_type.h" 45 #include "net/nqe/effective_connection_type.h"
45 #include "net/nqe/network_quality_estimator.h" 46 #include "net/nqe/network_quality_estimator.h"
46 #include "net/ssl/client_cert_store.h" 47 #include "net/ssl/client_cert_store.h"
47 #include "net/ssl/ssl_cert_request_info.h" 48 #include "net/ssl/ssl_cert_request_info.h"
48 #include "net/ssl/ssl_private_key.h" 49 #include "net/ssl/ssl_private_key.h"
49 #include "net/test/cert_test_util.h" 50 #include "net/test/cert_test_util.h"
50 #include "net/test/embedded_test_server/embedded_test_server.h" 51 #include "net/test/embedded_test_server/embedded_test_server.h"
51 #include "net/test/test_data_directory.h" 52 #include "net/test/test_data_directory.h"
53 #include "net/test/url_request/url_request_failed_job.h"
52 #include "net/url_request/url_request.h" 54 #include "net/url_request/url_request.h"
53 #include "net/url_request/url_request_filter.h" 55 #include "net/url_request/url_request_filter.h"
54 #include "net/url_request/url_request_interceptor.h" 56 #include "net/url_request/url_request_interceptor.h"
55 #include "net/url_request/url_request_job_factory.h" 57 #include "net/url_request/url_request_job_factory.h"
56 #include "net/url_request/url_request_job_factory_impl.h" 58 #include "net/url_request/url_request_job_factory_impl.h"
57 #include "net/url_request/url_request_test_job.h" 59 #include "net/url_request/url_request_test_job.h"
58 #include "net/url_request/url_request_test_util.h" 60 #include "net/url_request/url_request_test_util.h"
59 #include "storage/browser/blob/shareable_file_reference.h" 61 #include "storage/browser/blob/shareable_file_reference.h"
60 #include "testing/gtest/include/gtest/gtest.h" 62 #include "testing/gtest/include/gtest/gtest.h"
61 63
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 class ResourceHandlerStub : public ResourceHandler { 274 class ResourceHandlerStub : public ResourceHandler {
273 public: 275 public:
274 explicit ResourceHandlerStub(net::URLRequest* request) 276 explicit ResourceHandlerStub(net::URLRequest* request)
275 : ResourceHandler(request), 277 : ResourceHandler(request),
276 read_buffer_(new net::IOBuffer(kReadBufSize)), 278 read_buffer_(new net::IOBuffer(kReadBufSize)),
277 defer_request_on_will_start_(false), 279 defer_request_on_will_start_(false),
278 expect_reads_(true), 280 expect_reads_(true),
279 cancel_on_read_completed_(false), 281 cancel_on_read_completed_(false),
280 defer_eof_(false), 282 defer_eof_(false),
281 received_on_will_read_(false), 283 received_on_will_read_(false),
282 received_eof_(false), 284 on_read_eof_(false),
283 received_response_completed_(false), 285 received_response_completed_(false),
284 received_request_redirected_(false), 286 received_request_redirected_(false),
285 total_bytes_downloaded_(0), 287 total_bytes_downloaded_(0),
286 observed_effective_connection_type_( 288 observed_effective_connection_type_(
287 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} 289 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {}
288 290
289 // If true, defers the resource load in OnWillStart. 291 // If true, defers the resource load in OnWillStart.
290 void set_defer_request_on_will_start(bool defer_request_on_will_start) { 292 void set_defer_on_will_start(bool defer_request_on_will_start) {
291 defer_request_on_will_start_ = defer_request_on_will_start; 293 defer_request_on_will_start_ = defer_request_on_will_start;
292 } 294 }
293 295
294 // If true, expect OnWillRead / OnReadCompleted pairs for handling 296 // If true, expect OnWillRead / OnReadCompleted pairs for handling
295 // data. Otherwise, expect OnDataDownloaded. 297 // data. Otherwise, expect OnDataDownloaded.
296 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; } 298 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; }
297 299
298 // If true, cancel the request in OnReadCompleted by returning false. 300 // If true, cancel the request in OnReadCompleted by returning false.
299 void set_cancel_on_read_completed(bool cancel_on_read_completed) { 301 void set_cancel_on_read_completed(bool cancel_on_read_completed) {
300 cancel_on_read_completed_ = cancel_on_read_completed; 302 cancel_on_read_completed_ = cancel_on_read_completed;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 deferred_run_loop_.Quit(); 351 deferred_run_loop_.Quit();
350 } 352 }
351 return true; 353 return true;
352 } 354 }
353 355
354 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 356 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
355 int* buf_size, 357 int* buf_size,
356 int min_size) override { 358 int min_size) override {
357 EXPECT_TRUE(expect_reads_); 359 EXPECT_TRUE(expect_reads_);
358 EXPECT_FALSE(received_on_will_read_); 360 EXPECT_FALSE(received_on_will_read_);
359 EXPECT_FALSE(received_eof_); 361 EXPECT_FALSE(on_read_eof_);
360 EXPECT_FALSE(received_response_completed_); 362 EXPECT_FALSE(received_response_completed_);
361 363
362 *buf = read_buffer_; 364 *buf = read_buffer_;
363 *buf_size = kReadBufSize; 365 *buf_size = kReadBufSize;
364 received_on_will_read_ = true; 366 received_on_will_read_ = true;
365 return true; 367 return true;
366 } 368 }
367 369
368 bool OnReadCompleted(int bytes_read, bool* defer) override { 370 bool OnReadCompleted(int bytes_read, bool* defer) override {
369 EXPECT_TRUE(received_on_will_read_); 371 EXPECT_TRUE(received_on_will_read_);
370 EXPECT_TRUE(expect_reads_); 372 EXPECT_TRUE(expect_reads_);
371 EXPECT_FALSE(received_response_completed_); 373 EXPECT_FALSE(received_response_completed_);
372 374
373 if (bytes_read == 0) { 375 if (bytes_read == 0) {
374 received_eof_ = true; 376 on_read_eof_ = true;
375 if (defer_eof_) { 377 if (defer_eof_) {
376 defer_eof_ = false; 378 defer_eof_ = false;
377 *defer = true; 379 *defer = true;
378 deferred_run_loop_.Quit(); 380 deferred_run_loop_.Quit();
379 } 381 }
380 } 382 }
381 383
382 // Need another OnWillRead() call before seeing an OnReadCompleted(). 384 // Need another OnWillRead() call before seeing an OnReadCompleted().
383 received_on_will_read_ = false; 385 received_on_will_read_ = false;
384 386
385 return !cancel_on_read_completed_; 387 return !cancel_on_read_completed_;
386 } 388 }
387 389
388 void OnResponseCompleted(const net::URLRequestStatus& status, 390 void OnResponseCompleted(const net::URLRequestStatus& status,
389 bool* defer) override { 391 bool* defer) override {
390 EXPECT_FALSE(received_response_completed_); 392 EXPECT_FALSE(received_response_completed_);
391 if (status.is_success() && expect_reads_) 393 if (status.is_success() && expect_reads_)
392 EXPECT_TRUE(received_eof_); 394 EXPECT_TRUE(on_read_eof_);
393 395
394 received_response_completed_ = true; 396 received_response_completed_ = true;
395 status_ = status; 397 status_ = status;
396 response_completed_run_loop_.Quit(); 398 response_completed_run_loop_.Quit();
397 } 399 }
398 400
399 void OnDataDownloaded(int bytes_downloaded) override { 401 void OnDataDownloaded(int bytes_downloaded) override {
400 EXPECT_FALSE(expect_reads_); 402 EXPECT_FALSE(expect_reads_);
401 total_bytes_downloaded_ += bytes_downloaded; 403 total_bytes_downloaded_ += bytes_downloaded;
402 } 404 }
403 405
404 // Waits for the the first deferred step to run, if there is one. 406 // Waits for the the first deferred step to run, if there is one.
405 void WaitForDeferredStep() { 407 void WaitUntilDeferred() {
406 DCHECK(defer_request_on_will_start_ || defer_eof_); 408 DCHECK(defer_request_on_will_start_ || defer_eof_);
407 deferred_run_loop_.Run(); 409 deferred_run_loop_.Run();
408 } 410 }
409 411
410 // Waits until the response has completed. 412 // Waits until the response has completed.
411 void WaitForResponseComplete() { 413 void WaitUntilResponseComplete() {
412 response_completed_run_loop_.Run(); 414 response_completed_run_loop_.Run();
413 EXPECT_TRUE(received_response_completed_); 415 EXPECT_TRUE(received_response_completed_);
414 } 416 }
415 417
416 private: 418 private:
417 scoped_refptr<net::IOBuffer> read_buffer_; 419 scoped_refptr<net::IOBuffer> read_buffer_;
418 420
419 bool defer_request_on_will_start_; 421 bool defer_request_on_will_start_;
420 bool expect_reads_; 422 bool expect_reads_;
421 bool cancel_on_read_completed_; 423 bool cancel_on_read_completed_;
422 bool defer_eof_; 424 bool defer_eof_;
423 425
424 GURL start_url_; 426 GURL start_url_;
425 scoped_refptr<ResourceResponse> response_; 427 scoped_refptr<ResourceResponse> response_;
426 scoped_refptr<ResourceResponse> redirect_response_; 428 scoped_refptr<ResourceResponse> redirect_response_;
427 bool received_on_will_read_; 429 bool received_on_will_read_;
428 bool received_eof_; 430 bool on_read_eof_;
429 bool received_response_completed_; 431 bool received_response_completed_;
430 bool received_request_redirected_; 432 bool received_request_redirected_;
431 net::URLRequestStatus status_; 433 net::URLRequestStatus status_;
432 int total_bytes_downloaded_; 434 int total_bytes_downloaded_;
433 base::RunLoop deferred_run_loop_; 435 base::RunLoop deferred_run_loop_;
434 base::RunLoop response_completed_run_loop_; 436 base::RunLoop response_completed_run_loop_;
435 std::unique_ptr<base::RunLoop> wait_for_progress_run_loop_; 437 std::unique_ptr<base::RunLoop> wait_for_progress_run_loop_;
436 net::EffectiveConnectionType observed_effective_connection_type_; 438 net::EffectiveConnectionType observed_effective_connection_type_;
437 }; 439 };
438 440
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 test_url_request_context_(true), 560 test_url_request_context_(true),
559 resource_context_(&test_url_request_context_), 561 resource_context_(&test_url_request_context_),
560 raw_ptr_resource_handler_(NULL), 562 raw_ptr_resource_handler_(NULL),
561 raw_ptr_to_request_(NULL) { 563 raw_ptr_to_request_(NULL) {
562 test_url_request_context_.set_job_factory(&job_factory_); 564 test_url_request_context_.set_job_factory(&job_factory_);
563 test_url_request_context_.set_network_quality_estimator( 565 test_url_request_context_.set_network_quality_estimator(
564 &network_quality_estimator_); 566 &network_quality_estimator_);
565 test_url_request_context_.Init(); 567 test_url_request_context_.Init();
566 } 568 }
567 569
568 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); } 570 GURL test_url() const {
571 return net::URLRequestTestJob::test_url_redirect_to_url_1();
572 }
569 573
570 TestNetworkQualityEstimator* network_quality_estimator() { 574 TestNetworkQualityEstimator* network_quality_estimator() {
571 return &network_quality_estimator_; 575 return &network_quality_estimator_;
572 } 576 }
573 577
574 std::string test_data() const { 578 std::string test_data() const {
575 return net::URLRequestTestJob::test_data_1(); 579 return net::URLRequestTestJob::test_data_1();
576 } 580 }
577 581
578 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 582 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
579 CreateProtocolHandler() { 583 CreateProtocolHandler() {
580 return net::URLRequestTestJob::CreateProtocolHandler(); 584 return net::URLRequestTestJob::CreateProtocolHandler();
581 } 585 }
582 586
583 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( 587 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler(
584 std::unique_ptr<ResourceHandlerStub> leaf_handler, 588 std::unique_ptr<TestResourceHandler> leaf_handler,
585 net::URLRequest* request) { 589 net::URLRequest* request) {
586 return std::move(leaf_handler); 590 return std::move(leaf_handler);
587 } 591 }
588 592
589 // Replaces loader_ with a new one for |request|. 593 // Replaces loader_ with a new one for |request|.
590 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, 594 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request,
591 ResourceType resource_type, 595 ResourceType resource_type,
592 bool belongs_to_main_frame) { 596 bool belongs_to_main_frame) {
593 raw_ptr_to_request_ = request.get(); 597 raw_ptr_to_request_ = request.get();
594 598
595 // A request marked as a main frame request must also belong to a main 599 // A request marked as a main frame request must also belong to a main
596 // frame. 600 // frame.
597 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || 601 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) ||
598 belongs_to_main_frame); 602 belongs_to_main_frame);
599 603
600 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 604 RenderFrameHost* rfh = web_contents_->GetMainFrame();
601 ResourceRequestInfo::AllocateForTesting( 605 ResourceRequestInfo::AllocateForTesting(
602 request.get(), resource_type, &resource_context_, 606 request.get(), resource_type, &resource_context_,
603 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 607 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
604 rfh->GetRoutingID(), belongs_to_main_frame, 608 rfh->GetRoutingID(), belongs_to_main_frame,
605 false /* parent_is_main_frame */, true /* allow_download */, 609 false /* parent_is_main_frame */, true /* allow_download */,
606 false /* is_async */, false /* is_using_lofi_ */); 610 false /* is_async */, false /* is_using_lofi_ */);
607 std::unique_ptr<ResourceHandlerStub> resource_handler( 611 std::unique_ptr<TestResourceHandler> resource_handler(
608 new ResourceHandlerStub(request.get())); 612 new TestResourceHandler(nullptr, nullptr));
609 raw_ptr_resource_handler_ = resource_handler.get(); 613 raw_ptr_resource_handler_ = resource_handler.get();
610 loader_.reset(new ResourceLoader( 614 loader_.reset(new ResourceLoader(
611 std::move(request), 615 std::move(request),
612 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), 616 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_),
613 this)); 617 this));
614 } 618 }
615 619
616 void SetUp() override { 620 void SetUp() override {
617 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 621 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
622 net::URLRequestFailedJob::AddUrlHandler();
618 623
619 browser_context_.reset(new TestBrowserContext()); 624 browser_context_.reset(new TestBrowserContext());
620 scoped_refptr<SiteInstance> site_instance = 625 scoped_refptr<SiteInstance> site_instance =
621 SiteInstance::Create(browser_context_.get()); 626 SiteInstance::Create(browser_context_.get());
622 web_contents_.reset( 627 web_contents_.reset(
623 TestWebContents::Create(browser_context_.get(), site_instance.get())); 628 TestWebContents::Create(browser_context_.get(), site_instance.get()));
624 629 SetUpResourceLoaderForUrl(test_url());
625 std::unique_ptr<net::URLRequest> request(
626 resource_context_.GetRequestContext()->CreateRequest(
627 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
628 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true);
629 } 630 }
630 631
631 void TearDown() override { 632 void TearDown() override {
632 // Destroy the WebContents and pump the event loop before destroying 633 // Destroy the WebContents and pump the event loop before destroying
633 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 634 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
634 // tasks complete. 635 // tasks complete.
635 web_contents_.reset(); 636 web_contents_.reset();
637
638 // Clean up handlers.
639 net::URLRequestFilter::GetInstance()->ClearHandlers();
640
636 base::RunLoop().RunUntilIdle(); 641 base::RunLoop().RunUntilIdle();
637 } 642 }
638 643
644 void SetUpResourceLoaderForUrl(const GURL& test_url) {
645 std::unique_ptr<net::URLRequest> request(
646 resource_context_.GetRequestContext()->CreateRequest(
647 test_url, net::DEFAULT_PRIORITY, nullptr /* delegate */));
648 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true);
649 }
650
639 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { 651 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) {
640 dummy_cert_store_ = std::move(store); 652 dummy_cert_store_ = std::move(store);
641 } 653 }
642 654
643 // ResourceLoaderDelegate: 655 // ResourceLoaderDelegate:
644 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 656 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
645 ResourceLoader* loader, 657 ResourceLoader* loader,
646 net::AuthChallengeInfo* auth_info) override { 658 net::AuthChallengeInfo* auth_info) override {
647 return NULL; 659 return NULL;
648 } 660 }
649 bool HandleExternalProtocol(ResourceLoader* loader, 661 bool HandleExternalProtocol(ResourceLoader* loader,
650 const GURL& url) override { 662 const GURL& url) override {
663 EXPECT_EQ(loader, loader_.get());
651 return false; 664 return false;
652 } 665 }
653 void DidStartRequest(ResourceLoader* loader) override {} 666 void DidStartRequest(ResourceLoader* loader) override {
667 EXPECT_EQ(loader, loader_.get());
668 EXPECT_EQ(0, did_finish_loading_);
669 EXPECT_EQ(0, did_start_request_);
670 ++did_start_request_;
671 }
654 void DidReceiveRedirect(ResourceLoader* loader, 672 void DidReceiveRedirect(ResourceLoader* loader,
655 const GURL& new_url, 673 const GURL& new_url,
656 ResourceResponse* response) override {} 674 ResourceResponse* response) override {
657 void DidReceiveResponse(ResourceLoader* loader) override {} 675 EXPECT_EQ(loader, loader_.get());
658 void DidFinishLoading(ResourceLoader* loader) override {} 676 EXPECT_EQ(0, did_finish_loading_);
677 EXPECT_EQ(0, did_receive_response_);
678 EXPECT_EQ(1, did_start_request_);
679 ++did_received_redirect_;
680 }
681 void DidReceiveResponse(ResourceLoader* loader) override {
682 EXPECT_EQ(loader, loader_.get());
683 EXPECT_EQ(0, did_finish_loading_);
684 EXPECT_EQ(0, did_receive_response_);
685 EXPECT_EQ(1, did_start_request_);
686 ++did_receive_response_;
687 }
688 void DidFinishLoading(ResourceLoader* loader) override {
689 EXPECT_EQ(loader, loader_.get());
690 EXPECT_EQ(0, did_finish_loading_);
691 ++did_finish_loading_;
692 }
659 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( 693 std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
660 ResourceLoader* loader) override { 694 ResourceLoader* loader) override {
661 return std::move(dummy_cert_store_); 695 return std::move(dummy_cert_store_);
662 } 696 }
663 697
664 TestBrowserThreadBundle thread_bundle_; 698 TestBrowserThreadBundle thread_bundle_;
665 RenderViewHostTestEnabler rvh_test_enabler_; 699 RenderViewHostTestEnabler rvh_test_enabler_;
666 700
701 // Record which ResourceDispatcherHostDelegate methods have been invoked.
702 int did_start_request_ = 0;
703 int did_received_redirect_ = 0;
704 int did_receive_response_ = 0;
705 int did_finish_loading_ = 0;
706
667 net::URLRequestJobFactoryImpl job_factory_; 707 net::URLRequestJobFactoryImpl job_factory_;
668 TestNetworkQualityEstimator network_quality_estimator_; 708 TestNetworkQualityEstimator network_quality_estimator_;
669 net::TestURLRequestContext test_url_request_context_; 709 net::TestURLRequestContext test_url_request_context_;
670 MockResourceContext resource_context_; 710 MockResourceContext resource_context_;
671 std::unique_ptr<TestBrowserContext> browser_context_; 711 std::unique_ptr<TestBrowserContext> browser_context_;
672 std::unique_ptr<TestWebContents> web_contents_; 712 std::unique_ptr<TestWebContents> web_contents_;
673 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; 713 std::unique_ptr<net::ClientCertStore> dummy_cert_store_;
674 714
675 // The ResourceLoader owns the URLRequest and the ResourceHandler. 715 // The ResourceLoader owns the URLRequest and the ResourceHandler.
676 ResourceHandlerStub* raw_ptr_resource_handler_; 716 TestResourceHandler* raw_ptr_resource_handler_;
677 net::URLRequest* raw_ptr_to_request_; 717 net::URLRequest* raw_ptr_to_request_;
678 std::unique_ptr<ResourceLoader> loader_; 718 std::unique_ptr<ResourceLoader> loader_;
679 }; 719 };
680 720
681 class ClientCertResourceLoaderTest : public ResourceLoaderTest { 721 class ClientCertResourceLoaderTest : public ResourceLoaderTest {
682 protected: 722 protected:
683 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 723 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
684 CreateProtocolHandler() override { 724 CreateProtocolHandler() override {
685 return base::WrapUnique(new MockClientCertJobProtocolHandler); 725 return base::WrapUnique(new MockClientCertJobProtocolHandler);
686 } 726 }
727
728 void SetUp() override {
729 ResourceLoaderTest::SetUp();
730 // These tests don't expect any redirects.
731 SetUpResourceLoaderForUrl(net::URLRequestTestJob::test_url_1());
732 }
687 }; 733 };
688 734
689 // A ResourceLoaderTest that intercepts https://example.test and 735 // A ResourceLoaderTest that intercepts https://example.test and
690 // https://example-redirect.test URLs and sets SSL info on the 736 // https://example-redirect.test URLs and sets SSL info on the
691 // responses. The latter serves a Location: header in the response. 737 // responses. The latter serves a Location: header in the response.
692 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { 738 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest {
693 public: 739 public:
694 HTTPSSecurityInfoResourceLoaderTest() 740 HTTPSSecurityInfoResourceLoaderTest()
695 : ResourceLoaderTest(), 741 : ResourceLoaderTest(),
696 test_https_url_("https://example.test"), 742 test_https_url_("https://example.test"),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 SetClientCertStore(std::move(test_store)); 779 SetClientCertStore(std::move(test_store));
734 780
735 // Plug in test content browser client. 781 // Plug in test content browser client.
736 SelectCertificateBrowserClient test_client; 782 SelectCertificateBrowserClient test_client;
737 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 783 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
738 784
739 // Start the request and wait for it to pause. 785 // Start the request and wait for it to pause.
740 loader_->StartRequest(); 786 loader_->StartRequest();
741 test_client.WaitForSelectCertificate(); 787 test_client.WaitForSelectCertificate();
742 788
743 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 789 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
744 790
745 // Check if the test store was queried against correct |cert_authorities|. 791 // Check if the test store was queried against correct |cert_authorities|.
746 EXPECT_EQ(1, store_request_count); 792 EXPECT_EQ(1, store_request_count);
747 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), 793 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(),
748 store_requested_authorities); 794 store_requested_authorities);
749 795
750 // Check if the retrieved certificates were passed to the content browser 796 // Check if the retrieved certificates were passed to the content browser
751 // client. 797 // client.
752 EXPECT_EQ(1, test_client.call_count()); 798 EXPECT_EQ(1, test_client.call_count());
753 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 799 EXPECT_EQ(dummy_certs, test_client.passed_certs());
754 800
755 // Continue the request. 801 // Continue the request.
756 test_client.ContinueWithCertificate(nullptr); 802 test_client.ContinueWithCertificate(nullptr);
757 raw_ptr_resource_handler_->WaitForResponseComplete(); 803 raw_ptr_resource_handler_->WaitUntilResponseComplete();
758 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 804 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
759 805
760 // Restore the original content browser client. 806 // Restore the original content browser client.
761 SetBrowserClientForTesting(old_client); 807 SetBrowserClientForTesting(old_client);
762 } 808 }
763 809
764 // Tests that client certificates are requested on a platform with NULL 810 // Tests that client certificates are requested on a platform with NULL
765 // ClientCertStore. 811 // ClientCertStore.
766 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { 812 TEST_F(ClientCertResourceLoaderTest, WithNullStore) {
767 // Plug in test content browser client. 813 // Plug in test content browser client.
768 SelectCertificateBrowserClient test_client; 814 SelectCertificateBrowserClient test_client;
769 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 815 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
770 816
771 // Start the request and wait for it to pause. 817 // Start the request and wait for it to pause.
772 loader_->StartRequest(); 818 loader_->StartRequest();
773 test_client.WaitForSelectCertificate(); 819 test_client.WaitForSelectCertificate();
774 820
775 // Check if the SelectClientCertificate was called on the content browser 821 // Check if the SelectClientCertificate was called on the content browser
776 // client. 822 // client.
777 EXPECT_EQ(1, test_client.call_count()); 823 EXPECT_EQ(1, test_client.call_count());
778 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 824 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
779 825
780 // Continue the request. 826 // Continue the request.
781 test_client.ContinueWithCertificate(nullptr); 827 test_client.ContinueWithCertificate(nullptr);
782 raw_ptr_resource_handler_->WaitForResponseComplete(); 828 raw_ptr_resource_handler_->WaitUntilResponseComplete();
783 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 829 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
784 830
785 // Restore the original content browser client. 831 // Restore the original content browser client.
786 SetBrowserClientForTesting(old_client); 832 SetBrowserClientForTesting(old_client);
787 } 833 }
788 834
789 // Tests that the ContentBrowserClient may cancel a certificate request. 835 // Tests that the ContentBrowserClient may cancel a certificate request.
790 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { 836 TEST_F(ClientCertResourceLoaderTest, CancelSelection) {
791 // Plug in test content browser client. 837 // Plug in test content browser client.
792 SelectCertificateBrowserClient test_client; 838 SelectCertificateBrowserClient test_client;
793 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 839 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
794 840
795 // Start the request and wait for it to pause. 841 // Start the request and wait for it to pause.
796 loader_->StartRequest(); 842 loader_->StartRequest();
797 test_client.WaitForSelectCertificate(); 843 test_client.WaitForSelectCertificate();
798 844
799 // Check if the SelectClientCertificate was called on the content browser 845 // Check if the SelectClientCertificate was called on the content browser
800 // client. 846 // client.
801 EXPECT_EQ(1, test_client.call_count()); 847 EXPECT_EQ(1, test_client.call_count());
802 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 848 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
803 849
804 // Cancel the request. 850 // Cancel the request.
805 test_client.CancelCertificateSelection(); 851 test_client.CancelCertificateSelection();
806 raw_ptr_resource_handler_->WaitForResponseComplete(); 852 raw_ptr_resource_handler_->WaitUntilResponseComplete();
807 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 853 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
808 raw_ptr_resource_handler_->status().error()); 854 raw_ptr_resource_handler_->final_status().error());
809 855
810 // Restore the original content browser client. 856 // Restore the original content browser client.
811 SetBrowserClientForTesting(old_client); 857 SetBrowserClientForTesting(old_client);
812 } 858 }
813 859
814 // Verifies that requests without WebContents attached abort. 860 // Verifies that requests without WebContents attached abort.
815 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { 861 TEST_F(ClientCertResourceLoaderTest, NoWebContents) {
816 // Destroy the WebContents before starting the request. 862 // Destroy the WebContents before starting the request.
817 web_contents_.reset(); 863 web_contents_.reset();
818 864
819 // Plug in test content browser client. 865 // Plug in test content browser client.
820 SelectCertificateBrowserClient test_client; 866 SelectCertificateBrowserClient test_client;
821 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 867 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
822 868
823 // Start the request and wait for it to complete. 869 // Start the request and wait for it to complete.
824 loader_->StartRequest(); 870 loader_->StartRequest();
825 raw_ptr_resource_handler_->WaitForResponseComplete(); 871 raw_ptr_resource_handler_->WaitUntilResponseComplete();
826 872
827 // Check that SelectClientCertificate wasn't called and the request aborted. 873 // Check that SelectClientCertificate wasn't called and the request aborted.
828 EXPECT_EQ(0, test_client.call_count()); 874 EXPECT_EQ(0, test_client.call_count());
829 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 875 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
830 raw_ptr_resource_handler_->status().error()); 876 raw_ptr_resource_handler_->final_status().error());
831 877
832 // Restore the original content browser client. 878 // Restore the original content browser client.
833 SetBrowserClientForTesting(old_client); 879 SetBrowserClientForTesting(old_client);
834 } 880 }
835 881
836 // Verifies that ClientCertStore's callback doesn't crash if called after the 882 // Verifies that ClientCertStore's callback doesn't crash if called after the
837 // loader is destroyed. 883 // loader is destroyed.
838 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { 884 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) {
839 base::RunLoop loader_destroyed_run_loop; 885 base::RunLoop loader_destroyed_run_loop;
840 LoaderDestroyingCertStore* test_store = 886 LoaderDestroyingCertStore* test_store =
841 new LoaderDestroyingCertStore(&loader_, 887 new LoaderDestroyingCertStore(&loader_,
842 loader_destroyed_run_loop.QuitClosure()); 888 loader_destroyed_run_loop.QuitClosure());
843 SetClientCertStore(base::WrapUnique(test_store)); 889 SetClientCertStore(base::WrapUnique(test_store));
844 890
845 loader_->StartRequest(); 891 loader_->StartRequest();
846 loader_destroyed_run_loop.Run(); 892 loader_destroyed_run_loop.Run();
847 EXPECT_FALSE(loader_); 893 EXPECT_FALSE(loader_);
848 894
849 // Pump the event loop to ensure nothing asynchronous crashes either. 895 // Pump the event loop to ensure nothing asynchronous crashes either.
850 base::RunLoop().RunUntilIdle(); 896 base::RunLoop().RunUntilIdle();
851 } 897 }
852 898
853 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { 899 // Test the case the ResourceHandler defers nothing.
854 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 900 TEST_F(ResourceLoaderTest, SyncResourceHandler) {
901 loader_->StartRequest();
902 raw_ptr_resource_handler_->WaitUntilResponseComplete();
903 EXPECT_EQ(1, did_start_request_);
904 EXPECT_EQ(1, did_received_redirect_);
905 EXPECT_EQ(1, did_receive_response_);
906 EXPECT_EQ(1, did_finish_loading_);
907 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
908 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
909 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
910 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
911 }
912
913 // Test the case the ResourceHandler defers everything.
914 TEST_F(ResourceLoaderTest, AsyncResourceHandler) {
915 raw_ptr_resource_handler_->set_defer_on_will_start(true);
916 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
917 raw_ptr_resource_handler_->set_defer_on_response_started(true);
918 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
919 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
920 raw_ptr_resource_handler_->set_defer_on_response_completed(true);
921
922 // Start and run until OnWillStart.
923 loader_->StartRequest();
924 raw_ptr_resource_handler_->WaitUntilDeferred();
925 // Spinning the message loop should not advance the state further.
926 base::RunLoop().RunUntilIdle();
927 EXPECT_EQ(0, did_start_request_);
928 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
929 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
930 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
931
932 // Resume and run until OnRequestRedirected.
933 raw_ptr_resource_handler_->Resume();
934 raw_ptr_resource_handler_->WaitUntilDeferred();
935 // Spinning the message loop should not advance the state further.
936 base::RunLoop().RunUntilIdle();
937 EXPECT_EQ(1, did_received_redirect_);
938 EXPECT_EQ(0, did_receive_response_);
939 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
940 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
941 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
942
943 // Resume and run until OnResponseStarted.
944 raw_ptr_resource_handler_->Resume();
945 raw_ptr_resource_handler_->WaitUntilDeferred();
946 // Spinning the message loop should not advance the state further.
947 base::RunLoop().RunUntilIdle();
948 EXPECT_EQ(1, did_receive_response_);
949 EXPECT_EQ(0, did_finish_loading_);
950 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
951 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
952 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
953
954 // Resume and run until OnReadCompleted.
955 raw_ptr_resource_handler_->Resume();
956 raw_ptr_resource_handler_->WaitUntilDeferred();
957 // Spinning the message loop should not advance the state further.
958 base::RunLoop().RunUntilIdle();
959 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called());
960 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
961 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
962 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
963
964 // Resume and run until the final 0-byte read, signalling EOF.
965 raw_ptr_resource_handler_->Resume();
966 raw_ptr_resource_handler_->WaitUntilDeferred();
967 // Spinning the message loop should not advance the state further.
968 base::RunLoop().RunUntilIdle();
969 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
970 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
971 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
972
973 // Resume and run until OnResponseCompleted is called, which again defers the
974 // request.
975 raw_ptr_resource_handler_->Resume();
976 raw_ptr_resource_handler_->WaitUntilResponseComplete();
977 EXPECT_EQ(0, did_finish_loading_);
978 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
979 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
980 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
981
982 // Resume and run until all pending tasks. Note that OnResponseCompleted was
983 // invoked in the previous section, so can't use RunUntilCompleted().
984 raw_ptr_resource_handler_->Resume();
985 base::RunLoop().RunUntilIdle();
986 EXPECT_EQ(1, did_finish_loading_);
987 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
988 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
989 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
990 }
991
992 TEST_F(ResourceLoaderTest, SyncCancelOnWillStart) {
993 raw_ptr_resource_handler_->set_on_will_start_result(false);
994
995 loader_->StartRequest();
996 raw_ptr_resource_handler_->WaitUntilResponseComplete();
997 base::RunLoop().RunUntilIdle();
998 EXPECT_EQ(0, did_start_request_);
999 EXPECT_EQ(1, did_finish_loading_);
1000 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1001 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1002 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1003 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1004
1005 EXPECT_EQ(net::ERR_ABORTED,
1006 raw_ptr_resource_handler_->final_status().error());
1007 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1008 }
1009
1010 TEST_F(ResourceLoaderTest, SyncCancelOnRequestRedirected) {
1011 raw_ptr_resource_handler_->set_on_request_redirected_result(false);
1012
1013 loader_->StartRequest();
1014 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1015 base::RunLoop().RunUntilIdle();
1016 EXPECT_EQ(1, did_received_redirect_);
1017 EXPECT_EQ(0, did_receive_response_);
1018 EXPECT_EQ(1, did_finish_loading_);
1019 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
1020 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
1021 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1022 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1023
1024 EXPECT_EQ(net::ERR_ABORTED,
1025 raw_ptr_resource_handler_->final_status().error());
1026 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1027 }
1028
1029 TEST_F(ResourceLoaderTest, SyncCancelOnResponseStarted) {
1030 raw_ptr_resource_handler_->set_on_response_started_result(false);
1031
1032 loader_->StartRequest();
1033 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1034 base::RunLoop().RunUntilIdle();
1035 EXPECT_EQ(1, did_receive_response_);
1036 EXPECT_EQ(1, did_finish_loading_);
1037 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1038 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
1039 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1040 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1041
1042 EXPECT_EQ(net::ERR_ABORTED,
1043 raw_ptr_resource_handler_->final_status().error());
1044 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1045 }
1046
1047 TEST_F(ResourceLoaderTest, SyncCancelOnWillRead) {
1048 raw_ptr_resource_handler_->set_on_will_read_result(false);
1049
1050 loader_->StartRequest();
1051 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1052 base::RunLoop().RunUntilIdle();
1053 EXPECT_EQ(1, did_receive_response_);
1054 EXPECT_EQ(1, did_finish_loading_);
1055 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called());
1056 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1057 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1058
1059 EXPECT_EQ(net::ERR_ABORTED,
1060 raw_ptr_resource_handler_->final_status().error());
1061 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1062 }
1063
1064 TEST_F(ResourceLoaderTest, SyncCancelOnReadCompleted) {
1065 raw_ptr_resource_handler_->set_on_read_completed_result(false);
1066
1067 loader_->StartRequest();
1068 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1069 base::RunLoop().RunUntilIdle();
1070 EXPECT_EQ(1, did_receive_response_);
1071 EXPECT_EQ(1, did_finish_loading_);
1072 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
1073 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
1074 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1075
1076 EXPECT_EQ(net::ERR_ABORTED,
1077 raw_ptr_resource_handler_->final_status().error());
1078 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size());
1079 }
1080
1081 // This test is broken because ResourceLoader assumes when
1082 // URLRequest::was_pending() is false, canceling the request will not result in
1083 // a completion notification. This isn't the case - whether or not there's a
1084 // notification depends on whether URLRequestJob::NotifyDone() has been invoked
1085 // yet - something the URLRequest doesn't even know about, itself. As a result,
1086 // the ResourceLoader is notified of cancellation twice.
1087 // TODO(mmenke): Fix this.
1088 TEST_F(ResourceLoaderTest, DISABLED_SyncCancelOnReceivedEof) {
1089 raw_ptr_resource_handler_->set_on_on_read_eof_result(false);
1090
1091 loader_->StartRequest();
1092 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1093 base::RunLoop().RunUntilIdle();
1094 EXPECT_EQ(1, did_receive_response_);
1095 EXPECT_EQ(1, did_finish_loading_);
1096 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
1097 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1098
1099 EXPECT_EQ(net::ERR_ABORTED,
1100 raw_ptr_resource_handler_->final_status().error());
1101 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
1102 }
1103
1104 TEST_F(ResourceLoaderTest, AsyncCancelOnWillStart) {
1105 raw_ptr_resource_handler_->set_defer_on_will_start(true);
1106
1107 loader_->StartRequest();
1108 raw_ptr_resource_handler_->WaitUntilDeferred();
1109 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1110 base::RunLoop().RunUntilIdle();
1111 EXPECT_EQ(0, did_start_request_);
1112 EXPECT_EQ(1, did_finish_loading_);
1113 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1114 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1115 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1116 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1117
1118 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1119 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1120 }
1121
1122 TEST_F(ResourceLoaderTest, AsyncCancelOnRequestRedirected) {
1123 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
1124
1125 loader_->StartRequest();
1126 raw_ptr_resource_handler_->WaitUntilDeferred();
1127 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1128 base::RunLoop().RunUntilIdle();
1129 EXPECT_EQ(1, did_received_redirect_);
1130 EXPECT_EQ(0, did_receive_response_);
1131 EXPECT_EQ(1, did_finish_loading_);
1132 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
1133 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
1134 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1135 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1136
1137 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1138 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1139 }
1140
1141 TEST_F(ResourceLoaderTest, AsyncCancelOnResponseStarted) {
1142 raw_ptr_resource_handler_->set_defer_on_response_started(true);
1143
1144 loader_->StartRequest();
1145 raw_ptr_resource_handler_->WaitUntilDeferred();
1146 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1147 base::RunLoop().RunUntilIdle();
1148 EXPECT_EQ(1, did_receive_response_);
1149 EXPECT_EQ(1, did_finish_loading_);
1150 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1151 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
1152 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1153 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1154
1155 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1156 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1157 }
1158
1159 TEST_F(ResourceLoaderTest, AsyncCancelOnReadCompleted) {
1160 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
1161
1162 loader_->StartRequest();
1163 raw_ptr_resource_handler_->WaitUntilDeferred();
1164 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1165 base::RunLoop().RunUntilIdle();
1166 EXPECT_EQ(1, did_receive_response_);
1167 EXPECT_EQ(1, did_finish_loading_);
1168 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
1169 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
1170 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1171
1172 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1173 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size());
1174 }
1175
1176 TEST_F(ResourceLoaderTest, AsyncCancelOnReceivedEof) {
1177 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
1178
1179 loader_->StartRequest();
1180 raw_ptr_resource_handler_->WaitUntilDeferred();
1181 // Have to spin the message loop for the test to pass - see comment on sync
1182 // version of this test for explanation.
1183 // TODO(mmenke): Remove this line once that's fixed.
1184 base::RunLoop().RunUntilIdle();
1185 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1186 base::RunLoop().RunUntilIdle();
1187 EXPECT_EQ(1, did_receive_response_);
1188 EXPECT_EQ(1, did_finish_loading_);
1189 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
1190 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1191
1192 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1193 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
1194 }
1195
1196 TEST_F(ResourceLoaderTest, RequestFailsOnStart) {
1197 SetUpResourceLoaderForUrl(
1198 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1199 net::URLRequestFailedJob::START, net::ERR_FAILED));
1200
1201 loader_->StartRequest();
1202 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1203 base::RunLoop().RunUntilIdle();
1204 EXPECT_EQ(0, did_received_redirect_);
1205 EXPECT_EQ(0, did_receive_response_);
1206 EXPECT_EQ(1, did_finish_loading_);
1207 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1208 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1209 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1210 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1211 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1212 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1213 }
1214
1215 TEST_F(ResourceLoaderTest, RequestFailsOnReadSync) {
1216 SetUpResourceLoaderForUrl(
1217 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1218 net::URLRequestFailedJob::READ_SYNC, net::ERR_FAILED));
1219
1220 loader_->StartRequest();
1221 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1222 base::RunLoop().RunUntilIdle();
1223 EXPECT_EQ(0, did_received_redirect_);
1224 EXPECT_EQ(1, did_receive_response_);
1225 EXPECT_EQ(1, did_finish_loading_);
1226 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1227 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1228 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1229 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1230 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1231 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1232 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1233 }
1234
1235 TEST_F(ResourceLoaderTest, RequestFailsOnReadAsync) {
1236 SetUpResourceLoaderForUrl(
1237 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1238 net::URLRequestFailedJob::READ_ASYNC, net::ERR_FAILED));
1239
1240 loader_->StartRequest();
1241 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1242 base::RunLoop().RunUntilIdle();
1243 EXPECT_EQ(0, did_received_redirect_);
1244 EXPECT_EQ(1, did_receive_response_);
1245 EXPECT_EQ(1, did_finish_loading_);
1246 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1247 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1248 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1249 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1250 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1251 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1252 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1253 }
1254
1255 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringStart) {
1256 SetUpResourceLoaderForUrl(
1257 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1258 net::URLRequestFailedJob::START, net::ERR_IO_PENDING));
1259
1260 loader_->StartRequest();
1261 base::RunLoop().RunUntilIdle();
1262 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1263 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1264 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1265 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1266
1267 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1268 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1269
1270 EXPECT_EQ(0, did_received_redirect_);
1271 EXPECT_EQ(0, did_receive_response_);
1272 EXPECT_EQ(1, did_finish_loading_);
1273 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1274 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1275 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1276 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1277 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1278 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1279 }
1280
1281 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) {
1282 SetUpResourceLoaderForUrl(
1283 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1284 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING));
1285
1286 loader_->StartRequest();
1287 base::RunLoop().RunUntilIdle();
1288 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1289 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1290 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1291 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1292 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1293
1294 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1295 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1296 EXPECT_EQ(0, did_received_redirect_);
1297 EXPECT_EQ(1, did_receive_response_);
1298 EXPECT_EQ(1, did_finish_loading_);
1299 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1300 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1301 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1302 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1303 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1304 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1305 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1306 }
1307
1308 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) {
1309 raw_ptr_resource_handler_->set_defer_on_will_start(true);
855 1310
856 loader_->StartRequest(); 1311 loader_->StartRequest();
857 loader_->CancelRequest(true); 1312 loader_->CancelRequest(true);
858 static_cast<ResourceController*>(loader_.get())->Resume(); 1313 static_cast<ResourceController*>(loader_.get())->Resume();
859 } 1314 }
860 1315
861 // Tests that no invariants are broken if a ResourceHandler cancels during
862 // OnReadCompleted.
863 TEST_F(ResourceLoaderTest, CancelOnReadCompleted) {
864 raw_ptr_resource_handler_->set_cancel_on_read_completed(true);
865
866 loader_->StartRequest();
867 raw_ptr_resource_handler_->WaitForResponseComplete();
868
869 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
870 EXPECT_EQ(net::URLRequestStatus::CANCELED,
871 raw_ptr_resource_handler_->status().status());
872 }
873
874 // Tests that no invariants are broken if a ResourceHandler defers EOF.
875 TEST_F(ResourceLoaderTest, DeferEOF) {
876 raw_ptr_resource_handler_->set_defer_eof(true);
877
878 loader_->StartRequest();
879 raw_ptr_resource_handler_->WaitForDeferredStep();
880
881 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
882 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
883
884 raw_ptr_resource_handler_->Resume();
885 raw_ptr_resource_handler_->WaitForResponseComplete();
886 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
887 raw_ptr_resource_handler_->status().status());
888 }
889
890 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { 1316 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest {
891 public: 1317 public:
892 ResourceLoaderRedirectToFileTest() 1318 ResourceLoaderRedirectToFileTest()
893 : file_stream_(NULL), 1319 : file_stream_(NULL),
894 redirect_to_file_resource_handler_(NULL) { 1320 redirect_to_file_resource_handler_(NULL) {
895 } 1321 }
896 1322
897 ~ResourceLoaderRedirectToFileTest() override { 1323 ~ResourceLoaderRedirectToFileTest() override {
898 // Releasing the loader should result in destroying the file asynchronously. 1324 // Releasing the loader should result in destroying the file asynchronously.
899 file_stream_ = nullptr; 1325 file_stream_ = nullptr;
900 deletable_file_ = nullptr; 1326 deletable_file_ = nullptr;
901 loader_.reset(); 1327 loader_.reset();
902 1328
903 // Wait for the task to delete the file to run, and make sure the file is 1329 // Wait for the task to delete the file to run, and make sure the file is
904 // cleaned up. 1330 // cleaned up.
905 base::RunLoop().RunUntilIdle(); 1331 base::RunLoop().RunUntilIdle();
906 EXPECT_FALSE(base::PathExists(temp_path())); 1332 EXPECT_FALSE(base::PathExists(temp_path()));
907 } 1333 }
908 1334
909 base::FilePath temp_path() const { return temp_path_; } 1335 base::FilePath temp_path() const { return temp_path_; }
910 ShareableFileReference* deletable_file() const { 1336 ShareableFileReference* deletable_file() const {
911 return deletable_file_.get(); 1337 return deletable_file_.get();
912 } 1338 }
913 net::testing::MockFileStream* file_stream() const { return file_stream_; } 1339 net::testing::MockFileStream* file_stream() const { return file_stream_; }
914 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { 1340 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const {
915 return redirect_to_file_resource_handler_; 1341 return redirect_to_file_resource_handler_;
916 } 1342 }
917 1343
918 std::unique_ptr<ResourceHandler> WrapResourceHandler( 1344 std::unique_ptr<ResourceHandler> WrapResourceHandler(
919 std::unique_ptr<ResourceHandlerStub> leaf_handler, 1345 std::unique_ptr<TestResourceHandler> leaf_handler,
920 net::URLRequest* request) override { 1346 net::URLRequest* request) override {
921 leaf_handler->set_expect_reads(false); 1347 leaf_handler->set_expect_on_data_downloaded(true);
922 1348
923 // Make a temporary file. 1349 // Make a temporary file.
924 CHECK(base::CreateTemporaryFile(&temp_path_)); 1350 CHECK(base::CreateTemporaryFile(&temp_path_));
925 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | 1351 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY |
926 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; 1352 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC;
927 base::File file(temp_path_, flags); 1353 base::File file(temp_path_, flags);
928 CHECK(file.IsValid()); 1354 CHECK(file.IsValid());
929 1355
930 // Create mock file streams and a ShareableFileReference. 1356 // Create mock file streams and a ShareableFileReference.
931 std::unique_ptr<net::testing::MockFileStream> file_stream( 1357 std::unique_ptr<net::testing::MockFileStream> file_stream(
(...skipping 29 matching lines...) Expand all
961 // These are owned by the ResourceLoader. 1387 // These are owned by the ResourceLoader.
962 net::testing::MockFileStream* file_stream_; 1388 net::testing::MockFileStream* file_stream_;
963 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; 1389 RedirectToFileResourceHandler* redirect_to_file_resource_handler_;
964 }; 1390 };
965 1391
966 // Tests that a RedirectToFileResourceHandler works and forwards everything 1392 // Tests that a RedirectToFileResourceHandler works and forwards everything
967 // downstream. 1393 // downstream.
968 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { 1394 TEST_F(ResourceLoaderRedirectToFileTest, Basic) {
969 // Run it to completion. 1395 // Run it to completion.
970 loader_->StartRequest(); 1396 loader_->StartRequest();
971 raw_ptr_resource_handler_->WaitForResponseComplete(); 1397 raw_ptr_resource_handler_->WaitUntilResponseComplete();
972 1398
973 // Check that the handler forwarded all information to the downstream handler. 1399 // Check that the handler forwarded all information to the downstream handler.
974 EXPECT_EQ(temp_path(), 1400 EXPECT_EQ(
975 raw_ptr_resource_handler_->response()->head.download_file_path); 1401 temp_path(),
1402 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
976 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1403 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
977 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1404 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
978 raw_ptr_resource_handler_->status().status()); 1405 raw_ptr_resource_handler_->final_status().status());
979 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1406 EXPECT_EQ(test_data().size(), static_cast<size_t>(
980 raw_ptr_resource_handler_->total_bytes_downloaded())); 1407 raw_ptr_resource_handler_->total_bytes_downloaded()));
981 1408
982 // Check that the data was written to the file. 1409 // Check that the data was written to the file.
983 std::string contents; 1410 std::string contents;
984 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1411 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
985 EXPECT_EQ(test_data(), contents); 1412 EXPECT_EQ(test_data(), contents);
986 } 1413 }
987 1414
988 // Tests that RedirectToFileResourceHandler handles errors in creating the 1415 // Tests that RedirectToFileResourceHandler handles errors in creating the
989 // temporary file. 1416 // temporary file.
990 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { 1417 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) {
991 // Swap out the create temporary function. 1418 // Swap out the create temporary function.
992 redirect_to_file_resource_handler()-> 1419 redirect_to_file_resource_handler()->
993 SetCreateTemporaryFileStreamFunctionForTesting( 1420 SetCreateTemporaryFileStreamFunctionForTesting(
994 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); 1421 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED));
995 1422
996 // Run it to completion. 1423 // Run it to completion.
997 loader_->StartRequest(); 1424 loader_->StartRequest();
998 raw_ptr_resource_handler_->WaitForResponseComplete(); 1425 raw_ptr_resource_handler_->WaitUntilResponseComplete();
999 1426
1000 // To downstream, the request was canceled. 1427 // To downstream, the request was canceled.
1001 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1428 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1002 raw_ptr_resource_handler_->status().status()); 1429 raw_ptr_resource_handler_->final_status().status());
1003 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1430 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1004 } 1431 }
1005 1432
1006 // Tests that RedirectToFileResourceHandler handles synchronous write errors. 1433 // Tests that RedirectToFileResourceHandler handles synchronous write errors.
1007 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { 1434 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) {
1008 file_stream()->set_forced_error(net::ERR_FAILED); 1435 file_stream()->set_forced_error(net::ERR_FAILED);
1009 1436
1010 // Run it to completion. 1437 // Run it to completion.
1011 loader_->StartRequest(); 1438 loader_->StartRequest();
1012 raw_ptr_resource_handler_->WaitForResponseComplete(); 1439 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1013 1440
1014 // To downstream, the request was canceled sometime after it started, but 1441 // To downstream, the request was canceled sometime after it started, but
1015 // before any data was written. 1442 // before any data was written.
1016 EXPECT_EQ(temp_path(), 1443 EXPECT_EQ(
1017 raw_ptr_resource_handler_->response()->head.download_file_path); 1444 temp_path(),
1445 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1018 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1446 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1019 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1447 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1020 raw_ptr_resource_handler_->status().status()); 1448 raw_ptr_resource_handler_->final_status().status());
1021 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1449 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1022 } 1450 }
1023 1451
1024 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. 1452 // Tests that RedirectToFileResourceHandler handles asynchronous write errors.
1025 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { 1453 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) {
1026 file_stream()->set_forced_error_async(net::ERR_FAILED); 1454 file_stream()->set_forced_error_async(net::ERR_FAILED);
1027 1455
1028 // Run it to completion. 1456 // Run it to completion.
1029 loader_->StartRequest(); 1457 loader_->StartRequest();
1030 raw_ptr_resource_handler_->WaitForResponseComplete(); 1458 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1031 1459
1032 // To downstream, the request was canceled sometime after it started, but 1460 // To downstream, the request was canceled sometime after it started, but
1033 // before any data was written. 1461 // before any data was written.
1034 EXPECT_EQ(temp_path(), 1462 EXPECT_EQ(
1035 raw_ptr_resource_handler_->response()->head.download_file_path); 1463 temp_path(),
1464 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1036 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1465 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1037 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1466 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1038 raw_ptr_resource_handler_->status().status()); 1467 raw_ptr_resource_handler_->final_status().status());
1039 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1468 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1040 } 1469 }
1041 1470
1042 // Tests that RedirectToFileHandler defers completion if there are outstanding 1471 // Tests that RedirectToFileHandler defers completion if there are outstanding
1043 // writes and accounts for errors which occur in that time. 1472 // writes and accounts for errors which occur in that time.
1044 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { 1473 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) {
1045 // Program the MockFileStream to error asynchronously, but throttle the 1474 // Program the MockFileStream to error asynchronously, but throttle the
1046 // callback. 1475 // callback.
1047 file_stream()->set_forced_error_async(net::ERR_FAILED); 1476 file_stream()->set_forced_error_async(net::ERR_FAILED);
1048 file_stream()->ThrottleCallbacks(); 1477 file_stream()->ThrottleCallbacks();
1049 1478
1050 // Run it as far as it will go. 1479 // Run it as far as it will go.
1051 loader_->StartRequest(); 1480 loader_->StartRequest();
1052 base::RunLoop().RunUntilIdle(); 1481 base::RunLoop().RunUntilIdle();
1053 1482
1054 // At this point, the request should have completed. 1483 // At this point, the request should have completed.
1055 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1484 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1056 raw_ptr_to_request_->status().status()); 1485 raw_ptr_to_request_->status().status());
1057 1486
1058 // However, the resource loader stack is stuck somewhere after receiving the 1487 // However, the resource loader stack is stuck somewhere after receiving the
1059 // response. 1488 // response.
1060 EXPECT_EQ(temp_path(), 1489 EXPECT_EQ(
1061 raw_ptr_resource_handler_->response()->head.download_file_path); 1490 temp_path(),
1491 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1062 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1492 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1063 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1493 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1064 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1494 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1065 1495
1066 // Now, release the floodgates. 1496 // Now, release the floodgates.
1067 file_stream()->ReleaseCallbacks(); 1497 file_stream()->ReleaseCallbacks();
1068 raw_ptr_resource_handler_->WaitForResponseComplete(); 1498 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1069 1499
1070 // Although the URLRequest was successful, the leaf handler sees a failure 1500 // Although the URLRequest was successful, the leaf handler sees a failure
1071 // because the write never completed. 1501 // because the write never completed.
1072 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1502 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1073 raw_ptr_resource_handler_->status().status()); 1503 raw_ptr_resource_handler_->final_status().status());
1074 } 1504 }
1075 1505
1076 // Tests that a RedirectToFileResourceHandler behaves properly when the 1506 // Tests that a RedirectToFileResourceHandler behaves properly when the
1077 // downstream handler defers OnWillStart. 1507 // downstream handler defers OnWillStart.
1078 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { 1508 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) {
1079 // Defer OnWillStart. 1509 // Defer OnWillStart.
1080 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 1510 raw_ptr_resource_handler_->set_defer_on_will_start(true);
1081 1511
1082 // Run as far as we'll go. 1512 // Run as far as we'll go.
1083 loader_->StartRequest(); 1513 loader_->StartRequest();
1084 raw_ptr_resource_handler_->WaitForDeferredStep(); 1514 raw_ptr_resource_handler_->WaitUntilDeferred();
1085 1515
1086 // The request should have stopped at OnWillStart. 1516 // The request should have stopped at OnWillStart.
1087 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1517 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1088 EXPECT_FALSE(raw_ptr_resource_handler_->response()); 1518 EXPECT_FALSE(raw_ptr_resource_handler_->resource_response());
1089 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1519 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1090 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1520 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1091 1521
1092 // Now resume the request. Now we complete. 1522 // Now resume the request. Now we complete.
1093 raw_ptr_resource_handler_->Resume(); 1523 raw_ptr_resource_handler_->Resume();
1094 raw_ptr_resource_handler_->WaitForResponseComplete(); 1524 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1095 1525
1096 // Check that the handler forwarded all information to the downstream handler. 1526 // Check that the handler forwarded all information to the downstream handler.
1097 EXPECT_EQ(temp_path(), 1527 EXPECT_EQ(
1098 raw_ptr_resource_handler_->response()->head.download_file_path); 1528 temp_path(),
1529 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1099 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1530 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1100 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1531 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1101 raw_ptr_resource_handler_->status().status()); 1532 raw_ptr_resource_handler_->final_status().status());
1102 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1533 EXPECT_EQ(test_data().size(), static_cast<size_t>(
1103 raw_ptr_resource_handler_->total_bytes_downloaded())); 1534 raw_ptr_resource_handler_->total_bytes_downloaded()));
1104 1535
1105 // Check that the data was written to the file. 1536 // Check that the data was written to the file.
1106 std::string contents; 1537 std::string contents;
1107 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1538 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
1108 EXPECT_EQ(test_data(), contents); 1539 EXPECT_EQ(test_data(), contents);
1109 } 1540 }
1110 1541
1111 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1542 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1112 public: 1543 public:
1113 void VerifyEffectiveConnectionType( 1544 void VerifyEffectiveConnectionType(
1114 ResourceType resource_type, 1545 ResourceType resource_type,
1115 bool belongs_to_main_frame, 1546 bool belongs_to_main_frame,
1116 net::EffectiveConnectionType set_type, 1547 net::EffectiveConnectionType set_type,
1117 net::EffectiveConnectionType expected_type) { 1548 net::EffectiveConnectionType expected_type) {
1118 network_quality_estimator()->set_effective_connection_type(set_type); 1549 network_quality_estimator()->set_effective_connection_type(set_type);
1119 1550
1120 // Start the request and wait for it to finish. 1551 // Start the request and wait for it to finish.
1121 std::unique_ptr<net::URLRequest> request( 1552 std::unique_ptr<net::URLRequest> request(
1122 resource_context_.GetRequestContext()->CreateRequest( 1553 resource_context_.GetRequestContext()->CreateRequest(
1123 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 1554 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
1124 SetUpResourceLoader(std::move(request), resource_type, 1555 SetUpResourceLoader(std::move(request), resource_type,
1125 belongs_to_main_frame); 1556 belongs_to_main_frame);
1126 1557
1127 // Send the request and wait until it completes. 1558 // Send the request and wait until it completes.
1128 loader_->StartRequest(); 1559 loader_->StartRequest();
1129 raw_ptr_resource_handler_->WaitForResponseComplete(); 1560 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1130 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1561 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1131 raw_ptr_to_request_->status().status()); 1562 raw_ptr_to_request_->status().status());
1132 1563
1133 EXPECT_EQ(expected_type, 1564 EXPECT_EQ(expected_type, raw_ptr_resource_handler_->resource_response()
1134 raw_ptr_resource_handler_->observed_effective_connection_type()); 1565 ->head.effective_connection_type);
1135 } 1566 }
1136 }; 1567 };
1137 1568
1138 // Tests that the effective connection type is set on main frame requests. 1569 // Tests that the effective connection type is set on main frame requests.
1139 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { 1570 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) {
1140 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, 1571 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true,
1141 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, 1572 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
1142 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1573 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1143 } 1574 }
1144 1575
(...skipping 14 matching lines...) Expand all
1159 1590
1160 // Tests that the effective connection type is not set on non-main frame 1591 // Tests that the effective connection type is not set on non-main frame
1161 // requests. 1592 // requests.
1162 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { 1593 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1163 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, 1594 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false,
1164 net::EFFECTIVE_CONNECTION_TYPE_3G, 1595 net::EFFECTIVE_CONNECTION_TYPE_3G,
1165 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1596 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1166 } 1597 }
1167 1598
1168 } // namespace content 1599 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698