| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // TODO(akalin): Move all the test data to somewhere under net/. | 47 // TODO(akalin): Move all the test data to somewhere under net/. |
| 48 const base::FilePath::CharType kDocRoot[] = | 48 const base::FilePath::CharType kDocRoot[] = |
| 49 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); | 49 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); |
| 50 const char kTestServerFilePrefix[] = "files/"; | 50 const char kTestServerFilePrefix[] = "files/"; |
| 51 | 51 |
| 52 class ThrottlingTestURLRequestContext : public TestURLRequestContext { | 52 class ThrottlingTestURLRequestContext : public TestURLRequestContext { |
| 53 public: | 53 public: |
| 54 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { | 54 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { |
| 55 set_throttler_manager(&throttler_manager_); | 55 set_throttler_manager(&throttler_manager_); |
| 56 Init(); | 56 Init(); |
| 57 DCHECK(throttler_manager() != NULL); | 57 DCHECK(throttler_manager() != nullptr); |
| 58 } | 58 } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 URLRequestThrottlerManager throttler_manager_; | 61 URLRequestThrottlerManager throttler_manager_; |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 class ThrottlingTestURLRequestContextGetter | 64 class ThrottlingTestURLRequestContextGetter |
| 65 : public TestURLRequestContextGetter { | 65 : public TestURLRequestContextGetter { |
| 66 public: | 66 public: |
| 67 ThrottlingTestURLRequestContextGetter( | 67 ThrottlingTestURLRequestContextGetter( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 78 ~ThrottlingTestURLRequestContextGetter() override {} | 78 ~ThrottlingTestURLRequestContextGetter() override {} |
| 79 | 79 |
| 80 TestURLRequestContext* const context_; | 80 TestURLRequestContext* const context_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 } // namespace | 83 } // namespace |
| 84 | 84 |
| 85 class URLFetcherTest : public testing::Test, | 85 class URLFetcherTest : public testing::Test, |
| 86 public URLFetcherDelegate { | 86 public URLFetcherDelegate { |
| 87 public: | 87 public: |
| 88 URLFetcherTest() : fetcher_(NULL), expected_status_code_(200) {} | 88 URLFetcherTest() |
| 89 : io_message_loop_proxy_(base::MessageLoopProxy::current()), |
| 90 fetcher_(nullptr), |
| 91 expected_status_code_(200) {} |
| 89 | 92 |
| 90 static int GetNumFetcherCores() { | 93 static int GetNumFetcherCores() { |
| 91 return URLFetcherImpl::GetNumFetcherCores(); | 94 return URLFetcherImpl::GetNumFetcherCores(); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // Creates a URLFetcher, using the program's main thread to do IO. | 97 // Creates a URLFetcher, using the program's main thread to do IO. |
| 95 virtual void CreateFetcher(const GURL& url); | 98 virtual void CreateFetcher(const GURL& url); |
| 96 | 99 |
| 97 // URLFetcherDelegate: | 100 // URLFetcherDelegate: |
| 98 // Subclasses that override this should either call this function or | 101 // Subclasses that override this should either call this function or |
| 99 // CleanupAfterFetchComplete() at the end of their processing, depending on | 102 // CleanupAfterFetchComplete() at the end of their processing, depending on |
| 100 // whether they want to check for a non-empty HTTP 200 response or not. | 103 // whether they want to check for a non-empty HTTP 200 response or not. |
| 101 void OnURLFetchComplete(const URLFetcher* source) override; | 104 void OnURLFetchComplete(const URLFetcher* source) override; |
| 102 | 105 |
| 103 // Deletes |fetcher| and terminates the message loop. | 106 // Deletes |fetcher| and terminates the message loop. |
| 104 void CleanupAfterFetchComplete(); | 107 void CleanupAfterFetchComplete(); |
| 105 | 108 |
| 106 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 109 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
| 107 return io_message_loop_proxy_; | 110 return io_message_loop_proxy_; |
| 108 } | 111 } |
| 109 | 112 |
| 110 TestURLRequestContext* request_context() { | 113 TestURLRequestContext* request_context() { |
| 111 return context_.get(); | 114 return context_.get(); |
| 112 } | 115 } |
| 113 | 116 |
| 114 protected: | 117 protected: |
| 115 // testing::Test: | 118 // testing::Test: |
| 116 void SetUp() override { | 119 void SetUp() override { |
| 117 testing::Test::SetUp(); | 120 SetUpServer(); |
| 121 ASSERT_TRUE(test_server_->Start()); |
| 118 | 122 |
| 119 context_.reset(new ThrottlingTestURLRequestContext()); | 123 context_.reset(new ThrottlingTestURLRequestContext()); |
| 120 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | |
| 121 | 124 |
| 122 #if defined(USE_NSS) || defined(OS_IOS) | 125 #if defined(USE_NSS) || defined(OS_IOS) |
| 123 crypto::EnsureNSSInit(); | 126 crypto::EnsureNSSInit(); |
| 124 EnsureNSSHttpIOInit(); | 127 EnsureNSSHttpIOInit(); |
| 125 #endif | 128 #endif |
| 126 } | 129 } |
| 127 | 130 |
| 128 void TearDown() override { | 131 void TearDown() override { |
| 129 #if defined(USE_NSS) || defined(OS_IOS) | 132 #if defined(USE_NSS) || defined(OS_IOS) |
| 130 ShutdownNSSHttpIO(); | 133 ShutdownNSSHttpIO(); |
| 131 #endif | 134 #endif |
| 132 } | 135 } |
| 133 | 136 |
| 137 // Initializes |test_server_| without starting it. Allows subclasses to use |
| 138 // their own server configuration. |
| 139 virtual void SetUpServer() { |
| 140 test_server_.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 141 SpawnedTestServer::kLocalhost, |
| 142 base::FilePath(kDocRoot))); |
| 143 } |
| 144 |
| 134 // URLFetcher is designed to run on the main UI thread, but in our tests | 145 // URLFetcher is designed to run on the main UI thread, but in our tests |
| 135 // we assume that the current thread is the IO thread where the URLFetcher | 146 // we assume that the current thread is the IO thread where the URLFetcher |
| 136 // dispatches its requests to. When we wish to simulate being used from | 147 // dispatches its requests to. When we wish to simulate being used from |
| 137 // a UI thread, we dispatch a worker thread to do so. | 148 // a UI thread, we dispatch a worker thread to do so. |
| 138 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 149 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 139 | 150 |
| 151 scoped_ptr<SpawnedTestServer> test_server_; |
| 152 |
| 140 URLFetcherImpl* fetcher_; | 153 URLFetcherImpl* fetcher_; |
| 141 scoped_ptr<TestURLRequestContext> context_; | 154 scoped_ptr<TestURLRequestContext> context_; |
| 142 int expected_status_code_; | 155 int expected_status_code_; |
| 143 }; | 156 }; |
| 144 | 157 |
| 145 // A test fixture that uses a MockHostResolver, so that name resolutions can | 158 // A test fixture that uses a MockHostResolver, so that name resolutions can |
| 146 // be manipulated by the tests to keep connections in the resolving state. | 159 // be manipulated by the tests to keep connections in the resolving state. |
| 147 class URLFetcherMockDnsTest : public URLFetcherTest { | 160 class URLFetcherMockDnsTest : public URLFetcherTest { |
| 148 public: | 161 public: |
| 149 // testing::Test: | 162 // testing::Test: |
| 150 void SetUp() override; | 163 void SetUp() override; |
| 151 | 164 |
| 152 // URLFetcherTest: | 165 // URLFetcherTest: |
| 153 void CreateFetcher(const GURL& url) override; | 166 void CreateFetcher(const GURL& url) override; |
| 154 | 167 |
| 155 // URLFetcherDelegate: | 168 // URLFetcherDelegate: |
| 156 void OnURLFetchComplete(const URLFetcher* source) override; | 169 void OnURLFetchComplete(const URLFetcher* source) override; |
| 157 | 170 |
| 158 protected: | 171 protected: |
| 159 GURL test_url_; | 172 GURL test_url_; |
| 160 scoped_ptr<SpawnedTestServer> test_server_; | |
| 161 MockHostResolver resolver_; | 173 MockHostResolver resolver_; |
| 162 scoped_ptr<URLFetcher> completed_fetcher_; | 174 scoped_ptr<URLFetcher> completed_fetcher_; |
| 163 }; | 175 }; |
| 164 | 176 |
| 165 void URLFetcherTest::CreateFetcher(const GURL& url) { | 177 void URLFetcherTest::CreateFetcher(const GURL& url) { |
| 166 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 178 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
| 167 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 179 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| 168 io_message_loop_proxy().get(), request_context())); | 180 io_message_loop_proxy().get(), request_context())); |
| 169 fetcher_->Start(); | 181 fetcher_->Start(); |
| 170 } | 182 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 194 void URLFetcherMockDnsTest::SetUp() { | 206 void URLFetcherMockDnsTest::SetUp() { |
| 195 URLFetcherTest::SetUp(); | 207 URLFetcherTest::SetUp(); |
| 196 | 208 |
| 197 resolver_.set_ondemand_mode(true); | 209 resolver_.set_ondemand_mode(true); |
| 198 resolver_.rules()->AddRule("example.com", "127.0.0.1"); | 210 resolver_.rules()->AddRule("example.com", "127.0.0.1"); |
| 199 | 211 |
| 200 context_.reset(new TestURLRequestContext(true)); | 212 context_.reset(new TestURLRequestContext(true)); |
| 201 context_->set_host_resolver(&resolver_); | 213 context_->set_host_resolver(&resolver_); |
| 202 context_->Init(); | 214 context_->Init(); |
| 203 | 215 |
| 204 test_server_.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | |
| 205 SpawnedTestServer::kLocalhost, | |
| 206 base::FilePath(kDocRoot))); | |
| 207 ASSERT_TRUE(test_server_->Start()); | |
| 208 | |
| 209 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is | 216 // test_server_.GetURL() returns a URL with 127.0.0.1 (kLocalhost), that is |
| 210 // immediately resolved by the MockHostResolver. Use a hostname instead to | 217 // immediately resolved by the MockHostResolver. Use a hostname instead to |
| 211 // trigger an async resolve. | 218 // trigger an async resolve. |
| 212 test_url_ = GURL( | 219 test_url_ = GURL( |
| 213 base::StringPrintf("http://example.com:%d/defaultresponse", | 220 base::StringPrintf("http://example.com:%d/defaultresponse", |
| 214 test_server_->host_port_pair().port())); | 221 test_server_->host_port_pair().port())); |
| 215 ASSERT_TRUE(test_url_.is_valid()); | 222 ASSERT_TRUE(test_url_.is_valid()); |
| 216 } | 223 } |
| 217 | 224 |
| 218 void URLFetcherMockDnsTest::CreateFetcher(const GURL& url) { | 225 void URLFetcherMockDnsTest::CreateFetcher(const GURL& url) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 | 428 |
| 422 private: | 429 private: |
| 423 Time start_time_; | 430 Time start_time_; |
| 424 }; | 431 }; |
| 425 | 432 |
| 426 // Version of URLFetcherTest that tests bad HTTPS requests. | 433 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 427 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 434 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 428 public: | 435 public: |
| 429 URLFetcherBadHTTPSTest(); | 436 URLFetcherBadHTTPSTest(); |
| 430 | 437 |
| 438 // URLFetcherTest: |
| 439 void SetUpServer() override; |
| 440 |
| 431 // URLFetcherDelegate: | 441 // URLFetcherDelegate: |
| 432 void OnURLFetchComplete(const URLFetcher* source) override; | 442 void OnURLFetchComplete(const URLFetcher* source) override; |
| 433 | 443 |
| 434 private: | 444 private: |
| 435 base::FilePath cert_dir_; | 445 base::FilePath cert_dir_; |
| 436 }; | 446 }; |
| 437 | 447 |
| 438 // Version of URLFetcherTest that tests request cancellation on shutdown. | 448 // Version of URLFetcherTest that tests request cancellation on shutdown. |
| 439 class URLFetcherCancelTest : public URLFetcherTest { | 449 class URLFetcherCancelTest : public URLFetcherTest { |
| 440 public: | 450 public: |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 | 844 |
| 835 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | 845 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { |
| 836 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 846 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
| 837 cert_dir_ = cert_dir_.AppendASCII("chrome"); | 847 cert_dir_ = cert_dir_.AppendASCII("chrome"); |
| 838 cert_dir_ = cert_dir_.AppendASCII("test"); | 848 cert_dir_ = cert_dir_.AppendASCII("test"); |
| 839 cert_dir_ = cert_dir_.AppendASCII("data"); | 849 cert_dir_ = cert_dir_.AppendASCII("data"); |
| 840 cert_dir_ = cert_dir_.AppendASCII("ssl"); | 850 cert_dir_ = cert_dir_.AppendASCII("ssl"); |
| 841 cert_dir_ = cert_dir_.AppendASCII("certificates"); | 851 cert_dir_ = cert_dir_.AppendASCII("certificates"); |
| 842 } | 852 } |
| 843 | 853 |
| 854 void URLFetcherBadHTTPSTest::SetUpServer() { |
| 855 SpawnedTestServer::SSLOptions ssl_options( |
| 856 SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 857 test_server_.reset(new SpawnedTestServer( |
| 858 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath(kDocRoot))); |
| 859 } |
| 860 |
| 844 // The "server certificate expired" error should result in automatic | 861 // The "server certificate expired" error should result in automatic |
| 845 // cancellation of the request by | 862 // cancellation of the request by |
| 846 // URLRequest::Delegate::OnSSLCertificateError. | 863 // URLRequest::Delegate::OnSSLCertificateError. |
| 847 void URLFetcherBadHTTPSTest::OnURLFetchComplete( | 864 void URLFetcherBadHTTPSTest::OnURLFetchComplete( |
| 848 const URLFetcher* source) { | 865 const URLFetcher* source) { |
| 849 // This part is different from URLFetcherTest::OnURLFetchComplete | 866 // This part is different from URLFetcherTest::OnURLFetchComplete |
| 850 // because this test expects the request to be cancelled. | 867 // because this test expects the request to be cancelled. |
| 851 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); | 868 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); |
| 852 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); | 869 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); |
| 853 EXPECT_EQ(-1, source->GetResponseCode()); | 870 EXPECT_EQ(-1, source->GetResponseCode()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 935 | 952 |
| 936 EXPECT_TRUE(base::ContentsEqual(expected_file_, file_path_)); | 953 EXPECT_TRUE(base::ContentsEqual(expected_file_, file_path_)); |
| 937 } else { | 954 } else { |
| 938 EXPECT_FALSE(source->GetStatus().is_success()); | 955 EXPECT_FALSE(source->GetStatus().is_success()); |
| 939 EXPECT_EQ(expected_file_error_, source->GetStatus().error()); | 956 EXPECT_EQ(expected_file_error_, source->GetStatus().error()); |
| 940 } | 957 } |
| 941 CleanupAfterFetchComplete(); | 958 CleanupAfterFetchComplete(); |
| 942 } | 959 } |
| 943 | 960 |
| 944 TEST_F(URLFetcherTest, SameThreadsTest) { | 961 TEST_F(URLFetcherTest, SameThreadsTest) { |
| 945 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 946 SpawnedTestServer::kLocalhost, | |
| 947 base::FilePath(kDocRoot)); | |
| 948 ASSERT_TRUE(test_server.Start()); | |
| 949 | |
| 950 // Create the fetcher on the main thread. Since IO will happen on the main | 962 // Create the fetcher on the main thread. Since IO will happen on the main |
| 951 // thread, this will test URLFetcher's ability to do everything on one | 963 // thread, this will test URLFetcher's ability to do everything on one |
| 952 // thread. | 964 // thread. |
| 953 CreateFetcher(test_server.GetURL("defaultresponse")); | 965 CreateFetcher(test_server_->GetURL("defaultresponse")); |
| 954 | 966 |
| 955 base::MessageLoop::current()->Run(); | 967 base::MessageLoop::current()->Run(); |
| 956 } | 968 } |
| 957 | 969 |
| 958 TEST_F(URLFetcherTest, DifferentThreadsTest) { | 970 TEST_F(URLFetcherTest, DifferentThreadsTest) { |
| 959 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 960 SpawnedTestServer::kLocalhost, | |
| 961 base::FilePath(kDocRoot)); | |
| 962 ASSERT_TRUE(test_server.Start()); | |
| 963 | |
| 964 // Create a separate thread that will create the URLFetcher. The current | 971 // Create a separate thread that will create the URLFetcher. The current |
| 965 // (main) thread will do the IO, and when the fetch is complete it will | 972 // (main) thread will do the IO, and when the fetch is complete it will |
| 966 // terminate the main thread's message loop; then the other thread's | 973 // terminate the main thread's message loop; then the other thread's |
| 967 // message loop will be shut down automatically as the thread goes out of | 974 // message loop will be shut down automatically as the thread goes out of |
| 968 // scope. | 975 // scope. |
| 969 base::Thread t("URLFetcher test thread"); | 976 base::Thread t("URLFetcher test thread"); |
| 970 ASSERT_TRUE(t.Start()); | 977 ASSERT_TRUE(t.Start()); |
| 971 t.message_loop()->PostTask( | 978 t.message_loop()->PostTask( |
| 972 FROM_HERE, | 979 FROM_HERE, |
| 973 base::Bind(&URLFetcherTest::CreateFetcher, | 980 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), |
| 974 base::Unretained(this), | 981 test_server_->GetURL("defaultresponse"))); |
| 975 test_server.GetURL("defaultresponse"))); | |
| 976 | 982 |
| 977 base::MessageLoop::current()->Run(); | 983 base::MessageLoop::current()->Run(); |
| 978 } | 984 } |
| 979 | 985 |
| 980 void CancelAllOnIO() { | 986 void CancelAllOnIO() { |
| 981 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | 987 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); |
| 982 URLFetcherImpl::CancelAll(); | 988 URLFetcherImpl::CancelAll(); |
| 983 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | 989 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); |
| 984 } | 990 } |
| 985 | 991 |
| 986 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 992 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
| 987 TEST_F(URLFetcherTest, CancelAll) { | 993 TEST_F(URLFetcherTest, CancelAll) { |
| 988 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 989 SpawnedTestServer::kLocalhost, | |
| 990 base::FilePath(kDocRoot)); | |
| 991 ASSERT_TRUE(test_server.Start()); | |
| 992 EXPECT_EQ(0, GetNumFetcherCores()); | 994 EXPECT_EQ(0, GetNumFetcherCores()); |
| 993 | 995 |
| 994 CreateFetcher(test_server.GetURL("defaultresponse")); | 996 CreateFetcher(test_server_->GetURL("defaultresponse")); |
| 995 io_message_loop_proxy()->PostTaskAndReply( | 997 io_message_loop_proxy()->PostTaskAndReply( |
| 996 FROM_HERE, base::Bind(&CancelAllOnIO), base::MessageLoop::QuitClosure()); | 998 FROM_HERE, base::Bind(&CancelAllOnIO), base::MessageLoop::QuitClosure()); |
| 997 base::MessageLoop::current()->Run(); | 999 base::MessageLoop::current()->Run(); |
| 998 EXPECT_EQ(0, GetNumFetcherCores()); | 1000 EXPECT_EQ(0, GetNumFetcherCores()); |
| 999 delete fetcher_; | 1001 delete fetcher_; |
| 1000 } | 1002 } |
| 1001 | 1003 |
| 1002 TEST_F(URLFetcherMockDnsTest, DontRetryOnNetworkChangedByDefault) { | 1004 TEST_F(URLFetcherMockDnsTest, DontRetryOnNetworkChangedByDefault) { |
| 1003 EXPECT_EQ(0, GetNumFetcherCores()); | 1005 EXPECT_EQ(0, GetNumFetcherCores()); |
| 1004 EXPECT_FALSE(resolver_.has_pending_requests()); | 1006 EXPECT_FALSE(resolver_.has_pending_requests()); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 EXPECT_EQ(0, GetNumFetcherCores()); | 1103 EXPECT_EQ(0, GetNumFetcherCores()); |
| 1102 EXPECT_FALSE(resolver_.has_pending_requests()); | 1104 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 1103 ASSERT_TRUE(completed_fetcher_); | 1105 ASSERT_TRUE(completed_fetcher_); |
| 1104 | 1106 |
| 1105 // This time the request succeeded. | 1107 // This time the request succeeded. |
| 1106 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | 1108 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); |
| 1107 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | 1109 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); |
| 1108 } | 1110 } |
| 1109 | 1111 |
| 1110 TEST_F(URLFetcherPostTest, Basic) { | 1112 TEST_F(URLFetcherPostTest, Basic) { |
| 1111 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1113 CreateFetcher(test_server_->GetURL("echo")); |
| 1112 SpawnedTestServer::kLocalhost, | |
| 1113 base::FilePath(kDocRoot)); | |
| 1114 ASSERT_TRUE(test_server.Start()); | |
| 1115 | |
| 1116 CreateFetcher(test_server.GetURL("echo")); | |
| 1117 base::MessageLoop::current()->Run(); | 1114 base::MessageLoop::current()->Run(); |
| 1118 } | 1115 } |
| 1119 | 1116 |
| 1120 TEST_F(URLFetcherPostFileTest, Basic) { | 1117 TEST_F(URLFetcherPostFileTest, Basic) { |
| 1121 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1118 CreateFetcher(test_server_->GetURL("echo")); |
| 1122 SpawnedTestServer::kLocalhost, | |
| 1123 base::FilePath(kDocRoot)); | |
| 1124 ASSERT_TRUE(test_server.Start()); | |
| 1125 | |
| 1126 CreateFetcher(test_server.GetURL("echo")); | |
| 1127 base::MessageLoop::current()->Run(); | 1119 base::MessageLoop::current()->Run(); |
| 1128 } | 1120 } |
| 1129 | 1121 |
| 1130 TEST_F(URLFetcherPostFileTest, Range) { | 1122 TEST_F(URLFetcherPostFileTest, Range) { |
| 1131 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1132 SpawnedTestServer::kLocalhost, | |
| 1133 base::FilePath(kDocRoot)); | |
| 1134 ASSERT_TRUE(test_server.Start()); | |
| 1135 | |
| 1136 SetUploadRange(30, 100); | 1123 SetUploadRange(30, 100); |
| 1137 | 1124 |
| 1138 CreateFetcher(test_server.GetURL("echo")); | 1125 CreateFetcher(test_server_->GetURL("echo")); |
| 1139 base::MessageLoop::current()->Run(); | 1126 base::MessageLoop::current()->Run(); |
| 1140 } | 1127 } |
| 1141 | 1128 |
| 1142 TEST_F(URLFetcherSetUploadFactoryTest, Basic) { | 1129 TEST_F(URLFetcherSetUploadFactoryTest, Basic) { |
| 1143 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1130 CreateFetcher(test_server_->GetURL("echo")); |
| 1144 SpawnedTestServer::kLocalhost, | |
| 1145 base::FilePath(kDocRoot)); | |
| 1146 ASSERT_TRUE(test_server.Start()); | |
| 1147 | |
| 1148 CreateFetcher(test_server.GetURL("echo")); | |
| 1149 base::MessageLoop::current()->Run(); | 1131 base::MessageLoop::current()->Run(); |
| 1150 ASSERT_EQ(1u, create_stream_count()); | 1132 ASSERT_EQ(1u, create_stream_count()); |
| 1151 } | 1133 } |
| 1152 | 1134 |
| 1153 TEST_F(URLFetcherSetUploadFactoryTest, Retry) { | 1135 TEST_F(URLFetcherSetUploadFactoryTest, Retry) { |
| 1154 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1155 SpawnedTestServer::kLocalhost, | |
| 1156 base::FilePath(kDocRoot)); | |
| 1157 ASSERT_TRUE(test_server.Start()); | |
| 1158 expected_status_code_ = 500; | 1136 expected_status_code_ = 500; |
| 1159 CreateFetcher(test_server.GetURL("echo?status=500")); | 1137 CreateFetcher(test_server_->GetURL("echo?status=500")); |
| 1160 base::MessageLoop::current()->Run(); | 1138 base::MessageLoop::current()->Run(); |
| 1161 ASSERT_EQ(2u, create_stream_count()); | 1139 ASSERT_EQ(2u, create_stream_count()); |
| 1162 } | 1140 } |
| 1163 | 1141 |
| 1164 TEST_F(URLFetcherEmptyPostTest, Basic) { | 1142 TEST_F(URLFetcherEmptyPostTest, Basic) { |
| 1165 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1143 CreateFetcher(test_server_->GetURL("echo")); |
| 1166 SpawnedTestServer::kLocalhost, | |
| 1167 base::FilePath(kDocRoot)); | |
| 1168 ASSERT_TRUE(test_server.Start()); | |
| 1169 | |
| 1170 CreateFetcher(test_server.GetURL("echo")); | |
| 1171 base::MessageLoop::current()->Run(); | 1144 base::MessageLoop::current()->Run(); |
| 1172 } | 1145 } |
| 1173 | 1146 |
| 1174 TEST_F(URLFetcherUploadProgressTest, Basic) { | 1147 TEST_F(URLFetcherUploadProgressTest, Basic) { |
| 1175 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1148 CreateFetcher(test_server_->GetURL("echo")); |
| 1176 SpawnedTestServer::kLocalhost, | |
| 1177 base::FilePath(kDocRoot)); | |
| 1178 ASSERT_TRUE(test_server.Start()); | |
| 1179 | |
| 1180 CreateFetcher(test_server.GetURL("echo")); | |
| 1181 base::MessageLoop::current()->Run(); | 1149 base::MessageLoop::current()->Run(); |
| 1182 } | 1150 } |
| 1183 | 1151 |
| 1184 TEST_F(URLFetcherDownloadProgressTest, Basic) { | 1152 TEST_F(URLFetcherDownloadProgressTest, Basic) { |
| 1185 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1186 SpawnedTestServer::kLocalhost, | |
| 1187 base::FilePath(kDocRoot)); | |
| 1188 ASSERT_TRUE(test_server.Start()); | |
| 1189 | |
| 1190 // Get a file large enough to require more than one read into | 1153 // Get a file large enough to require more than one read into |
| 1191 // URLFetcher::Core's IOBuffer. | 1154 // URLFetcher::Core's IOBuffer. |
| 1192 static const char kFileToFetch[] = "animate1.gif"; | 1155 static const char kFileToFetch[] = "animate1.gif"; |
| 1193 // Hardcoded file size - it cannot be easily fetched when a remote http server | 1156 // Hardcoded file size - it cannot be easily fetched when a remote http server |
| 1194 // is used for testing. | 1157 // is used for testing. |
| 1195 static const int64 kFileSize = 19021; | 1158 static const int64 kFileSize = 19021; |
| 1196 | 1159 |
| 1197 expected_total_ = kFileSize; | 1160 expected_total_ = kFileSize; |
| 1198 | 1161 |
| 1199 CreateFetcher(test_server.GetURL( | 1162 CreateFetcher( |
| 1200 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1163 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1201 | 1164 |
| 1202 base::MessageLoop::current()->Run(); | 1165 base::MessageLoop::current()->Run(); |
| 1203 } | 1166 } |
| 1204 | 1167 |
| 1205 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { | 1168 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { |
| 1206 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1207 SpawnedTestServer::kLocalhost, | |
| 1208 base::FilePath(kDocRoot)); | |
| 1209 ASSERT_TRUE(test_server.Start()); | |
| 1210 | |
| 1211 // Get a file large enough to require more than one read into | 1169 // Get a file large enough to require more than one read into |
| 1212 // URLFetcher::Core's IOBuffer. | 1170 // URLFetcher::Core's IOBuffer. |
| 1213 static const char kFileToFetch[] = "animate1.gif"; | 1171 static const char kFileToFetch[] = "animate1.gif"; |
| 1214 CreateFetcher(test_server.GetURL( | 1172 CreateFetcher( |
| 1215 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1173 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1216 | 1174 |
| 1217 base::MessageLoop::current()->Run(); | 1175 base::MessageLoop::current()->Run(); |
| 1218 } | 1176 } |
| 1219 | 1177 |
| 1220 TEST_F(URLFetcherHeadersTest, Headers) { | 1178 TEST_F(URLFetcherHeadersTest, Headers) { |
| 1221 SpawnedTestServer test_server( | 1179 CreateFetcher(test_server_->GetURL("set-header?cache-control: private")); |
| 1222 SpawnedTestServer::TYPE_HTTP, | |
| 1223 SpawnedTestServer::kLocalhost, | |
| 1224 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 1225 ASSERT_TRUE(test_server.Start()); | |
| 1226 | |
| 1227 CreateFetcher(test_server.GetURL("files/with-headers.html")); | |
| 1228 base::MessageLoop::current()->Run(); | 1180 base::MessageLoop::current()->Run(); |
| 1229 // The actual tests are in the URLFetcherHeadersTest fixture. | 1181 // The actual tests are in the URLFetcherHeadersTest fixture. |
| 1230 } | 1182 } |
| 1231 | 1183 |
| 1232 TEST_F(URLFetcherSocketAddressTest, SocketAddress) { | 1184 TEST_F(URLFetcherSocketAddressTest, SocketAddress) { |
| 1233 SpawnedTestServer test_server( | 1185 expected_port_ = test_server_->host_port_pair().port(); |
| 1234 SpawnedTestServer::TYPE_HTTP, | |
| 1235 SpawnedTestServer::kLocalhost, | |
| 1236 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); | |
| 1237 ASSERT_TRUE(test_server.Start()); | |
| 1238 expected_port_ = test_server.host_port_pair().port(); | |
| 1239 | 1186 |
| 1240 // Reusing "with-headers.html" but doesn't really matter. | 1187 CreateFetcher(test_server_->GetURL("defaultresponse")); |
| 1241 CreateFetcher(test_server.GetURL("files/with-headers.html")); | |
| 1242 base::MessageLoop::current()->Run(); | 1188 base::MessageLoop::current()->Run(); |
| 1243 // The actual tests are in the URLFetcherSocketAddressTest fixture. | 1189 // The actual tests are in the URLFetcherSocketAddressTest fixture. |
| 1244 } | 1190 } |
| 1245 | 1191 |
| 1246 TEST_F(URLFetcherStopOnRedirectTest, StopOnRedirect) { | 1192 TEST_F(URLFetcherStopOnRedirectTest, StopOnRedirect) { |
| 1247 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1248 SpawnedTestServer::kLocalhost, | |
| 1249 base::FilePath(kDocRoot)); | |
| 1250 ASSERT_TRUE(test_server.Start()); | |
| 1251 | |
| 1252 CreateFetcher( | 1193 CreateFetcher( |
| 1253 test_server.GetURL(std::string("server-redirect?") + kRedirectTarget)); | 1194 test_server_->GetURL(std::string("server-redirect?") + kRedirectTarget)); |
| 1254 base::MessageLoop::current()->Run(); | 1195 base::MessageLoop::current()->Run(); |
| 1255 EXPECT_TRUE(callback_called_); | 1196 EXPECT_TRUE(callback_called_); |
| 1256 } | 1197 } |
| 1257 | 1198 |
| 1258 TEST_F(URLFetcherProtectTest, Overload) { | 1199 TEST_F(URLFetcherProtectTest, Overload) { |
| 1259 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1200 GURL url(test_server_->GetURL("defaultresponse")); |
| 1260 SpawnedTestServer::kLocalhost, | |
| 1261 base::FilePath(kDocRoot)); | |
| 1262 ASSERT_TRUE(test_server.Start()); | |
| 1263 | |
| 1264 GURL url(test_server.GetURL("defaultresponse")); | |
| 1265 | 1201 |
| 1266 // Registers an entry for test url. It only allows 3 requests to be sent | 1202 // Registers an entry for test url. It only allows 3 requests to be sent |
| 1267 // in 200 milliseconds. | 1203 // in 200 milliseconds. |
| 1268 scoped_refptr<URLRequestThrottlerEntry> entry( | 1204 scoped_refptr<URLRequestThrottlerEntry> entry( |
| 1269 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | 1205 new URLRequestThrottlerEntry(request_context()->throttler_manager(), |
| 1270 std::string(), | 1206 std::string(), |
| 1271 200, | 1207 200, |
| 1272 3, | 1208 3, |
| 1273 1, | 1209 1, |
| 1274 2.0, | 1210 2.0, |
| 1275 0.0, | 1211 0.0, |
| 1276 256)); | 1212 256)); |
| 1277 request_context()->throttler_manager() | 1213 request_context()->throttler_manager() |
| 1278 ->OverrideEntryForTests(url, entry.get()); | 1214 ->OverrideEntryForTests(url, entry.get()); |
| 1279 | 1215 |
| 1280 CreateFetcher(url); | 1216 CreateFetcher(url); |
| 1281 | 1217 |
| 1282 base::MessageLoop::current()->Run(); | 1218 base::MessageLoop::current()->Run(); |
| 1283 } | 1219 } |
| 1284 | 1220 |
| 1285 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | 1221 TEST_F(URLFetcherProtectTest, ServerUnavailable) { |
| 1286 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1222 GURL url(test_server_->GetURL("files/server-unavailable.html")); |
| 1287 SpawnedTestServer::kLocalhost, | |
| 1288 base::FilePath(kDocRoot)); | |
| 1289 ASSERT_TRUE(test_server.Start()); | |
| 1290 | |
| 1291 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
| 1292 | 1223 |
| 1293 // Registers an entry for test url. The backoff time is calculated by: | 1224 // Registers an entry for test url. The backoff time is calculated by: |
| 1294 // new_backoff = 2.0 * old_backoff + 0 | 1225 // new_backoff = 2.0 * old_backoff + 0 |
| 1295 // and maximum backoff time is 256 milliseconds. | 1226 // and maximum backoff time is 256 milliseconds. |
| 1296 // Maximum retries allowed is set to 11. | 1227 // Maximum retries allowed is set to 11. |
| 1297 scoped_refptr<URLRequestThrottlerEntry> entry( | 1228 scoped_refptr<URLRequestThrottlerEntry> entry( |
| 1298 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | 1229 new URLRequestThrottlerEntry(request_context()->throttler_manager(), |
| 1299 std::string(), | 1230 std::string(), |
| 1300 200, | 1231 200, |
| 1301 3, | 1232 3, |
| 1302 1, | 1233 1, |
| 1303 2.0, | 1234 2.0, |
| 1304 0.0, | 1235 0.0, |
| 1305 256)); | 1236 256)); |
| 1306 request_context()->throttler_manager() | 1237 request_context()->throttler_manager() |
| 1307 ->OverrideEntryForTests(url, entry.get()); | 1238 ->OverrideEntryForTests(url, entry.get()); |
| 1308 | 1239 |
| 1309 CreateFetcher(url); | 1240 CreateFetcher(url); |
| 1310 | 1241 |
| 1311 base::MessageLoop::current()->Run(); | 1242 base::MessageLoop::current()->Run(); |
| 1312 } | 1243 } |
| 1313 | 1244 |
| 1314 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | 1245 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { |
| 1315 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1246 GURL url(test_server_->GetURL("files/server-unavailable.html")); |
| 1316 SpawnedTestServer::kLocalhost, | |
| 1317 base::FilePath(kDocRoot)); | |
| 1318 ASSERT_TRUE(test_server.Start()); | |
| 1319 | |
| 1320 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
| 1321 | 1247 |
| 1322 // Registers an entry for test url. The backoff time is calculated by: | 1248 // Registers an entry for test url. The backoff time is calculated by: |
| 1323 // new_backoff = 2.0 * old_backoff + 0 | 1249 // new_backoff = 2.0 * old_backoff + 0 |
| 1324 // and maximum backoff time is 150000 milliseconds. | 1250 // and maximum backoff time is 150000 milliseconds. |
| 1325 // Maximum retries allowed is set to 11. | 1251 // Maximum retries allowed is set to 11. |
| 1326 scoped_refptr<URLRequestThrottlerEntry> entry( | 1252 scoped_refptr<URLRequestThrottlerEntry> entry( |
| 1327 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | 1253 new URLRequestThrottlerEntry(request_context()->throttler_manager(), |
| 1328 std::string(), | 1254 std::string(), |
| 1329 200, | 1255 200, |
| 1330 3, | 1256 3, |
| 1331 100, | 1257 100, |
| 1332 2.0, | 1258 2.0, |
| 1333 0.0, | 1259 0.0, |
| 1334 150000)); | 1260 150000)); |
| 1335 // Total time if *not* for not doing automatic backoff would be 150s. | 1261 // Total time if *not* for not doing automatic backoff would be 150s. |
| 1336 // In reality it should be "as soon as server responds". | 1262 // In reality it should be "as soon as server responds". |
| 1337 request_context()->throttler_manager() | 1263 request_context()->throttler_manager() |
| 1338 ->OverrideEntryForTests(url, entry.get()); | 1264 ->OverrideEntryForTests(url, entry.get()); |
| 1339 | 1265 |
| 1340 CreateFetcher(url); | 1266 CreateFetcher(url); |
| 1341 | 1267 |
| 1342 base::MessageLoop::current()->Run(); | 1268 base::MessageLoop::current()->Run(); |
| 1343 } | 1269 } |
| 1344 | 1270 |
| 1345 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { | 1271 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { |
| 1346 SpawnedTestServer::SSLOptions ssl_options( | 1272 CreateFetcher(test_server_->GetURL("defaultresponse")); |
| 1347 SpawnedTestServer::SSLOptions::CERT_EXPIRED); | |
| 1348 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
| 1349 ssl_options, | |
| 1350 base::FilePath(kDocRoot)); | |
| 1351 ASSERT_TRUE(test_server.Start()); | |
| 1352 | |
| 1353 CreateFetcher(test_server.GetURL("defaultresponse")); | |
| 1354 base::MessageLoop::current()->Run(); | 1273 base::MessageLoop::current()->Run(); |
| 1355 } | 1274 } |
| 1356 | 1275 |
| 1357 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 1276 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
| 1358 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1277 GURL url(test_server_->GetURL("files/server-unavailable.html")); |
| 1359 SpawnedTestServer::kLocalhost, | |
| 1360 base::FilePath(kDocRoot)); | |
| 1361 ASSERT_TRUE(test_server.Start()); | |
| 1362 | |
| 1363 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
| 1364 | 1278 |
| 1365 // Create a separate thread that will create the URLFetcher. The current | 1279 // Create a separate thread that will create the URLFetcher. The current |
| 1366 // (main) thread will do the IO, and when the fetch is complete it will | 1280 // (main) thread will do the IO, and when the fetch is complete it will |
| 1367 // terminate the main thread's message loop; then the other thread's | 1281 // terminate the main thread's message loop; then the other thread's |
| 1368 // message loop will be shut down automatically as the thread goes out of | 1282 // message loop will be shut down automatically as the thread goes out of |
| 1369 // scope. | 1283 // scope. |
| 1370 base::Thread t("URLFetcher test thread"); | 1284 base::Thread t("URLFetcher test thread"); |
| 1371 ASSERT_TRUE(t.Start()); | 1285 ASSERT_TRUE(t.Start()); |
| 1372 t.message_loop()->PostTask( | 1286 t.message_loop()->PostTask( |
| 1373 FROM_HERE, | 1287 FROM_HERE, |
| 1374 base::Bind(&URLFetcherCancelTest::CreateFetcher, | 1288 base::Bind(&URLFetcherCancelTest::CreateFetcher, |
| 1375 base::Unretained(this), url)); | 1289 base::Unretained(this), url)); |
| 1376 | 1290 |
| 1377 base::MessageLoop::current()->Run(); | 1291 base::MessageLoop::current()->Run(); |
| 1378 } | 1292 } |
| 1379 | 1293 |
| 1380 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | 1294 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { |
| 1381 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | 1295 GURL url(test_server_->GetURL("files/server-unavailable.html")); |
| 1382 SpawnedTestServer::kLocalhost, | |
| 1383 base::FilePath(kDocRoot)); | |
| 1384 ASSERT_TRUE(test_server.Start()); | |
| 1385 | |
| 1386 GURL url(test_server.GetURL("files/server-unavailable.html")); | |
| 1387 | 1296 |
| 1388 // Register an entry for test url. | 1297 // Register an entry for test url. |
| 1389 // Using a sliding window of 4 seconds, and max of 1 request, under a fast | 1298 // Using a sliding window of 4 seconds, and max of 1 request, under a fast |
| 1390 // run we expect to have a 4 second delay when posting the Start task. | 1299 // run we expect to have a 4 second delay when posting the Start task. |
| 1391 scoped_refptr<URLRequestThrottlerEntry> entry( | 1300 scoped_refptr<URLRequestThrottlerEntry> entry( |
| 1392 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | 1301 new URLRequestThrottlerEntry(request_context()->throttler_manager(), |
| 1393 std::string(), | 1302 std::string(), |
| 1394 4000, | 1303 4000, |
| 1395 1, | 1304 1, |
| 1396 2000, | 1305 2000, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1409 base::Thread t("URLFetcher test thread"); | 1318 base::Thread t("URLFetcher test thread"); |
| 1410 ASSERT_TRUE(t.Start()); | 1319 ASSERT_TRUE(t.Start()); |
| 1411 t.message_loop()->PostTask( | 1320 t.message_loop()->PostTask( |
| 1412 FROM_HERE, | 1321 FROM_HERE, |
| 1413 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); | 1322 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); |
| 1414 | 1323 |
| 1415 base::MessageLoop::current()->Run(); | 1324 base::MessageLoop::current()->Run(); |
| 1416 } | 1325 } |
| 1417 | 1326 |
| 1418 TEST_F(URLFetcherMultipleAttemptTest, SameData) { | 1327 TEST_F(URLFetcherMultipleAttemptTest, SameData) { |
| 1419 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1420 SpawnedTestServer::kLocalhost, | |
| 1421 base::FilePath(kDocRoot)); | |
| 1422 ASSERT_TRUE(test_server.Start()); | |
| 1423 | |
| 1424 // Create the fetcher on the main thread. Since IO will happen on the main | 1328 // Create the fetcher on the main thread. Since IO will happen on the main |
| 1425 // thread, this will test URLFetcher's ability to do everything on one | 1329 // thread, this will test URLFetcher's ability to do everything on one |
| 1426 // thread. | 1330 // thread. |
| 1427 CreateFetcher(test_server.GetURL("defaultresponse")); | 1331 CreateFetcher(test_server_->GetURL("defaultresponse")); |
| 1428 | 1332 |
| 1429 base::MessageLoop::current()->Run(); | 1333 base::MessageLoop::current()->Run(); |
| 1430 } | 1334 } |
| 1431 | 1335 |
| 1432 TEST_F(URLFetcherFileTest, SmallGet) { | 1336 TEST_F(URLFetcherFileTest, SmallGet) { |
| 1433 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1434 SpawnedTestServer::kLocalhost, | |
| 1435 base::FilePath(kDocRoot)); | |
| 1436 ASSERT_TRUE(test_server.Start()); | |
| 1437 | |
| 1438 base::ScopedTempDir temp_dir; | 1337 base::ScopedTempDir temp_dir; |
| 1439 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1338 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1440 | 1339 |
| 1441 // Get a small file. | 1340 // Get a small file. |
| 1442 static const char kFileToFetch[] = "simple.html"; | 1341 static const char kFileToFetch[] = "simple.html"; |
| 1443 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1342 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1444 CreateFetcherForFile( | 1343 CreateFetcherForFile( |
| 1445 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1344 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1446 temp_dir.path().AppendASCII(kFileToFetch)); | 1345 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1447 | 1346 |
| 1448 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1347 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1449 | 1348 |
| 1450 ASSERT_FALSE(base::PathExists(file_path_)) | 1349 ASSERT_FALSE(base::PathExists(file_path_)) |
| 1451 << file_path_.value() << " not removed."; | 1350 << file_path_.value() << " not removed."; |
| 1452 } | 1351 } |
| 1453 | 1352 |
| 1454 TEST_F(URLFetcherFileTest, LargeGet) { | 1353 TEST_F(URLFetcherFileTest, LargeGet) { |
| 1455 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1456 SpawnedTestServer::kLocalhost, | |
| 1457 base::FilePath(kDocRoot)); | |
| 1458 ASSERT_TRUE(test_server.Start()); | |
| 1459 | |
| 1460 base::ScopedTempDir temp_dir; | 1354 base::ScopedTempDir temp_dir; |
| 1461 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1355 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1462 | 1356 |
| 1463 // Get a file large enough to require more than one read into | 1357 // Get a file large enough to require more than one read into |
| 1464 // URLFetcher::Core's IOBuffer. | 1358 // URLFetcher::Core's IOBuffer. |
| 1465 static const char kFileToFetch[] = "animate1.gif"; | 1359 static const char kFileToFetch[] = "animate1.gif"; |
| 1466 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1360 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1467 CreateFetcherForFile( | 1361 CreateFetcherForFile( |
| 1468 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1362 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1469 temp_dir.path().AppendASCII(kFileToFetch)); | 1363 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1470 | 1364 |
| 1471 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1365 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1472 } | 1366 } |
| 1473 | 1367 |
| 1474 TEST_F(URLFetcherFileTest, SavedOutputFileOwnerhisp) { | 1368 TEST_F(URLFetcherFileTest, SavedOutputFileOwnerhisp) { |
| 1475 // If the caller takes the ownership of the output file, the file should | 1369 // If the caller takes the ownership of the output file, the file should |
| 1476 // persist even after URLFetcher is gone. If not, the file must be deleted. | 1370 // persist even after URLFetcher is gone. If not, the file must be deleted. |
| 1477 const bool kTake[] = {false, true}; | 1371 const bool kTake[] = {false, true}; |
| 1478 for (size_t i = 0; i < arraysize(kTake); ++i) { | 1372 for (size_t i = 0; i < arraysize(kTake); ++i) { |
| 1479 take_ownership_of_file_ = kTake[i]; | 1373 take_ownership_of_file_ = kTake[i]; |
| 1480 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1481 SpawnedTestServer::kLocalhost, | |
| 1482 base::FilePath(kDocRoot)); | |
| 1483 ASSERT_TRUE(test_server.Start()); | |
| 1484 | |
| 1485 base::ScopedTempDir temp_dir; | 1374 base::ScopedTempDir temp_dir; |
| 1486 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1375 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1487 | 1376 |
| 1488 // Get a small file. | 1377 // Get a small file. |
| 1489 static const char kFileToFetch[] = "simple.html"; | 1378 static const char kFileToFetch[] = "simple.html"; |
| 1490 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1379 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1491 CreateFetcherForFile( | 1380 CreateFetcherForFile( |
| 1492 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1381 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1493 temp_dir.path().AppendASCII(kFileToFetch)); | 1382 temp_dir.path().AppendASCII(kFileToFetch)); |
| 1494 | 1383 |
| 1495 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1384 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1496 | 1385 |
| 1497 base::MessageLoop::current()->RunUntilIdle(); | 1386 base::MessageLoop::current()->RunUntilIdle(); |
| 1498 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | 1387 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << |
| 1499 "FilePath: " << file_path_.value(); | 1388 "FilePath: " << file_path_.value(); |
| 1500 } | 1389 } |
| 1501 } | 1390 } |
| 1502 | 1391 |
| 1503 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { | 1392 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { |
| 1504 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1505 SpawnedTestServer::kLocalhost, | |
| 1506 base::FilePath(kDocRoot)); | |
| 1507 ASSERT_TRUE(test_server.Start()); | |
| 1508 | |
| 1509 base::ScopedTempDir temp_dir; | 1393 base::ScopedTempDir temp_dir; |
| 1510 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1394 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1511 | 1395 |
| 1512 // Create a file before trying to fetch. | 1396 // Create a file before trying to fetch. |
| 1513 static const char kFileToFetch[] = "simple.html"; | 1397 static const char kFileToFetch[] = "simple.html"; |
| 1514 std::string data(10000, '?'); // Meant to be larger than simple.html. | 1398 std::string data(10000, '?'); // Meant to be larger than simple.html. |
| 1515 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1399 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 1516 ASSERT_EQ(static_cast<int>(data.size()), | 1400 ASSERT_EQ(static_cast<int>(data.size()), |
| 1517 base::WriteFile(file_path_, data.data(), data.size())); | 1401 base::WriteFile(file_path_, data.data(), data.size())); |
| 1518 ASSERT_TRUE(base::PathExists(file_path_)); | 1402 ASSERT_TRUE(base::PathExists(file_path_)); |
| 1519 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1403 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1520 ASSERT_FALSE(base::ContentsEqual(file_path_, expected_file_)); | 1404 ASSERT_FALSE(base::ContentsEqual(file_path_, expected_file_)); |
| 1521 | 1405 |
| 1522 // Get a small file. | 1406 // Get a small file. |
| 1523 CreateFetcherForFile( | 1407 CreateFetcherForFile( |
| 1524 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1408 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1525 file_path_); | 1409 file_path_); |
| 1526 | 1410 |
| 1527 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1411 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1528 } | 1412 } |
| 1529 | 1413 |
| 1530 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { | 1414 TEST_F(URLFetcherFileTest, TryToOverwriteDirectory) { |
| 1531 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1532 SpawnedTestServer::kLocalhost, | |
| 1533 base::FilePath(kDocRoot)); | |
| 1534 ASSERT_TRUE(test_server.Start()); | |
| 1535 | |
| 1536 base::ScopedTempDir temp_dir; | 1415 base::ScopedTempDir temp_dir; |
| 1537 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1416 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1538 | 1417 |
| 1539 // Create a directory before trying to fetch. | 1418 // Create a directory before trying to fetch. |
| 1540 static const char kFileToFetch[] = "simple.html"; | 1419 static const char kFileToFetch[] = "simple.html"; |
| 1541 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1420 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
| 1542 ASSERT_TRUE(base::CreateDirectory(file_path_)); | 1421 ASSERT_TRUE(base::CreateDirectory(file_path_)); |
| 1543 ASSERT_TRUE(base::PathExists(file_path_)); | 1422 ASSERT_TRUE(base::PathExists(file_path_)); |
| 1544 | 1423 |
| 1545 // Get a small file. | 1424 // Get a small file. |
| 1546 expected_file_error_ = ERR_ACCESS_DENIED; | 1425 expected_file_error_ = ERR_ACCESS_DENIED; |
| 1547 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1426 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1548 CreateFetcherForFile( | 1427 CreateFetcherForFile( |
| 1549 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1428 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1550 file_path_); | 1429 file_path_); |
| 1551 | 1430 |
| 1552 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1431 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1553 | 1432 |
| 1554 base::MessageLoop::current()->RunUntilIdle(); | 1433 base::MessageLoop::current()->RunUntilIdle(); |
| 1555 } | 1434 } |
| 1556 | 1435 |
| 1557 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { | 1436 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { |
| 1558 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1559 SpawnedTestServer::kLocalhost, | |
| 1560 base::FilePath(kDocRoot)); | |
| 1561 ASSERT_TRUE(test_server.Start()); | |
| 1562 | |
| 1563 // Get a small file. | 1437 // Get a small file. |
| 1564 static const char kFileToFetch[] = "simple.html"; | 1438 static const char kFileToFetch[] = "simple.html"; |
| 1565 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1439 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1566 CreateFetcherForTempFile( | 1440 CreateFetcherForTempFile( |
| 1567 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1441 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1568 | 1442 |
| 1569 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1443 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1570 | 1444 |
| 1571 ASSERT_FALSE(base::PathExists(file_path_)) | 1445 ASSERT_FALSE(base::PathExists(file_path_)) |
| 1572 << file_path_.value() << " not removed."; | 1446 << file_path_.value() << " not removed."; |
| 1573 } | 1447 } |
| 1574 | 1448 |
| 1575 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { | 1449 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { |
| 1576 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1577 SpawnedTestServer::kLocalhost, | |
| 1578 base::FilePath(kDocRoot)); | |
| 1579 ASSERT_TRUE(test_server.Start()); | |
| 1580 | |
| 1581 // Get a file large enough to require more than one read into | 1450 // Get a file large enough to require more than one read into |
| 1582 // URLFetcher::Core's IOBuffer. | 1451 // URLFetcher::Core's IOBuffer. |
| 1583 static const char kFileToFetch[] = "animate1.gif"; | 1452 static const char kFileToFetch[] = "animate1.gif"; |
| 1584 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1453 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1585 CreateFetcherForTempFile(test_server.GetURL( | 1454 CreateFetcherForTempFile( |
| 1586 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1455 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1587 | 1456 |
| 1588 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1457 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1589 } | 1458 } |
| 1590 | 1459 |
| 1591 TEST_F(URLFetcherFileTest, SavedOutputTempFileOwnerhisp) { | 1460 TEST_F(URLFetcherFileTest, SavedOutputTempFileOwnerhisp) { |
| 1592 // If the caller takes the ownership of the temp file, the file should persist | 1461 // If the caller takes the ownership of the temp file, the file should persist |
| 1593 // even after URLFetcher is gone. If not, the file must be deleted. | 1462 // even after URLFetcher is gone. If not, the file must be deleted. |
| 1594 const bool kTake[] = {false, true}; | 1463 const bool kTake[] = {false, true}; |
| 1595 for (size_t i = 0; i < arraysize(kTake); ++i) { | 1464 for (size_t i = 0; i < arraysize(kTake); ++i) { |
| 1596 take_ownership_of_file_ = kTake[i]; | 1465 take_ownership_of_file_ = kTake[i]; |
| 1597 | 1466 |
| 1598 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1599 SpawnedTestServer::kLocalhost, | |
| 1600 base::FilePath(kDocRoot)); | |
| 1601 ASSERT_TRUE(test_server.Start()); | |
| 1602 | |
| 1603 // Get a small file. | 1467 // Get a small file. |
| 1604 static const char kFileToFetch[] = "simple.html"; | 1468 static const char kFileToFetch[] = "simple.html"; |
| 1605 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | 1469 expected_file_ = test_server_->GetDocumentRoot().AppendASCII(kFileToFetch); |
| 1606 CreateFetcherForTempFile(test_server.GetURL( | 1470 CreateFetcherForTempFile(test_server_->GetURL( |
| 1607 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1471 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 1608 | 1472 |
| 1609 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1473 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 1610 | 1474 |
| 1611 base::MessageLoop::current()->RunUntilIdle(); | 1475 base::MessageLoop::current()->RunUntilIdle(); |
| 1612 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | 1476 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << |
| 1613 "FilePath: " << file_path_.value(); | 1477 "FilePath: " << file_path_.value(); |
| 1614 } | 1478 } |
| 1615 } | 1479 } |
| 1616 | 1480 |
| 1617 } // namespace | 1481 } // namespace |
| 1618 | 1482 |
| 1619 } // namespace net | 1483 } // namespace net |
| OLD | NEW |