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

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

Issue 2543633004: Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: Remove old test handler (No longer being used) 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 GURL test_url() const {
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 nit, suggestion: Given the contrast below with tes
mmenke 2016/12/02 21:26:52 Done - called it test_redirect_url(), and hoisted
400 return net::URLRequestTestJob::test_url_redirect_to_url_1();
401 }
569 402
570 TestNetworkQualityEstimator* network_quality_estimator() { 403 TestNetworkQualityEstimator* network_quality_estimator() {
571 return &network_quality_estimator_; 404 return &network_quality_estimator_;
572 } 405 }
573 406
574 std::string test_data() const { 407 std::string test_data() const {
575 return net::URLRequestTestJob::test_data_1(); 408 return net::URLRequestTestJob::test_data_1();
576 } 409 }
577 410
578 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 411 virtual std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
579 CreateProtocolHandler() { 412 CreateProtocolHandler() {
580 return net::URLRequestTestJob::CreateProtocolHandler(); 413 return net::URLRequestTestJob::CreateProtocolHandler();
581 } 414 }
582 415
583 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler( 416 virtual std::unique_ptr<ResourceHandler> WrapResourceHandler(
584 std::unique_ptr<ResourceHandlerStub> leaf_handler, 417 std::unique_ptr<TestResourceHandler> leaf_handler,
585 net::URLRequest* request) { 418 net::URLRequest* request) {
586 return std::move(leaf_handler); 419 return std::move(leaf_handler);
587 } 420 }
588 421
589 // Replaces loader_ with a new one for |request|. 422 // Replaces loader_ with a new one for |request|.
590 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request, 423 void SetUpResourceLoader(std::unique_ptr<net::URLRequest> request,
591 ResourceType resource_type, 424 ResourceType resource_type,
592 bool belongs_to_main_frame) { 425 bool belongs_to_main_frame) {
593 raw_ptr_to_request_ = request.get(); 426 raw_ptr_to_request_ = request.get();
594 427
595 // A request marked as a main frame request must also belong to a main 428 // A request marked as a main frame request must also belong to a main
596 // frame. 429 // frame.
597 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) || 430 ASSERT_TRUE((resource_type != RESOURCE_TYPE_MAIN_FRAME) ||
598 belongs_to_main_frame); 431 belongs_to_main_frame);
599 432
600 RenderFrameHost* rfh = web_contents_->GetMainFrame(); 433 RenderFrameHost* rfh = web_contents_->GetMainFrame();
601 ResourceRequestInfo::AllocateForTesting( 434 ResourceRequestInfo::AllocateForTesting(
602 request.get(), resource_type, &resource_context_, 435 request.get(), resource_type, &resource_context_,
603 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(), 436 rfh->GetProcess()->GetID(), rfh->GetRenderViewHost()->GetRoutingID(),
604 rfh->GetRoutingID(), belongs_to_main_frame, 437 rfh->GetRoutingID(), belongs_to_main_frame,
605 false /* parent_is_main_frame */, true /* allow_download */, 438 false /* parent_is_main_frame */, true /* allow_download */,
606 false /* is_async */, false /* is_using_lofi_ */); 439 false /* is_async */, false /* is_using_lofi_ */);
607 std::unique_ptr<ResourceHandlerStub> resource_handler( 440 std::unique_ptr<TestResourceHandler> resource_handler(
608 new ResourceHandlerStub(request.get())); 441 new TestResourceHandler(nullptr, nullptr));
609 raw_ptr_resource_handler_ = resource_handler.get(); 442 raw_ptr_resource_handler_ = resource_handler.get();
610 loader_.reset(new ResourceLoader( 443 loader_.reset(new ResourceLoader(
611 std::move(request), 444 std::move(request),
612 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_), 445 WrapResourceHandler(std::move(resource_handler), raw_ptr_to_request_),
613 this)); 446 this));
614 } 447 }
615 448
616 void SetUp() override { 449 void SetUp() override {
617 job_factory_.SetProtocolHandler("test", CreateProtocolHandler()); 450 job_factory_.SetProtocolHandler("test", CreateProtocolHandler());
451 net::URLRequestFailedJob::AddUrlHandler();
618 452
619 browser_context_.reset(new TestBrowserContext()); 453 browser_context_.reset(new TestBrowserContext());
620 scoped_refptr<SiteInstance> site_instance = 454 scoped_refptr<SiteInstance> site_instance =
621 SiteInstance::Create(browser_context_.get()); 455 SiteInstance::Create(browser_context_.get());
622 web_contents_.reset( 456 web_contents_.reset(
623 TestWebContents::Create(browser_context_.get(), site_instance.get())); 457 TestWebContents::Create(browser_context_.get(), site_instance.get()));
624 458 SetUpResourceLoaderForUrl(test_url());
625 std::unique_ptr<net::URLRequest> request(
626 resource_context_.GetRequestContext()->CreateRequest(
627 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
628 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true);
629 } 459 }
630 460
631 void TearDown() override { 461 void TearDown() override {
632 // Destroy the WebContents and pump the event loop before destroying 462 // Destroy the WebContents and pump the event loop before destroying
633 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup 463 // |rvh_test_enabler_| and |thread_bundle_|. This lets asynchronous cleanup
634 // tasks complete. 464 // tasks complete.
635 web_contents_.reset(); 465 web_contents_.reset();
466
467 // Clean up handlers.
468 net::URLRequestFilter::GetInstance()->ClearHandlers();
469
636 base::RunLoop().RunUntilIdle(); 470 base::RunLoop().RunUntilIdle();
637 } 471 }
638 472
473 void SetUpResourceLoaderForUrl(const GURL& test_url) {
474 std::unique_ptr<net::URLRequest> request(
475 resource_context_.GetRequestContext()->CreateRequest(
476 test_url, net::DEFAULT_PRIORITY, nullptr /* delegate */));
477 SetUpResourceLoader(std::move(request), RESOURCE_TYPE_MAIN_FRAME, true);
478 }
479
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 nit, suggestion: It would feel more natural to me
mmenke 2016/12/02 21:26:52 It's down here to it doesn't interrupt the two tes
639 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) { 480 void SetClientCertStore(std::unique_ptr<net::ClientCertStore> store) {
640 dummy_cert_store_ = std::move(store); 481 dummy_cert_store_ = std::move(store);
641 } 482 }
642 483
643 // ResourceLoaderDelegate: 484 // ResourceLoaderDelegate:
644 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( 485 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
645 ResourceLoader* loader, 486 ResourceLoader* loader,
646 net::AuthChallengeInfo* auth_info) override { 487 net::AuthChallengeInfo* auth_info) override {
647 return NULL; 488 return NULL;
648 } 489 }
649 bool HandleExternalProtocol(ResourceLoader* loader, 490 bool HandleExternalProtocol(ResourceLoader* loader,
650 const GURL& url) override { 491 const GURL& url) override {
492 EXPECT_EQ(loader, loader_.get());
651 return false; 493 return false;
652 } 494 }
653 void DidStartRequest(ResourceLoader* loader) override {} 495 void DidStartRequest(ResourceLoader* loader) override {
496 EXPECT_EQ(loader, loader_.get());
497 EXPECT_EQ(0, did_finish_loading_);
498 EXPECT_EQ(0, did_start_request_);
499 ++did_start_request_;
500 }
654 void DidReceiveRedirect(ResourceLoader* loader, 501 void DidReceiveRedirect(ResourceLoader* loader,
655 const GURL& new_url, 502 const GURL& new_url,
656 ResourceResponse* response) override {} 503 ResourceResponse* response) override {
657 void DidReceiveResponse(ResourceLoader* loader) override {} 504 EXPECT_EQ(loader, loader_.get());
658 void DidFinishLoading(ResourceLoader* loader) override {} 505 EXPECT_EQ(0, did_finish_loading_);
506 EXPECT_EQ(0, did_receive_response_);
507 EXPECT_EQ(1, did_start_request_);
508 ++did_received_redirect_;
509 }
510 void DidReceiveResponse(ResourceLoader* loader) override {
511 EXPECT_EQ(loader, loader_.get());
512 EXPECT_EQ(0, did_finish_loading_);
513 EXPECT_EQ(0, did_receive_response_);
514 EXPECT_EQ(1, did_start_request_);
515 ++did_receive_response_;
516 }
517 void DidFinishLoading(ResourceLoader* loader) override {
518 EXPECT_EQ(loader, loader_.get());
519 EXPECT_EQ(0, did_finish_loading_);
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 Huh. I presume given the abundance of EXPECTs tha
mmenke 2016/12/02 21:26:52 There's no documentation. I believe these calls w
520 ++did_finish_loading_;
521 }
659 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( 522 std::unique_ptr<net::ClientCertStore> CreateClientCertStore(
660 ResourceLoader* loader) override { 523 ResourceLoader* loader) override {
661 return std::move(dummy_cert_store_); 524 return std::move(dummy_cert_store_);
662 } 525 }
663 526
664 TestBrowserThreadBundle thread_bundle_; 527 TestBrowserThreadBundle thread_bundle_;
665 RenderViewHostTestEnabler rvh_test_enabler_; 528 RenderViewHostTestEnabler rvh_test_enabler_;
666 529
530 // Record which ResourceDispatcherHostDelegate methods have been invoked.
531 int did_start_request_ = 0;
532 int did_received_redirect_ = 0;
533 int did_receive_response_ = 0;
534 int did_finish_loading_ = 0;
535
667 net::URLRequestJobFactoryImpl job_factory_; 536 net::URLRequestJobFactoryImpl job_factory_;
668 TestNetworkQualityEstimator network_quality_estimator_; 537 TestNetworkQualityEstimator network_quality_estimator_;
669 net::TestURLRequestContext test_url_request_context_; 538 net::TestURLRequestContext test_url_request_context_;
670 MockResourceContext resource_context_; 539 MockResourceContext resource_context_;
671 std::unique_ptr<TestBrowserContext> browser_context_; 540 std::unique_ptr<TestBrowserContext> browser_context_;
672 std::unique_ptr<TestWebContents> web_contents_; 541 std::unique_ptr<TestWebContents> web_contents_;
673 std::unique_ptr<net::ClientCertStore> dummy_cert_store_; 542 std::unique_ptr<net::ClientCertStore> dummy_cert_store_;
674 543
675 // The ResourceLoader owns the URLRequest and the ResourceHandler. 544 // The ResourceLoader owns the URLRequest and the ResourceHandler.
676 ResourceHandlerStub* raw_ptr_resource_handler_; 545 TestResourceHandler* raw_ptr_resource_handler_;
677 net::URLRequest* raw_ptr_to_request_; 546 net::URLRequest* raw_ptr_to_request_;
678 std::unique_ptr<ResourceLoader> loader_; 547 std::unique_ptr<ResourceLoader> loader_;
679 }; 548 };
680 549
681 class ClientCertResourceLoaderTest : public ResourceLoaderTest { 550 class ClientCertResourceLoaderTest : public ResourceLoaderTest {
682 protected: 551 protected:
683 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler> 552 std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>
684 CreateProtocolHandler() override { 553 CreateProtocolHandler() override {
685 return base::WrapUnique(new MockClientCertJobProtocolHandler); 554 return base::WrapUnique(new MockClientCertJobProtocolHandler);
686 } 555 }
556
557 void SetUp() override {
558 ResourceLoaderTest::SetUp();
559 // These tests don't expect any redirects.
560 SetUpResourceLoaderForUrl(net::URLRequestTestJob::test_url_1());
561 }
687 }; 562 };
688 563
689 // A ResourceLoaderTest that intercepts https://example.test and 564 // A ResourceLoaderTest that intercepts https://example.test and
690 // https://example-redirect.test URLs and sets SSL info on the 565 // https://example-redirect.test URLs and sets SSL info on the
691 // responses. The latter serves a Location: header in the response. 566 // responses. The latter serves a Location: header in the response.
692 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest { 567 class HTTPSSecurityInfoResourceLoaderTest : public ResourceLoaderTest {
693 public: 568 public:
694 HTTPSSecurityInfoResourceLoaderTest() 569 HTTPSSecurityInfoResourceLoaderTest()
695 : ResourceLoaderTest(), 570 : ResourceLoaderTest(),
696 test_https_url_("https://example.test"), 571 test_https_url_("https://example.test"),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 SetClientCertStore(std::move(test_store)); 608 SetClientCertStore(std::move(test_store));
734 609
735 // Plug in test content browser client. 610 // Plug in test content browser client.
736 SelectCertificateBrowserClient test_client; 611 SelectCertificateBrowserClient test_client;
737 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 612 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
738 613
739 // Start the request and wait for it to pause. 614 // Start the request and wait for it to pause.
740 loader_->StartRequest(); 615 loader_->StartRequest();
741 test_client.WaitForSelectCertificate(); 616 test_client.WaitForSelectCertificate();
742 617
743 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 618 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
744 619
745 // Check if the test store was queried against correct |cert_authorities|. 620 // Check if the test store was queried against correct |cert_authorities|.
746 EXPECT_EQ(1, store_request_count); 621 EXPECT_EQ(1, store_request_count);
747 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(), 622 EXPECT_EQ(MockClientCertURLRequestJob::test_authorities(),
748 store_requested_authorities); 623 store_requested_authorities);
749 624
750 // Check if the retrieved certificates were passed to the content browser 625 // Check if the retrieved certificates were passed to the content browser
751 // client. 626 // client.
752 EXPECT_EQ(1, test_client.call_count()); 627 EXPECT_EQ(1, test_client.call_count());
753 EXPECT_EQ(dummy_certs, test_client.passed_certs()); 628 EXPECT_EQ(dummy_certs, test_client.passed_certs());
754 629
755 // Continue the request. 630 // Continue the request.
756 test_client.ContinueWithCertificate(nullptr); 631 test_client.ContinueWithCertificate(nullptr);
757 raw_ptr_resource_handler_->WaitForResponseComplete(); 632 raw_ptr_resource_handler_->WaitUntilResponseComplete();
758 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 633 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
759 634
760 // Restore the original content browser client. 635 // Restore the original content browser client.
761 SetBrowserClientForTesting(old_client); 636 SetBrowserClientForTesting(old_client);
762 } 637 }
763 638
764 // Tests that client certificates are requested on a platform with NULL 639 // Tests that client certificates are requested on a platform with NULL
765 // ClientCertStore. 640 // ClientCertStore.
766 TEST_F(ClientCertResourceLoaderTest, WithNullStore) { 641 TEST_F(ClientCertResourceLoaderTest, WithNullStore) {
767 // Plug in test content browser client. 642 // Plug in test content browser client.
768 SelectCertificateBrowserClient test_client; 643 SelectCertificateBrowserClient test_client;
769 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 644 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
770 645
771 // Start the request and wait for it to pause. 646 // Start the request and wait for it to pause.
772 loader_->StartRequest(); 647 loader_->StartRequest();
773 test_client.WaitForSelectCertificate(); 648 test_client.WaitForSelectCertificate();
774 649
775 // Check if the SelectClientCertificate was called on the content browser 650 // Check if the SelectClientCertificate was called on the content browser
776 // client. 651 // client.
777 EXPECT_EQ(1, test_client.call_count()); 652 EXPECT_EQ(1, test_client.call_count());
778 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 653 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
779 654
780 // Continue the request. 655 // Continue the request.
781 test_client.ContinueWithCertificate(nullptr); 656 test_client.ContinueWithCertificate(nullptr);
782 raw_ptr_resource_handler_->WaitForResponseComplete(); 657 raw_ptr_resource_handler_->WaitUntilResponseComplete();
783 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->status().error()); 658 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
784 659
785 // Restore the original content browser client. 660 // Restore the original content browser client.
786 SetBrowserClientForTesting(old_client); 661 SetBrowserClientForTesting(old_client);
787 } 662 }
788 663
789 // Tests that the ContentBrowserClient may cancel a certificate request. 664 // Tests that the ContentBrowserClient may cancel a certificate request.
790 TEST_F(ClientCertResourceLoaderTest, CancelSelection) { 665 TEST_F(ClientCertResourceLoaderTest, CancelSelection) {
791 // Plug in test content browser client. 666 // Plug in test content browser client.
792 SelectCertificateBrowserClient test_client; 667 SelectCertificateBrowserClient test_client;
793 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 668 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
794 669
795 // Start the request and wait for it to pause. 670 // Start the request and wait for it to pause.
796 loader_->StartRequest(); 671 loader_->StartRequest();
797 test_client.WaitForSelectCertificate(); 672 test_client.WaitForSelectCertificate();
798 673
799 // Check if the SelectClientCertificate was called on the content browser 674 // Check if the SelectClientCertificate was called on the content browser
800 // client. 675 // client.
801 EXPECT_EQ(1, test_client.call_count()); 676 EXPECT_EQ(1, test_client.call_count());
802 EXPECT_EQ(net::CertificateList(), test_client.passed_certs()); 677 EXPECT_EQ(net::CertificateList(), test_client.passed_certs());
803 678
804 // Cancel the request. 679 // Cancel the request.
805 test_client.CancelCertificateSelection(); 680 test_client.CancelCertificateSelection();
806 raw_ptr_resource_handler_->WaitForResponseComplete(); 681 raw_ptr_resource_handler_->WaitUntilResponseComplete();
807 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 682 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
808 raw_ptr_resource_handler_->status().error()); 683 raw_ptr_resource_handler_->final_status().error());
809 684
810 // Restore the original content browser client. 685 // Restore the original content browser client.
811 SetBrowserClientForTesting(old_client); 686 SetBrowserClientForTesting(old_client);
812 } 687 }
813 688
814 // Verifies that requests without WebContents attached abort. 689 // Verifies that requests without WebContents attached abort.
815 TEST_F(ClientCertResourceLoaderTest, NoWebContents) { 690 TEST_F(ClientCertResourceLoaderTest, NoWebContents) {
816 // Destroy the WebContents before starting the request. 691 // Destroy the WebContents before starting the request.
817 web_contents_.reset(); 692 web_contents_.reset();
818 693
819 // Plug in test content browser client. 694 // Plug in test content browser client.
820 SelectCertificateBrowserClient test_client; 695 SelectCertificateBrowserClient test_client;
821 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client); 696 ContentBrowserClient* old_client = SetBrowserClientForTesting(&test_client);
822 697
823 // Start the request and wait for it to complete. 698 // Start the request and wait for it to complete.
824 loader_->StartRequest(); 699 loader_->StartRequest();
825 raw_ptr_resource_handler_->WaitForResponseComplete(); 700 raw_ptr_resource_handler_->WaitUntilResponseComplete();
826 701
827 // Check that SelectClientCertificate wasn't called and the request aborted. 702 // Check that SelectClientCertificate wasn't called and the request aborted.
828 EXPECT_EQ(0, test_client.call_count()); 703 EXPECT_EQ(0, test_client.call_count());
829 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 704 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED,
830 raw_ptr_resource_handler_->status().error()); 705 raw_ptr_resource_handler_->final_status().error());
831 706
832 // Restore the original content browser client. 707 // Restore the original content browser client.
833 SetBrowserClientForTesting(old_client); 708 SetBrowserClientForTesting(old_client);
834 } 709 }
835 710
836 // Verifies that ClientCertStore's callback doesn't crash if called after the 711 // Verifies that ClientCertStore's callback doesn't crash if called after the
837 // loader is destroyed. 712 // loader is destroyed.
838 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) { 713 TEST_F(ClientCertResourceLoaderTest, StoreAsyncCancel) {
839 base::RunLoop loader_destroyed_run_loop; 714 base::RunLoop loader_destroyed_run_loop;
840 LoaderDestroyingCertStore* test_store = 715 LoaderDestroyingCertStore* test_store =
841 new LoaderDestroyingCertStore(&loader_, 716 new LoaderDestroyingCertStore(&loader_,
842 loader_destroyed_run_loop.QuitClosure()); 717 loader_destroyed_run_loop.QuitClosure());
843 SetClientCertStore(base::WrapUnique(test_store)); 718 SetClientCertStore(base::WrapUnique(test_store));
844 719
845 loader_->StartRequest(); 720 loader_->StartRequest();
846 loader_destroyed_run_loop.Run(); 721 loader_destroyed_run_loop.Run();
847 EXPECT_FALSE(loader_); 722 EXPECT_FALSE(loader_);
848 723
849 // Pump the event loop to ensure nothing asynchronous crashes either. 724 // Pump the event loop to ensure nothing asynchronous crashes either.
850 base::RunLoop().RunUntilIdle(); 725 base::RunLoop().RunUntilIdle();
851 } 726 }
852 727
853 TEST_F(ResourceLoaderTest, ResumeCancelledRequest) { 728 // Test the case the ResourceHandler defers nothing.
854 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 729 TEST_F(ResourceLoaderTest, SyncResourceHandler) {
730 loader_->StartRequest();
731 raw_ptr_resource_handler_->WaitUntilResponseComplete();
732 EXPECT_EQ(1, did_start_request_);
733 EXPECT_EQ(1, did_received_redirect_);
734 EXPECT_EQ(1, did_receive_response_);
735 EXPECT_EQ(1, did_finish_loading_);
736 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
737 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
738 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
739 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
740 }
741
742 // Test the case the ResourceHandler defers everything.
743 TEST_F(ResourceLoaderTest, AsyncResourceHandler) {
744 raw_ptr_resource_handler_->set_defer_on_will_start(true);
745 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
746 raw_ptr_resource_handler_->set_defer_on_response_started(true);
747 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
748 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
749 raw_ptr_resource_handler_->set_defer_on_response_completed(true);
750
751 // Start and run until OnWillStart.
752 loader_->StartRequest();
753 raw_ptr_resource_handler_->WaitUntilDeferred();
754 // Spinning the message loop should not advance the state further.
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 Hmmmm. This is testing to make sure that state do
mmenke 2016/12/02 21:26:52 I've just copied the "most imporant" EXPECT_EQ(1..
755 base::RunLoop().RunUntilIdle();
756 EXPECT_EQ(0, did_start_request_);
757 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
758 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
759 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
760
761 // Resume and run until OnRequestRedirected.
762 raw_ptr_resource_handler_->Resume();
763 raw_ptr_resource_handler_->WaitUntilDeferred();
764 // Spinning the message loop should not advance the state further.
765 base::RunLoop().RunUntilIdle();
766 EXPECT_EQ(1, did_received_redirect_);
767 EXPECT_EQ(0, did_receive_response_);
768 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
769 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
770 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
771
772 // Resume and run until OnResponseStarted.
773 raw_ptr_resource_handler_->Resume();
774 raw_ptr_resource_handler_->WaitUntilDeferred();
775 // Spinning the message loop should not advance the state further.
776 base::RunLoop().RunUntilIdle();
777 EXPECT_EQ(1, did_receive_response_);
778 EXPECT_EQ(0, did_finish_loading_);
779 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
780 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
781 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
782
783 // Resume and run until OnReadCompleted.
784 raw_ptr_resource_handler_->Resume();
785 raw_ptr_resource_handler_->WaitUntilDeferred();
786 // Spinning the message loop should not advance the state further.
787 base::RunLoop().RunUntilIdle();
788 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_read_called());
789 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
790 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
791 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
792
793 // Resume and run until the final 0-byte read, signalling EOF.
794 raw_ptr_resource_handler_->Resume();
795 raw_ptr_resource_handler_->WaitUntilDeferred();
796 // Spinning the message loop should not advance the state further.
797 base::RunLoop().RunUntilIdle();
798 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
799 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
800 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
801
802 // Resume and run until OnResponseCompleted is called, which again defers the
803 // request.
804 raw_ptr_resource_handler_->Resume();
805 raw_ptr_resource_handler_->WaitUntilResponseComplete();
806 EXPECT_EQ(0, did_finish_loading_);
807 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
808 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
809 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
810
811 // Resume and run until all pending tasks. Note that OnResponseCompleted was
812 // invoked in the previous section, so can't use RunUntilCompleted().
813 raw_ptr_resource_handler_->Resume();
814 base::RunLoop().RunUntilIdle();
815 EXPECT_EQ(1, did_finish_loading_);
816 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
817 EXPECT_EQ(net::OK, raw_ptr_resource_handler_->final_status().error());
818 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
819 }
820
821 TEST_F(ResourceLoaderTest, SyncCancelOnWillStart) {
822 raw_ptr_resource_handler_->set_on_will_start_result(false);
823
824 loader_->StartRequest();
825 raw_ptr_resource_handler_->WaitUntilResponseComplete();
826 base::RunLoop().RunUntilIdle();
827 EXPECT_EQ(0, did_start_request_);
828 EXPECT_EQ(1, did_finish_loading_);
829 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
830 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
831 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
832 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
833
834 EXPECT_EQ(net::ERR_ABORTED,
835 raw_ptr_resource_handler_->final_status().error());
836 EXPECT_EQ("", raw_ptr_resource_handler_->body());
837 }
838
839 TEST_F(ResourceLoaderTest, SyncCancelOnRequestRedirected) {
840 raw_ptr_resource_handler_->set_on_request_redirected_result(false);
841
842 loader_->StartRequest();
843 raw_ptr_resource_handler_->WaitUntilResponseComplete();
844 base::RunLoop().RunUntilIdle();
845 EXPECT_EQ(1, did_received_redirect_);
846 EXPECT_EQ(0, did_receive_response_);
847 EXPECT_EQ(1, did_finish_loading_);
848 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
849 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
850 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
851 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
852
853 EXPECT_EQ(net::ERR_ABORTED,
854 raw_ptr_resource_handler_->final_status().error());
855 EXPECT_EQ("", raw_ptr_resource_handler_->body());
856 }
857
858 TEST_F(ResourceLoaderTest, SyncCancelOnResponseStarted) {
859 raw_ptr_resource_handler_->set_on_response_started_result(false);
860
861 loader_->StartRequest();
862 raw_ptr_resource_handler_->WaitUntilResponseComplete();
863 base::RunLoop().RunUntilIdle();
864 EXPECT_EQ(1, did_receive_response_);
865 EXPECT_EQ(1, did_finish_loading_);
866 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
867 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
868 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
869 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
870
871 EXPECT_EQ(net::ERR_ABORTED,
872 raw_ptr_resource_handler_->final_status().error());
873 EXPECT_EQ("", raw_ptr_resource_handler_->body());
874 }
875
876 TEST_F(ResourceLoaderTest, SyncCancelOnWillRead) {
877 raw_ptr_resource_handler_->set_on_will_read_result(false);
878
879 loader_->StartRequest();
880 raw_ptr_resource_handler_->WaitUntilResponseComplete();
881 base::RunLoop().RunUntilIdle();
882 EXPECT_EQ(1, did_receive_response_);
883 EXPECT_EQ(1, did_finish_loading_);
884 EXPECT_EQ(1, 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, SyncCancelOnReadCompleted) {
894 raw_ptr_resource_handler_->set_on_read_completed_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_read_completed_called());
902 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
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_LT(0u, raw_ptr_resource_handler_->body().size());
908 }
909
910 // This test is broken because ResourceLoader assumes when
911 // URLRequest::was_pending() is false, canceling the request will not result in
912 // a completion notification. This isn't the case - whether or not there's a
913 // notification depends on whether URLRequestJob::NotifyDone() has been invoked
914 // yet - something the URLRequest doesn't even know about, itself. As a result,
915 // the ResourceLoader is notified of cancellation twice.
916 // TODO(mmenke): Fix this.
917 TEST_F(ResourceLoaderTest, DISABLED_SyncCancelOnReceivedEof) {
918 raw_ptr_resource_handler_->set_on_on_read_eof_result(false);
919
920 loader_->StartRequest();
921 raw_ptr_resource_handler_->WaitUntilResponseComplete();
922 base::RunLoop().RunUntilIdle();
923 EXPECT_EQ(1, did_receive_response_);
924 EXPECT_EQ(1, did_finish_loading_);
925 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
926 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
927
928 EXPECT_EQ(net::ERR_ABORTED,
929 raw_ptr_resource_handler_->final_status().error());
930 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
931 }
932
933 TEST_F(ResourceLoaderTest, AsyncCancelOnWillStart) {
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 Suggestion, nitty nit: I'm wanting to be precise i
mmenke 2016/12/02 21:26:52 I'd call these async in-band. The async is trigge
934 raw_ptr_resource_handler_->set_defer_on_will_start(true);
935
936 loader_->StartRequest();
937 raw_ptr_resource_handler_->WaitUntilDeferred();
938 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
939 base::RunLoop().RunUntilIdle();
940 EXPECT_EQ(0, did_start_request_);
941 EXPECT_EQ(1, did_finish_loading_);
942 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
943 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
944 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
945 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
946
947 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
948 EXPECT_EQ("", raw_ptr_resource_handler_->body());
949 }
950
951 TEST_F(ResourceLoaderTest, AsyncCancelOnRequestRedirected) {
952 raw_ptr_resource_handler_->set_defer_on_request_redirected(true);
953
954 loader_->StartRequest();
955 raw_ptr_resource_handler_->WaitUntilDeferred();
956 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
957 base::RunLoop().RunUntilIdle();
958 EXPECT_EQ(1, did_received_redirect_);
959 EXPECT_EQ(0, did_receive_response_);
960 EXPECT_EQ(1, did_finish_loading_);
961 EXPECT_EQ(1, raw_ptr_resource_handler_->on_request_redirected_called());
962 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
963 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
964 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
965
966 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
967 EXPECT_EQ("", raw_ptr_resource_handler_->body());
968 }
969
970 TEST_F(ResourceLoaderTest, AsyncCancelOnResponseStarted) {
971 raw_ptr_resource_handler_->set_defer_on_response_started(true);
972
973 loader_->StartRequest();
974 raw_ptr_resource_handler_->WaitUntilDeferred();
975 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
976 base::RunLoop().RunUntilIdle();
977 EXPECT_EQ(1, did_receive_response_);
978 EXPECT_EQ(1, did_finish_loading_);
979 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
980 EXPECT_EQ(0, raw_ptr_resource_handler_->on_will_read_called());
981 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
982 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
983
984 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
985 EXPECT_EQ("", raw_ptr_resource_handler_->body());
986 }
987
988 TEST_F(ResourceLoaderTest, AsyncCancelOnReadCompleted) {
989 raw_ptr_resource_handler_->set_defer_on_read_completed(true);
990
991 loader_->StartRequest();
992 raw_ptr_resource_handler_->WaitUntilDeferred();
993 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
994 base::RunLoop().RunUntilIdle();
995 EXPECT_EQ(1, did_receive_response_);
996 EXPECT_EQ(1, did_finish_loading_);
997 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_completed_called());
998 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_eof());
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_LT(0u, raw_ptr_resource_handler_->body().size());
1003 }
1004
1005 TEST_F(ResourceLoaderTest, AsyncCancelOnReceivedEof) {
1006 raw_ptr_resource_handler_->set_defer_on_read_eof(true);
1007
1008 loader_->StartRequest();
1009 raw_ptr_resource_handler_->WaitUntilDeferred();
1010 // Have to spin the message loop for the test to pass - see comment on sync
1011 // version of this test for explanation.
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 FWIW, I read over the explanation on the sync test
mmenke 2016/12/02 21:26:52 The issue is URLRequest/URLRequestJob. If you loo
Randy Smith (Not in Mondays) 2016/12/06 20:46:44 That makes sense, but I still think there's a lacu
mmenke 2016/12/06 21:09:53 The next CL in this chain removes the extra spin,
1012 // TODO(mmenke): Remove this line once that's fixed.
1013 base::RunLoop().RunUntilIdle();
1014 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1015 base::RunLoop().RunUntilIdle();
1016 EXPECT_EQ(1, did_receive_response_);
1017 EXPECT_EQ(1, did_finish_loading_);
1018 EXPECT_EQ(1, raw_ptr_resource_handler_->on_read_eof());
1019 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1020
1021 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1022 EXPECT_EQ(test_data(), raw_ptr_resource_handler_->body());
1023 }
1024
1025 TEST_F(ResourceLoaderTest, RequestFailsOnStart) {
1026 SetUpResourceLoaderForUrl(
1027 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1028 net::URLRequestFailedJob::START, net::ERR_FAILED));
1029
1030 loader_->StartRequest();
1031 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1032 base::RunLoop().RunUntilIdle();
1033 EXPECT_EQ(0, did_received_redirect_);
1034 EXPECT_EQ(0, did_receive_response_);
1035 EXPECT_EQ(1, did_finish_loading_);
1036 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1037 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1038 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1039 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1040 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1041 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1042 }
1043
1044 TEST_F(ResourceLoaderTest, RequestFailsOnReadSync) {
1045 SetUpResourceLoaderForUrl(
1046 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1047 net::URLRequestFailedJob::READ_SYNC, net::ERR_FAILED));
1048
1049 loader_->StartRequest();
1050 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1051 base::RunLoop().RunUntilIdle();
1052 EXPECT_EQ(0, did_received_redirect_);
1053 EXPECT_EQ(1, did_receive_response_);
1054 EXPECT_EQ(1, did_finish_loading_);
1055 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1056 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1057 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1058 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1059 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1060 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1061 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1062 }
1063
1064 TEST_F(ResourceLoaderTest, RequestFailsOnReadAsync) {
1065 SetUpResourceLoaderForUrl(
1066 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1067 net::URLRequestFailedJob::READ_ASYNC, net::ERR_FAILED));
1068
1069 loader_->StartRequest();
1070 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1071 base::RunLoop().RunUntilIdle();
1072 EXPECT_EQ(0, did_received_redirect_);
1073 EXPECT_EQ(1, did_receive_response_);
1074 EXPECT_EQ(1, did_finish_loading_);
1075 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1076 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1077 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1078 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1079 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1080 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1081 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1082 }
1083
1084 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringStart) {
1085 SetUpResourceLoaderForUrl(
1086 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1087 net::URLRequestFailedJob::START, net::ERR_IO_PENDING));
1088
1089 loader_->StartRequest();
1090 base::RunLoop().RunUntilIdle();
1091 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1092 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1093 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1094 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1095
1096 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1097 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1098
1099 EXPECT_EQ(0, did_received_redirect_);
1100 EXPECT_EQ(0, did_receive_response_);
1101 EXPECT_EQ(1, did_finish_loading_);
1102 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1103 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1104 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1105 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1106 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1107 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1108 }
1109
1110 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) {
1111 SetUpResourceLoaderForUrl(
1112 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1113 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING));
1114
1115 loader_->StartRequest();
1116 base::RunLoop().RunUntilIdle();
1117 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1118 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1119 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1120 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1121 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1122
1123 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED);
1124 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1125 EXPECT_EQ(0, did_received_redirect_);
1126 EXPECT_EQ(1, did_receive_response_);
1127 EXPECT_EQ(1, did_finish_loading_);
1128 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1129 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1130 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1131 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1132 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1133 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error());
1134 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1135 }
1136
1137 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) {
1138 raw_ptr_resource_handler_->set_defer_on_will_start(true);
855 1139
856 loader_->StartRequest(); 1140 loader_->StartRequest();
857 loader_->CancelRequest(true); 1141 loader_->CancelRequest(true);
858 static_cast<ResourceController*>(loader_.get())->Resume(); 1142 static_cast<ResourceController*>(loader_.get())->Resume();
859 } 1143 }
860 1144
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 { 1145 class ResourceLoaderRedirectToFileTest : public ResourceLoaderTest {
891 public: 1146 public:
892 ResourceLoaderRedirectToFileTest() 1147 ResourceLoaderRedirectToFileTest()
893 : file_stream_(NULL), 1148 : file_stream_(NULL),
894 redirect_to_file_resource_handler_(NULL) { 1149 redirect_to_file_resource_handler_(NULL) {
895 } 1150 }
896 1151
897 ~ResourceLoaderRedirectToFileTest() override { 1152 ~ResourceLoaderRedirectToFileTest() override {
898 // Releasing the loader should result in destroying the file asynchronously. 1153 // Releasing the loader should result in destroying the file asynchronously.
899 file_stream_ = nullptr; 1154 file_stream_ = nullptr;
900 deletable_file_ = nullptr; 1155 deletable_file_ = nullptr;
901 loader_.reset(); 1156 loader_.reset();
902 1157
903 // Wait for the task to delete the file to run, and make sure the file is 1158 // Wait for the task to delete the file to run, and make sure the file is
904 // cleaned up. 1159 // cleaned up.
905 base::RunLoop().RunUntilIdle(); 1160 base::RunLoop().RunUntilIdle();
906 EXPECT_FALSE(base::PathExists(temp_path())); 1161 EXPECT_FALSE(base::PathExists(temp_path()));
907 } 1162 }
908 1163
909 base::FilePath temp_path() const { return temp_path_; } 1164 base::FilePath temp_path() const { return temp_path_; }
910 ShareableFileReference* deletable_file() const { 1165 ShareableFileReference* deletable_file() const {
911 return deletable_file_.get(); 1166 return deletable_file_.get();
912 } 1167 }
913 net::testing::MockFileStream* file_stream() const { return file_stream_; } 1168 net::testing::MockFileStream* file_stream() const { return file_stream_; }
914 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { 1169 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const {
915 return redirect_to_file_resource_handler_; 1170 return redirect_to_file_resource_handler_;
916 } 1171 }
917 1172
918 std::unique_ptr<ResourceHandler> WrapResourceHandler( 1173 std::unique_ptr<ResourceHandler> WrapResourceHandler(
919 std::unique_ptr<ResourceHandlerStub> leaf_handler, 1174 std::unique_ptr<TestResourceHandler> leaf_handler,
920 net::URLRequest* request) override { 1175 net::URLRequest* request) override {
921 leaf_handler->set_expect_reads(false); 1176 leaf_handler->set_expect_on_data_downloaded(true);
922 1177
923 // Make a temporary file. 1178 // Make a temporary file.
924 CHECK(base::CreateTemporaryFile(&temp_path_)); 1179 CHECK(base::CreateTemporaryFile(&temp_path_));
925 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | 1180 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY |
926 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; 1181 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC;
927 base::File file(temp_path_, flags); 1182 base::File file(temp_path_, flags);
928 CHECK(file.IsValid()); 1183 CHECK(file.IsValid());
929 1184
930 // Create mock file streams and a ShareableFileReference. 1185 // Create mock file streams and a ShareableFileReference.
931 std::unique_ptr<net::testing::MockFileStream> file_stream( 1186 std::unique_ptr<net::testing::MockFileStream> file_stream(
(...skipping 29 matching lines...) Expand all
961 // These are owned by the ResourceLoader. 1216 // These are owned by the ResourceLoader.
962 net::testing::MockFileStream* file_stream_; 1217 net::testing::MockFileStream* file_stream_;
963 RedirectToFileResourceHandler* redirect_to_file_resource_handler_; 1218 RedirectToFileResourceHandler* redirect_to_file_resource_handler_;
964 }; 1219 };
965 1220
966 // Tests that a RedirectToFileResourceHandler works and forwards everything 1221 // Tests that a RedirectToFileResourceHandler works and forwards everything
967 // downstream. 1222 // downstream.
968 TEST_F(ResourceLoaderRedirectToFileTest, Basic) { 1223 TEST_F(ResourceLoaderRedirectToFileTest, Basic) {
969 // Run it to completion. 1224 // Run it to completion.
970 loader_->StartRequest(); 1225 loader_->StartRequest();
971 raw_ptr_resource_handler_->WaitForResponseComplete(); 1226 raw_ptr_resource_handler_->WaitUntilResponseComplete();
972 1227
973 // Check that the handler forwarded all information to the downstream handler. 1228 // Check that the handler forwarded all information to the downstream handler.
974 EXPECT_EQ(temp_path(), 1229 EXPECT_EQ(
975 raw_ptr_resource_handler_->response()->head.download_file_path); 1230 temp_path(),
1231 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
976 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1232 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
977 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1233 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
978 raw_ptr_resource_handler_->status().status()); 1234 raw_ptr_resource_handler_->final_status().status());
979 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1235 EXPECT_EQ(test_data().size(), static_cast<size_t>(
980 raw_ptr_resource_handler_->total_bytes_downloaded())); 1236 raw_ptr_resource_handler_->total_bytes_downloaded()));
981 1237
982 // Check that the data was written to the file. 1238 // Check that the data was written to the file.
983 std::string contents; 1239 std::string contents;
984 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1240 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
985 EXPECT_EQ(test_data(), contents); 1241 EXPECT_EQ(test_data(), contents);
986 } 1242 }
987 1243
988 // Tests that RedirectToFileResourceHandler handles errors in creating the 1244 // Tests that RedirectToFileResourceHandler handles errors in creating the
989 // temporary file. 1245 // temporary file.
990 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) { 1246 TEST_F(ResourceLoaderRedirectToFileTest, CreateTemporaryError) {
991 // Swap out the create temporary function. 1247 // Swap out the create temporary function.
992 redirect_to_file_resource_handler()-> 1248 redirect_to_file_resource_handler()->
993 SetCreateTemporaryFileStreamFunctionForTesting( 1249 SetCreateTemporaryFileStreamFunctionForTesting(
994 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED)); 1250 base::Bind(&CreateTemporaryError, base::File::FILE_ERROR_FAILED));
995 1251
996 // Run it to completion. 1252 // Run it to completion.
997 loader_->StartRequest(); 1253 loader_->StartRequest();
998 raw_ptr_resource_handler_->WaitForResponseComplete(); 1254 raw_ptr_resource_handler_->WaitUntilResponseComplete();
999 1255
1000 // To downstream, the request was canceled. 1256 // To downstream, the request was canceled.
1001 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1257 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1002 raw_ptr_resource_handler_->status().status()); 1258 raw_ptr_resource_handler_->final_status().status());
1003 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1259 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1004 } 1260 }
1005 1261
1006 // Tests that RedirectToFileResourceHandler handles synchronous write errors. 1262 // Tests that RedirectToFileResourceHandler handles synchronous write errors.
1007 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) { 1263 TEST_F(ResourceLoaderRedirectToFileTest, WriteError) {
1008 file_stream()->set_forced_error(net::ERR_FAILED); 1264 file_stream()->set_forced_error(net::ERR_FAILED);
1009 1265
1010 // Run it to completion. 1266 // Run it to completion.
1011 loader_->StartRequest(); 1267 loader_->StartRequest();
1012 raw_ptr_resource_handler_->WaitForResponseComplete(); 1268 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1013 1269
1014 // To downstream, the request was canceled sometime after it started, but 1270 // To downstream, the request was canceled sometime after it started, but
1015 // before any data was written. 1271 // before any data was written.
1016 EXPECT_EQ(temp_path(), 1272 EXPECT_EQ(
1017 raw_ptr_resource_handler_->response()->head.download_file_path); 1273 temp_path(),
1274 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1018 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1275 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1019 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1276 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1020 raw_ptr_resource_handler_->status().status()); 1277 raw_ptr_resource_handler_->final_status().status());
1021 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1278 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1022 } 1279 }
1023 1280
1024 // Tests that RedirectToFileResourceHandler handles asynchronous write errors. 1281 // Tests that RedirectToFileResourceHandler handles asynchronous write errors.
1025 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) { 1282 TEST_F(ResourceLoaderRedirectToFileTest, WriteErrorAsync) {
1026 file_stream()->set_forced_error_async(net::ERR_FAILED); 1283 file_stream()->set_forced_error_async(net::ERR_FAILED);
1027 1284
1028 // Run it to completion. 1285 // Run it to completion.
1029 loader_->StartRequest(); 1286 loader_->StartRequest();
1030 raw_ptr_resource_handler_->WaitForResponseComplete(); 1287 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1031 1288
1032 // To downstream, the request was canceled sometime after it started, but 1289 // To downstream, the request was canceled sometime after it started, but
1033 // before any data was written. 1290 // before any data was written.
1034 EXPECT_EQ(temp_path(), 1291 EXPECT_EQ(
1035 raw_ptr_resource_handler_->response()->head.download_file_path); 1292 temp_path(),
1293 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1036 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1294 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1037 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1295 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1038 raw_ptr_resource_handler_->status().status()); 1296 raw_ptr_resource_handler_->final_status().status());
1039 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1297 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1040 } 1298 }
1041 1299
1042 // Tests that RedirectToFileHandler defers completion if there are outstanding 1300 // Tests that RedirectToFileHandler defers completion if there are outstanding
1043 // writes and accounts for errors which occur in that time. 1301 // writes and accounts for errors which occur in that time.
1044 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) { 1302 TEST_F(ResourceLoaderRedirectToFileTest, DeferCompletion) {
1045 // Program the MockFileStream to error asynchronously, but throttle the 1303 // Program the MockFileStream to error asynchronously, but throttle the
1046 // callback. 1304 // callback.
1047 file_stream()->set_forced_error_async(net::ERR_FAILED); 1305 file_stream()->set_forced_error_async(net::ERR_FAILED);
1048 file_stream()->ThrottleCallbacks(); 1306 file_stream()->ThrottleCallbacks();
1049 1307
1050 // Run it as far as it will go. 1308 // Run it as far as it will go.
1051 loader_->StartRequest(); 1309 loader_->StartRequest();
1052 base::RunLoop().RunUntilIdle(); 1310 base::RunLoop().RunUntilIdle();
1053 1311
1054 // At this point, the request should have completed. 1312 // At this point, the request should have completed.
1055 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1313 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1056 raw_ptr_to_request_->status().status()); 1314 raw_ptr_to_request_->status().status());
1057 1315
1058 // However, the resource loader stack is stuck somewhere after receiving the 1316 // However, the resource loader stack is stuck somewhere after receiving the
1059 // response. 1317 // response.
1060 EXPECT_EQ(temp_path(), 1318 EXPECT_EQ(
1061 raw_ptr_resource_handler_->response()->head.download_file_path); 1319 temp_path(),
1320 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1062 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1321 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1063 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1322 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1064 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1323 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1065 1324
1066 // Now, release the floodgates. 1325 // Now, release the floodgates.
1067 file_stream()->ReleaseCallbacks(); 1326 file_stream()->ReleaseCallbacks();
1068 raw_ptr_resource_handler_->WaitForResponseComplete(); 1327 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1069 1328
1070 // Although the URLRequest was successful, the leaf handler sees a failure 1329 // Although the URLRequest was successful, the leaf handler sees a failure
1071 // because the write never completed. 1330 // because the write never completed.
1072 EXPECT_EQ(net::URLRequestStatus::CANCELED, 1331 EXPECT_EQ(net::URLRequestStatus::CANCELED,
1073 raw_ptr_resource_handler_->status().status()); 1332 raw_ptr_resource_handler_->final_status().status());
1074 } 1333 }
1075 1334
1076 // Tests that a RedirectToFileResourceHandler behaves properly when the 1335 // Tests that a RedirectToFileResourceHandler behaves properly when the
1077 // downstream handler defers OnWillStart. 1336 // downstream handler defers OnWillStart.
1078 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) { 1337 TEST_F(ResourceLoaderRedirectToFileTest, DownstreamDeferStart) {
1079 // Defer OnWillStart. 1338 // Defer OnWillStart.
1080 raw_ptr_resource_handler_->set_defer_request_on_will_start(true); 1339 raw_ptr_resource_handler_->set_defer_on_will_start(true);
1081 1340
1082 // Run as far as we'll go. 1341 // Run as far as we'll go.
1083 loader_->StartRequest(); 1342 loader_->StartRequest();
1084 raw_ptr_resource_handler_->WaitForDeferredStep(); 1343 raw_ptr_resource_handler_->WaitUntilDeferred();
1085 1344
1086 // The request should have stopped at OnWillStart. 1345 // The request should have stopped at OnWillStart.
1087 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1346 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1088 EXPECT_FALSE(raw_ptr_resource_handler_->response()); 1347 EXPECT_FALSE(raw_ptr_resource_handler_->resource_response());
1089 EXPECT_FALSE(raw_ptr_resource_handler_->received_response_completed()); 1348 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1090 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded()); 1349 EXPECT_EQ(0, raw_ptr_resource_handler_->total_bytes_downloaded());
1091 1350
1092 // Now resume the request. Now we complete. 1351 // Now resume the request. Now we complete.
1093 raw_ptr_resource_handler_->Resume(); 1352 raw_ptr_resource_handler_->Resume();
1094 raw_ptr_resource_handler_->WaitForResponseComplete(); 1353 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1095 1354
1096 // Check that the handler forwarded all information to the downstream handler. 1355 // Check that the handler forwarded all information to the downstream handler.
1097 EXPECT_EQ(temp_path(), 1356 EXPECT_EQ(
1098 raw_ptr_resource_handler_->response()->head.download_file_path); 1357 temp_path(),
1358 raw_ptr_resource_handler_->resource_response()->head.download_file_path);
1099 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url()); 1359 EXPECT_EQ(test_url(), raw_ptr_resource_handler_->start_url());
1100 EXPECT_EQ(net::URLRequestStatus::SUCCESS, 1360 EXPECT_EQ(net::URLRequestStatus::SUCCESS,
1101 raw_ptr_resource_handler_->status().status()); 1361 raw_ptr_resource_handler_->final_status().status());
1102 EXPECT_EQ(test_data().size(), static_cast<size_t>( 1362 EXPECT_EQ(test_data().size(), static_cast<size_t>(
1103 raw_ptr_resource_handler_->total_bytes_downloaded())); 1363 raw_ptr_resource_handler_->total_bytes_downloaded()));
1104 1364
1105 // Check that the data was written to the file. 1365 // Check that the data was written to the file.
1106 std::string contents; 1366 std::string contents;
1107 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); 1367 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents));
1108 EXPECT_EQ(test_data(), contents); 1368 EXPECT_EQ(test_data(), contents);
1109 } 1369 }
1110 1370
1111 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1371 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1112 public: 1372 public:
1113 void VerifyEffectiveConnectionType( 1373 void VerifyEffectiveConnectionType(
1114 ResourceType resource_type, 1374 ResourceType resource_type,
1115 bool belongs_to_main_frame, 1375 bool belongs_to_main_frame,
1116 net::EffectiveConnectionType set_type, 1376 net::EffectiveConnectionType set_type,
1117 net::EffectiveConnectionType expected_type) { 1377 net::EffectiveConnectionType expected_type) {
1118 network_quality_estimator()->set_effective_connection_type(set_type); 1378 network_quality_estimator()->set_effective_connection_type(set_type);
1119 1379
1120 // Start the request and wait for it to finish. 1380 // Start the request and wait for it to finish.
1121 std::unique_ptr<net::URLRequest> request( 1381 std::unique_ptr<net::URLRequest> request(
1122 resource_context_.GetRequestContext()->CreateRequest( 1382 resource_context_.GetRequestContext()->CreateRequest(
1123 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */)); 1383 test_url(), net::DEFAULT_PRIORITY, nullptr /* delegate */));
1124 SetUpResourceLoader(std::move(request), resource_type, 1384 SetUpResourceLoader(std::move(request), resource_type,
1125 belongs_to_main_frame); 1385 belongs_to_main_frame);
1126 1386
1127 // Send the request and wait until it completes. 1387 // Send the request and wait until it completes.
1128 loader_->StartRequest(); 1388 loader_->StartRequest();
1129 raw_ptr_resource_handler_->WaitForResponseComplete(); 1389 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1130 ASSERT_EQ(net::URLRequestStatus::SUCCESS, 1390 ASSERT_EQ(net::URLRequestStatus::SUCCESS,
1131 raw_ptr_to_request_->status().status()); 1391 raw_ptr_to_request_->status().status());
1132 1392
1133 EXPECT_EQ(expected_type, 1393 EXPECT_EQ(expected_type, raw_ptr_resource_handler_->resource_response()
1134 raw_ptr_resource_handler_->observed_effective_connection_type()); 1394 ->head.effective_connection_type);
1135 } 1395 }
1136 }; 1396 };
1137 1397
1138 // Tests that the effective connection type is set on main frame requests. 1398 // Tests that the effective connection type is set on main frame requests.
1139 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) { 1399 TEST_F(EffectiveConnectionTypeResourceLoaderTest, Slow2G) {
1140 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true, 1400 VerifyEffectiveConnectionType(RESOURCE_TYPE_MAIN_FRAME, true,
1141 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, 1401 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G,
1142 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); 1402 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G);
1143 } 1403 }
1144 1404
(...skipping 14 matching lines...) Expand all
1159 1419
1160 // Tests that the effective connection type is not set on non-main frame 1420 // Tests that the effective connection type is not set on non-main frame
1161 // requests. 1421 // requests.
1162 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { 1422 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1163 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, 1423 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false,
1164 net::EFFECTIVE_CONNECTION_TYPE_3G, 1424 net::EFFECTIVE_CONNECTION_TYPE_3G,
1165 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1425 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1166 } 1426 }
1167 1427
1168 } // namespace content 1428 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698