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

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

Issue 2543633004: Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: Merge 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 : net::URLRequestTestJob::test_headers(); 260 : net::URLRequestTestJob::test_headers();
259 return new MockHTTPSURLRequestJob(request, network_delegate, headers, 261 return new MockHTTPSURLRequestJob(request, network_delegate, headers,
260 "dummy response", true); 262 "dummy response", true);
261 } 263 }
262 264
263 private: 265 private:
264 bool redirect_; 266 bool redirect_;
265 }; 267 };
266 268
267 // Arbitrary read buffer size. 269 // Arbitrary read buffer size.
268 const int kReadBufSize = 1024;
269
270 // Dummy implementation of ResourceHandler, instance of which is needed to
271 // initialize ResourceLoader.
272 class ResourceHandlerStub : public ResourceHandler {
273 public:
274 explicit ResourceHandlerStub(net::URLRequest* request)
275 : ResourceHandler(request),
276 read_buffer_(new net::IOBuffer(kReadBufSize)),
277 defer_request_on_will_start_(false),
278 expect_reads_(true),
279 cancel_on_read_completed_(false),
280 defer_eof_(false),
281 received_on_will_read_(false),
282 received_eof_(false),
283 received_response_completed_(false),
284 received_request_redirected_(false),
285 total_bytes_downloaded_(0),
286 observed_effective_connection_type_(
287 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {}
288
289 // If true, defers the resource load in OnWillStart.
290 void set_defer_request_on_will_start(bool defer_request_on_will_start) {
291 defer_request_on_will_start_ = defer_request_on_will_start;
292 }
293
294 // If true, expect OnWillRead / OnReadCompleted pairs for handling
295 // data. Otherwise, expect OnDataDownloaded.
296 void set_expect_reads(bool expect_reads) { expect_reads_ = expect_reads; }
297
298 // If true, cancel the request in OnReadCompleted by returning false.
299 void set_cancel_on_read_completed(bool cancel_on_read_completed) {
300 cancel_on_read_completed_ = cancel_on_read_completed;
301 }
302
303 // If true, cancel the request in OnReadCompleted by returning false.
304 void set_defer_eof(bool defer_eof) { defer_eof_ = defer_eof; }
305
306 const GURL& start_url() const { return start_url_; }
307 ResourceResponse* response() const { return response_.get(); }
308 ResourceResponse* redirect_response() const {
309 return redirect_response_.get();
310 }
311 bool received_response_completed() const {
312 return received_response_completed_;
313 }
314 bool received_request_redirected() const {
315 return received_request_redirected_;
316 }
317 const net::URLRequestStatus& status() const { return status_; }
318 int total_bytes_downloaded() const { return total_bytes_downloaded_; }
319
320 net::EffectiveConnectionType observed_effective_connection_type() const {
321 return observed_effective_connection_type_;
322 }
323
324 void Resume() {
325 controller()->Resume();
326 }
327
328 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
329 ResourceResponse* response,
330 bool* defer) override {
331 redirect_response_ = response;
332 received_request_redirected_ = true;
333 return true;
334 }
335
336 bool OnResponseStarted(ResourceResponse* response, bool* defer) override {
337 EXPECT_FALSE(response_.get());
338 response_ = response;
339 observed_effective_connection_type_ =
340 response->head.effective_connection_type;
341 return true;
342 }
343
344 bool OnWillStart(const GURL& url, bool* defer) override {
345 EXPECT_TRUE(start_url_.is_empty());
346 start_url_ = url;
347 if (defer_request_on_will_start_) {
348 *defer = true;
349 deferred_run_loop_.Quit();
350 }
351 return true;
352 }
353
354 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
355 int* buf_size,
356 int min_size) override {
357 EXPECT_TRUE(expect_reads_);
358 EXPECT_FALSE(received_on_will_read_);
359 EXPECT_FALSE(received_eof_);
360 EXPECT_FALSE(received_response_completed_);
361
362 *buf = read_buffer_;
363 *buf_size = kReadBufSize;
364 received_on_will_read_ = true;
365 return true;
366 }
367
368 bool OnReadCompleted(int bytes_read, bool* defer) override {
369 EXPECT_TRUE(received_on_will_read_);
370 EXPECT_TRUE(expect_reads_);
371 EXPECT_FALSE(received_response_completed_);
372
373 if (bytes_read == 0) {
374 received_eof_ = true;
375 if (defer_eof_) {
376 defer_eof_ = false;
377 *defer = true;
378 deferred_run_loop_.Quit();
379 }
380 }
381
382 // Need another OnWillRead() call before seeing an OnReadCompleted().
383 received_on_will_read_ = false;
384
385 return !cancel_on_read_completed_;
386 }
387
388 void OnResponseCompleted(const net::URLRequestStatus& status,
389 bool* defer) override {
390 EXPECT_FALSE(received_response_completed_);
391 if (status.is_success() && expect_reads_)
392 EXPECT_TRUE(received_eof_);
393
394 received_response_completed_ = true;
395 status_ = status;
396 response_completed_run_loop_.Quit();
397 }
398
399 void OnDataDownloaded(int bytes_downloaded) override {
400 EXPECT_FALSE(expect_reads_);
401 total_bytes_downloaded_ += bytes_downloaded;
402 }
403
404 // Waits for the the first deferred step to run, if there is one.
405 void WaitForDeferredStep() {
406 DCHECK(defer_request_on_will_start_ || defer_eof_);
407 deferred_run_loop_.Run();
408 }
409
410 // Waits until the response has completed.
411 void WaitForResponseComplete() {
412 response_completed_run_loop_.Run();
413 EXPECT_TRUE(received_response_completed_);
414 }
415
416 private:
417 scoped_refptr<net::IOBuffer> read_buffer_;
418
419 bool defer_request_on_will_start_;
420 bool expect_reads_;
421 bool cancel_on_read_completed_;
422 bool defer_eof_;
423
424 GURL start_url_;
425 scoped_refptr<ResourceResponse> response_;
426 scoped_refptr<ResourceResponse> redirect_response_;
427 bool received_on_will_read_;
428 bool received_eof_;
429 bool received_response_completed_;
430 bool received_request_redirected_;
431 net::URLRequestStatus status_;
432 int total_bytes_downloaded_;
433 base::RunLoop deferred_run_loop_;
434 base::RunLoop response_completed_run_loop_;
435 std::unique_ptr<base::RunLoop> wait_for_progress_run_loop_;
436 net::EffectiveConnectionType observed_effective_connection_type_;
437 };
438
439 // Test browser client that captures calls to SelectClientCertificates and 270 // Test browser client that captures calls to SelectClientCertificates and
440 // records the arguments of the most recent call for later inspection. 271 // records the arguments of the most recent call for later inspection.
441 class SelectCertificateBrowserClient : public TestContentBrowserClient { 272 class SelectCertificateBrowserClient : public TestContentBrowserClient {
442 public: 273 public:
443 SelectCertificateBrowserClient() : call_count_(0) {} 274 SelectCertificateBrowserClient() : call_count_(0) {}
444 275
445 // Waits until the first call to SelectClientCertificate. 276 // Waits until the first call to SelectClientCertificate.
446 void WaitForSelectCertificate() { 277 void WaitForSelectCertificate() {
447 select_certificate_run_loop_.Run(); 278 select_certificate_run_loop_.Run();
448 // Process any pending messages - just so tests can check if 279 // Process any pending messages - just so tests can check if
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 test_url_request_context_(true), 389 test_url_request_context_(true),
559 resource_context_(&test_url_request_context_), 390 resource_context_(&test_url_request_context_),
560 raw_ptr_resource_handler_(NULL), 391 raw_ptr_resource_handler_(NULL),
561 raw_ptr_to_request_(NULL) { 392 raw_ptr_to_request_(NULL) {
562 test_url_request_context_.set_job_factory(&job_factory_); 393 test_url_request_context_.set_job_factory(&job_factory_);
563 test_url_request_context_.set_network_quality_estimator( 394 test_url_request_context_.set_network_quality_estimator(
564 &network_quality_estimator_); 395 &network_quality_estimator_);
565 test_url_request_context_.Init(); 396 test_url_request_context_.Init();
566 } 397 }
567 398
568 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); } 399 // URL with a response body of test_data().
400 GURL test_direct_url() const { return net::URLRequestTestJob::test_url_1(); }
569 401
570 TestNetworkQualityEstimator* network_quality_estimator() { 402 // URL that redirects to test_direct_url(). The ResourceLoader is set up to
571 return &network_quality_estimator_; 403 // use this URL by default.
404 GURL test_redirect_url() const {
405 return net::URLRequestTestJob::test_url_redirect_to_url_1();
572 } 406 }
573 407
574 std::string test_data() const { 408 std::string test_data() const {
575 return net::URLRequestTestJob::test_data_1(); 409 return net::URLRequestTestJob::test_data_1();
576 } 410 }
577 411
412 TestNetworkQualityEstimator* network_quality_estimator() {
413 return &network_quality_estimator_;
414 }
415
578 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 416 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
579 CreateProtocolHandler() { 417 CreateProtocolHandler() {
580 return net::URLRequestTestJob::CreateProtocolHandler(); 418 return net::URLRequestTestJob::CreateProtocolHandler();
581 } 419 }
582 420
583 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( 421 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler(
584 std::unique_ptr<ResourceHandlerStub> leaf_handler, 422 std::unique_ptr<TestResourceHandler> leaf_handler,
585 net::URLRequest* request) { 423 net::URLRequest* request) {
586 return std::move(leaf_handler); 424 return std::move(leaf_handler);
587 } 425 }
588 426
589 // Replaces loader_ with a new one for |request|. 427 // Replaces loader_ with a new one for |request|.
590 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, 428 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request,
591 ResourceType resource_type, 429 ResourceType resource_type,
592 bool belongs_to_main_frame) { 430 bool belongs_to_main_frame) {
593 raw_ptr_to_request_ = request.get(); 431 raw_ptr_to_request_ = request.get();
594 432
595 // A request marked as a main frame request must also belong to a main 433 // A request marked as a main frame request must also belong to a main
596 // frame. 434 // frame.
597 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || 435 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) ||
598 belongs_to_main_frame); 436 belongs_to_main_frame);
599 437
600 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 438 RenderFrameHost* rfh = web_contents_->GetMainFrame();
601 ResourceRequestInfo::AllocateForTesting( 439 ResourceRequestInfo::AllocateForTesting(
602 request.get(), resource_type, &resource_context_, 440 request.get(), resource_type, &resource_context_,
603 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 441 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
604 rfh->GetRoutingID(), belongs_to_main_frame, 442 rfh->GetRoutingID(), belongs_to_main_frame,
605 false /* parent_is_main_frame */, true /* allow_download */, 443 false /* parent_is_main_frame */, true /* allow_download */,
606 false /* is_async */, false /* is_using_lofi_ */); 444 false /* is_async */, false /* is_using_lofi_ */);
607 std::unique_ptr<ResourceHandlerStub> resource_handler( 445 std::unique_ptr<TestResourceHandler> resource_handler(
608 new ResourceHandlerStub(request.get())); 446 new TestResourceHandler(nullptr, nullptr));
609 raw_ptr_resource_handler_ = resource_handler.get(); 447 raw_ptr_resource_handler_ = resource_handler.get();
610 loader_.reset(new ResourceLoader( 448 loader_.reset(new ResourceLoader(
611 std::move(request), 449 std::move(request),
612 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), 450 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_),
613 this)); 451 this));
614 } 452 }
615 453
454 void SetUpResourceLoaderForUrl(const GURL& test_url) {
455 std::unique_ptr<net::URLRequest> request(
456 resource_context_.GetRequestContext()->CreateRequest(
457 test_url, net::DEFAULT_PRIORITY, nullptr /* delegate */));
458 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true);
459 }
460
616 void SetUp() override { 461 void SetUp() override {
617 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 462 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
463 net::URLRequestFailedJob::AddUrlHandler();
618 464
619 browser_context_.reset(new TestBrowserContext()); 465 browser_context_.reset(new TestBrowserContext());
620 scoped_refptr<SiteInstance> site_instance = 466 scoped_refptr<SiteInstance> site_instance =
621 SiteInstance::Create(browser_context_.get()); 467 SiteInstance::Create(browser_context_.get());
622 web_contents_.reset( 468 web_contents_.reset(
623 TestWebContents::Create(browser_context_.get(), site_instance.get())); 469 TestWebContents::Create(browser_context_.get(), site_instance.get()));
624 470 SetUpResourceLoaderForUrl(test_redirect_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 } 471 }
630 472
631 void TearDown() override { 473 void TearDown() override {
632 // Destroy the WebContents and pump the event loop before destroying 474 // Destroy the WebContents and pump the event loop before destroying
633 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 475 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
634 // tasks complete. 476 // tasks complete.
635 web_contents_.reset(); 477 web_contents_.reset();
478
479 // Clean up handlers.
480 net::URLRequestFilter::GetInstance()->ClearHandlers();
481
636 base::RunLoop().RunUntilIdle(); 482 base::RunLoop().RunUntilIdle();
637 } 483 }
638 484
639 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { 485 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) {
640 dummy_cert_store_ = std::move(store); 486 dummy_cert_store_ = std::move(store);
641 } 487 }
642 488
643 // ResourceLoaderDelegate: 489 // ResourceLoaderDelegate:
644 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 490 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
645 ResourceLoader* loader, 491 ResourceLoader* loader,
646 net::AuthChallengeInfo* auth_info) override { 492 net::AuthChallengeInfo* auth_info) override {
647 return NULL; 493 return NULL;
648 } 494 }
649 bool HandleExternalProtocol(ResourceLoader* loader, 495 bool HandleExternalProtocol(ResourceLoader* loader,
650 const GURL& url) override { 496 const GURL& url) override {
497 EXPECT_EQ(loader, loader_.get());
651 return false; 498 return false;
652 } 499 }
653 void DidStartRequest(ResourceLoader* loader) override {} 500 void DidStartRequest(ResourceLoader* loader) override {
501 EXPECT_EQ(loader, loader_.get());
502 EXPECT_EQ(0, did_finish_loading_);
503 EXPECT_EQ(0, did_start_request_);
504 ++did_start_request_;
505 }
654 void DidReceiveRedirect(ResourceLoader* loader, 506 void DidReceiveRedirect(ResourceLoader* loader,
655 const GURL& new_url, 507 const GURL& new_url,
656 ResourceResponse* response) override {} 508 ResourceResponse* response) override {
657 void DidReceiveResponse(ResourceLoader* loader) override {} 509 EXPECT_EQ(loader, loader_.get());
658 void DidFinishLoading(ResourceLoader* loader) override {} 510 EXPECT_EQ(0, did_finish_loading_);
511 EXPECT_EQ(0, did_receive_response_);
512 EXPECT_EQ(1, did_start_request_);
513 ++did_received_redirect_;
514 }
515 void DidReceiveResponse(ResourceLoader* loader) override {
516 EXPECT_EQ(loader, loader_.get());
517 EXPECT_EQ(0, did_finish_loading_);
518 EXPECT_EQ(0, did_receive_response_);
519 EXPECT_EQ(1, did_start_request_);
520 ++did_receive_response_;
521 }
522 void DidFinishLoading(ResourceLoader* loader) override {
523 EXPECT_EQ(loader, loader_.get());
524 EXPECT_EQ(0, did_finish_loading_);
525 ++did_finish_loading_;
526 }
659 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( 527 std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
660 ResourceLoader* loader) override { 528 ResourceLoader* loader) override {
661 return std::move(dummy_cert_store_); 529 return std::move(dummy_cert_store_);
662 } 530 }
663 531
664 TestBrowserThreadBundle thread_bundle_; 532 TestBrowserThreadBundle thread_bundle_;
665 RenderViewHostTestEnabler rvh_test_enabler_; 533 RenderViewHostTestEnabler rvh_test_enabler_;
666 534
535 // Record which ResourceDispatcherHostDelegate methods have been invoked.
536 int did_start_request_ = 0;
537 int did_received_redirect_ = 0;
538 int did_receive_response_ = 0;
539 int did_finish_loading_ = 0;
540
667 net::URLRequestJobFactoryImpl job_factory_; 541 net::URLRequestJobFactoryImpl job_factory_;
668 TestNetworkQualityEstimator network_quality_estimator_; 542 TestNetworkQualityEstimator network_quality_estimator_;
669 net::TestURLRequestContext test_url_request_context_; 543 net::TestURLRequestContext test_url_request_context_;
670 MockResourceContext resource_context_; 544 MockResourceContext resource_context_;
671 std::unique_ptr<TestBrowserContext> browser_context_; 545 std::unique_ptr<TestBrowserContext> browser_context_;
672 std::unique_ptr<TestWebContents> web_contents_; 546 std::unique_ptr<TestWebContents> web_contents_;
673 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; 547 std::unique_ptr<net::ClientCertStore> dummy_cert_store_;
674 548
675 // The ResourceLoader owns the URLRequest and the ResourceHandler. 549 // The ResourceLoader owns the URLRequest and the ResourceHandler.
676 ResourceHandlerStub* raw_ptr_resource_handler_; 550 TestResourceHandler* raw_ptr_resource_handler_;
677 net::URLRequest* raw_ptr_to_request_; 551 net::URLRequest* raw_ptr_to_request_;
678 std::unique_ptr<ResourceLoader> loader_; 552 std::unique_ptr<ResourceLoader> loader_;
679 }; 553 };
680 554
681 class ClientCertResourceLoaderTest : public ResourceLoaderTest { 555 class ClientCertResourceLoaderTest : public ResourceLoaderTest {
682 protected: 556 protected:
683 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 557 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
684 CreateProtocolHandler() override { 558 CreateProtocolHandler() override {
685 return base::WrapUnique(new MockClientCertJobProtocolHandler); 559 return base::WrapUnique(new MockClientCertJobProtocolHandler);
686 } 560 }
561
562 void SetUp() override {
563 ResourceLoaderTest::SetUp();
564 // These tests don't expect any redirects.
565 SetUpResourceLoaderForUrl(test_direct_url());
566 }
687 }; 567 };
688 568
689 // A ResourceLoaderTest that intercepts https://example.test and 569 // A ResourceLoaderTest that intercepts https://example.test and
690 // https://example-redirect.test URLs and sets SSL info on the 570 // https://example-redirect.test URLs and sets SSL info on the
691 // responses. The latter serves a Location: header in the response. 571 // responses. The latter serves a Location: header in the response.
692 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { 572 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest {
693 public: 573 public:
694 HTTPSSecurityInfoResourceLoaderTest() 574 HTTPSSecurityInfoResourceLoaderTest()
695 : ResourceLoaderTest(), 575 : ResourceLoaderTest(),
696 test_https_url_("https://example.test"), 576 test_https_url_("https://example.test"),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 SetClientCertStore(std::move(test_store)); 613 SetClientCertStore(std::move(test_store));
734 614
735 // Plug in test content browser client. 615 // Plug in test content browser client.
736 SelectCertificateBrowserClient test_client; 616 SelectCertificateBrowserClient test_client;
737 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 617 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
738 618
739 // Start the request and wait for it to pause. 619 // Start the request and wait for it to pause.
740 loader_->StartRequest(); 620 loader_->StartRequest();
741 test_client.WaitForSelectCertificate(); 621 test_client.WaitForSelectCertificate();
742 622
743 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 623 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
744 624
745 // Check if the test store was queried against correct |cert_authorities|. 625 // Check if the test store was queried against correct |cert_authorities|.
746 EXPECT_EQ(1, store_request_count); 626 EXPECT_EQ(1, store_request_count);
747 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), 627 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(),
748 store_requested_authorities); 628 store_requested_authorities);
749 629
750 // Check if the retrieved certificates were passed to the content browser 630 // Check if the retrieved certificates were passed to the content browser
751 // client. 631 // client.
752 EXPECT_EQ(1, test_client.call_count()); 632 EXPECT_EQ(1, test_client.call_count());
753 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 633 EXPECT_EQ(dummy_certs, test_client.passed_certs());
754 634
755 // Continue the request. 635 // Continue the request.
756 test_client.ContinueWithCertificate(nullptr); 636 test_client.ContinueWithCertificate(nullptr);
757 raw_ptr_resource_handler_->WaitForResponseComplete(); 637 raw_ptr_resource_handler_->WaitUntilResponseComplete();
758 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 638 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
759 639
760 // Restore the original content browser client. 640 // Restore the original content browser client.
761 SetBrowserClientForTesting(old_client); 641 SetBrowserClientForTesting(old_client);
762 } 642 }
763 643
764 // Tests that client certificates are requested on a platform with NULL 644 // Tests that client certificates are requested on a platform with NULL
765 // ClientCertStore. 645 // ClientCertStore.
766 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { 646 TEST_F(ClientCertResourceLoaderTest, WithNullStore) {
767 // Plug in test content browser client. 647 // Plug in test content browser client.
768 SelectCertificateBrowserClient test_client; 648 SelectCertificateBrowserClient test_client;
769 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 649 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
770 650
771 // Start the request and wait for it to pause. 651 // Start the request and wait for it to pause.
772 loader_->StartRequest(); 652 loader_->StartRequest();
773 test_client.WaitForSelectCertificate(); 653 test_client.WaitForSelectCertificate();
774 654
775 // Check if the SelectClientCertificate was called on the content browser 655 // Check if the SelectClientCertificate was called on the content browser
776 // client. 656 // client.
777 EXPECT_EQ(1, test_client.call_count()); 657 EXPECT_EQ(1, test_client.call_count());
778 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 658 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
779 659
780 // Continue the request. 660 // Continue the request.
781 test_client.ContinueWithCertificate(nullptr); 661 test_client.ContinueWithCertificate(nullptr);
782 raw_ptr_resource_handler_->WaitForResponseComplete(); 662 raw_ptr_resource_handler_->WaitUntilResponseComplete();
783 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 663 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
784 664
785 // Restore the original content browser client. 665 // Restore the original content browser client.
786 SetBrowserClientForTesting(old_client); 666 SetBrowserClientForTesting(old_client);
787 } 667 }
788 668
789 // Tests that the ContentBrowserClient may cancel a certificate request. 669 // Tests that the ContentBrowserClient may cancel a certificate request.
790 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { 670 TEST_F(ClientCertResourceLoaderTest, CancelSelection) {
791 // Plug in test content browser client. 671 // Plug in test content browser client.
792 SelectCertificateBrowserClient test_client; 672 SelectCertificateBrowserClient test_client;
793 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 673 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
794 674
795 // Start the request and wait for it to pause. 675 // Start the request and wait for it to pause.
796 loader_->StartRequest(); 676 loader_->StartRequest();
797 test_client.WaitForSelectCertificate(); 677 test_client.WaitForSelectCertificate();
798 678
799 // Check if the SelectClientCertificate was called on the content browser 679 // Check if the SelectClientCertificate was called on the content browser
800 // client. 680 // client.
801 EXPECT_EQ(1, test_client.call_count()); 681 EXPECT_EQ(1, test_client.call_count());
802 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 682 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
803 683
804 // Cancel the request. 684 // Cancel the request.
805 test_client.CancelCertificateSelection(); 685 test_client.CancelCertificateSelection();
806 raw_ptr_resource_handler_->WaitForResponseComplete(); 686 raw_ptr_resource_handler_->WaitUntilResponseComplete();
807 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 687 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
808 raw_ptr_resource_handler_->status().error()); 688 raw_ptr_resource_handler_->final_status().error());
809 689
810 // Restore the original content browser client. 690 // Restore the original content browser client.
811 SetBrowserClientForTesting(old_client); 691 SetBrowserClientForTesting(old_client);
812 } 692 }
813 693
814 // Verifies that requests without WebContents attached abort. 694 // Verifies that requests without WebContents attached abort.
815 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { 695 TEST_F(ClientCertResourceLoaderTest, NoWebContents) {
816 // Destroy the WebContents before starting the request. 696 // Destroy the WebContents before starting the request.
817 web_contents_.reset(); 697 web_contents_.reset();
818 698
819 // Plug in test content browser client. 699 // Plug in test content browser client.
820 SelectCertificateBrowserClient test_client; 700 SelectCertificateBrowserClient test_client;
821 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 701 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
822 702
823 // Start the request and wait for it to complete. 703 // Start the request and wait for it to complete.
824 loader_->StartRequest(); 704 loader_->StartRequest();
825 raw_ptr_resource_handler_->WaitForResponseComplete(); 705 raw_ptr_resource_handler_->WaitUntilResponseComplete();
826 706
827 // Check that SelectClientCertificate wasn't called and the request aborted. 707 // Check that SelectClientCertificate wasn't called and the request aborted.
828 EXPECT_EQ(0, test_client.call_count()); 708 EXPECT_EQ(0, test_client.call_count());
829 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 709 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
830 raw_ptr_resource_handler_->status().error()); 710 raw_ptr_resource_handler_->final_status().error());
831 711
832 // Restore the original content browser client. 712 // Restore the original content browser client.
833 SetBrowserClientForTesting(old_client); 713 SetBrowserClientForTesting(old_client);
834 } 714 }
835 715
836 // Verifies that ClientCertStore's callback doesn't crash if called after the 716 // Verifies that ClientCertStore's callback doesn't crash if called after the
837 // loader is destroyed. 717 // loader is destroyed.
838 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { 718 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) {
839 base::RunLoop loader_destroyed_run_loop; 719 base::RunLoop loader_destroyed_run_loop;
840 LoaderDestroyingCertStore* test_store = 720 LoaderDestroyingCertStore* test_store =
841 new LoaderDestroyingCertStore(&loader_, 721 new LoaderDestroyingCertStore(&loader_,
842 loader_destroyed_run_loop.QuitClosure()); 722 loader_destroyed_run_loop.QuitClosure());
843 SetClientCertStore(base::WrapUnique(test_store)); 723 SetClientCertStore(base::WrapUnique(test_store));
844 724
845 loader_->StartRequest(); 725 loader_->StartRequest();
846 loader_destroyed_run_loop.Run(); 726 loader_destroyed_run_loop.Run();
847 EXPECT_FALSE(loader_); 727 EXPECT_FALSE(loader_);
848 728
849 // Pump the event loop to ensure nothing asynchronous crashes either. 729 // Pump the event loop to ensure nothing asynchronous crashes either.
850 base::RunLoop().RunUntilIdle(); 730 base::RunLoop().RunUntilIdle();
851 } 731 }
852 732
853 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { 733 // Test the case the ResourceHandler defers nothing.
854 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 734 TEST_F(ResourceLoaderTest, SyncResourceHandler) {
735 loader_->StartRequest();
736 raw_ptr_resource_handler_->WaitUntilResponseComplete();
737 EXPECT_EQ(1, did_start_request_);
738 EXPECT_EQ(1, did_received_redirect_);
739 EXPECT_EQ(1, did_receive_response_);
740 EXPECT_EQ(1, did_finish_loading_);
741 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
742 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
743 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
744 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
745 }
746
747 // Test the case the ResourceHandler defers everything.
748 TEST_F(ResourceLoaderTest, AsyncResourceHandler) {
749 raw_ptr_resource_handler_->set_defer_on_will_start(true);
750 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
751 raw_ptr_resource_handler_->set_defer_on_response_started(true);
752 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
753 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
754 raw_ptr_resource_handler_->set_defer_on_response_completed(true);
755
756 // Start and run until OnWillStart.
757 loader_->StartRequest();
758 raw_ptr_resource_handler_->WaitUntilDeferred();
759 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
760
761 // Spinning the message loop should not advance the state further.
762 base::RunLoop().RunUntilIdle();
763 EXPECT_EQ(0, did_start_request_);
764 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
765 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
766 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
767
768 // Resume and run until OnRequestRedirected.
769 raw_ptr_resource_handler_->Resume();
770 raw_ptr_resource_handler_->WaitUntilDeferred();
771 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
772
773 // Spinning the message loop should not advance the state further.
774 base::RunLoop().RunUntilIdle();
775 EXPECT_EQ(1, did_received_redirect_);
776 EXPECT_EQ(0, did_receive_response_);
777 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
778 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
779 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
780
781 // Resume and run until OnResponseStarted.
782 raw_ptr_resource_handler_->Resume();
783 raw_ptr_resource_handler_->WaitUntilDeferred();
784 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
785
786 // Spinning the message loop should not advance the state further.
787 base::RunLoop().RunUntilIdle();
788 EXPECT_EQ(1, did_receive_response_);
789 EXPECT_EQ(0, did_finish_loading_);
790 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
791 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
792 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
793
794 // Resume and run until OnReadCompleted.
795 raw_ptr_resource_handler_->Resume();
796 raw_ptr_resource_handler_->WaitUntilDeferred();
797 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
798
799 // Spinning the message loop should not advance the state further.
800 base::RunLoop().RunUntilIdle();
801 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called());
802 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
803 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
804 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
805
806 // Resume and run until the final 0-byte read, signalling EOF.
807 raw_ptr_resource_handler_->Resume();
808 raw_ptr_resource_handler_->WaitUntilDeferred();
809 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
810
811 // Spinning the message loop should not advance the state further.
812 base::RunLoop().RunUntilIdle();
813 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
814 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
815 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
816
817 // Resume and run until OnResponseCompleted is called, which again defers the
818 // request.
819 raw_ptr_resource_handler_->Resume();
820 raw_ptr_resource_handler_->WaitUntilResponseComplete();
821 EXPECT_EQ(0, did_finish_loading_);
822 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
823 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
824 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
825
826 // Resume and run until all pending tasks. Note that OnResponseCompleted was
827 // invoked in the previous section, so can't use RunUntilCompleted().
828 raw_ptr_resource_handler_->Resume();
829 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
830
831 base::RunLoop().RunUntilIdle();
832 EXPECT_EQ(1, did_finish_loading_);
833 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
834 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
835 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
836 }
837
838 TEST_F(ResourceLoaderTest, SyncCancelOnWillStart) {
839 raw_ptr_resource_handler_->set_on_will_start_result(false);
840
841 loader_->StartRequest();
842 raw_ptr_resource_handler_->WaitUntilResponseComplete();
843 base::RunLoop().RunUntilIdle();
844 EXPECT_EQ(0, did_start_request_);
845 EXPECT_EQ(1, did_finish_loading_);
846 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
847 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
848 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
849 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
850
851 EXPECT_EQ(net::ERR_ABORTED,
852 raw_ptr_resource_handler_->final_status().error());
853 EXPECT_EQ("", raw_ptr_resource_handler_->body());
854 }
855
856 TEST_F(ResourceLoaderTest, SyncCancelOnRequestRedirected) {
857 raw_ptr_resource_handler_->set_on_request_redirected_result(false);
858
859 loader_->StartRequest();
860 raw_ptr_resource_handler_->WaitUntilResponseComplete();
861 base::RunLoop().RunUntilIdle();
862 EXPECT_EQ(1, did_received_redirect_);
863 EXPECT_EQ(0, did_receive_response_);
864 EXPECT_EQ(1, did_finish_loading_);
865 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
866 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
867 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
868 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
869
870 EXPECT_EQ(net::ERR_ABORTED,
871 raw_ptr_resource_handler_->final_status().error());
872 EXPECT_EQ("", raw_ptr_resource_handler_->body());
873 }
874
875 TEST_F(ResourceLoaderTest, SyncCancelOnResponseStarted) {
876 raw_ptr_resource_handler_->set_on_response_started_result(false);
877
878 loader_->StartRequest();
879 raw_ptr_resource_handler_->WaitUntilResponseComplete();
880 base::RunLoop().RunUntilIdle();
881 EXPECT_EQ(1, did_receive_response_);
882 EXPECT_EQ(1, did_finish_loading_);
883 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
884 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
885 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
886 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
887
888 EXPECT_EQ(net::ERR_ABORTED,
889 raw_ptr_resource_handler_->final_status().error());
890 EXPECT_EQ("", raw_ptr_resource_handler_->body());
891 }
892
893 TEST_F(ResourceLoaderTest, SyncCancelOnWillRead) {
894 raw_ptr_resource_handler_->set_on_will_read_result(false);
895
896 loader_->StartRequest();
897 raw_ptr_resource_handler_->WaitUntilResponseComplete();
898 base::RunLoop().RunUntilIdle();
899 EXPECT_EQ(1, did_receive_response_);
900 EXPECT_EQ(1, did_finish_loading_);
901 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called());
902 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
903 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
904
905 EXPECT_EQ(net::ERR_ABORTED,
906 raw_ptr_resource_handler_->final_status().error());
907 EXPECT_EQ("", raw_ptr_resource_handler_->body());
908 }
909
910 TEST_F(ResourceLoaderTest, SyncCancelOnReadCompleted) {
911 raw_ptr_resource_handler_->set_on_read_completed_result(false);
912
913 loader_->StartRequest();
914 raw_ptr_resource_handler_->WaitUntilResponseComplete();
915 base::RunLoop().RunUntilIdle();
916 EXPECT_EQ(1, did_receive_response_);
917 EXPECT_EQ(1, did_finish_loading_);
918 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
919 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
920 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
921
922 EXPECT_EQ(net::ERR_ABORTED,
923 raw_ptr_resource_handler_->final_status().error());
924 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size());
925 }
926
927 // This test is broken because ResourceLoader assumes when
928 // URLRequest::was_pending() is false, canceling the request will not result in
929 // a completion notification. This isn't the case - whether or not there's a
930 // notification depends on whether URLRequestJob::NotifyDone() has been invoked
931 // yet - something the URLRequest doesn't even know about, itself. As a result,
932 // the ResourceLoader is notified of cancellation twice.
933 // TODO(mmenke): Fix this.
934 TEST_F(ResourceLoaderTest, DISABLED_SyncCancelOnReceivedEof) {
935 raw_ptr_resource_handler_->set_on_on_read_eof_result(false);
936
937 loader_->StartRequest();
938 raw_ptr_resource_handler_->WaitUntilResponseComplete();
939 base::RunLoop().RunUntilIdle();
940 EXPECT_EQ(1, did_receive_response_);
941 EXPECT_EQ(1, did_finish_loading_);
942 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
943 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
944
945 EXPECT_EQ(net::ERR_ABORTED,
946 raw_ptr_resource_handler_->final_status().error());
947 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
948 }
949
950 TEST_F(ResourceLoaderTest, AsyncCancelOnWillStart) {
951 raw_ptr_resource_handler_->set_defer_on_will_start(true);
952
953 loader_->StartRequest();
954 raw_ptr_resource_handler_->WaitUntilDeferred();
955 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
956 base::RunLoop().RunUntilIdle();
957 EXPECT_EQ(0, did_start_request_);
958 EXPECT_EQ(1, did_finish_loading_);
959 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
960 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
961 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
962 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
963
964 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
965 EXPECT_EQ("", raw_ptr_resource_handler_->body());
966 }
967
968 TEST_F(ResourceLoaderTest, AsyncCancelOnRequestRedirected) {
969 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
970
971 loader_->StartRequest();
972 raw_ptr_resource_handler_->WaitUntilDeferred();
973 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
974 base::RunLoop().RunUntilIdle();
975 EXPECT_EQ(1, did_received_redirect_);
976 EXPECT_EQ(0, did_receive_response_);
977 EXPECT_EQ(1, did_finish_loading_);
978 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
979 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
980 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
981 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
982
983 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
984 EXPECT_EQ("", raw_ptr_resource_handler_->body());
985 }
986
987 TEST_F(ResourceLoaderTest, AsyncCancelOnResponseStarted) {
988 raw_ptr_resource_handler_->set_defer_on_response_started(true);
989
990 loader_->StartRequest();
991 raw_ptr_resource_handler_->WaitUntilDeferred();
992 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
993 base::RunLoop().RunUntilIdle();
994 EXPECT_EQ(1, did_receive_response_);
995 EXPECT_EQ(1, did_finish_loading_);
996 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
997 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
998 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
999 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1000
1001 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1002 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1003 }
1004
1005 TEST_F(ResourceLoaderTest, AsyncCancelOnReadCompleted) {
1006 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
1007
1008 loader_->StartRequest();
1009 raw_ptr_resource_handler_->WaitUntilDeferred();
1010 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1011 base::RunLoop().RunUntilIdle();
1012 EXPECT_EQ(1, did_receive_response_);
1013 EXPECT_EQ(1, did_finish_loading_);
1014 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
1015 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
1016 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1017
1018 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1019 EXPECT_LT(0u, raw_ptr_resource_handler_->body().size());
1020 }
1021
1022 TEST_F(ResourceLoaderTest, AsyncCancelOnReceivedEof) {
1023 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
1024
1025 loader_->StartRequest();
1026 raw_ptr_resource_handler_->WaitUntilDeferred();
1027 // Have to spin the message loop for the test to pass - see comment on sync
1028 // version of this test for explanation.
1029 // TODO(mmenke): Remove this line once that's fixed.
1030 base::RunLoop().RunUntilIdle();
1031 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1032 base::RunLoop().RunUntilIdle();
1033 EXPECT_EQ(1, did_receive_response_);
1034 EXPECT_EQ(1, did_finish_loading_);
1035 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
1036 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1037
1038 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1039 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
1040 }
1041
1042 TEST_F(ResourceLoaderTest, RequestFailsOnStart) {
1043 SetUpResourceLoaderForUrl(
1044 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1045 net::URLRequestFailedJob::START, net::ERR_FAILED));
1046
1047 loader_->StartRequest();
1048 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1049 base::RunLoop().RunUntilIdle();
1050 EXPECT_EQ(0, did_received_redirect_);
1051 EXPECT_EQ(0, did_receive_response_);
1052 EXPECT_EQ(1, did_finish_loading_);
1053 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1054 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1055 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1056 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1057 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1058 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1059 }
1060
1061 TEST_F(ResourceLoaderTest, RequestFailsOnReadSync) {
1062 SetUpResourceLoaderForUrl(
1063 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1064 net::URLRequestFailedJob::READ_SYNC, net::ERR_FAILED));
1065
1066 loader_->StartRequest();
1067 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1068 base::RunLoop().RunUntilIdle();
1069 EXPECT_EQ(0, did_received_redirect_);
1070 EXPECT_EQ(1, did_receive_response_);
1071 EXPECT_EQ(1, did_finish_loading_);
1072 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1073 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1074 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1075 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1076 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1077 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1078 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1079 }
1080
1081 TEST_F(ResourceLoaderTest, RequestFailsOnReadAsync) {
1082 SetUpResourceLoaderForUrl(
1083 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1084 net::URLRequestFailedJob::READ_ASYNC, net::ERR_FAILED));
1085
1086 loader_->StartRequest();
1087 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1088 base::RunLoop().RunUntilIdle();
1089 EXPECT_EQ(0, did_received_redirect_);
1090 EXPECT_EQ(1, did_receive_response_);
1091 EXPECT_EQ(1, did_finish_loading_);
1092 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1093 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1094 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1095 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1096 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1097 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1098 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1099 }
1100
1101 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringStart) {
1102 SetUpResourceLoaderForUrl(
1103 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1104 net::URLRequestFailedJob::START, net::ERR_IO_PENDING));
1105
1106 loader_->StartRequest();
1107 base::RunLoop().RunUntilIdle();
1108 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1109 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1110 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1111 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1112
1113 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1114 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1115
1116 EXPECT_EQ(0, did_received_redirect_);
1117 EXPECT_EQ(0, did_receive_response_);
1118 EXPECT_EQ(1, did_finish_loading_);
1119 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1120 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1121 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1122 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1123 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1124 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1125 }
1126
1127 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) {
1128 SetUpResourceLoaderForUrl(
1129 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1130 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING));
1131
1132 loader_->StartRequest();
1133 base::RunLoop().RunUntilIdle();
1134 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1135 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1136 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1137 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1138 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1139
1140 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1141 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1142 EXPECT_EQ(0, did_received_redirect_);
1143 EXPECT_EQ(1, did_receive_response_);
1144 EXPECT_EQ(1, did_finish_loading_);
1145 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1146 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1147 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1148 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1149 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1150 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1151 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1152 }
1153
1154 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) {
1155 raw_ptr_resource_handler_->set_defer_on_will_start(true);
855 1156
856 loader_->StartRequest(); 1157 loader_->StartRequest();
857 loader_->CancelRequest(true); 1158 loader_->CancelRequest(true);
858 static_cast<ResourceController*>(loader_.get())->Resume(); 1159 static_cast<ResourceController*>(loader_.get())->Resume();
859 } 1160 }
860 1161
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 { 1162 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest {
891 public: 1163 public:
892 ResourceLoaderRedirectToFileTest() 1164 ResourceLoaderRedirectToFileTest()
893 : file_stream_(NULL), 1165 : file_stream_(NULL),
894 redirect_to_file_resource_handler_(NULL) { 1166 redirect_to_file_resource_handler_(NULL) {
895 } 1167 }
896 1168
897 ~ResourceLoaderRedirectToFileTest() override { 1169 ~ResourceLoaderRedirectToFileTest() override {
898 // Releasing the loader should result in destroying the file asynchronously. 1170 // Releasing the loader should result in destroying the file asynchronously.
899 file_stream_ = nullptr; 1171 file_stream_ = nullptr;
900 deletable_file_ = nullptr; 1172 deletable_file_ = nullptr;
901 loader_.reset(); 1173 loader_.reset();
902 1174
903 // Wait for the task to delete the file to run, and make sure the file is 1175 // Wait for the task to delete the file to run, and make sure the file is
904 // cleaned up. 1176 // cleaned up.
905 base::RunLoop().RunUntilIdle(); 1177 base::RunLoop().RunUntilIdle();
906 EXPECT_FALSE(base::PathExists(temp_path())); 1178 EXPECT_FALSE(base::PathExists(temp_path()));
907 } 1179 }
908 1180
909 base::FilePath temp_path() const { return temp_path_; } 1181 base::FilePath temp_path() const { return temp_path_; }
910 ShareableFileReference* deletable_file() const { 1182 ShareableFileReference* deletable_file() const {
911 return deletable_file_.get(); 1183 return deletable_file_.get();
912 } 1184 }
913 net::testing::MockFileStream* file_stream() const { return file_stream_; } 1185 net::testing::MockFileStream* file_stream() const { return file_stream_; }
914 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { 1186 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const {
915 return redirect_to_file_resource_handler_; 1187 return redirect_to_file_resource_handler_;
916 } 1188 }
917 1189
918 std::unique_ptr<ResourceHandler> WrapResourceHandler( 1190 std::unique_ptr<ResourceHandler> WrapResourceHandler(
919 std::unique_ptr<ResourceHandlerStub> leaf_handler, 1191 std::unique_ptr<TestResourceHandler> leaf_handler,
920 net::URLRequest* request) override { 1192 net::URLRequest* request) override {
921 leaf_handler->set_expect_reads(false); 1193 leaf_handler->set_expect_on_data_downloaded(true);
922 1194
923 // Make a temporary file. 1195 // Make a temporary file.
924 CHECK(base::CreateTemporaryFile(&temp_path_)); 1196 CHECK(base::CreateTemporaryFile(&temp_path_));
925 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | 1197 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY |
926 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; 1198 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC;
927 base::File file(temp_path_, flags); 1199 base::File file(temp_path_, flags);
928 CHECK(file.IsValid()); 1200 CHECK(file.IsValid());
929 1201
930 // Create mock file streams and a ShareableFileReference. 1202 // Create mock file streams and a ShareableFileReference.
931 std::unique_ptr<net::testing::MockFileStream> file_stream( 1203 std::unique_ptr<net::testing::MockFileStream> file_stream(
(...skipping 29 matching lines...) Expand all
961 // These are owned by the ResourceLoader. 1233 // These are owned by the ResourceLoader.
962 net::testing::MockFileStream* file_stream_; 1234 net::testing::MockFileStream* file_stream_;
963 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; 1235 RedirectToFileResourceHandler* redirect_to_file_resource_handler_;
964 }; 1236 };
965 1237
966 // Tests that a RedirectToFileResourceHandler works and forwards everything 1238 // Tests that a RedirectToFileResourceHandler works and forwards everything
967 // downstream. 1239 // downstream.
968 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { 1240 TEST_F(ResourceLoaderRedirectToFileTest, Basic) {
969 // Run it to completion. 1241 // Run it to completion.
970 loader_->StartRequest(); 1242 loader_->StartRequest();
971 raw_ptr_resource_handler_->WaitForResponseComplete(); 1243 raw_ptr_resource_handler_->WaitUntilResponseComplete();
972 1244
973 // Check that the handler forwarded all information to the downstream handler. 1245 // Check that the handler forwarded all information to the downstream handler.
974 EXPECT_EQ(temp_path(), 1246 EXPECT_EQ(
975 raw_ptr_resource_handler_->response()->head.download_file_path); 1247 temp_path(),
976 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1248 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1249 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
977 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1250 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
978 raw_ptr_resource_handler_->status().status()); 1251 raw_ptr_resource_handler_->final_status().status());
979 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1252 EXPECT_EQ(test_data().size(), static_cast<size_t>(
980 raw_ptr_resource_handler_->total_bytes_downloaded())); 1253 raw_ptr_resource_handler_->total_bytes_downloaded()));
981 1254
982 // Check that the data was written to the file. 1255 // Check that the data was written to the file.
983 std::string contents; 1256 std::string contents;
984 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1257 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
985 EXPECT_EQ(test_data(), contents); 1258 EXPECT_EQ(test_data(), contents);
986 } 1259 }
987 1260
988 // Tests that RedirectToFileResourceHandler handles errors in creating the 1261 // Tests that RedirectToFileResourceHandler handles errors in creating the
989 // temporary file. 1262 // temporary file.
990 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { 1263 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) {
991 // Swap out the create temporary function. 1264 // Swap out the create temporary function.
992 redirect_to_file_resource_handler()-> 1265 redirect_to_file_resource_handler()->
993 SetCreateTemporaryFileStreamFunctionForTesting( 1266 SetCreateTemporaryFileStreamFunctionForTesting(
994 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); 1267 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED));
995 1268
996 // Run it to completion. 1269 // Run it to completion.
997 loader_->StartRequest(); 1270 loader_->StartRequest();
998 raw_ptr_resource_handler_->WaitForResponseComplete(); 1271 raw_ptr_resource_handler_->WaitUntilResponseComplete();
999 1272
1000 // To downstream, the request was canceled. 1273 // To downstream, the request was canceled.
1001 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1274 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1002 raw_ptr_resource_handler_->status().status()); 1275 raw_ptr_resource_handler_->final_status().status());
1003 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1276 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1004 } 1277 }
1005 1278
1006 // Tests that RedirectToFileResourceHandler handles synchronous write errors. 1279 // Tests that RedirectToFileResourceHandler handles synchronous write errors.
1007 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { 1280 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) {
1008 file_stream()->set_forced_error(net::ERR_FAILED); 1281 file_stream()->set_forced_error(net::ERR_FAILED);
1009 1282
1010 // Run it to completion. 1283 // Run it to completion.
1011 loader_->StartRequest(); 1284 loader_->StartRequest();
1012 raw_ptr_resource_handler_->WaitForResponseComplete(); 1285 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1013 1286
1014 // To downstream, the request was canceled sometime after it started, but 1287 // To downstream, the request was canceled sometime after it started, but
1015 // before any data was written. 1288 // before any data was written.
1016 EXPECT_EQ(temp_path(), 1289 EXPECT_EQ(
1017 raw_ptr_resource_handler_->response()->head.download_file_path); 1290 temp_path(),
1018 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1291 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1292 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1019 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1293 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1020 raw_ptr_resource_handler_->status().status()); 1294 raw_ptr_resource_handler_->final_status().status());
1021 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1295 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1022 } 1296 }
1023 1297
1024 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. 1298 // Tests that RedirectToFileResourceHandler handles asynchronous write errors.
1025 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { 1299 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) {
1026 file_stream()->set_forced_error_async(net::ERR_FAILED); 1300 file_stream()->set_forced_error_async(net::ERR_FAILED);
1027 1301
1028 // Run it to completion. 1302 // Run it to completion.
1029 loader_->StartRequest(); 1303 loader_->StartRequest();
1030 raw_ptr_resource_handler_->WaitForResponseComplete(); 1304 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1031 1305
1032 // To downstream, the request was canceled sometime after it started, but 1306 // To downstream, the request was canceled sometime after it started, but
1033 // before any data was written. 1307 // before any data was written.
1034 EXPECT_EQ(temp_path(), 1308 EXPECT_EQ(
1035 raw_ptr_resource_handler_->response()->head.download_file_path); 1309 temp_path(),
1036 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1310 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1311 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1037 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1312 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1038 raw_ptr_resource_handler_->status().status()); 1313 raw_ptr_resource_handler_->final_status().status());
1039 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1314 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1040 } 1315 }
1041 1316
1042 // Tests that RedirectToFileHandler defers completion if there are outstanding 1317 // Tests that RedirectToFileHandler defers completion if there are outstanding
1043 // writes and accounts for errors which occur in that time. 1318 // writes and accounts for errors which occur in that time.
1044 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { 1319 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) {
1045 // Program the MockFileStream to error asynchronously, but throttle the 1320 // Program the MockFileStream to error asynchronously, but throttle the
1046 // callback. 1321 // callback.
1047 file_stream()->set_forced_error_async(net::ERR_FAILED); 1322 file_stream()->set_forced_error_async(net::ERR_FAILED);
1048 file_stream()->ThrottleCallbacks(); 1323 file_stream()->ThrottleCallbacks();
1049 1324
1050 // Run it as far as it will go. 1325 // Run it as far as it will go.
1051 loader_->StartRequest(); 1326 loader_->StartRequest();
1052 base::RunLoop().RunUntilIdle(); 1327 base::RunLoop().RunUntilIdle();
1053 1328
1054 // At this point, the request should have completed. 1329 // At this point, the request should have completed.
1055 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1330 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1056 raw_ptr_to_request_->status().status()); 1331 raw_ptr_to_request_->status().status());
1057 1332
1058 // However, the resource loader stack is stuck somewhere after receiving the 1333 // However, the resource loader stack is stuck somewhere after receiving the
1059 // response. 1334 // response.
1060 EXPECT_EQ(temp_path(), 1335 EXPECT_EQ(
1061 raw_ptr_resource_handler_->response()->head.download_file_path); 1336 temp_path(),
1062 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1337 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1063 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1338 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1339 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1064 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1340 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1065 1341
1066 // Now, release the floodgates. 1342 // Now, release the floodgates.
1067 file_stream()->ReleaseCallbacks(); 1343 file_stream()->ReleaseCallbacks();
1068 raw_ptr_resource_handler_->WaitForResponseComplete(); 1344 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1069 1345
1070 // Although the URLRequest was successful, the leaf handler sees a failure 1346 // Although the URLRequest was successful, the leaf handler sees a failure
1071 // because the write never completed. 1347 // because the write never completed.
1072 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1348 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1073 raw_ptr_resource_handler_->status().status()); 1349 raw_ptr_resource_handler_->final_status().status());
1074 } 1350 }
1075 1351
1076 // Tests that a RedirectToFileResourceHandler behaves properly when the 1352 // Tests that a RedirectToFileResourceHandler behaves properly when the
1077 // downstream handler defers OnWillStart. 1353 // downstream handler defers OnWillStart.
1078 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { 1354 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) {
1079 // Defer OnWillStart. 1355 // Defer OnWillStart.
1080 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 1356 raw_ptr_resource_handler_->set_defer_on_will_start(true);
1081 1357
1082 // Run as far as we'll go. 1358 // Run as far as we'll go.
1083 loader_->StartRequest(); 1359 loader_->StartRequest();
1084 raw_ptr_resource_handler_->WaitForDeferredStep(); 1360 raw_ptr_resource_handler_->WaitUntilDeferred();
1085 1361
1086 // The request should have stopped at OnWillStart. 1362 // The request should have stopped at OnWillStart.
1087 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1363 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1088 EXPECT_FALSE(raw_ptr_resource_handler_->response()); 1364 EXPECT_FALSE(raw_ptr_resource_handler_->resource_response());
1089 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1365 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1090 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1366 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1091 1367
1092 // Now resume the request. Now we complete. 1368 // Now resume the request. Now we complete.
1093 raw_ptr_resource_handler_->Resume(); 1369 raw_ptr_resource_handler_->Resume();
1094 raw_ptr_resource_handler_->WaitForResponseComplete(); 1370 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1095 1371
1096 // Check that the handler forwarded all information to the downstream handler. 1372 // Check that the handler forwarded all information to the downstream handler.
1097 EXPECT_EQ(temp_path(), 1373 EXPECT_EQ(
1098 raw_ptr_resource_handler_->response()->head.download_file_path); 1374 temp_path(),
1099 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1375 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1376 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1100 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1377 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1101 raw_ptr_resource_handler_->status().status()); 1378 raw_ptr_resource_handler_->final_status().status());
1102 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1379 EXPECT_EQ(test_data().size(), static_cast<size_t>(
1103 raw_ptr_resource_handler_->total_bytes_downloaded())); 1380 raw_ptr_resource_handler_->total_bytes_downloaded()));
1104 1381
1105 // Check that the data was written to the file. 1382 // Check that the data was written to the file.
1106 std::string contents; 1383 std::string contents;
1107 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1384 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
1108 EXPECT_EQ(test_data(), contents); 1385 EXPECT_EQ(test_data(), contents);
1109 } 1386 }
1110 1387
1111 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1388 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1112 public: 1389 public:
1113 void VerifyEffectiveConnectionType( 1390 void VerifyEffectiveConnectionType(
1114 ResourceType resource_type, 1391 ResourceType resource_type,
1115 bool belongs_to_main_frame, 1392 bool belongs_to_main_frame,
1116 net::EffectiveConnectionType set_type, 1393 net::EffectiveConnectionType set_type,
1117 net::EffectiveConnectionType expected_type) { 1394 net::EffectiveConnectionType expected_type) {
1118 network_quality_estimator()->set_effective_connection_type(set_type); 1395 network_quality_estimator()->set_effective_connection_type(set_type);
1119 1396
1120 // Start the request and wait for it to finish. 1397 // Start the request and wait for it to finish.
1121 std::unique_ptr<net::URLRequest> request( 1398 std::unique_ptr<net::URLRequest> request(
1122 resource_context_.GetRequestContext()->CreateRequest( 1399 resource_context_.GetRequestContext()->CreateRequest(
1123 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 1400 test_redirect_url(), net::DEFAULT_PRIORITY,
1401 nullptr /* delegate */));
1124 SetUpResourceLoader(std::move(request), resource_type, 1402 SetUpResourceLoader(std::move(request), resource_type,
1125 belongs_to_main_frame); 1403 belongs_to_main_frame);
1126 1404
1127 // Send the request and wait until it completes. 1405 // Send the request and wait until it completes.
1128 loader_->StartRequest(); 1406 loader_->StartRequest();
1129 raw_ptr_resource_handler_->WaitForResponseComplete(); 1407 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1130 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1408 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1131 raw_ptr_to_request_->status().status()); 1409 raw_ptr_to_request_->status().status());
1132 1410
1133 EXPECT_EQ(expected_type, 1411 EXPECT_EQ(expected_type, raw_ptr_resource_handler_->resource_response()
1134 raw_ptr_resource_handler_->observed_effective_connection_type()); 1412 ->head.effective_connection_type);
1135 } 1413 }
1136 }; 1414 };
1137 1415
1138 // Tests that the effective connection type is set on main frame requests. 1416 // Tests that the effective connection type is set on main frame requests.
1139 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { 1417 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) {
1140 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, 1418 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true,
1141 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, 1419 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
1142 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1420 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1143 } 1421 }
1144 1422
(...skipping 14 matching lines...) Expand all
1159 1437
1160 // Tests that the effective connection type is not set on non-main frame 1438 // Tests that the effective connection type is not set on non-main frame
1161 // requests. 1439 // requests.
1162 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { 1440 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1163 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, 1441 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false,
1164 net::EFFECTIVE_CONNECTION_TYPE_3G, 1442 net::EFFECTIVE_CONNECTION_TYPE_3G,
1165 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1443 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1166 } 1444 }
1167 1445
1168 } // namespace content 1446 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/loader/test_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698