OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 // a UI thread, we dispatch a worker thread to do so. | 129 // a UI thread, we dispatch a worker thread to do so. |
130 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 130 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
131 | 131 |
132 URLFetcherImpl* fetcher_; | 132 URLFetcherImpl* fetcher_; |
133 const scoped_ptr<TestURLRequestContext> context_; | 133 const scoped_ptr<TestURLRequestContext> context_; |
134 }; | 134 }; |
135 | 135 |
136 void URLFetcherTest::CreateFetcher(const GURL& url) { | 136 void URLFetcherTest::CreateFetcher(const GURL& url) { |
137 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 137 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
138 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 138 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
139 io_message_loop_proxy(), request_context())); | 139 io_message_loop_proxy().get(), request_context())); |
140 fetcher_->Start(); | 140 fetcher_->Start(); |
141 } | 141 } |
142 | 142 |
143 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | 143 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
144 EXPECT_TRUE(source->GetStatus().is_success()); | 144 EXPECT_TRUE(source->GetStatus().is_success()); |
145 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 145 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
146 | 146 |
147 std::string data; | 147 std::string data; |
148 EXPECT_TRUE(source->GetResponseAsString(&data)); | 148 EXPECT_TRUE(source->GetResponseAsString(&data)); |
149 EXPECT_FALSE(data.empty()); | 149 EXPECT_FALSE(data.empty()); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 | 339 |
340 // Registers an entry for test url. The backoff time is calculated by: | 340 // Registers an entry for test url. The backoff time is calculated by: |
341 // new_backoff = 2.0 * old_backoff + 0 | 341 // new_backoff = 2.0 * old_backoff + 0 |
342 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 342 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
343 // Maximum retries allowed is set to 2. | 343 // Maximum retries allowed is set to 2. |
344 scoped_refptr<URLRequestThrottlerEntry> entry( | 344 scoped_refptr<URLRequestThrottlerEntry> entry( |
345 new URLRequestThrottlerEntry( | 345 new URLRequestThrottlerEntry( |
346 context_->throttler_manager(), | 346 context_->throttler_manager(), |
347 "", 200, 3, 2000, 2.0, 0.0, 4000)); | 347 "", 200, 3, 2000, 2.0, 0.0, 4000)); |
348 context_->throttler_manager()->OverrideEntryForTests( | 348 context_->throttler_manager()->OverrideEntryForTests( |
349 throttle_for_url_, entry); | 349 throttle_for_url_, entry.get()); |
350 | 350 |
351 context_created_.Signal(); | 351 context_created_.Signal(); |
352 } | 352 } |
353 return context_.get(); | 353 return context_.get(); |
354 } | 354 } |
355 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 355 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
356 return io_message_loop_proxy_; | 356 return io_message_loop_proxy_; |
357 } | 357 } |
358 void WaitForContextCreation() { | 358 void WaitForContextCreation() { |
359 context_created_.Wait(); | 359 context_created_.Wait(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 bool take_ownership_of_file_; | 399 bool take_ownership_of_file_; |
400 | 400 |
401 // Expected file error code for the test. | 401 // Expected file error code for the test. |
402 // PLATFORM_FILE_OK when expecting success. | 402 // PLATFORM_FILE_OK when expecting success. |
403 base::PlatformFileError expected_file_error_; | 403 base::PlatformFileError expected_file_error_; |
404 }; | 404 }; |
405 | 405 |
406 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 406 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
407 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 407 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
408 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 408 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
409 io_message_loop_proxy(), request_context())); | 409 io_message_loop_proxy().get(), request_context())); |
410 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 410 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
411 "bobsyeruncle"); | 411 "bobsyeruncle"); |
412 fetcher_->Start(); | 412 fetcher_->Start(); |
413 } | 413 } |
414 | 414 |
415 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 415 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
416 std::string data; | 416 std::string data; |
417 EXPECT_TRUE(source->GetResponseAsString(&data)); | 417 EXPECT_TRUE(source->GetResponseAsString(&data)); |
418 EXPECT_EQ(std::string("bobsyeruncle"), data); | 418 EXPECT_EQ(std::string("bobsyeruncle"), data); |
419 URLFetcherTest::OnURLFetchComplete(source); | 419 URLFetcherTest::OnURLFetchComplete(source); |
(...skipping 16 matching lines...) Expand all Loading... |
436 EXPECT_TRUE(data.empty()); | 436 EXPECT_TRUE(data.empty()); |
437 | 437 |
438 CleanupAfterFetchComplete(); | 438 CleanupAfterFetchComplete(); |
439 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, | 439 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, |
440 // since it expects a non-empty response. | 440 // since it expects a non-empty response. |
441 } | 441 } |
442 | 442 |
443 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | 443 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
444 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 444 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
445 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 445 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
446 io_message_loop_proxy(), request_context())); | 446 io_message_loop_proxy().get(), request_context())); |
447 previous_progress_ = 0; | 447 previous_progress_ = 0; |
448 fetcher_->Start(); | 448 fetcher_->Start(); |
449 } | 449 } |
450 | 450 |
451 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | 451 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
452 const URLFetcher* source, int64 current, int64 total) { | 452 const URLFetcher* source, int64 current, int64 total) { |
453 // Increasing between 0 and total. | 453 // Increasing between 0 and total. |
454 EXPECT_LE(0, current); | 454 EXPECT_LE(0, current); |
455 EXPECT_GE(total, current); | 455 EXPECT_GE(total, current); |
456 EXPECT_LE(previous_progress_, current); | 456 EXPECT_LE(previous_progress_, current); |
457 previous_progress_ = current; | 457 previous_progress_ = current; |
458 EXPECT_EQ(expected_total_, total); | 458 EXPECT_EQ(expected_total_, total); |
459 } | 459 } |
460 | 460 |
461 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | 461 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
462 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 462 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
463 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 463 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
464 io_message_loop_proxy(), request_context())); | 464 io_message_loop_proxy().get(), request_context())); |
465 cancelled_ = false; | 465 cancelled_ = false; |
466 fetcher_->Start(); | 466 fetcher_->Start(); |
467 } | 467 } |
468 | 468 |
469 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( | 469 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( |
470 const URLFetcher* source, int64 current, int64 total) { | 470 const URLFetcher* source, int64 current, int64 total) { |
471 EXPECT_FALSE(cancelled_); | 471 EXPECT_FALSE(cancelled_); |
472 if (!cancelled_) { | 472 if (!cancelled_) { |
473 cancelled_ = true; | 473 cancelled_ = true; |
474 CleanupAfterFetchComplete(); | 474 CleanupAfterFetchComplete(); |
475 } | 475 } |
476 } | 476 } |
477 | 477 |
478 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( | 478 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( |
479 const URLFetcher* source) { | 479 const URLFetcher* source) { |
480 // Should have been cancelled. | 480 // Should have been cancelled. |
481 ADD_FAILURE(); | 481 ADD_FAILURE(); |
482 CleanupAfterFetchComplete(); | 482 CleanupAfterFetchComplete(); |
483 } | 483 } |
484 | 484 |
485 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { | 485 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { |
486 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 486 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
487 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 487 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
488 io_message_loop_proxy(), request_context())); | 488 io_message_loop_proxy().get(), request_context())); |
489 previous_progress_ = 0; | 489 previous_progress_ = 0; |
490 // Large enough data to require more than one read from UploadDataStream. | 490 // Large enough data to require more than one read from UploadDataStream. |
491 chunk_.assign(1<<16, 'a'); | 491 chunk_.assign(1<<16, 'a'); |
492 // Use chunked upload to wait for a timer event of progress notification. | 492 // Use chunked upload to wait for a timer event of progress notification. |
493 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); | 493 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); |
494 fetcher_->Start(); | 494 fetcher_->Start(); |
495 number_of_chunks_added_ = 1; | 495 number_of_chunks_added_ = 1; |
496 fetcher_->AppendChunkToUpload(chunk_, false); | 496 fetcher_->AppendChunkToUpload(chunk_, false); |
497 } | 497 } |
498 | 498 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest() | 535 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest() |
536 : callback_called_(false) { | 536 : callback_called_(false) { |
537 } | 537 } |
538 | 538 |
539 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() { | 539 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() { |
540 } | 540 } |
541 | 541 |
542 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) { | 542 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) { |
543 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 543 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
544 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 544 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
545 io_message_loop_proxy(), request_context())); | 545 io_message_loop_proxy().get(), request_context())); |
546 fetcher_->SetStopOnRedirect(true); | 546 fetcher_->SetStopOnRedirect(true); |
547 fetcher_->Start(); | 547 fetcher_->Start(); |
548 } | 548 } |
549 | 549 |
550 void URLFetcherStopOnRedirectTest::OnURLFetchComplete( | 550 void URLFetcherStopOnRedirectTest::OnURLFetchComplete( |
551 const URLFetcher* source) { | 551 const URLFetcher* source) { |
552 callback_called_ = true; | 552 callback_called_ = true; |
553 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL()); | 553 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL()); |
554 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); | 554 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); |
555 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); | 555 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); |
556 EXPECT_EQ(301, source->GetResponseCode()); | 556 EXPECT_EQ(301, source->GetResponseCode()); |
557 CleanupAfterFetchComplete(); | 557 CleanupAfterFetchComplete(); |
558 } | 558 } |
559 | 559 |
560 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 560 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
561 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 561 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
562 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 562 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
563 io_message_loop_proxy(), request_context())); | 563 io_message_loop_proxy().get(), request_context())); |
564 start_time_ = Time::Now(); | 564 start_time_ = Time::Now(); |
565 fetcher_->SetMaxRetries(11); | 565 fetcher_->SetMaxRetries(11); |
566 fetcher_->Start(); | 566 fetcher_->Start(); |
567 } | 567 } |
568 | 568 |
569 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { | 569 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { |
570 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 570 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
571 if (source->GetResponseCode() >= 500) { | 571 if (source->GetResponseCode() >= 500) { |
572 // Now running ServerUnavailable test. | 572 // Now running ServerUnavailable test. |
573 // It takes more than 1 second to finish all 11 requests. | 573 // It takes more than 1 second to finish all 11 requests. |
574 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 574 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
575 EXPECT_TRUE(source->GetStatus().is_success()); | 575 EXPECT_TRUE(source->GetStatus().is_success()); |
576 std::string data; | 576 std::string data; |
577 EXPECT_TRUE(source->GetResponseAsString(&data)); | 577 EXPECT_TRUE(source->GetResponseAsString(&data)); |
578 EXPECT_FALSE(data.empty()); | 578 EXPECT_FALSE(data.empty()); |
579 CleanupAfterFetchComplete(); | 579 CleanupAfterFetchComplete(); |
580 } else { | 580 } else { |
581 // Now running Overload test. | 581 // Now running Overload test. |
582 static int count = 0; | 582 static int count = 0; |
583 count++; | 583 count++; |
584 if (count < 20) { | 584 if (count < 20) { |
585 fetcher_->SetRequestContext( | 585 fetcher_->SetRequestContext( |
586 new ThrottlingTestURLRequestContextGetter( | 586 new ThrottlingTestURLRequestContextGetter( |
587 io_message_loop_proxy(), request_context())); | 587 io_message_loop_proxy().get(), request_context())); |
588 fetcher_->Start(); | 588 fetcher_->Start(); |
589 } else { | 589 } else { |
590 // We have already sent 20 requests continuously. And we expect that | 590 // We have already sent 20 requests continuously. And we expect that |
591 // it takes more than 1 second due to the overload protection settings. | 591 // it takes more than 1 second due to the overload protection settings. |
592 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 592 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
593 URLFetcherTest::OnURLFetchComplete(source); | 593 URLFetcherTest::OnURLFetchComplete(source); |
594 } | 594 } |
595 } | 595 } |
596 } | 596 } |
597 | 597 |
598 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 598 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
599 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 599 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
600 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 600 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
601 io_message_loop_proxy(), request_context())); | 601 io_message_loop_proxy().get(), request_context())); |
602 fetcher_->SetAutomaticallyRetryOn5xx(false); | 602 fetcher_->SetAutomaticallyRetryOn5xx(false); |
603 start_time_ = Time::Now(); | 603 start_time_ = Time::Now(); |
604 fetcher_->SetMaxRetries(11); | 604 fetcher_->SetMaxRetries(11); |
605 fetcher_->Start(); | 605 fetcher_->Start(); |
606 } | 606 } |
607 | 607 |
608 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 608 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
609 const URLFetcher* source) { | 609 const URLFetcher* source) { |
610 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | 610 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); |
611 if (source->GetResponseCode() >= 500) { | 611 if (source->GetResponseCode() >= 500) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 EXPECT_TRUE(source->GetCookies().empty()); | 650 EXPECT_TRUE(source->GetCookies().empty()); |
651 std::string data; | 651 std::string data; |
652 EXPECT_TRUE(source->GetResponseAsString(&data)); | 652 EXPECT_TRUE(source->GetResponseAsString(&data)); |
653 EXPECT_TRUE(data.empty()); | 653 EXPECT_TRUE(data.empty()); |
654 CleanupAfterFetchComplete(); | 654 CleanupAfterFetchComplete(); |
655 } | 655 } |
656 | 656 |
657 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 657 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
658 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 658 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
659 CancelTestURLRequestContextGetter* context_getter = | 659 CancelTestURLRequestContextGetter* context_getter = |
660 new CancelTestURLRequestContextGetter(io_message_loop_proxy(), | 660 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), |
661 url); | 661 url); |
662 fetcher_->SetRequestContext(context_getter); | 662 fetcher_->SetRequestContext(context_getter); |
663 fetcher_->SetMaxRetries(2); | 663 fetcher_->SetMaxRetries(2); |
664 fetcher_->Start(); | 664 fetcher_->Start(); |
665 // We need to wait for the creation of the URLRequestContext, since we | 665 // We need to wait for the creation of the URLRequestContext, since we |
666 // rely on it being destroyed as a signal to end the test. | 666 // rely on it being destroyed as a signal to end the test. |
667 context_getter->WaitForContextCreation(); | 667 context_getter->WaitForContextCreation(); |
668 CancelRequest(); | 668 CancelRequest(); |
669 } | 669 } |
670 | 670 |
(...skipping 14 matching lines...) Expand all Loading... |
685 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | 685 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( |
686 const URLFetcher* source) { | 686 const URLFetcher* source) { |
687 EXPECT_TRUE(source->GetStatus().is_success()); | 687 EXPECT_TRUE(source->GetStatus().is_success()); |
688 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 688 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
689 std::string data; | 689 std::string data; |
690 EXPECT_TRUE(source->GetResponseAsString(&data)); | 690 EXPECT_TRUE(source->GetResponseAsString(&data)); |
691 EXPECT_FALSE(data.empty()); | 691 EXPECT_FALSE(data.empty()); |
692 if (!data.empty() && data_.empty()) { | 692 if (!data.empty() && data_.empty()) { |
693 data_ = data; | 693 data_ = data; |
694 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 694 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
695 io_message_loop_proxy(), request_context())); | 695 io_message_loop_proxy().get(), request_context())); |
696 fetcher_->Start(); | 696 fetcher_->Start(); |
697 } else { | 697 } else { |
698 EXPECT_EQ(data, data_); | 698 EXPECT_EQ(data, data_); |
699 CleanupAfterFetchComplete(); | 699 CleanupAfterFetchComplete(); |
700 } | 700 } |
701 } | 701 } |
702 | 702 |
703 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, | 703 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, |
704 const FilePath& file_path) { | 704 const FilePath& file_path) { |
705 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 705 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
706 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 706 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
707 io_message_loop_proxy(), request_context())); | 707 io_message_loop_proxy().get(), request_context())); |
708 | 708 |
709 // Use the IO message loop to do the file operations in this test. | 709 // Use the IO message loop to do the file operations in this test. |
710 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); | 710 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); |
711 fetcher_->Start(); | 711 fetcher_->Start(); |
712 } | 712 } |
713 | 713 |
714 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { | 714 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { |
715 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 715 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
716 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 716 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
717 io_message_loop_proxy(), request_context())); | 717 io_message_loop_proxy().get(), request_context())); |
718 | 718 |
719 // Use the IO message loop to do the file operations in this test. | 719 // Use the IO message loop to do the file operations in this test. |
720 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | 720 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
721 fetcher_->Start(); | 721 fetcher_->Start(); |
722 } | 722 } |
723 | 723 |
724 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) { | 724 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) { |
725 if (expected_file_error_ == base::PLATFORM_FILE_OK) { | 725 if (expected_file_error_ == base::PLATFORM_FILE_OK) { |
726 EXPECT_TRUE(source->GetStatus().is_success()); | 726 EXPECT_TRUE(source->GetStatus().is_success()); |
727 EXPECT_EQ(source->GetResponseCode(), 200); | 727 EXPECT_EQ(source->GetResponseCode(), 200); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 ASSERT_TRUE(test_server.Start()); | 923 ASSERT_TRUE(test_server.Start()); |
924 | 924 |
925 GURL url(test_server.GetURL("defaultresponse")); | 925 GURL url(test_server.GetURL("defaultresponse")); |
926 | 926 |
927 // Registers an entry for test url. It only allows 3 requests to be sent | 927 // Registers an entry for test url. It only allows 3 requests to be sent |
928 // in 200 milliseconds. | 928 // in 200 milliseconds. |
929 scoped_refptr<URLRequestThrottlerEntry> entry( | 929 scoped_refptr<URLRequestThrottlerEntry> entry( |
930 new URLRequestThrottlerEntry( | 930 new URLRequestThrottlerEntry( |
931 request_context()->throttler_manager(), | 931 request_context()->throttler_manager(), |
932 "", 200, 3, 1, 2.0, 0.0, 256)); | 932 "", 200, 3, 1, 2.0, 0.0, 256)); |
933 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | 933 request_context()->throttler_manager()-> |
| 934 OverrideEntryForTests(url, entry.get()); |
934 | 935 |
935 CreateFetcher(url); | 936 CreateFetcher(url); |
936 | 937 |
937 MessageLoop::current()->Run(); | 938 MessageLoop::current()->Run(); |
938 } | 939 } |
939 | 940 |
940 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | 941 TEST_F(URLFetcherProtectTest, ServerUnavailable) { |
941 TestServer test_server(TestServer::TYPE_HTTP, | 942 TestServer test_server(TestServer::TYPE_HTTP, |
942 TestServer::kLocalhost, | 943 TestServer::kLocalhost, |
943 FilePath(kDocRoot)); | 944 FilePath(kDocRoot)); |
944 ASSERT_TRUE(test_server.Start()); | 945 ASSERT_TRUE(test_server.Start()); |
945 | 946 |
946 GURL url(test_server.GetURL("files/server-unavailable.html")); | 947 GURL url(test_server.GetURL("files/server-unavailable.html")); |
947 | 948 |
948 // Registers an entry for test url. The backoff time is calculated by: | 949 // Registers an entry for test url. The backoff time is calculated by: |
949 // new_backoff = 2.0 * old_backoff + 0 | 950 // new_backoff = 2.0 * old_backoff + 0 |
950 // and maximum backoff time is 256 milliseconds. | 951 // and maximum backoff time is 256 milliseconds. |
951 // Maximum retries allowed is set to 11. | 952 // Maximum retries allowed is set to 11. |
952 scoped_refptr<URLRequestThrottlerEntry> entry( | 953 scoped_refptr<URLRequestThrottlerEntry> entry( |
953 new URLRequestThrottlerEntry( | 954 new URLRequestThrottlerEntry( |
954 request_context()->throttler_manager(), | 955 request_context()->throttler_manager(), |
955 "", 200, 3, 1, 2.0, 0.0, 256)); | 956 "", 200, 3, 1, 2.0, 0.0, 256)); |
956 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | 957 request_context()->throttler_manager()-> |
| 958 OverrideEntryForTests(url, entry.get()); |
957 | 959 |
958 CreateFetcher(url); | 960 CreateFetcher(url); |
959 | 961 |
960 MessageLoop::current()->Run(); | 962 MessageLoop::current()->Run(); |
961 } | 963 } |
962 | 964 |
963 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | 965 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { |
964 TestServer test_server(TestServer::TYPE_HTTP, | 966 TestServer test_server(TestServer::TYPE_HTTP, |
965 TestServer::kLocalhost, | 967 TestServer::kLocalhost, |
966 FilePath(kDocRoot)); | 968 FilePath(kDocRoot)); |
967 ASSERT_TRUE(test_server.Start()); | 969 ASSERT_TRUE(test_server.Start()); |
968 | 970 |
969 GURL url(test_server.GetURL("files/server-unavailable.html")); | 971 GURL url(test_server.GetURL("files/server-unavailable.html")); |
970 | 972 |
971 // Registers an entry for test url. The backoff time is calculated by: | 973 // Registers an entry for test url. The backoff time is calculated by: |
972 // new_backoff = 2.0 * old_backoff + 0 | 974 // new_backoff = 2.0 * old_backoff + 0 |
973 // and maximum backoff time is 150000 milliseconds. | 975 // and maximum backoff time is 150000 milliseconds. |
974 // Maximum retries allowed is set to 11. | 976 // Maximum retries allowed is set to 11. |
975 scoped_refptr<URLRequestThrottlerEntry> entry( | 977 scoped_refptr<URLRequestThrottlerEntry> entry( |
976 new URLRequestThrottlerEntry( | 978 new URLRequestThrottlerEntry( |
977 request_context()->throttler_manager(), | 979 request_context()->throttler_manager(), |
978 "", 200, 3, 100, 2.0, 0.0, 150000)); | 980 "", 200, 3, 100, 2.0, 0.0, 150000)); |
979 // Total time if *not* for not doing automatic backoff would be 150s. | 981 // Total time if *not* for not doing automatic backoff would be 150s. |
980 // In reality it should be "as soon as server responds". | 982 // In reality it should be "as soon as server responds". |
981 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | 983 request_context()->throttler_manager()-> |
| 984 OverrideEntryForTests(url, entry.get()); |
982 | 985 |
983 CreateFetcher(url); | 986 CreateFetcher(url); |
984 | 987 |
985 MessageLoop::current()->Run(); | 988 MessageLoop::current()->Run(); |
986 } | 989 } |
987 | 990 |
988 #if defined(OS_MACOSX) | 991 #if defined(OS_MACOSX) |
989 // SIGSEGV on Mac: http://crbug.com/60426 | 992 // SIGSEGV on Mac: http://crbug.com/60426 |
990 TEST_F(URLFetcherBadHTTPSTest, DISABLED_BadHTTPSTest) { | 993 TEST_F(URLFetcherBadHTTPSTest, DISABLED_BadHTTPSTest) { |
991 #else | 994 #else |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 | 1041 |
1039 GURL url(test_server.GetURL("files/server-unavailable.html")); | 1042 GURL url(test_server.GetURL("files/server-unavailable.html")); |
1040 | 1043 |
1041 // Register an entry for test url. | 1044 // Register an entry for test url. |
1042 // Using a sliding window of 4 seconds, and max of 1 request, under a fast | 1045 // Using a sliding window of 4 seconds, and max of 1 request, under a fast |
1043 // run we expect to have a 4 second delay when posting the Start task. | 1046 // run we expect to have a 4 second delay when posting the Start task. |
1044 scoped_refptr<URLRequestThrottlerEntry> entry( | 1047 scoped_refptr<URLRequestThrottlerEntry> entry( |
1045 new URLRequestThrottlerEntry( | 1048 new URLRequestThrottlerEntry( |
1046 request_context()->throttler_manager(), | 1049 request_context()->throttler_manager(), |
1047 "", 4000, 1, 2000, 2.0, 0.0, 4000)); | 1050 "", 4000, 1, 2000, 2.0, 0.0, 4000)); |
1048 request_context()->throttler_manager()->OverrideEntryForTests(url, entry); | 1051 request_context()->throttler_manager()-> |
| 1052 OverrideEntryForTests(url, entry.get()); |
1049 // Fake that a request has just started. | 1053 // Fake that a request has just started. |
1050 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); | 1054 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); |
1051 | 1055 |
1052 // The next request we try to send will be delayed by ~4 seconds. | 1056 // The next request we try to send will be delayed by ~4 seconds. |
1053 // The slower the test runs, the less the delay will be (since it takes the | 1057 // The slower the test runs, the less the delay will be (since it takes the |
1054 // time difference from now). | 1058 // time difference from now). |
1055 | 1059 |
1056 base::Thread t("URLFetcher test thread"); | 1060 base::Thread t("URLFetcher test thread"); |
1057 ASSERT_TRUE(t.Start()); | 1061 ASSERT_TRUE(t.Start()); |
1058 t.message_loop()->PostTask( | 1062 t.message_loop()->PostTask( |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1249 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1246 | 1250 |
1247 MessageLoop::current()->RunAllPending(); | 1251 MessageLoop::current()->RunAllPending(); |
1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1252 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1249 << file_path_.value() << " not removed."; | 1253 << file_path_.value() << " not removed."; |
1250 } | 1254 } |
1251 | 1255 |
1252 } // namespace | 1256 } // namespace |
1253 | 1257 |
1254 } // namespace net | 1258 } // namespace net |
OLD | NEW |