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

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

Issue 2557433005: Revert of Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: 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"
26 #include "content/public/browser/client_certificate_delegate.h" 25 #include "content/public/browser/client_certificate_delegate.h"
27 #include "content/public/browser/resource_request_info.h" 26 #include "content/public/browser/resource_request_info.h"
28 #include "content/public/common/content_paths.h" 27 #include "content/public/common/content_paths.h"
29 #include "content/public/common/resource_response.h" 28 #include "content/public/common/resource_response.h"
30 #include "content/public/common/resource_type.h" 29 #include "content/public/common/resource_type.h"
31 #include "content/public/test/mock_resource_context.h" 30 #include "content/public/test/mock_resource_context.h"
32 #include "content/public/test/test_browser_context.h" 31 #include "content/public/test/test_browser_context.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 32 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "content/public/test/test_renderer_host.h" 33 #include "content/public/test/test_renderer_host.h"
35 #include "content/test/test_content_browser_client.h" 34 #include "content/test/test_content_browser_client.h"
36 #include "content/test/test_web_contents.h" 35 #include "content/test/test_web_contents.h"
37 #include "ipc/ipc_message.h" 36 #include "ipc/ipc_message.h"
38 #include "net/base/chunked_upload_data_stream.h" 37 #include "net/base/chunked_upload_data_stream.h"
39 #include "net/base/io_buffer.h" 38 #include "net/base/io_buffer.h"
40 #include "net/base/mock_file_stream.h" 39 #include "net/base/mock_file_stream.h"
41 #include "net/base/net_errors.h" 40 #include "net/base/net_errors.h"
42 #include "net/base/request_priority.h" 41 #include "net/base/request_priority.h"
43 #include "net/base/upload_bytes_element_reader.h" 42 #include "net/base/upload_bytes_element_reader.h"
44 #include "net/cert/x509_certificate.h" 43 #include "net/cert/x509_certificate.h"
45 #include "net/nqe/effective_connection_type.h" 44 #include "net/nqe/effective_connection_type.h"
46 #include "net/nqe/network_quality_estimator.h" 45 #include "net/nqe/network_quality_estimator.h"
47 #include "net/ssl/client_cert_store.h" 46 #include "net/ssl/client_cert_store.h"
48 #include "net/ssl/ssl_cert_request_info.h" 47 #include "net/ssl/ssl_cert_request_info.h"
49 #include "net/ssl/ssl_private_key.h" 48 #include "net/ssl/ssl_private_key.h"
50 #include "net/test/cert_test_util.h" 49 #include "net/test/cert_test_util.h"
51 #include "net/test/embedded_test_server/embedded_test_server.h" 50 #include "net/test/embedded_test_server/embedded_test_server.h"
52 #include "net/test/test_data_directory.h" 51 #include "net/test/test_data_directory.h"
53 #include "net/test/url_request/url_request_failed_job.h"
54 #include "net/url_request/url_request.h" 52 #include "net/url_request/url_request.h"
55 #include "net/url_request/url_request_filter.h" 53 #include "net/url_request/url_request_filter.h"
56 #include "net/url_request/url_request_interceptor.h" 54 #include "net/url_request/url_request_interceptor.h"
57 #include "net/url_request/url_request_job_factory.h" 55 #include "net/url_request/url_request_job_factory.h"
58 #include "net/url_request/url_request_job_factory_impl.h" 56 #include "net/url_request/url_request_job_factory_impl.h"
59 #include "net/url_request/url_request_test_job.h" 57 #include "net/url_request/url_request_test_job.h"
60 #include "net/url_request/url_request_test_util.h" 58 #include "net/url_request/url_request_test_util.h"
61 #include "storage/browser/blob/shareable_file_reference.h" 59 #include "storage/browser/blob/shareable_file_reference.h"
62 #include "testing/gtest/include/gtest/gtest.h" 60 #include "testing/gtest/include/gtest/gtest.h"
63 61
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 : net::URLRequestTestJob::test_headers(); 258 : net::URLRequestTestJob::test_headers();
261 return new MockHTTPSURLRequestJob(request, network_delegate, headers, 259 return new MockHTTPSURLRequestJob(request, network_delegate, headers,
262 "dummy response", true); 260 "dummy response", true);
263 } 261 }
264 262
265 private: 263 private:
266 bool redirect_; 264 bool redirect_;
267 }; 265 };
268 266
269 // Arbitrary read buffer size. 267 // 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
270 // Test browser client that captures calls to SelectClientCertificates and 439 // Test browser client that captures calls to SelectClientCertificates and
271 // records the arguments of the most recent call for later inspection. 440 // records the arguments of the most recent call for later inspection.
272 class SelectCertificateBrowserClient : public TestContentBrowserClient { 441 class SelectCertificateBrowserClient : public TestContentBrowserClient {
273 public: 442 public:
274 SelectCertificateBrowserClient() : call_count_(0) {} 443 SelectCertificateBrowserClient() : call_count_(0) {}
275 444
276 // Waits until the first call to SelectClientCertificate. 445 // Waits until the first call to SelectClientCertificate.
277 void WaitForSelectCertificate() { 446 void WaitForSelectCertificate() {
278 select_certificate_run_loop_.Run(); 447 select_certificate_run_loop_.Run();
279 // Process any pending messages - just so tests can check if 448 // Process any pending messages - just so tests can check if
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 test_url_request_context_(true), 558 test_url_request_context_(true),
390 resource_context_(&test_url_request_context_), 559 resource_context_(&test_url_request_context_),
391 raw_ptr_resource_handler_(NULL), 560 raw_ptr_resource_handler_(NULL),
392 raw_ptr_to_request_(NULL) { 561 raw_ptr_to_request_(NULL) {
393 test_url_request_context_.set_job_factory(&job_factory_); 562 test_url_request_context_.set_job_factory(&job_factory_);
394 test_url_request_context_.set_network_quality_estimator( 563 test_url_request_context_.set_network_quality_estimator(
395 &network_quality_estimator_); 564 &network_quality_estimator_);
396 test_url_request_context_.Init(); 565 test_url_request_context_.Init();
397 } 566 }
398 567
399 // URL with a response body of test_data(). 568 GURL test_url() const { return net::URLRequestTestJob::test_url_1(); }
400 GURL test_direct_url() const { return net::URLRequestTestJob::test_url_1(); }
401 569
402 // URL that redirects to test_direct_url(). The ResourceLoader is set up to 570 TestNetworkQualityEstimator* network_quality_estimator() {
403 // use this URL by default. 571 return &network_quality_estimator_;
404 GURL test_redirect_url() const {
405 return net::URLRequestTestJob::test_url_redirect_to_url_1();
406 } 572 }
407 573
408 std::string test_data() const { 574 std::string test_data() const {
409 return net::URLRequestTestJob::test_data_1(); 575 return net::URLRequestTestJob::test_data_1();
410 } 576 }
411 577
412 TestNetworkQualityEstimator* network_quality_estimator() {
413 return &network_quality_estimator_;
414 }
415
416 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 578 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
417 CreateProtocolHandler() { 579 CreateProtocolHandler() {
418 return net::URLRequestTestJob::CreateProtocolHandler(); 580 return net::URLRequestTestJob::CreateProtocolHandler();
419 } 581 }
420 582
421 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( 583 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler(
422 std::unique_ptr<TestResourceHandler> leaf_handler, 584 std::unique_ptr<ResourceHandlerStub> leaf_handler,
423 net::URLRequest* request) { 585 net::URLRequest* request) {
424 return std::move(leaf_handler); 586 return std::move(leaf_handler);
425 } 587 }
426 588
427 // Replaces loader_ with a new one for |request|. 589 // Replaces loader_ with a new one for |request|.
428 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, 590 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request,
429 ResourceType resource_type, 591 ResourceType resource_type,
430 bool belongs_to_main_frame) { 592 bool belongs_to_main_frame) {
431 raw_ptr_to_request_ = request.get(); 593 raw_ptr_to_request_ = request.get();
432 594
433 // A request marked as a main frame request must also belong to a main 595 // A request marked as a main frame request must also belong to a main
434 // frame. 596 // frame.
435 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || 597 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) ||
436 belongs_to_main_frame); 598 belongs_to_main_frame);
437 599
438 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 600 RenderFrameHost* rfh = web_contents_->GetMainFrame();
439 ResourceRequestInfo::AllocateForTesting( 601 ResourceRequestInfo::AllocateForTesting(
440 request.get(), resource_type, &resource_context_, 602 request.get(), resource_type, &resource_context_,
441 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 603 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
442 rfh->GetRoutingID(), belongs_to_main_frame, 604 rfh->GetRoutingID(), belongs_to_main_frame,
443 false /* parent_is_main_frame */, true /* allow_download */, 605 false /* parent_is_main_frame */, true /* allow_download */,
444 false /* is_async */, false /* is_using_lofi_ */); 606 false /* is_async */, false /* is_using_lofi_ */);
445 std::unique_ptr<TestResourceHandler> resource_handler( 607 std::unique_ptr<ResourceHandlerStub> resource_handler(
446 new TestResourceHandler(nullptr, nullptr)); 608 new ResourceHandlerStub(request.get()));
447 raw_ptr_resource_handler_ = resource_handler.get(); 609 raw_ptr_resource_handler_ = resource_handler.get();
448 loader_.reset(new ResourceLoader( 610 loader_.reset(new ResourceLoader(
449 std::move(request), 611 std::move(request),
450 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), 612 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_),
451 this)); 613 this));
452 } 614 }
453 615
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
461 void SetUp() override { 616 void SetUp() override {
462 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 617 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
463 net::URLRequestFailedJob::AddUrlHandler();
464 618
465 browser_context_.reset(new TestBrowserContext()); 619 browser_context_.reset(new TestBrowserContext());
466 scoped_refptr<SiteInstance> site_instance = 620 scoped_refptr<SiteInstance> site_instance =
467 SiteInstance::Create(browser_context_.get()); 621 SiteInstance::Create(browser_context_.get());
468 web_contents_.reset( 622 web_contents_.reset(
469 TestWebContents::Create(browser_context_.get(), site_instance.get())); 623 TestWebContents::Create(browser_context_.get(), site_instance.get()));
470 SetUpResourceLoaderForUrl(test_redirect_url()); 624
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);
471 } 629 }
472 630
473 void TearDown() override { 631 void TearDown() override {
474 // Destroy the WebContents and pump the event loop before destroying 632 // Destroy the WebContents and pump the event loop before destroying
475 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 633 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
476 // tasks complete. 634 // tasks complete.
477 web_contents_.reset(); 635 web_contents_.reset();
478
479 // Clean up handlers.
480 net::URLRequestFilter::GetInstance()->ClearHandlers();
481
482 base::RunLoop().RunUntilIdle(); 636 base::RunLoop().RunUntilIdle();
483 } 637 }
484 638
485 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { 639 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) {
486 dummy_cert_store_ = std::move(store); 640 dummy_cert_store_ = std::move(store);
487 } 641 }
488 642
489 // ResourceLoaderDelegate: 643 // ResourceLoaderDelegate:
490 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 644 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
491 ResourceLoader* loader, 645 ResourceLoader* loader,
492 net::AuthChallengeInfo* auth_info) override { 646 net::AuthChallengeInfo* auth_info) override {
493 return NULL; 647 return NULL;
494 } 648 }
495 bool HandleExternalProtocol(ResourceLoader* loader, 649 bool HandleExternalProtocol(ResourceLoader* loader,
496 const GURL& url) override { 650 const GURL& url) override {
497 EXPECT_EQ(loader, loader_.get());
498 return false; 651 return false;
499 } 652 }
500 void DidStartRequest(ResourceLoader* loader) override { 653 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 }
506 void DidReceiveRedirect(ResourceLoader* loader, 654 void DidReceiveRedirect(ResourceLoader* loader,
507 const GURL& new_url, 655 const GURL& new_url,
508 ResourceResponse* response) override { 656 ResourceResponse* response) override {}
509 EXPECT_EQ(loader, loader_.get()); 657 void DidReceiveResponse(ResourceLoader* loader) override {}
510 EXPECT_EQ(0, did_finish_loading_); 658 void DidFinishLoading(ResourceLoader* loader) override {}
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 }
527 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( 659 std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
528 ResourceLoader* loader) override { 660 ResourceLoader* loader) override {
529 return std::move(dummy_cert_store_); 661 return std::move(dummy_cert_store_);
530 } 662 }
531 663
532 TestBrowserThreadBundle thread_bundle_; 664 TestBrowserThreadBundle thread_bundle_;
533 RenderViewHostTestEnabler rvh_test_enabler_; 665 RenderViewHostTestEnabler rvh_test_enabler_;
534 666
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
541 net::URLRequestJobFactoryImpl job_factory_; 667 net::URLRequestJobFactoryImpl job_factory_;
542 TestNetworkQualityEstimator network_quality_estimator_; 668 TestNetworkQualityEstimator network_quality_estimator_;
543 net::TestURLRequestContext test_url_request_context_; 669 net::TestURLRequestContext test_url_request_context_;
544 MockResourceContext resource_context_; 670 MockResourceContext resource_context_;
545 std::unique_ptr<TestBrowserContext> browser_context_; 671 std::unique_ptr<TestBrowserContext> browser_context_;
546 std::unique_ptr<TestWebContents> web_contents_; 672 std::unique_ptr<TestWebContents> web_contents_;
547 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; 673 std::unique_ptr<net::ClientCertStore> dummy_cert_store_;
548 674
549 // The ResourceLoader owns the URLRequest and the ResourceHandler. 675 // The ResourceLoader owns the URLRequest and the ResourceHandler.
550 TestResourceHandler* raw_ptr_resource_handler_; 676 ResourceHandlerStub* raw_ptr_resource_handler_;
551 net::URLRequest* raw_ptr_to_request_; 677 net::URLRequest* raw_ptr_to_request_;
552 std::unique_ptr<ResourceLoader> loader_; 678 std::unique_ptr<ResourceLoader> loader_;
553 }; 679 };
554 680
555 class ClientCertResourceLoaderTest : public ResourceLoaderTest { 681 class ClientCertResourceLoaderTest : public ResourceLoaderTest {
556 protected: 682 protected:
557 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 683 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
558 CreateProtocolHandler() override { 684 CreateProtocolHandler() override {
559 return base::WrapUnique(new MockClientCertJobProtocolHandler); 685 return base::WrapUnique(new MockClientCertJobProtocolHandler);
560 } 686 }
561
562 void SetUp() override {
563 ResourceLoaderTest::SetUp();
564 // These tests don't expect any redirects.
565 SetUpResourceLoaderForUrl(test_direct_url());
566 }
567 }; 687 };
568 688
569 // A ResourceLoaderTest that intercepts https://example.test and 689 // A ResourceLoaderTest that intercepts https://example.test and
570 // https://example-redirect.test URLs and sets SSL info on the 690 // https://example-redirect.test URLs and sets SSL info on the
571 // responses. The latter serves a Location: header in the response. 691 // responses. The latter serves a Location: header in the response.
572 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { 692 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest {
573 public: 693 public:
574 HTTPSSecurityInfoResourceLoaderTest() 694 HTTPSSecurityInfoResourceLoaderTest()
575 : ResourceLoaderTest(), 695 : ResourceLoaderTest(),
576 test_https_url_("https://example.test"), 696 test_https_url_("https://example.test"),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 SetClientCertStore(std::move(test_store)); 733 SetClientCertStore(std::move(test_store));
614 734
615 // Plug in test content browser client. 735 // Plug in test content browser client.
616 SelectCertificateBrowserClient test_client; 736 SelectCertificateBrowserClient test_client;
617 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 737 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
618 738
619 // Start the request and wait for it to pause. 739 // Start the request and wait for it to pause.
620 loader_->StartRequest(); 740 loader_->StartRequest();
621 test_client.WaitForSelectCertificate(); 741 test_client.WaitForSelectCertificate();
622 742
623 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); 743 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
624 744
625 // Check if the test store was queried against correct |cert_authorities|. 745 // Check if the test store was queried against correct |cert_authorities|.
626 EXPECT_EQ(1, store_request_count); 746 EXPECT_EQ(1, store_request_count);
627 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), 747 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(),
628 store_requested_authorities); 748 store_requested_authorities);
629 749
630 // Check if the retrieved certificates were passed to the content browser 750 // Check if the retrieved certificates were passed to the content browser
631 // client. 751 // client.
632 EXPECT_EQ(1, test_client.call_count()); 752 EXPECT_EQ(1, test_client.call_count());
633 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 753 EXPECT_EQ(dummy_certs, test_client.passed_certs());
634 754
635 // Continue the request. 755 // Continue the request.
636 test_client.ContinueWithCertificate(nullptr); 756 test_client.ContinueWithCertificate(nullptr);
637 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 757 raw_ptr_resource_handler_->WaitForResponseComplete();
638 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); 758 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error());
639 759
640 // Restore the original content browser client. 760 // Restore the original content browser client.
641 SetBrowserClientForTesting(old_client); 761 SetBrowserClientForTesting(old_client);
642 } 762 }
643 763
644 // Tests that client certificates are requested on a platform with NULL 764 // Tests that client certificates are requested on a platform with NULL
645 // ClientCertStore. 765 // ClientCertStore.
646 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { 766 TEST_F(ClientCertResourceLoaderTest, WithNullStore) {
647 // Plug in test content browser client. 767 // Plug in test content browser client.
648 SelectCertificateBrowserClient test_client; 768 SelectCertificateBrowserClient test_client;
649 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 769 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
650 770
651 // Start the request and wait for it to pause. 771 // Start the request and wait for it to pause.
652 loader_->StartRequest(); 772 loader_->StartRequest();
653 test_client.WaitForSelectCertificate(); 773 test_client.WaitForSelectCertificate();
654 774
655 // Check if the SelectClientCertificate was called on the content browser 775 // Check if the SelectClientCertificate was called on the content browser
656 // client. 776 // client.
657 EXPECT_EQ(1, test_client.call_count()); 777 EXPECT_EQ(1, test_client.call_count());
658 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 778 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
659 779
660 // Continue the request. 780 // Continue the request.
661 test_client.ContinueWithCertificate(nullptr); 781 test_client.ContinueWithCertificate(nullptr);
662 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 782 raw_ptr_resource_handler_->WaitForResponseComplete();
663 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error()); 783 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error());
664 784
665 // Restore the original content browser client. 785 // Restore the original content browser client.
666 SetBrowserClientForTesting(old_client); 786 SetBrowserClientForTesting(old_client);
667 } 787 }
668 788
669 // Tests that the ContentBrowserClient may cancel a certificate request. 789 // Tests that the ContentBrowserClient may cancel a certificate request.
670 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { 790 TEST_F(ClientCertResourceLoaderTest, CancelSelection) {
671 // Plug in test content browser client. 791 // Plug in test content browser client.
672 SelectCertificateBrowserClient test_client; 792 SelectCertificateBrowserClient test_client;
673 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 793 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
674 794
675 // Start the request and wait for it to pause. 795 // Start the request and wait for it to pause.
676 loader_->StartRequest(); 796 loader_->StartRequest();
677 test_client.WaitForSelectCertificate(); 797 test_client.WaitForSelectCertificate();
678 798
679 // Check if the SelectClientCertificate was called on the content browser 799 // Check if the SelectClientCertificate was called on the content browser
680 // client. 800 // client.
681 EXPECT_EQ(1, test_client.call_count()); 801 EXPECT_EQ(1, test_client.call_count());
682 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 802 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
683 803
684 // Cancel the request. 804 // Cancel the request.
685 test_client.CancelCertificateSelection(); 805 test_client.CancelCertificateSelection();
686 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 806 raw_ptr_resource_handler_->WaitForResponseComplete();
687 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 807 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
688 raw_ptr_resource_handler_->final_status().error()); 808 raw_ptr_resource_handler_->status().error());
689 809
690 // Restore the original content browser client. 810 // Restore the original content browser client.
691 SetBrowserClientForTesting(old_client); 811 SetBrowserClientForTesting(old_client);
692 } 812 }
693 813
694 // Verifies that requests without WebContents attached abort. 814 // Verifies that requests without WebContents attached abort.
695 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { 815 TEST_F(ClientCertResourceLoaderTest, NoWebContents) {
696 // Destroy the WebContents before starting the request. 816 // Destroy the WebContents before starting the request.
697 web_contents_.reset(); 817 web_contents_.reset();
698 818
699 // Plug in test content browser client. 819 // Plug in test content browser client.
700 SelectCertificateBrowserClient test_client; 820 SelectCertificateBrowserClient test_client;
701 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 821 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
702 822
703 // Start the request and wait for it to complete. 823 // Start the request and wait for it to complete.
704 loader_->StartRequest(); 824 loader_->StartRequest();
705 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 825 raw_ptr_resource_handler_->WaitForResponseComplete();
706 826
707 // Check that SelectClientCertificate wasn't called and the request aborted. 827 // Check that SelectClientCertificate wasn't called and the request aborted.
708 EXPECT_EQ(0, test_client.call_count()); 828 EXPECT_EQ(0, test_client.call_count());
709 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 829 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
710 raw_ptr_resource_handler_->final_status().error()); 830 raw_ptr_resource_handler_->status().error());
711 831
712 // Restore the original content browser client. 832 // Restore the original content browser client.
713 SetBrowserClientForTesting(old_client); 833 SetBrowserClientForTesting(old_client);
714 } 834 }
715 835
716 // Verifies that ClientCertStore's callback doesn't crash if called after the 836 // Verifies that ClientCertStore's callback doesn't crash if called after the
717 // loader is destroyed. 837 // loader is destroyed.
718 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { 838 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) {
719 base::RunLoop loader_destroyed_run_loop; 839 base::RunLoop loader_destroyed_run_loop;
720 LoaderDestroyingCertStore* test_store = 840 LoaderDestroyingCertStore* test_store =
721 new LoaderDestroyingCertStore(&loader_, 841 new LoaderDestroyingCertStore(&loader_,
722 loader_destroyed_run_loop.QuitClosure()); 842 loader_destroyed_run_loop.QuitClosure());
723 SetClientCertStore(base::WrapUnique(test_store)); 843 SetClientCertStore(base::WrapUnique(test_store));
724 844
725 loader_->StartRequest(); 845 loader_->StartRequest();
726 loader_destroyed_run_loop.Run(); 846 loader_destroyed_run_loop.Run();
727 EXPECT_FALSE(loader_); 847 EXPECT_FALSE(loader_);
728 848
729 // Pump the event loop to ensure nothing asynchronous crashes either. 849 // Pump the event loop to ensure nothing asynchronous crashes either.
730 base::RunLoop().RunUntilIdle(); 850 base::RunLoop().RunUntilIdle();
731 } 851 }
732 852
733 // Test the case the ResourceHandler defers nothing. 853 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) {
734 TEST_F(ResourceLoaderTest, SyncResourceHandler) { 854 raw_ptr_resource_handler_->set_defer_request_on_will_start(true);
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);
1156 855
1157 loader_->StartRequest(); 856 loader_->StartRequest();
1158 loader_->CancelRequest(true); 857 loader_->CancelRequest(true);
1159 static_cast<ResourceController*>(loader_.get())->Resume(); 858 static_cast<ResourceController*>(loader_.get())->Resume();
1160 } 859 }
1161 860
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
1162 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest { 890 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest {
1163 public: 891 public:
1164 ResourceLoaderRedirectToFileTest() 892 ResourceLoaderRedirectToFileTest()
1165 : file_stream_(NULL), 893 : file_stream_(NULL),
1166 redirect_to_file_resource_handler_(NULL) { 894 redirect_to_file_resource_handler_(NULL) {
1167 } 895 }
1168 896
1169 ~ResourceLoaderRedirectToFileTest() override { 897 ~ResourceLoaderRedirectToFileTest() override {
1170 // Releasing the loader should result in destroying the file asynchronously. 898 // Releasing the loader should result in destroying the file asynchronously.
1171 file_stream_ = nullptr; 899 file_stream_ = nullptr;
1172 deletable_file_ = nullptr; 900 deletable_file_ = nullptr;
1173 loader_.reset(); 901 loader_.reset();
1174 902
1175 // Wait for the task to delete the file to run, and make sure the file is 903 // Wait for the task to delete the file to run, and make sure the file is
1176 // cleaned up. 904 // cleaned up.
1177 base::RunLoop().RunUntilIdle(); 905 base::RunLoop().RunUntilIdle();
1178 EXPECT_FALSE(base::PathExists(temp_path())); 906 EXPECT_FALSE(base::PathExists(temp_path()));
1179 } 907 }
1180 908
1181 base::FilePath temp_path() const { return temp_path_; } 909 base::FilePath temp_path() const { return temp_path_; }
1182 ShareableFileReference* deletable_file() const { 910 ShareableFileReference* deletable_file() const {
1183 return deletable_file_.get(); 911 return deletable_file_.get();
1184 } 912 }
1185 net::testing::MockFileStream* file_stream() const { return file_stream_; } 913 net::testing::MockFileStream* file_stream() const { return file_stream_; }
1186 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { 914 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const {
1187 return redirect_to_file_resource_handler_; 915 return redirect_to_file_resource_handler_;
1188 } 916 }
1189 917
1190 std::unique_ptr<ResourceHandler> WrapResourceHandler( 918 std::unique_ptr<ResourceHandler> WrapResourceHandler(
1191 std::unique_ptr<TestResourceHandler> leaf_handler, 919 std::unique_ptr<ResourceHandlerStub> leaf_handler,
1192 net::URLRequest* request) override { 920 net::URLRequest* request) override {
1193 leaf_handler->set_expect_on_data_downloaded(true); 921 leaf_handler->set_expect_reads(false);
1194 922
1195 // Make a temporary file. 923 // Make a temporary file.
1196 CHECK(base::CreateTemporaryFile(&temp_path_)); 924 CHECK(base::CreateTemporaryFile(&temp_path_));
1197 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | 925 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY |
1198 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; 926 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC;
1199 base::File file(temp_path_, flags); 927 base::File file(temp_path_, flags);
1200 CHECK(file.IsValid()); 928 CHECK(file.IsValid());
1201 929
1202 // Create mock file streams and a ShareableFileReference. 930 // Create mock file streams and a ShareableFileReference.
1203 std::unique_ptr<net::testing::MockFileStream> file_stream( 931 std::unique_ptr<net::testing::MockFileStream> file_stream(
(...skipping 29 matching lines...) Expand all
1233 // These are owned by the ResourceLoader. 961 // These are owned by the ResourceLoader.
1234 net::testing::MockFileStream* file_stream_; 962 net::testing::MockFileStream* file_stream_;
1235 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; 963 RedirectToFileResourceHandler* redirect_to_file_resource_handler_;
1236 }; 964 };
1237 965
1238 // Tests that a RedirectToFileResourceHandler works and forwards everything 966 // Tests that a RedirectToFileResourceHandler works and forwards everything
1239 // downstream. 967 // downstream.
1240 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { 968 TEST_F(ResourceLoaderRedirectToFileTest, Basic) {
1241 // Run it to completion. 969 // Run it to completion.
1242 loader_->StartRequest(); 970 loader_->StartRequest();
1243 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 971 raw_ptr_resource_handler_->WaitForResponseComplete();
1244 972
1245 // Check that the handler forwarded all information to the downstream handler. 973 // Check that the handler forwarded all information to the downstream handler.
1246 EXPECT_EQ( 974 EXPECT_EQ(temp_path(),
1247 temp_path(), 975 raw_ptr_resource_handler_->response()->head.download_file_path);
1248 raw_ptr_resource_handler_->resource_response()->head.download_file_path); 976 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1249 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1250 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 977 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1251 raw_ptr_resource_handler_->final_status().status()); 978 raw_ptr_resource_handler_->status().status());
1252 EXPECT_EQ(test_data().size(), static_cast<size_t>( 979 EXPECT_EQ(test_data().size(), static_cast<size_t>(
1253 raw_ptr_resource_handler_->total_bytes_downloaded())); 980 raw_ptr_resource_handler_->total_bytes_downloaded()));
1254 981
1255 // Check that the data was written to the file. 982 // Check that the data was written to the file.
1256 std::string contents; 983 std::string contents;
1257 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 984 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
1258 EXPECT_EQ(test_data(), contents); 985 EXPECT_EQ(test_data(), contents);
1259 } 986 }
1260 987
1261 // Tests that RedirectToFileResourceHandler handles errors in creating the 988 // Tests that RedirectToFileResourceHandler handles errors in creating the
1262 // temporary file. 989 // temporary file.
1263 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { 990 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) {
1264 // Swap out the create temporary function. 991 // Swap out the create temporary function.
1265 redirect_to_file_resource_handler()-> 992 redirect_to_file_resource_handler()->
1266 SetCreateTemporaryFileStreamFunctionForTesting( 993 SetCreateTemporaryFileStreamFunctionForTesting(
1267 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); 994 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED));
1268 995
1269 // Run it to completion. 996 // Run it to completion.
1270 loader_->StartRequest(); 997 loader_->StartRequest();
1271 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 998 raw_ptr_resource_handler_->WaitForResponseComplete();
1272 999
1273 // To downstream, the request was canceled. 1000 // To downstream, the request was canceled.
1274 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1001 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1275 raw_ptr_resource_handler_->final_status().status()); 1002 raw_ptr_resource_handler_->status().status());
1276 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1003 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1277 } 1004 }
1278 1005
1279 // Tests that RedirectToFileResourceHandler handles synchronous write errors. 1006 // Tests that RedirectToFileResourceHandler handles synchronous write errors.
1280 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { 1007 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) {
1281 file_stream()->set_forced_error(net::ERR_FAILED); 1008 file_stream()->set_forced_error(net::ERR_FAILED);
1282 1009
1283 // Run it to completion. 1010 // Run it to completion.
1284 loader_->StartRequest(); 1011 loader_->StartRequest();
1285 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1012 raw_ptr_resource_handler_->WaitForResponseComplete();
1286 1013
1287 // To downstream, the request was canceled sometime after it started, but 1014 // To downstream, the request was canceled sometime after it started, but
1288 // before any data was written. 1015 // before any data was written.
1289 EXPECT_EQ( 1016 EXPECT_EQ(temp_path(),
1290 temp_path(), 1017 raw_ptr_resource_handler_->response()->head.download_file_path);
1291 raw_ptr_resource_handler_->resource_response()->head.download_file_path); 1018 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1292 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1293 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1019 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1294 raw_ptr_resource_handler_->final_status().status()); 1020 raw_ptr_resource_handler_->status().status());
1295 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1021 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1296 } 1022 }
1297 1023
1298 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. 1024 // Tests that RedirectToFileResourceHandler handles asynchronous write errors.
1299 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { 1025 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) {
1300 file_stream()->set_forced_error_async(net::ERR_FAILED); 1026 file_stream()->set_forced_error_async(net::ERR_FAILED);
1301 1027
1302 // Run it to completion. 1028 // Run it to completion.
1303 loader_->StartRequest(); 1029 loader_->StartRequest();
1304 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1030 raw_ptr_resource_handler_->WaitForResponseComplete();
1305 1031
1306 // To downstream, the request was canceled sometime after it started, but 1032 // To downstream, the request was canceled sometime after it started, but
1307 // before any data was written. 1033 // before any data was written.
1308 EXPECT_EQ( 1034 EXPECT_EQ(temp_path(),
1309 temp_path(), 1035 raw_ptr_resource_handler_->response()->head.download_file_path);
1310 raw_ptr_resource_handler_->resource_response()->head.download_file_path); 1036 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1311 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1312 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1037 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1313 raw_ptr_resource_handler_->final_status().status()); 1038 raw_ptr_resource_handler_->status().status());
1314 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1039 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1315 } 1040 }
1316 1041
1317 // Tests that RedirectToFileHandler defers completion if there are outstanding 1042 // Tests that RedirectToFileHandler defers completion if there are outstanding
1318 // writes and accounts for errors which occur in that time. 1043 // writes and accounts for errors which occur in that time.
1319 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { 1044 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) {
1320 // Program the MockFileStream to error asynchronously, but throttle the 1045 // Program the MockFileStream to error asynchronously, but throttle the
1321 // callback. 1046 // callback.
1322 file_stream()->set_forced_error_async(net::ERR_FAILED); 1047 file_stream()->set_forced_error_async(net::ERR_FAILED);
1323 file_stream()->ThrottleCallbacks(); 1048 file_stream()->ThrottleCallbacks();
1324 1049
1325 // Run it as far as it will go. 1050 // Run it as far as it will go.
1326 loader_->StartRequest(); 1051 loader_->StartRequest();
1327 base::RunLoop().RunUntilIdle(); 1052 base::RunLoop().RunUntilIdle();
1328 1053
1329 // At this point, the request should have completed. 1054 // At this point, the request should have completed.
1330 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1055 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1331 raw_ptr_to_request_->status().status()); 1056 raw_ptr_to_request_->status().status());
1332 1057
1333 // However, the resource loader stack is stuck somewhere after receiving the 1058 // However, the resource loader stack is stuck somewhere after receiving the
1334 // response. 1059 // response.
1335 EXPECT_EQ( 1060 EXPECT_EQ(temp_path(),
1336 temp_path(), 1061 raw_ptr_resource_handler_->response()->head.download_file_path);
1337 raw_ptr_resource_handler_->resource_response()->head.download_file_path); 1062 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1338 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); 1063 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
1339 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1340 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1064 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1341 1065
1342 // Now, release the floodgates. 1066 // Now, release the floodgates.
1343 file_stream()->ReleaseCallbacks(); 1067 file_stream()->ReleaseCallbacks();
1344 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1068 raw_ptr_resource_handler_->WaitForResponseComplete();
1345 1069
1346 // Although the URLRequest was successful, the leaf handler sees a failure 1070 // Although the URLRequest was successful, the leaf handler sees a failure
1347 // because the write never completed. 1071 // because the write never completed.
1348 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1072 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1349 raw_ptr_resource_handler_->final_status().status()); 1073 raw_ptr_resource_handler_->status().status());
1350 } 1074 }
1351 1075
1352 // Tests that a RedirectToFileResourceHandler behaves properly when the 1076 // Tests that a RedirectToFileResourceHandler behaves properly when the
1353 // downstream handler defers OnWillStart. 1077 // downstream handler defers OnWillStart.
1354 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { 1078 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) {
1355 // Defer OnWillStart. 1079 // Defer OnWillStart.
1356 raw_ptr_resource_handler_->set_defer_on_will_start(true); 1080 raw_ptr_resource_handler_->set_defer_request_on_will_start(true);
1357 1081
1358 // Run as far as we'll go. 1082 // Run as far as we'll go.
1359 loader_->StartRequest(); 1083 loader_->StartRequest();
1360 raw_ptr_resource_handler_->WaitUntilDeferred(); 1084 raw_ptr_resource_handler_->WaitForDeferredStep();
1361 1085
1362 // The request should have stopped at OnWillStart. 1086 // The request should have stopped at OnWillStart.
1363 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url()); 1087 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1364 EXPECT_FALSE(raw_ptr_resource_handler_->resource_response()); 1088 EXPECT_FALSE(raw_ptr_resource_handler_->response());
1365 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); 1089 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed());
1366 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1090 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1367 1091
1368 // Now resume the request. Now we complete. 1092 // Now resume the request. Now we complete.
1369 raw_ptr_resource_handler_->Resume(); 1093 raw_ptr_resource_handler_->Resume();
1370 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1094 raw_ptr_resource_handler_->WaitForResponseComplete();
1371 1095
1372 // Check that the handler forwarded all information to the downstream handler. 1096 // Check that the handler forwarded all information to the downstream handler.
1373 EXPECT_EQ( 1097 EXPECT_EQ(temp_path(),
1374 temp_path(), 1098 raw_ptr_resource_handler_->response()->head.download_file_path);
1375 raw_ptr_resource_handler_->resource_response()->head.download_file_path); 1099 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1376 EXPECT_EQ(test_redirect_url(), raw_ptr_resource_handler_->start_url());
1377 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1100 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1378 raw_ptr_resource_handler_->final_status().status()); 1101 raw_ptr_resource_handler_->status().status());
1379 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1102 EXPECT_EQ(test_data().size(), static_cast<size_t>(
1380 raw_ptr_resource_handler_->total_bytes_downloaded())); 1103 raw_ptr_resource_handler_->total_bytes_downloaded()));
1381 1104
1382 // Check that the data was written to the file. 1105 // Check that the data was written to the file.
1383 std::string contents; 1106 std::string contents;
1384 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1107 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
1385 EXPECT_EQ(test_data(), contents); 1108 EXPECT_EQ(test_data(), contents);
1386 } 1109 }
1387 1110
1388 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1111 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1389 public: 1112 public:
1390 void VerifyEffectiveConnectionType( 1113 void VerifyEffectiveConnectionType(
1391 ResourceType resource_type, 1114 ResourceType resource_type,
1392 bool belongs_to_main_frame, 1115 bool belongs_to_main_frame,
1393 net::EffectiveConnectionType set_type, 1116 net::EffectiveConnectionType set_type,
1394 net::EffectiveConnectionType expected_type) { 1117 net::EffectiveConnectionType expected_type) {
1395 network_quality_estimator()->set_effective_connection_type(set_type); 1118 network_quality_estimator()->set_effective_connection_type(set_type);
1396 1119
1397 // Start the request and wait for it to finish. 1120 // Start the request and wait for it to finish.
1398 std::unique_ptr<net::URLRequest> request( 1121 std::unique_ptr<net::URLRequest> request(
1399 resource_context_.GetRequestContext()->CreateRequest( 1122 resource_context_.GetRequestContext()->CreateRequest(
1400 test_redirect_url(), net::DEFAULT_PRIORITY, 1123 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
1401 nullptr /* delegate */));
1402 SetUpResourceLoader(std::move(request), resource_type, 1124 SetUpResourceLoader(std::move(request), resource_type,
1403 belongs_to_main_frame); 1125 belongs_to_main_frame);
1404 1126
1405 // Send the request and wait until it completes. 1127 // Send the request and wait until it completes.
1406 loader_->StartRequest(); 1128 loader_->StartRequest();
1407 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1129 raw_ptr_resource_handler_->WaitForResponseComplete();
1408 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1130 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1409 raw_ptr_to_request_->status().status()); 1131 raw_ptr_to_request_->status().status());
1410 1132
1411 EXPECT_EQ(expected_type, raw_ptr_resource_handler_->resource_response() 1133 EXPECT_EQ(expected_type,
1412 ->head.effective_connection_type); 1134 raw_ptr_resource_handler_->observed_effective_connection_type());
1413 } 1135 }
1414 }; 1136 };
1415 1137
1416 // Tests that the effective connection type is set on main frame requests. 1138 // Tests that the effective connection type is set on main frame requests.
1417 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { 1139 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) {
1418 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, 1140 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true,
1419 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, 1141 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
1420 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1142 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1421 } 1143 }
1422 1144
(...skipping 14 matching lines...) Expand all
1437 1159
1438 // Tests that the effective connection type is not set on non-main frame 1160 // Tests that the effective connection type is not set on non-main frame
1439 // requests. 1161 // requests.
1440 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { 1162 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1441 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, 1163 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false,
1442 net::EFFECTIVE_CONNECTION_TYPE_3G, 1164 net::EFFECTIVE_CONNECTION_TYPE_3G,
1443 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1165 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1444 } 1166 }
1445 1167
1446 } // namespace content 1168 } // 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