Chromium Code Reviews| 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 "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 protected: | 288 protected: |
| 289 FilePath expected_file_; | 289 FilePath expected_file_; |
| 290 FilePath temp_file_; | 290 FilePath temp_file_; |
| 291 | 291 |
| 292 // Set by the test. Used in OnURLFetchComplete() to decide if | 292 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 293 // the URLFetcher should own the temp file, so that we can test | 293 // the URLFetcher should own the temp file, so that we can test |
| 294 // disowning prevents the file from being deleted. | 294 // disowning prevents the file from being deleted. |
| 295 bool take_ownership_of_temp_file_; | 295 bool take_ownership_of_temp_file_; |
| 296 }; | 296 }; |
| 297 | 297 |
| 298 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | |
| 299 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | |
| 300 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | |
| 301 io_message_loop_proxy())); | |
| 302 | |
| 303 // Use the IO message loop to do the file operations in this test. | |
| 304 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | |
| 305 fetcher_->Start(); | |
| 306 } | |
| 307 | |
| 308 TEST_F(URLFetcherTempFileTest, SmallGet) { | |
| 309 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 310 net::TestServer::kLocalhost, | |
| 311 FilePath(kDocRoot)); | |
| 312 ASSERT_TRUE(test_server.Start()); | |
| 313 | |
| 314 // Get a small file. | |
| 315 static const char kFileToFetch[] = "simple.html"; | |
| 316 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 317 CreateFetcher( | |
| 318 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 319 | |
| 320 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 321 | |
| 322 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 323 << temp_file_.value() << " not removed."; | |
| 324 } | |
| 325 | |
| 326 TEST_F(URLFetcherTempFileTest, LargeGet) { | |
| 327 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 328 net::TestServer::kLocalhost, | |
| 329 FilePath(kDocRoot)); | |
| 330 ASSERT_TRUE(test_server.Start()); | |
| 331 | |
| 332 // Get a file large enough to require more than one read into | |
| 333 // URLFetcher::Core's IOBuffer. | |
| 334 static const char kFileToFetch[] = "animate1.gif"; | |
| 335 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 336 CreateFetcher(test_server.GetURL( | |
| 337 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 338 | |
| 339 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 340 } | |
| 341 | |
| 342 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { | |
| 343 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 344 net::TestServer::kLocalhost, | |
| 345 FilePath(kDocRoot)); | |
| 346 ASSERT_TRUE(test_server.Start()); | |
| 347 | |
| 348 // Get a small file. | |
| 349 static const char kFileToFetch[] = "simple.html"; | |
| 350 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 351 CreateFetcher(test_server.GetURL( | |
| 352 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 353 | |
| 354 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 355 | |
| 356 MessageLoop::current()->RunAllPending(); | |
| 357 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 358 << temp_file_.value() << " not removed."; | |
| 359 } | |
| 360 | |
| 361 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 298 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 362 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); | 299 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
| 363 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 300 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 364 io_message_loop_proxy())); | 301 io_message_loop_proxy())); |
| 365 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 302 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 366 "bobsyeruncle"); | 303 "bobsyeruncle"); |
| 367 fetcher_->Start(); | 304 fetcher_->Start(); |
| 368 } | 305 } |
| 369 | 306 |
| 370 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { | 307 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 594 EXPECT_TRUE(source->GetResponseAsFilePath( | 531 EXPECT_TRUE(source->GetResponseAsFilePath( |
| 595 take_ownership_of_temp_file_, &temp_file_)); | 532 take_ownership_of_temp_file_, &temp_file_)); |
| 596 | 533 |
| 597 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 534 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
| 598 | 535 |
| 599 delete fetcher_; | 536 delete fetcher_; |
| 600 | 537 |
| 601 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 538 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 602 } | 539 } |
| 603 | 540 |
| 541 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | |
| 542 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | |
| 543 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | |
| 544 io_message_loop_proxy())); | |
| 545 | |
| 546 // Use the IO message loop to do the file operations in this test. | |
| 547 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | |
| 548 fetcher_->Start(); | |
| 549 } | |
| 550 | |
| 604 TEST_F(URLFetcherTest, SameThreadsTest) { | 551 TEST_F(URLFetcherTest, SameThreadsTest) { |
| 605 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 552 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 606 net::TestServer::kLocalhost, | 553 net::TestServer::kLocalhost, |
| 607 FilePath(kDocRoot)); | 554 FilePath(kDocRoot)); |
| 608 ASSERT_TRUE(test_server.Start()); | 555 ASSERT_TRUE(test_server.Start()); |
| 609 | 556 |
| 610 // Create the fetcher on the main thread. Since IO will happen on the main | 557 // Create the fetcher on the main thread. Since IO will happen on the main |
| 611 // thread, this will test URLFetcher's ability to do everything on one | 558 // thread, this will test URLFetcher's ability to do everything on one |
| 612 // thread. | 559 // thread. |
| 613 CreateFetcher(test_server.GetURL("defaultresponse")); | 560 CreateFetcher(test_server.GetURL("defaultresponse")); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 635 ASSERT_TRUE(t.Start()); | 582 ASSERT_TRUE(t.Start()); |
| 636 t.message_loop()->PostTask( | 583 t.message_loop()->PostTask( |
| 637 FROM_HERE, | 584 FROM_HERE, |
| 638 base::Bind(&URLFetcherTest::CreateFetcher, | 585 base::Bind(&URLFetcherTest::CreateFetcher, |
| 639 base::Unretained(this), | 586 base::Unretained(this), |
| 640 test_server.GetURL("defaultresponse"))); | 587 test_server.GetURL("defaultresponse"))); |
| 641 | 588 |
| 642 MessageLoop::current()->Run(); | 589 MessageLoop::current()->Run(); |
| 643 } | 590 } |
| 644 | 591 |
| 592 void CancelAllOnIO() { | |
| 593 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | |
| 594 URLFetcherImpl::CancelAll(); | |
| 595 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | |
| 596 } | |
| 597 | |
| 598 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | |
| 599 TEST_F(URLFetcherTest, CancelAll) { | |
| 600 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 601 net::TestServer::kLocalhost, | |
| 602 FilePath(kDocRoot)); | |
| 603 ASSERT_TRUE(test_server.Start()); | |
| 604 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 605 | |
| 606 CreateFetcher(test_server.GetURL("defaultresponse")); | |
| 607 io_message_loop_proxy()->PostTaskAndReply( | |
| 608 FROM_HERE, | |
| 609 base::Bind(&CancelAllOnIO), | |
| 610 MessageLoop::QuitClosure()); | |
| 611 MessageLoop::current()->Run(); | |
| 612 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 613 delete fetcher_; | |
| 614 } | |
| 615 | |
| 645 #if defined(OS_MACOSX) | 616 #if defined(OS_MACOSX) |
| 646 // SIGSEGV on Mac: http://crbug.com/60426 | 617 // SIGSEGV on Mac: http://crbug.com/60426 |
| 647 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | 618 TEST_F(URLFetcherPostTest, DISABLED_Basic) { |
| 648 #else | 619 #else |
| 649 TEST_F(URLFetcherPostTest, Basic) { | 620 TEST_F(URLFetcherPostTest, Basic) { |
| 650 #endif | 621 #endif |
| 651 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 622 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 652 net::TestServer::kLocalhost, | 623 net::TestServer::kLocalhost, |
| 653 FilePath(kDocRoot)); | 624 FilePath(kDocRoot)); |
| 654 ASSERT_TRUE(test_server.Start()); | 625 ASSERT_TRUE(test_server.Start()); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 784 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
| 814 #endif | 785 #endif |
| 815 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 786 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 816 net::TestServer::kLocalhost, | 787 net::TestServer::kLocalhost, |
| 817 FilePath(kDocRoot)); | 788 FilePath(kDocRoot)); |
| 818 ASSERT_TRUE(test_server.Start()); | 789 ASSERT_TRUE(test_server.Start()); |
| 819 | 790 |
| 820 GURL url(test_server.GetURL("files/server-unavailable.html")); | 791 GURL url(test_server.GetURL("files/server-unavailable.html")); |
| 821 | 792 |
| 822 // Registers an entry for test url. The backoff time is calculated by: | 793 // Registers an entry for test url. The backoff time is calculated by: |
| 823 // new_backoff = 2.0 * old_backoff +0 | 794 // new_backoff = 2.0 * old_backoff + 0 |
|
hashimoto
2012/03/02 10:26:42
fixed a tiny format error.
| |
| 824 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 795 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
| 825 // Maximum retries allowed is set to 2. | 796 // Maximum retries allowed is set to 2. |
| 826 net::URLRequestThrottlerManager* manager = | 797 net::URLRequestThrottlerManager* manager = |
| 827 net::URLRequestThrottlerManager::GetInstance(); | 798 net::URLRequestThrottlerManager::GetInstance(); |
| 828 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 799 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
| 829 new net::URLRequestThrottlerEntry( | 800 new net::URLRequestThrottlerEntry( |
| 830 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); | 801 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); |
| 831 manager->OverrideEntryForTests(url, entry); | 802 manager->OverrideEntryForTests(url, entry); |
| 832 | 803 |
| 833 // Create a separate thread that will create the URLFetcher. The current | 804 // Create a separate thread that will create the URLFetcher. The current |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 888 ASSERT_TRUE(test_server.Start()); | 859 ASSERT_TRUE(test_server.Start()); |
| 889 | 860 |
| 890 // Create the fetcher on the main thread. Since IO will happen on the main | 861 // Create the fetcher on the main thread. Since IO will happen on the main |
| 891 // thread, this will test URLFetcher's ability to do everything on one | 862 // thread, this will test URLFetcher's ability to do everything on one |
| 892 // thread. | 863 // thread. |
| 893 CreateFetcher(test_server.GetURL("defaultresponse")); | 864 CreateFetcher(test_server.GetURL("defaultresponse")); |
| 894 | 865 |
| 895 MessageLoop::current()->Run(); | 866 MessageLoop::current()->Run(); |
| 896 } | 867 } |
| 897 | 868 |
| 898 void CancelAllOnIO() { | 869 TEST_F(URLFetcherTempFileTest, SmallGet) { |
| 899 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | |
| 900 URLFetcherImpl::CancelAll(); | |
| 901 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | |
| 902 } | |
| 903 | |
| 904 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | |
| 905 TEST_F(URLFetcherTest, CancelAll) { | |
| 906 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 870 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 907 net::TestServer::kLocalhost, | 871 net::TestServer::kLocalhost, |
| 908 FilePath(kDocRoot)); | 872 FilePath(kDocRoot)); |
| 909 ASSERT_TRUE(test_server.Start()); | 873 ASSERT_TRUE(test_server.Start()); |
| 910 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 911 | 874 |
| 912 CreateFetcher(test_server.GetURL("defaultresponse")); | 875 // Get a small file. |
| 913 io_message_loop_proxy()->PostTaskAndReply( | 876 static const char kFileToFetch[] = "simple.html"; |
| 914 FROM_HERE, | 877 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 915 base::Bind(&CancelAllOnIO), | 878 CreateFetcher( |
| 916 MessageLoop::QuitClosure()); | 879 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 917 MessageLoop::current()->Run(); | 880 |
| 918 EXPECT_EQ(0, GetNumFetcherCores()); | 881 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 919 delete fetcher_; | 882 |
| 883 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 884 << temp_file_.value() << " not removed."; | |
| 885 } | |
| 886 | |
| 887 TEST_F(URLFetcherTempFileTest, LargeGet) { | |
| 888 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 889 net::TestServer::kLocalhost, | |
| 890 FilePath(kDocRoot)); | |
| 891 ASSERT_TRUE(test_server.Start()); | |
| 892 | |
| 893 // Get a file large enough to require more than one read into | |
| 894 // URLFetcher::Core's IOBuffer. | |
| 895 static const char kFileToFetch[] = "animate1.gif"; | |
| 896 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 897 CreateFetcher(test_server.GetURL( | |
| 898 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 899 | |
| 900 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 901 } | |
| 902 | |
| 903 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { | |
| 904 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 905 net::TestServer::kLocalhost, | |
| 906 FilePath(kDocRoot)); | |
| 907 ASSERT_TRUE(test_server.Start()); | |
| 908 | |
| 909 // Get a small file. | |
| 910 static const char kFileToFetch[] = "simple.html"; | |
| 911 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 912 CreateFetcher(test_server.GetURL( | |
| 913 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 914 | |
| 915 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 916 | |
| 917 MessageLoop::current()->RunAllPending(); | |
| 918 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 919 << temp_file_.value() << " not removed."; | |
| 920 } | 920 } |
| 921 | 921 |
| 922 } // namespace. | 922 } // namespace. |
| OLD | NEW |