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 "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" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 13 #include "base/stringprintf.h" | |
| 13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "crypto/nss_util.h" | 17 #include "crypto/nss_util.h" |
| 18 #include "net/base/mock_host_resolver.h" | |
| 19 #include "net/base/network_change_notifier.h" | |
| 17 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 18 #include "net/test/test_server.h" | 21 #include "net/test/test_server.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 22 #include "net/url_request/url_request_throttler_manager.h" | 25 #include "net/url_request/url_request_throttler_manager.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 27 |
| 25 #if defined(USE_NSS) || defined(OS_IOS) | 28 #if defined(USE_NSS) || defined(OS_IOS) |
| 26 #include "net/ocsp/nss_ocsp.h" | 29 #include "net/ocsp/nss_ocsp.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 68 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
| 66 return context_; | 69 return context_; |
| 67 } | 70 } |
| 68 | 71 |
| 69 protected: | 72 protected: |
| 70 virtual ~ThrottlingTestURLRequestContextGetter() {} | 73 virtual ~ThrottlingTestURLRequestContextGetter() {} |
| 71 | 74 |
| 72 TestURLRequestContext* const context_; | 75 TestURLRequestContext* const context_; |
| 73 }; | 76 }; |
| 74 | 77 |
| 78 class TestNetworkChangeNotifier : public NetworkChangeNotifier { | |
|
pauljensen
2012/12/07 23:00:05
Using GMOCK for this might be a few lines shorter,
Joao da Silva
2012/12/10 15:32:42
These tests still need to invoke NotifyObserversOf
| |
| 79 public: | |
| 80 TestNetworkChangeNotifier() : connection_type_(CONNECTION_UNKNOWN) {} | |
| 81 | |
| 82 // Implementation of NetworkChangeNotifier: | |
| 83 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 84 return connection_type_; | |
| 85 } | |
| 86 | |
| 87 void SetCurrentConnectionType(ConnectionType type) { | |
| 88 connection_type_ = type; | |
| 89 NotifyObserversOfConnectionTypeChange(); | |
| 90 } | |
| 91 | |
| 92 private: | |
| 93 ConnectionType connection_type_; | |
| 94 }; | |
| 95 | |
| 75 } // namespace | 96 } // namespace |
| 76 | 97 |
| 77 class URLFetcherTest : public testing::Test, | 98 class URLFetcherTest : public testing::Test, |
| 78 public URLFetcherDelegate { | 99 public URLFetcherDelegate { |
| 79 public: | 100 public: |
| 80 URLFetcherTest() | 101 URLFetcherTest() |
| 81 : fetcher_(NULL), | 102 : fetcher_(NULL), |
| 82 context_(new ThrottlingTestURLRequestContext()) { | 103 context_(new ThrottlingTestURLRequestContext()) { |
| 83 } | 104 } |
| 84 | 105 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 99 void CleanupAfterFetchComplete(); | 120 void CleanupAfterFetchComplete(); |
| 100 | 121 |
| 101 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 122 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
| 102 return io_message_loop_proxy_; | 123 return io_message_loop_proxy_; |
| 103 } | 124 } |
| 104 | 125 |
| 105 TestURLRequestContext* request_context() { | 126 TestURLRequestContext* request_context() { |
| 106 return context_.get(); | 127 return context_.get(); |
| 107 } | 128 } |
| 108 | 129 |
| 130 void UseTestNetworkChangeNotifier() { | |
| 131 disable_default_notifier_.reset(new NetworkChangeNotifier::DisableForTest); | |
| 132 network_change_notifier_.reset(new TestNetworkChangeNotifier); | |
|
pauljensen
2012/12/07 23:00:05
Could |disable_default_notifier_| and |network_cha
Joao da Silva
2012/12/10 15:32:42
Done.
Joao da Silva
2012/12/10 15:32:42
Done.
| |
| 133 } | |
| 134 | |
| 109 protected: | 135 protected: |
| 110 // testing::Test: | 136 // testing::Test: |
| 111 virtual void SetUp() OVERRIDE { | 137 virtual void SetUp() OVERRIDE { |
| 112 testing::Test::SetUp(); | 138 testing::Test::SetUp(); |
| 113 | 139 |
| 114 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 140 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
| 115 | 141 |
| 116 #if defined(USE_NSS) || defined(OS_IOS) | 142 #if defined(USE_NSS) || defined(OS_IOS) |
| 117 crypto::EnsureNSSInit(); | 143 crypto::EnsureNSSInit(); |
| 118 EnsureNSSHttpIOInit(); | 144 EnsureNSSHttpIOInit(); |
| 119 #endif | 145 #endif |
| 120 } | 146 } |
| 121 | 147 |
| 122 virtual void TearDown() OVERRIDE { | 148 virtual void TearDown() OVERRIDE { |
| 123 #if defined(USE_NSS) || defined(OS_IOS) | 149 #if defined(USE_NSS) || defined(OS_IOS) |
| 124 ShutdownNSSHttpIO(); | 150 ShutdownNSSHttpIO(); |
| 125 #endif | 151 #endif |
| 126 } | 152 } |
| 127 | 153 |
| 128 // URLFetcher is designed to run on the main UI thread, but in our tests | 154 // URLFetcher is designed to run on the main UI thread, but in our tests |
| 129 // we assume that the current thread is the IO thread where the URLFetcher | 155 // we assume that the current thread is the IO thread where the URLFetcher |
| 130 // dispatches its requests to. When we wish to simulate being used from | 156 // dispatches its requests to. When we wish to simulate being used from |
| 131 // a UI thread, we dispatch a worker thread to do so. | 157 // a UI thread, we dispatch a worker thread to do so. |
| 132 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 158 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 133 | 159 |
| 134 URLFetcherImpl* fetcher_; | 160 URLFetcherImpl* fetcher_; |
| 135 const scoped_ptr<TestURLRequestContext> context_; | 161 scoped_ptr<TestURLRequestContext> context_; |
| 162 | |
| 163 scoped_ptr<NetworkChangeNotifier::DisableForTest> disable_default_notifier_; | |
| 164 scoped_ptr<TestNetworkChangeNotifier> network_change_notifier_; | |
| 136 }; | 165 }; |
| 137 | 166 |
| 138 void URLFetcherTest::CreateFetcher(const GURL& url) { | 167 void URLFetcherTest::CreateFetcher(const GURL& url) { |
| 139 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 168 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
| 140 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 169 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 141 io_message_loop_proxy(), request_context())); | 170 io_message_loop_proxy(), request_context())); |
| 142 fetcher_->Start(); | |
| 143 } | 171 } |
| 144 | 172 |
| 145 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | 173 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
| 146 EXPECT_TRUE(source->GetStatus().is_success()); | 174 EXPECT_TRUE(source->GetStatus().is_success()); |
| 147 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 175 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| 148 | 176 |
| 149 std::string data; | 177 std::string data; |
| 150 EXPECT_TRUE(source->GetResponseAsString(&data)); | 178 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 151 EXPECT_FALSE(data.empty()); | 179 EXPECT_FALSE(data.empty()); |
| 152 | 180 |
| 153 CleanupAfterFetchComplete(); | 181 CleanupAfterFetchComplete(); |
| 154 } | 182 } |
| 155 | 183 |
| 156 void URLFetcherTest::CleanupAfterFetchComplete() { | 184 void URLFetcherTest::CleanupAfterFetchComplete() { |
| 157 delete fetcher_; // Have to delete this here and not in the destructor, | 185 delete fetcher_; // Have to delete this here and not in the destructor, |
| 158 // because the destructor won't necessarily run on the | 186 // because the destructor won't necessarily run on the |
| 159 // same thread that CreateFetcher() did. | 187 // same thread that CreateFetcher() did. |
| 160 | 188 |
| 161 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 189 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 162 // If the current message loop is not the IO loop, it will be shut down when | 190 // If the current message loop is not the IO loop, it will be shut down when |
| 163 // the main loop returns and this thread subsequently goes out of scope. | 191 // the main loop returns and this thread subsequently goes out of scope. |
| 164 } | 192 } |
| 165 | 193 |
| 166 namespace { | 194 namespace { |
| 167 | 195 |
| 196 class URLFetcherMockDNSTest : public URLFetcherTest { | |
| 197 public: | |
| 198 // testing::Test: | |
| 199 virtual void SetUp() OVERRIDE; | |
| 200 | |
| 201 // URLFetcherDelegate: | |
| 202 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
| 203 | |
| 204 protected: | |
| 205 GURL test_url_; | |
| 206 scoped_ptr<TestServer> test_server_; | |
| 207 MockHostResolver resolver_; | |
| 208 scoped_ptr<URLFetcher> completed_fetcher_; | |
| 209 }; | |
| 210 | |
| 168 // Version of URLFetcherTest that does a POST instead | 211 // Version of URLFetcherTest that does a POST instead |
| 169 class URLFetcherPostTest : public URLFetcherTest { | 212 class URLFetcherPostTest : public URLFetcherTest { |
| 170 public: | 213 public: |
| 171 // URLFetcherTest: | 214 // URLFetcherTest: |
| 172 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 215 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 173 | 216 |
| 174 // URLFetcherDelegate: | 217 // URLFetcherDelegate: |
| 175 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 218 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 176 }; | 219 }; |
| 177 | 220 |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 // Set by the test. Used in OnURLFetchComplete() to decide if | 462 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 420 // the URLFetcher should own the temp file, so that we can test | 463 // the URLFetcher should own the temp file, so that we can test |
| 421 // disowning prevents the file from being deleted. | 464 // disowning prevents the file from being deleted. |
| 422 bool take_ownership_of_file_; | 465 bool take_ownership_of_file_; |
| 423 | 466 |
| 424 // Expected file error code for the test. | 467 // Expected file error code for the test. |
| 425 // PLATFORM_FILE_OK when expecting success. | 468 // PLATFORM_FILE_OK when expecting success. |
| 426 base::PlatformFileError expected_file_error_; | 469 base::PlatformFileError expected_file_error_; |
| 427 }; | 470 }; |
| 428 | 471 |
| 472 void URLFetcherMockDNSTest::SetUp() { | |
| 473 URLFetcherTest::SetUp(); | |
| 474 | |
| 475 context_.reset(); | |
| 476 UseTestNetworkChangeNotifier(); | |
| 477 ASSERT_TRUE(network_change_notifier_); | |
| 478 | |
| 479 resolver_.set_synchronous_mode(false); | |
| 480 resolver_.set_resolve_automatically(false); | |
| 481 resolver_.rules()->AddRule("example.com", "127.0.0.1"); | |
| 482 | |
| 483 context_.reset(new TestURLRequestContext(true)); | |
| 484 context_->set_host_resolver(&resolver_); | |
| 485 context_->Init(); | |
| 486 | |
| 487 test_server_.reset(new TestServer(TestServer::TYPE_HTTP, | |
| 488 TestServer::kLocalhost, | |
| 489 FilePath(kDocRoot))); | |
| 490 ASSERT_TRUE(test_server_->Start()); | |
| 491 | |
| 492 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is | |
| 493 // immediately resolved by the MockHostResolver. Use a hostname instead to | |
| 494 // trigger an async resolve. | |
| 495 test_url_ = GURL( | |
| 496 base::StringPrintf("http://example.com:%d/defaultresponse", | |
| 497 test_server_->host_port_pair().port())); | |
| 498 ASSERT_TRUE(test_url_.is_valid()); | |
| 499 } | |
| 500 | |
| 501 void URLFetcherMockDNSTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 502 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 503 ASSERT_EQ(fetcher_, source); | |
| 504 EXPECT_EQ(test_url_, source->GetOriginalURL()); | |
| 505 completed_fetcher_.reset(fetcher_); | |
| 506 } | |
| 507 | |
| 429 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 508 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 430 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | 509 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); |
| 431 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 510 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 432 io_message_loop_proxy(), request_context())); | 511 io_message_loop_proxy(), request_context())); |
| 433 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 512 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 434 "bobsyeruncle"); | 513 "bobsyeruncle"); |
| 435 fetcher_->Start(); | 514 fetcher_->Start(); |
| 436 } | 515 } |
| 437 | 516 |
| 438 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 517 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 821 CreateFetcher(test_server.GetURL("defaultresponse")); | 900 CreateFetcher(test_server.GetURL("defaultresponse")); |
| 822 io_message_loop_proxy()->PostTaskAndReply( | 901 io_message_loop_proxy()->PostTaskAndReply( |
| 823 FROM_HERE, | 902 FROM_HERE, |
| 824 base::Bind(&CancelAllOnIO), | 903 base::Bind(&CancelAllOnIO), |
| 825 MessageLoop::QuitClosure()); | 904 MessageLoop::QuitClosure()); |
| 826 MessageLoop::current()->Run(); | 905 MessageLoop::current()->Run(); |
| 827 EXPECT_EQ(0, GetNumFetcherCores()); | 906 EXPECT_EQ(0, GetNumFetcherCores()); |
| 828 delete fetcher_; | 907 delete fetcher_; |
| 829 } | 908 } |
| 830 | 909 |
| 910 TEST_F(URLFetcherMockDNSTest, DontRetryOnNetworkChangedByDefault) { | |
| 911 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 912 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 913 | |
| 914 // This posts a task to start the fetcher. | |
| 915 CreateFetcher(test_url_); | |
| 916 fetcher_->Start(); | |
| 917 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 918 MessageLoop::current()->RunUntilIdle(); | |
| 919 | |
| 920 // The fetcher is now running, but is pending the host resolve. | |
| 921 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 922 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 923 ASSERT_FALSE(completed_fetcher_); | |
| 924 | |
| 925 // A network change notification aborts the connect job. | |
| 926 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 927 MessageLoop::current()->RunUntilIdle(); | |
| 928 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 929 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 930 ASSERT_TRUE(completed_fetcher_); | |
| 931 | |
| 932 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
| 933 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
| 934 } | |
| 935 | |
| 936 TEST_F(URLFetcherMockDNSTest, RetryOnNetworkChangedAndFail) { | |
| 937 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 938 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 939 | |
| 940 // This posts a task to start the fetcher. | |
| 941 CreateFetcher(test_url_); | |
| 942 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 943 fetcher_->Start(); | |
| 944 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 945 MessageLoop::current()->RunUntilIdle(); | |
| 946 | |
| 947 // The fetcher is now running, but is pending the host resolve. | |
| 948 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 949 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 950 ASSERT_FALSE(completed_fetcher_); | |
| 951 | |
| 952 // Make it fail 3 times. | |
| 953 for (int i = 0; i < 3; ++i) { | |
| 954 // A network change notification aborts the connect job. | |
| 955 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 956 MessageLoop::current()->RunUntilIdle(); | |
| 957 | |
| 958 // But the fetcher retries automatically. | |
| 959 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 960 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 961 ASSERT_FALSE(completed_fetcher_); | |
| 962 } | |
| 963 | |
| 964 // A 4th failure doesn't trigger another retry, and propagates the error | |
| 965 // to the owner of the fetcher. | |
| 966 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 967 MessageLoop::current()->RunUntilIdle(); | |
| 968 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 969 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 970 ASSERT_TRUE(completed_fetcher_); | |
| 971 | |
| 972 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
| 973 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
| 974 } | |
| 975 | |
| 976 TEST_F(URLFetcherMockDNSTest, RetryOnNetworkChangedAndSucceed) { | |
| 977 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 978 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 979 | |
| 980 // This posts a task to start the fetcher. | |
| 981 CreateFetcher(test_url_); | |
| 982 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 983 fetcher_->Start(); | |
| 984 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 985 MessageLoop::current()->RunUntilIdle(); | |
| 986 | |
| 987 // The fetcher is now running, but is pending the host resolve. | |
| 988 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 989 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 990 ASSERT_FALSE(completed_fetcher_); | |
| 991 | |
| 992 // Make it fail 3 times. | |
| 993 for (int i = 0; i < 3; ++i) { | |
| 994 // A network change notification aborts the connect job. | |
| 995 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 996 MessageLoop::current()->RunUntilIdle(); | |
| 997 | |
| 998 // But the fetcher retries automatically. | |
| 999 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1000 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1001 ASSERT_FALSE(completed_fetcher_); | |
| 1002 } | |
| 1003 | |
| 1004 // Now let it succeed by resolving the pending request. | |
| 1005 resolver_.ResolveAllPending(); | |
| 1006 MessageLoop::current()->Run(); | |
| 1007 | |
| 1008 // URLFetcherMockDNSTest::OnURLFetchComplete() will quit the loop. | |
| 1009 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1010 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1011 ASSERT_TRUE(completed_fetcher_); | |
| 1012 | |
| 1013 // This time the request succeeded. | |
| 1014 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
| 1015 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
| 1016 } | |
| 1017 | |
| 1018 TEST_F(URLFetcherMockDNSTest, RetryAfterComingBackOnline) { | |
| 1019 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1020 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1021 | |
| 1022 // This posts a task to start the fetcher. | |
| 1023 CreateFetcher(test_url_); | |
| 1024 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | |
| 1025 fetcher_->Start(); | |
| 1026 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1027 MessageLoop::current()->RunUntilIdle(); | |
| 1028 | |
| 1029 // The fetcher is now running, but is pending the host resolve. | |
| 1030 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1031 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1032 ASSERT_FALSE(completed_fetcher_); | |
| 1033 | |
| 1034 // Make it fail by changing the connection type to offline. | |
| 1035 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | |
| 1036 NetworkChangeNotifier::GetConnectionType()); | |
| 1037 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
| 1038 network_change_notifier_->SetCurrentConnectionType( | |
| 1039 NetworkChangeNotifier::CONNECTION_NONE); | |
| 1040 // This makes the connect job fail: | |
| 1041 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 1042 resolver_.ResolveAllPending(); | |
| 1043 MessageLoop::current()->RunUntilIdle(); | |
| 1044 | |
| 1045 // The fetcher is now waiting for the connection to become online again. | |
| 1046 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | |
| 1047 NetworkChangeNotifier::GetConnectionType()); | |
| 1048 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
| 1049 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1050 ASSERT_FALSE(completed_fetcher_); | |
| 1051 // The core is still alive, but it dropped its request. | |
| 1052 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1053 | |
| 1054 // It should retry once the connection is back. | |
| 1055 network_change_notifier_->SetCurrentConnectionType( | |
| 1056 NetworkChangeNotifier::CONNECTION_WIFI); | |
| 1057 MessageLoop::current()->RunUntilIdle(); | |
| 1058 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1059 ASSERT_FALSE(completed_fetcher_); | |
| 1060 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1061 | |
| 1062 // Resolve the pending request; the fetcher should complete now. | |
| 1063 resolver_.ResolveAllPending(); | |
| 1064 MessageLoop::current()->Run(); | |
| 1065 | |
| 1066 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1067 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1068 ASSERT_TRUE(completed_fetcher_); | |
| 1069 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
| 1070 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
| 1071 } | |
| 1072 | |
| 831 #if defined(OS_MACOSX) | 1073 #if defined(OS_MACOSX) |
| 832 // SIGSEGV on Mac: http://crbug.com/60426 | 1074 // SIGSEGV on Mac: http://crbug.com/60426 |
| 833 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | 1075 TEST_F(URLFetcherPostTest, DISABLED_Basic) { |
| 834 #else | 1076 #else |
| 835 TEST_F(URLFetcherPostTest, Basic) { | 1077 TEST_F(URLFetcherPostTest, Basic) { |
| 836 #endif | 1078 #endif |
| 837 TestServer test_server(TestServer::TYPE_HTTP, | 1079 TestServer test_server(TestServer::TYPE_HTTP, |
| 838 TestServer::kLocalhost, | 1080 TestServer::kLocalhost, |
| 839 FilePath(kDocRoot)); | 1081 FilePath(kDocRoot)); |
| 840 ASSERT_TRUE(test_server.Start()); | 1082 ASSERT_TRUE(test_server.Start()); |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1271 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1513 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1272 | 1514 |
| 1273 MessageLoop::current()->RunUntilIdle(); | 1515 MessageLoop::current()->RunUntilIdle(); |
| 1274 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1516 ASSERT_FALSE(file_util::PathExists(file_path_)) |
| 1275 << file_path_.value() << " not removed."; | 1517 << file_path_.value() << " not removed."; |
| 1276 } | 1518 } |
| 1277 | 1519 |
| 1278 } // namespace | 1520 } // namespace |
| 1279 | 1521 |
| 1280 } // namespace net | 1522 } // namespace net |
| OLD | NEW |