OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "chrome/common/net/url_fetcher.h" | 9 #include "chrome/common/net/url_fetcher.h" |
10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
11 #include "net/test/test_server.h" | 11 #include "net/test/test_server.h" |
12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
13 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
14 #include "net/url_request/url_request_throttler_manager.h" | 14 #include "net/url_request/url_request_throttler_manager.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 #if defined(USE_NSS) | 17 #if defined(USE_NSS) |
18 #include "net/ocsp/nss_ocsp.h" | 18 #include "net/ocsp/nss_ocsp.h" |
19 #endif | 19 #endif |
20 | 20 |
21 using base::Time; | 21 using base::Time; |
22 using base::TimeDelta; | 22 using base::TimeDelta; |
23 | 23 |
24 // TODO(eroman): Add a regression test for http://crbug.com/40505. | 24 // TODO(eroman): Add a regression test for http://crbug.com/40505. |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); | 28 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); |
| 29 const std::string kTestServerFilePrefix = "files/"; |
29 | 30 |
30 class CurriedTask : public Task { | 31 class CurriedTask : public Task { |
31 public: | 32 public: |
32 CurriedTask(Task* task, MessageLoop* target_loop) | 33 CurriedTask(Task* task, MessageLoop* target_loop) |
33 : task_(task), | 34 : task_(task), |
34 target_loop_(target_loop) {} | 35 target_loop_(target_loop) {} |
35 | 36 |
36 virtual void Run() { | 37 virtual void Run() { |
37 target_loop_->PostTask(FROM_HERE, task_); | 38 target_loop_->PostTask(FROM_HERE, task_); |
38 } | 39 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 MessageLoopForIO io_loop_; | 118 MessageLoopForIO io_loop_; |
118 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 119 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
119 | 120 |
120 URLFetcher* fetcher_; | 121 URLFetcher* fetcher_; |
121 }; | 122 }; |
122 | 123 |
123 void URLFetcherTest::CreateFetcher(const GURL& url) { | 124 void URLFetcherTest::CreateFetcher(const GURL& url) { |
124 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 125 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
125 fetcher_->set_request_context(new TestURLRequestContextGetter( | 126 fetcher_->set_request_context(new TestURLRequestContextGetter( |
126 io_message_loop_proxy())); | 127 io_message_loop_proxy())); |
| 128 |
| 129 // Do file ops on the io thead. |
127 fetcher_->Start(); | 130 fetcher_->Start(); |
128 } | 131 } |
129 | 132 |
130 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, | 133 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, |
131 const GURL& url, | 134 const GURL& url, |
132 const net::URLRequestStatus& status, | 135 const net::URLRequestStatus& status, |
133 int response_code, | 136 int response_code, |
134 const net::ResponseCookies& cookies, | 137 const net::ResponseCookies& cookies, |
135 const std::string& data) { | 138 const std::string& data) { |
136 EXPECT_TRUE(status.is_success()); | 139 EXPECT_TRUE(status.is_success()); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 virtual void OnURLFetchComplete(const URLFetcher* source, | 301 virtual void OnURLFetchComplete(const URLFetcher* source, |
299 const GURL& url, | 302 const GURL& url, |
300 const net::URLRequestStatus& status, | 303 const net::URLRequestStatus& status, |
301 int response_code, | 304 int response_code, |
302 const net::ResponseCookies& cookies, | 305 const net::ResponseCookies& cookies, |
303 const std::string& data); | 306 const std::string& data); |
304 private: | 307 private: |
305 std::string data_; | 308 std::string data_; |
306 }; | 309 }; |
307 | 310 |
| 311 class URLFetcherTempFileTest : public URLFetcherTest { |
| 312 public: |
| 313 // URLFetcher::Delegate |
| 314 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 315 const GURL& url, |
| 316 const net::URLRequestStatus& status, |
| 317 int response_code, |
| 318 const net::ResponseCookies& cookies); |
| 319 |
| 320 virtual void CreateFetcher(const GURL& url); |
| 321 |
| 322 protected: |
| 323 FilePath expected_file_; |
| 324 FilePath temp_file_; |
| 325 }; |
| 326 |
| 327 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { |
| 328 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 329 fetcher_->set_request_context(new TestURLRequestContextGetter( |
| 330 io_message_loop_proxy())); |
| 331 |
| 332 // Use the IO message loop to do the file operations in this test. |
| 333 fetcher_->set_file_message_loop_proxy( |
| 334 io_message_loop_proxy()); |
| 335 |
| 336 fetcher_->set_response_destination(URLFetcher::TEMP_FILE); |
| 337 fetcher_->Start(); |
| 338 } |
| 339 |
| 340 TEST_F(URLFetcherTempFileTest, SmallGet) { |
| 341 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 342 ASSERT_TRUE(test_server.Start()); |
| 343 |
| 344 // Get a small file. |
| 345 const char* kFileToFetch = "simple.html"; |
| 346 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 347 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); |
| 348 |
| 349 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 350 |
| 351 ASSERT_TRUE(file_util::PathExists(temp_file_)) |
| 352 << temp_file_.value() << " not found."; |
| 353 |
| 354 // Deleting the fetcher should remove the file. |
| 355 delete fetcher_; |
| 356 |
| 357 MessageLoop::current()->RunAllPending(); |
| 358 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
| 359 << temp_file_.value() << " not removed."; |
| 360 } |
| 361 |
| 362 TEST_F(URLFetcherTempFileTest, LargeGet) { |
| 363 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 364 ASSERT_TRUE(test_server.Start()); |
| 365 |
| 366 // Get a file large enough to require more than one read into |
| 367 // URLFetcher::Core's IOBuffer. |
| 368 const char* kFileToFetch = "animate1.gif"; |
| 369 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 370 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); |
| 371 |
| 372 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 373 } |
| 374 |
308 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We | 375 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We |
309 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call | 376 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call |
310 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit | 377 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit |
311 // confusing in that case because GTest doesn't know about the refcounting. | 378 // confusing in that case because GTest doesn't know about the refcounting. |
312 // It's less confusing to just do it this way. | 379 // It's less confusing to just do it this way. |
313 class FetcherWrapperTask : public Task { | 380 class FetcherWrapperTask : public Task { |
314 public: | 381 public: |
315 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) | 382 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) |
316 : test_(test), url_(url) { } | 383 : test_(test), url_(url) { } |
317 virtual void Run() { | 384 virtual void Run() { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 delete fetcher_; // Have to delete this here and not in the destructor, | 599 delete fetcher_; // Have to delete this here and not in the destructor, |
533 // because the destructor won't necessarily run on the | 600 // because the destructor won't necessarily run on the |
534 // same thread that CreateFetcher() did. | 601 // same thread that CreateFetcher() did. |
535 | 602 |
536 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 603 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
537 // If the current message loop is not the IO loop, it will be shut down when | 604 // If the current message loop is not the IO loop, it will be shut down when |
538 // the main loop returns and this thread subsequently goes out of scope. | 605 // the main loop returns and this thread subsequently goes out of scope. |
539 } | 606 } |
540 } | 607 } |
541 | 608 |
| 609 void URLFetcherTempFileTest::OnURLFetchComplete( |
| 610 const URLFetcher* source, |
| 611 const GURL& url, |
| 612 const net::URLRequestStatus& status, |
| 613 int response_code, |
| 614 const net::ResponseCookies& cookies) { |
| 615 ASSERT_TRUE(source->GetResponseAsFilePath(&temp_file_)); |
| 616 |
| 617 ASSERT_TRUE(file_util::PathExists(temp_file_)) |
| 618 << temp_file_.value() << " not found."; |
| 619 ASSERT_TRUE(file_util::PathExists(expected_file_)) |
| 620 << expected_file_.value() << " not found."; |
| 621 ASSERT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
| 622 |
| 623 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 624 } |
| 625 |
542 TEST_F(URLFetcherTest, SameThreadsTest) { | 626 TEST_F(URLFetcherTest, SameThreadsTest) { |
543 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 627 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
544 ASSERT_TRUE(test_server.Start()); | 628 ASSERT_TRUE(test_server.Start()); |
545 | 629 |
546 // Create the fetcher on the main thread. Since IO will happen on the main | 630 // Create the fetcher on the main thread. Since IO will happen on the main |
547 // thread, this will test URLFetcher's ability to do everything on one | 631 // thread, this will test URLFetcher's ability to do everything on one |
548 // thread. | 632 // thread. |
549 CreateFetcher(test_server.GetURL("defaultresponse")); | 633 CreateFetcher(test_server.GetURL("defaultresponse")); |
550 | 634 |
551 MessageLoop::current()->Run(); | 635 MessageLoop::current()->Run(); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) { | 782 TEST_F(URLFetcherCancelTest, DISABLED_ReleasesContext) { |
699 #else | 783 #else |
700 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 784 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
701 #endif | 785 #endif |
702 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 786 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
703 ASSERT_TRUE(test_server.Start()); | 787 ASSERT_TRUE(test_server.Start()); |
704 | 788 |
705 GURL url(test_server.GetURL("files/server-unavailable.html")); | 789 GURL url(test_server.GetURL("files/server-unavailable.html")); |
706 | 790 |
707 // Registers an entry for test url. The backoff time is calculated by: | 791 // Registers an entry for test url. The backoff time is calculated by: |
708 // new_backoff = 2.0 * old_backoff + 0 | 792 // new_backoff = 2.0 * old_backoff +0 |
709 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 793 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
710 // Maximum retries allowed is set to 2. | 794 // Maximum retries allowed is set to 2. |
711 net::URLRequestThrottlerManager* manager = | 795 net::URLRequestThrottlerManager* manager = |
712 net::URLRequestThrottlerManager::GetInstance(); | 796 net::URLRequestThrottlerManager::GetInstance(); |
713 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 797 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
714 new net::URLRequestThrottlerEntry( | 798 new net::URLRequestThrottlerEntry( |
715 manager, 200, 3, 2000, 2.0, 0.0, 4000)); | 799 manager, 200, 3, 2000, 2.0, 0.0, 4000)); |
716 manager->OverrideEntryForTests(url, entry); | 800 manager->OverrideEntryForTests(url, entry); |
717 | 801 |
718 // Create a separate thread that will create the URLFetcher. The current | 802 // Create a separate thread that will create the URLFetcher. The current |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 FROM_HERE, | 867 FROM_HERE, |
784 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); | 868 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); |
785 MessageLoop::current()->Run(); | 869 MessageLoop::current()->Run(); |
786 EXPECT_EQ(1, GetNumFetcherCores()); | 870 EXPECT_EQ(1, GetNumFetcherCores()); |
787 URLFetcher::CancelAll(); | 871 URLFetcher::CancelAll(); |
788 EXPECT_EQ(0, GetNumFetcherCores()); | 872 EXPECT_EQ(0, GetNumFetcherCores()); |
789 delete fetcher_; | 873 delete fetcher_; |
790 } | 874 } |
791 | 875 |
792 } // namespace. | 876 } // namespace. |
OLD | NEW |