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" |
26 #include "testing/gmock/include/gmock/gmock.h" | |
szym
2012/12/10 18:36:31
Not necessary.
Joao da Silva
2012/12/11 13:36:43
Done.
| |
23 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
24 | 28 |
25 #if defined(USE_NSS) || defined(OS_IOS) | 29 #if defined(USE_NSS) || defined(OS_IOS) |
26 #include "net/ocsp/nss_ocsp.h" | 30 #include "net/ocsp/nss_ocsp.h" |
27 #endif | 31 #endif |
28 | 32 |
29 namespace net { | 33 namespace net { |
30 | 34 |
31 using base::Time; | 35 using base::Time; |
32 using base::TimeDelta; | 36 using base::TimeDelta; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 69 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
66 return context_; | 70 return context_; |
67 } | 71 } |
68 | 72 |
69 protected: | 73 protected: |
70 virtual ~ThrottlingTestURLRequestContextGetter() {} | 74 virtual ~ThrottlingTestURLRequestContextGetter() {} |
71 | 75 |
72 TestURLRequestContext* const context_; | 76 TestURLRequestContext* const context_; |
73 }; | 77 }; |
74 | 78 |
79 class TestNetworkChangeNotifier : public NetworkChangeNotifier { | |
80 public: | |
81 TestNetworkChangeNotifier() : connection_type_(CONNECTION_UNKNOWN) {} | |
82 | |
83 // Implementation of NetworkChangeNotifier: | |
84 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
85 return connection_type_; | |
86 } | |
87 | |
88 void SetCurrentConnectionType(ConnectionType type) { | |
89 connection_type_ = type; | |
90 NotifyObserversOfConnectionTypeChange(); | |
91 } | |
92 | |
93 private: | |
94 ConnectionType connection_type_; | |
95 }; | |
96 | |
75 } // namespace | 97 } // namespace |
76 | 98 |
77 class URLFetcherTest : public testing::Test, | 99 class URLFetcherTest : public testing::Test, |
78 public URLFetcherDelegate { | 100 public URLFetcherDelegate { |
79 public: | 101 public: |
80 URLFetcherTest() | 102 URLFetcherTest() |
81 : fetcher_(NULL), | 103 : fetcher_(NULL), |
82 context_(new ThrottlingTestURLRequestContext()) { | 104 context_(NULL) { |
83 } | 105 } |
84 | 106 |
85 static int GetNumFetcherCores() { | 107 static int GetNumFetcherCores() { |
86 return URLFetcherImpl::GetNumFetcherCores(); | 108 return URLFetcherImpl::GetNumFetcherCores(); |
87 } | 109 } |
88 | 110 |
89 // Creates a URLFetcher, using the program's main thread to do IO. | 111 // Creates a URLFetcher, using the program's main thread to do IO. |
90 virtual void CreateFetcher(const GURL& url); | 112 virtual void CreateFetcher(const GURL& url); |
91 | 113 |
92 // URLFetcherDelegate: | 114 // URLFetcherDelegate: |
(...skipping 11 matching lines...) Expand all Loading... | |
104 | 126 |
105 TestURLRequestContext* request_context() { | 127 TestURLRequestContext* request_context() { |
106 return context_.get(); | 128 return context_.get(); |
107 } | 129 } |
108 | 130 |
109 protected: | 131 protected: |
110 // testing::Test: | 132 // testing::Test: |
111 virtual void SetUp() OVERRIDE { | 133 virtual void SetUp() OVERRIDE { |
112 testing::Test::SetUp(); | 134 testing::Test::SetUp(); |
113 | 135 |
136 context_.reset(new ThrottlingTestURLRequestContext()); | |
114 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 137 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
115 | 138 |
116 #if defined(USE_NSS) || defined(OS_IOS) | 139 #if defined(USE_NSS) || defined(OS_IOS) |
117 crypto::EnsureNSSInit(); | 140 crypto::EnsureNSSInit(); |
118 EnsureNSSHttpIOInit(); | 141 EnsureNSSHttpIOInit(); |
119 #endif | 142 #endif |
120 } | 143 } |
121 | 144 |
122 virtual void TearDown() OVERRIDE { | 145 virtual void TearDown() OVERRIDE { |
123 #if defined(USE_NSS) || defined(OS_IOS) | 146 #if defined(USE_NSS) || defined(OS_IOS) |
124 ShutdownNSSHttpIO(); | 147 ShutdownNSSHttpIO(); |
125 #endif | 148 #endif |
126 } | 149 } |
127 | 150 |
128 // URLFetcher is designed to run on the main UI thread, but in our tests | 151 // 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 | 152 // 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 | 153 // dispatches its requests to. When we wish to simulate being used from |
131 // a UI thread, we dispatch a worker thread to do so. | 154 // a UI thread, we dispatch a worker thread to do so. |
132 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 155 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
133 | 156 |
134 URLFetcherImpl* fetcher_; | 157 URLFetcherImpl* fetcher_; |
135 const scoped_ptr<TestURLRequestContext> context_; | 158 scoped_ptr<TestURLRequestContext> context_; |
159 }; | |
160 | |
161 // A test fixture that uses a MockHostResolver, so that name resolutions can | |
162 // be manipulated by the tests to keep connections in the resolving state. | |
163 class URLFetcherMockDNSTest : public URLFetcherTest { | |
164 public: | |
165 // testing::Test: | |
166 virtual void SetUp() OVERRIDE; | |
167 | |
168 // URLFetcherTest: | |
169 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
170 | |
171 // URLFetcherDelegate: | |
172 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
173 | |
174 protected: | |
175 GURL test_url_; | |
176 scoped_ptr<TestServer> test_server_; | |
177 MockHostResolver resolver_; | |
178 scoped_ptr<URLFetcher> completed_fetcher_; | |
179 NetworkChangeNotifier::DisableForTest disable_default_notifier_; | |
180 TestNetworkChangeNotifier network_change_notifier_; | |
136 }; | 181 }; |
137 | 182 |
138 void URLFetcherTest::CreateFetcher(const GURL& url) { | 183 void URLFetcherTest::CreateFetcher(const GURL& url) { |
139 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 184 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
140 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 185 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
141 io_message_loop_proxy(), request_context())); | 186 io_message_loop_proxy(), request_context())); |
142 fetcher_->Start(); | 187 fetcher_->Start(); |
143 } | 188 } |
144 | 189 |
145 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | 190 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { |
(...skipping 10 matching lines...) Expand all Loading... | |
156 void URLFetcherTest::CleanupAfterFetchComplete() { | 201 void URLFetcherTest::CleanupAfterFetchComplete() { |
157 delete fetcher_; // Have to delete this here and not in the destructor, | 202 delete fetcher_; // Have to delete this here and not in the destructor, |
158 // because the destructor won't necessarily run on the | 203 // because the destructor won't necessarily run on the |
159 // same thread that CreateFetcher() did. | 204 // same thread that CreateFetcher() did. |
160 | 205 |
161 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 206 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 | 207 // 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. | 208 // the main loop returns and this thread subsequently goes out of scope. |
164 } | 209 } |
165 | 210 |
211 void URLFetcherMockDNSTest::SetUp() { | |
212 URLFetcherTest::SetUp(); | |
213 | |
214 resolver_.set_synchronous_mode(false); | |
215 resolver_.set_resolve_automatically(false); | |
216 resolver_.rules()->AddRule("example.com", "127.0.0.1"); | |
217 | |
218 context_.reset(new TestURLRequestContext(true)); | |
219 context_->set_host_resolver(&resolver_); | |
220 context_->Init(); | |
221 | |
222 test_server_.reset(new TestServer(TestServer::TYPE_HTTP, | |
223 TestServer::kLocalhost, | |
224 FilePath(kDocRoot))); | |
225 ASSERT_TRUE(test_server_->Start()); | |
226 | |
227 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is | |
228 // immediately resolved by the MockHostResolver. Use a hostname instead to | |
229 // trigger an async resolve. | |
230 test_url_ = GURL( | |
231 base::StringPrintf("http://example.com:%d/defaultresponse", | |
232 test_server_->host_port_pair().port())); | |
233 ASSERT_TRUE(test_url_.is_valid()); | |
234 } | |
235 | |
236 void URLFetcherMockDNSTest::CreateFetcher(const GURL& url) { | |
237 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
238 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
239 io_message_loop_proxy(), request_context())); | |
240 } | |
241 | |
242 void URLFetcherMockDNSTest::OnURLFetchComplete(const URLFetcher* source) { | |
243 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
244 ASSERT_EQ(fetcher_, source); | |
245 EXPECT_EQ(test_url_, source->GetOriginalURL()); | |
246 completed_fetcher_.reset(fetcher_); | |
247 } | |
248 | |
166 namespace { | 249 namespace { |
167 | 250 |
168 // Version of URLFetcherTest that does a POST instead | 251 // Version of URLFetcherTest that does a POST instead |
169 class URLFetcherPostTest : public URLFetcherTest { | 252 class URLFetcherPostTest : public URLFetcherTest { |
170 public: | 253 public: |
171 // URLFetcherTest: | 254 // URLFetcherTest: |
172 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 255 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
173 | 256 |
174 // URLFetcherDelegate: | 257 // URLFetcherDelegate: |
175 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 258 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); | 660 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); |
578 EXPECT_EQ(301, source->GetResponseCode()); | 661 EXPECT_EQ(301, source->GetResponseCode()); |
579 CleanupAfterFetchComplete(); | 662 CleanupAfterFetchComplete(); |
580 } | 663 } |
581 | 664 |
582 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 665 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
583 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 666 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
584 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 667 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
585 io_message_loop_proxy(), request_context())); | 668 io_message_loop_proxy(), request_context())); |
586 start_time_ = Time::Now(); | 669 start_time_ = Time::Now(); |
587 fetcher_->SetMaxRetries(11); | 670 fetcher_->SetMaxRetriesOn5xx(11); |
588 fetcher_->Start(); | 671 fetcher_->Start(); |
589 } | 672 } |
590 | 673 |
591 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { | 674 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { |
592 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 675 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
593 if (source->GetResponseCode() >= 500) { | 676 if (source->GetResponseCode() >= 500) { |
594 // Now running ServerUnavailable test. | 677 // Now running ServerUnavailable test. |
595 // It takes more than 1 second to finish all 11 requests. | 678 // It takes more than 1 second to finish all 11 requests. |
596 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 679 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
597 EXPECT_TRUE(source->GetStatus().is_success()); | 680 EXPECT_TRUE(source->GetStatus().is_success()); |
(...skipping 18 matching lines...) Expand all Loading... | |
616 } | 699 } |
617 } | 700 } |
618 } | 701 } |
619 | 702 |
620 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 703 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
621 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 704 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
622 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 705 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
623 io_message_loop_proxy(), request_context())); | 706 io_message_loop_proxy(), request_context())); |
624 fetcher_->SetAutomaticallyRetryOn5xx(false); | 707 fetcher_->SetAutomaticallyRetryOn5xx(false); |
625 start_time_ = Time::Now(); | 708 start_time_ = Time::Now(); |
626 fetcher_->SetMaxRetries(11); | 709 fetcher_->SetMaxRetriesOn5xx(11); |
627 fetcher_->Start(); | 710 fetcher_->Start(); |
628 } | 711 } |
629 | 712 |
630 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 713 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
631 const URLFetcher* source) { | 714 const URLFetcher* source) { |
632 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | 715 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); |
633 if (source->GetResponseCode() >= 500) { | 716 if (source->GetResponseCode() >= 500) { |
634 // Now running ServerUnavailable test. | 717 // Now running ServerUnavailable test. |
635 // It should get here on the first attempt, so almost immediately and | 718 // It should get here on the first attempt, so almost immediately and |
636 // *not* to attempt to execute all 11 requests (2.5 minutes). | 719 // *not* to attempt to execute all 11 requests (2.5 minutes). |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
675 EXPECT_TRUE(data.empty()); | 758 EXPECT_TRUE(data.empty()); |
676 CleanupAfterFetchComplete(); | 759 CleanupAfterFetchComplete(); |
677 } | 760 } |
678 | 761 |
679 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 762 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
680 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 763 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
681 CancelTestURLRequestContextGetter* context_getter = | 764 CancelTestURLRequestContextGetter* context_getter = |
682 new CancelTestURLRequestContextGetter(io_message_loop_proxy(), | 765 new CancelTestURLRequestContextGetter(io_message_loop_proxy(), |
683 url); | 766 url); |
684 fetcher_->SetRequestContext(context_getter); | 767 fetcher_->SetRequestContext(context_getter); |
685 fetcher_->SetMaxRetries(2); | 768 fetcher_->SetMaxRetriesOn5xx(2); |
686 fetcher_->Start(); | 769 fetcher_->Start(); |
687 // We need to wait for the creation of the URLRequestContext, since we | 770 // We need to wait for the creation of the URLRequestContext, since we |
688 // rely on it being destroyed as a signal to end the test. | 771 // rely on it being destroyed as a signal to end the test. |
689 context_getter->WaitForContextCreation(); | 772 context_getter->WaitForContextCreation(); |
690 CancelRequest(); | 773 CancelRequest(); |
691 } | 774 } |
692 | 775 |
693 void URLFetcherCancelTest::OnURLFetchComplete( | 776 void URLFetcherCancelTest::OnURLFetchComplete( |
694 const URLFetcher* source) { | 777 const URLFetcher* source) { |
695 // We should have cancelled the request before completion. | 778 // We should have cancelled the request before completion. |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 CreateFetcher(test_server.GetURL("defaultresponse")); | 904 CreateFetcher(test_server.GetURL("defaultresponse")); |
822 io_message_loop_proxy()->PostTaskAndReply( | 905 io_message_loop_proxy()->PostTaskAndReply( |
823 FROM_HERE, | 906 FROM_HERE, |
824 base::Bind(&CancelAllOnIO), | 907 base::Bind(&CancelAllOnIO), |
825 MessageLoop::QuitClosure()); | 908 MessageLoop::QuitClosure()); |
826 MessageLoop::current()->Run(); | 909 MessageLoop::current()->Run(); |
827 EXPECT_EQ(0, GetNumFetcherCores()); | 910 EXPECT_EQ(0, GetNumFetcherCores()); |
828 delete fetcher_; | 911 delete fetcher_; |
829 } | 912 } |
830 | 913 |
914 TEST_F(URLFetcherMockDNSTest, DontRetryOnNetworkChangedByDefault) { | |
915 EXPECT_EQ(0, GetNumFetcherCores()); | |
916 EXPECT_FALSE(resolver_.has_pending_requests()); | |
917 | |
918 // This posts a task to start the fetcher. | |
919 CreateFetcher(test_url_); | |
920 fetcher_->Start(); | |
921 EXPECT_EQ(0, GetNumFetcherCores()); | |
922 MessageLoop::current()->RunUntilIdle(); | |
923 | |
924 // The fetcher is now running, but is pending the host resolve. | |
925 EXPECT_EQ(1, GetNumFetcherCores()); | |
926 EXPECT_TRUE(resolver_.has_pending_requests()); | |
927 ASSERT_FALSE(completed_fetcher_); | |
928 | |
929 // A network change notification aborts the connect job. | |
930 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
931 MessageLoop::current()->RunUntilIdle(); | |
932 EXPECT_EQ(0, GetNumFetcherCores()); | |
933 EXPECT_FALSE(resolver_.has_pending_requests()); | |
934 ASSERT_TRUE(completed_fetcher_); | |
935 | |
936 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
937 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
938 } | |
939 | |
940 TEST_F(URLFetcherMockDNSTest, RetryOnNetworkChangedAndFail) { | |
941 EXPECT_EQ(0, GetNumFetcherCores()); | |
942 EXPECT_FALSE(resolver_.has_pending_requests()); | |
943 | |
944 // This posts a task to start the fetcher. | |
945 CreateFetcher(test_url_); | |
946 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
947 fetcher_->Start(); | |
948 EXPECT_EQ(0, GetNumFetcherCores()); | |
949 MessageLoop::current()->RunUntilIdle(); | |
950 | |
951 // The fetcher is now running, but is pending the host resolve. | |
952 EXPECT_EQ(1, GetNumFetcherCores()); | |
953 EXPECT_TRUE(resolver_.has_pending_requests()); | |
954 ASSERT_FALSE(completed_fetcher_); | |
955 | |
956 // Make it fail 3 times. | |
957 for (int i = 0; i < 3; ++i) { | |
958 // A network change notification aborts the connect job. | |
959 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
960 MessageLoop::current()->RunUntilIdle(); | |
961 | |
962 // But the fetcher retries automatically. | |
963 EXPECT_EQ(1, GetNumFetcherCores()); | |
964 EXPECT_TRUE(resolver_.has_pending_requests()); | |
965 ASSERT_FALSE(completed_fetcher_); | |
966 } | |
967 | |
968 // A 4th failure doesn't trigger another retry, and propagates the error | |
969 // to the owner of the fetcher. | |
970 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
971 MessageLoop::current()->RunUntilIdle(); | |
972 EXPECT_EQ(0, GetNumFetcherCores()); | |
973 EXPECT_FALSE(resolver_.has_pending_requests()); | |
974 ASSERT_TRUE(completed_fetcher_); | |
975 | |
976 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
977 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
978 } | |
979 | |
980 TEST_F(URLFetcherMockDNSTest, RetryOnNetworkChangedAndSucceed) { | |
981 EXPECT_EQ(0, GetNumFetcherCores()); | |
982 EXPECT_FALSE(resolver_.has_pending_requests()); | |
983 | |
984 // This posts a task to start the fetcher. | |
985 CreateFetcher(test_url_); | |
986 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
987 fetcher_->Start(); | |
988 EXPECT_EQ(0, GetNumFetcherCores()); | |
989 MessageLoop::current()->RunUntilIdle(); | |
990 | |
991 // The fetcher is now running, but is pending the host resolve. | |
992 EXPECT_EQ(1, GetNumFetcherCores()); | |
993 EXPECT_TRUE(resolver_.has_pending_requests()); | |
994 ASSERT_FALSE(completed_fetcher_); | |
995 | |
996 // Make it fail 3 times. | |
997 for (int i = 0; i < 3; ++i) { | |
998 // A network change notification aborts the connect job. | |
999 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
1000 MessageLoop::current()->RunUntilIdle(); | |
1001 | |
1002 // But the fetcher retries automatically. | |
1003 EXPECT_EQ(1, GetNumFetcherCores()); | |
1004 EXPECT_TRUE(resolver_.has_pending_requests()); | |
1005 ASSERT_FALSE(completed_fetcher_); | |
1006 } | |
1007 | |
1008 // Now let it succeed by resolving the pending request. | |
1009 resolver_.ResolveAllPending(); | |
1010 MessageLoop::current()->Run(); | |
1011 | |
1012 // URLFetcherMockDNSTest::OnURLFetchComplete() will quit the loop. | |
1013 EXPECT_EQ(0, GetNumFetcherCores()); | |
1014 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1015 ASSERT_TRUE(completed_fetcher_); | |
1016 | |
1017 // This time the request succeeded. | |
1018 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
1019 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
1020 } | |
1021 | |
1022 TEST_F(URLFetcherMockDNSTest, RetryAfterComingBackOnline) { | |
1023 EXPECT_EQ(0, GetNumFetcherCores()); | |
1024 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1025 | |
1026 // This posts a task to start the fetcher. | |
1027 CreateFetcher(test_url_); | |
1028 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | |
1029 fetcher_->Start(); | |
1030 EXPECT_EQ(0, GetNumFetcherCores()); | |
1031 MessageLoop::current()->RunUntilIdle(); | |
1032 | |
1033 // The fetcher is now running, but is pending the host resolve. | |
1034 EXPECT_EQ(1, GetNumFetcherCores()); | |
1035 EXPECT_TRUE(resolver_.has_pending_requests()); | |
1036 ASSERT_FALSE(completed_fetcher_); | |
1037 | |
1038 // Make it fail by changing the connection type to offline. | |
1039 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | |
1040 NetworkChangeNotifier::GetConnectionType()); | |
1041 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
1042 network_change_notifier_.SetCurrentConnectionType( | |
1043 NetworkChangeNotifier::CONNECTION_NONE); | |
1044 // This makes the connect job fail: | |
1045 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
1046 resolver_.ResolveAllPending(); | |
1047 MessageLoop::current()->RunUntilIdle(); | |
1048 | |
1049 // The fetcher is now waiting for the connection to become online again. | |
1050 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | |
1051 NetworkChangeNotifier::GetConnectionType()); | |
1052 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
1053 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1054 ASSERT_FALSE(completed_fetcher_); | |
1055 // The core is still alive, but it dropped its request. | |
1056 EXPECT_EQ(0, GetNumFetcherCores()); | |
1057 | |
1058 // It should retry once the connection is back. | |
1059 network_change_notifier_.SetCurrentConnectionType( | |
1060 NetworkChangeNotifier::CONNECTION_WIFI); | |
1061 MessageLoop::current()->RunUntilIdle(); | |
1062 EXPECT_EQ(1, GetNumFetcherCores()); | |
1063 ASSERT_FALSE(completed_fetcher_); | |
1064 EXPECT_TRUE(resolver_.has_pending_requests()); | |
1065 | |
1066 // Resolve the pending request; the fetcher should complete now. | |
1067 resolver_.ResolveAllPending(); | |
1068 MessageLoop::current()->Run(); | |
1069 | |
1070 EXPECT_EQ(0, GetNumFetcherCores()); | |
1071 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1072 ASSERT_TRUE(completed_fetcher_); | |
1073 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
1074 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
1075 } | |
1076 | |
1077 TEST_F(URLFetcherMockDNSTest, StartOnlyWhenOnline) { | |
1078 // Start offline. | |
1079 network_change_notifier_.SetCurrentConnectionType( | |
1080 NetworkChangeNotifier::CONNECTION_NONE); | |
1081 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
1082 EXPECT_EQ(0, GetNumFetcherCores()); | |
1083 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1084 | |
1085 // Create a fetcher that retries on network changes. It will try to connect | |
1086 // only once the network is back online. | |
1087 CreateFetcher(test_url_); | |
1088 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | |
1089 fetcher_->Start(); | |
1090 MessageLoop::current()->RunUntilIdle(); | |
1091 EXPECT_EQ(0, GetNumFetcherCores()); | |
1092 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1093 | |
1094 // It should retry once the connection is back. | |
1095 network_change_notifier_.SetCurrentConnectionType( | |
1096 NetworkChangeNotifier::CONNECTION_WIFI); | |
1097 MessageLoop::current()->RunUntilIdle(); | |
1098 EXPECT_EQ(1, GetNumFetcherCores()); | |
1099 ASSERT_FALSE(completed_fetcher_); | |
1100 EXPECT_TRUE(resolver_.has_pending_requests()); | |
1101 | |
1102 // Resolve the pending request; the fetcher should complete now. | |
1103 resolver_.ResolveAllPending(); | |
1104 MessageLoop::current()->Run(); | |
1105 | |
1106 EXPECT_EQ(0, GetNumFetcherCores()); | |
1107 EXPECT_FALSE(resolver_.has_pending_requests()); | |
1108 ASSERT_TRUE(completed_fetcher_); | |
1109 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
1110 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
1111 } | |
1112 | |
831 #if defined(OS_MACOSX) | 1113 #if defined(OS_MACOSX) |
832 // SIGSEGV on Mac: http://crbug.com/60426 | 1114 // SIGSEGV on Mac: http://crbug.com/60426 |
833 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | 1115 TEST_F(URLFetcherPostTest, DISABLED_Basic) { |
834 #else | 1116 #else |
835 TEST_F(URLFetcherPostTest, Basic) { | 1117 TEST_F(URLFetcherPostTest, Basic) { |
836 #endif | 1118 #endif |
837 TestServer test_server(TestServer::TYPE_HTTP, | 1119 TestServer test_server(TestServer::TYPE_HTTP, |
838 TestServer::kLocalhost, | 1120 TestServer::kLocalhost, |
839 FilePath(kDocRoot)); | 1121 FilePath(kDocRoot)); |
840 ASSERT_TRUE(test_server.Start()); | 1122 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(). | 1553 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1272 | 1554 |
1273 MessageLoop::current()->RunUntilIdle(); | 1555 MessageLoop::current()->RunUntilIdle(); |
1274 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1556 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1275 << file_path_.value() << " not removed."; | 1557 << file_path_.value() << " not removed."; |
1276 } | 1558 } |
1277 | 1559 |
1278 } // namespace | 1560 } // namespace |
1279 | 1561 |
1280 } // namespace net | 1562 } // namespace net |
OLD | NEW |