| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 URLFetcherTest() : fetcher_(NULL) { } | 40 URLFetcherTest() : fetcher_(NULL) { } |
| 41 | 41 |
| 42 static int GetNumFetcherCores() { | 42 static int GetNumFetcherCores() { |
| 43 return URLFetcherImpl::GetNumFetcherCores(); | 43 return URLFetcherImpl::GetNumFetcherCores(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Creates a URLFetcher, using the program's main thread to do IO. | 46 // Creates a URLFetcher, using the program's main thread to do IO. |
| 47 virtual void CreateFetcher(const GURL& url); | 47 virtual void CreateFetcher(const GURL& url); |
| 48 | 48 |
| 49 // content::URLFetcherDelegate | 49 // content::URLFetcherDelegate |
| 50 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 50 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 51 | 51 |
| 52 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 52 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
| 53 return io_message_loop_proxy_; | 53 return io_message_loop_proxy_; |
| 54 } | 54 } |
| 55 | 55 |
| 56 protected: | 56 protected: |
| 57 virtual void SetUp() { | 57 virtual void SetUp() OVERRIDE { |
| 58 testing::Test::SetUp(); | 58 testing::Test::SetUp(); |
| 59 | 59 |
| 60 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 60 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
| 61 | 61 |
| 62 #if defined(USE_NSS) | 62 #if defined(USE_NSS) |
| 63 crypto::EnsureNSSInit(); | 63 crypto::EnsureNSSInit(); |
| 64 net::EnsureOCSPInit(); | 64 net::EnsureOCSPInit(); |
| 65 #endif | 65 #endif |
| 66 } | 66 } |
| 67 | 67 |
| 68 virtual void TearDown() { | 68 virtual void TearDown() OVERRIDE { |
| 69 #if defined(USE_NSS) | 69 #if defined(USE_NSS) |
| 70 net::ShutdownOCSP(); | 70 net::ShutdownOCSP(); |
| 71 #endif | 71 #endif |
| 72 } | 72 } |
| 73 | 73 |
| 74 // URLFetcher is designed to run on the main UI thread, but in our tests | 74 // URLFetcher is designed to run on the main UI thread, but in our tests |
| 75 // we assume that the current thread is the IO thread where the URLFetcher | 75 // we assume that the current thread is the IO thread where the URLFetcher |
| 76 // dispatches its requests to. When we wish to simulate being used from | 76 // dispatches its requests to. When we wish to simulate being used from |
| 77 // a UI thread, we dispatch a worker thread to do so. | 77 // a UI thread, we dispatch a worker thread to do so. |
| 78 MessageLoopForIO io_loop_; | 78 MessageLoopForIO io_loop_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 103 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 103 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 104 // If the current message loop is not the IO loop, it will be shut down when | 104 // If the current message loop is not the IO loop, it will be shut down when |
| 105 // the main loop returns and this thread subsequently goes out of scope. | 105 // the main loop returns and this thread subsequently goes out of scope. |
| 106 } | 106 } |
| 107 | 107 |
| 108 namespace { | 108 namespace { |
| 109 | 109 |
| 110 // Version of URLFetcherTest that does a POST instead | 110 // Version of URLFetcherTest that does a POST instead |
| 111 class URLFetcherPostTest : public URLFetcherTest { | 111 class URLFetcherPostTest : public URLFetcherTest { |
| 112 public: | 112 public: |
| 113 virtual void CreateFetcher(const GURL& url); | 113 // URLFetcherTest override. |
| 114 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 114 | 115 |
| 115 // content::URLFetcherDelegate | 116 // content::URLFetcherDelegate |
| 116 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 117 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 117 }; | 118 }; |
| 118 | 119 |
| 119 // Version of URLFetcherTest that tests download progress reports. | 120 // Version of URLFetcherTest that tests download progress reports. |
| 120 class URLFetcherDownloadProgressTest : public URLFetcherTest { | 121 class URLFetcherDownloadProgressTest : public URLFetcherTest { |
| 121 public: | 122 public: |
| 122 virtual void CreateFetcher(const GURL& url); | 123 // URLFetcherTest override. |
| 124 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 123 | 125 |
| 124 // content::URLFetcherDelegate | 126 // content::URLFetcherDelegate |
| 125 virtual void OnURLFetchDownloadProgress(const content::URLFetcher* source, | 127 virtual void OnURLFetchDownloadProgress(const content::URLFetcher* source, |
| 126 int64 current, int64 total); | 128 int64 current, int64 total) OVERRIDE; |
| 127 protected: | 129 protected: |
| 128 int64 previous_progress_; | 130 int64 previous_progress_; |
| 129 int64 expected_total_; | 131 int64 expected_total_; |
| 130 }; | 132 }; |
| 131 | 133 |
| 132 /// Version of URLFetcherTest that tests progress reports at cancellation. | 134 /// Version of URLFetcherTest that tests progress reports at cancellation. |
| 133 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | 135 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
| 134 public: | 136 public: |
| 135 virtual void CreateFetcher(const GURL& url); | 137 // URLFetcherTest override. |
| 138 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 136 | 139 |
| 137 // content::URLFetcherDelegate | 140 // content::URLFetcherDelegate |
| 138 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 141 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 139 virtual void OnURLFetchDownloadProgress(const content::URLFetcher* source, | 142 virtual void OnURLFetchDownloadProgress(const content::URLFetcher* source, |
| 140 int64 current, int64 total); | 143 int64 current, int64 total) OVERRIDE; |
| 141 protected: | 144 protected: |
| 142 bool cancelled_; | 145 bool cancelled_; |
| 143 }; | 146 }; |
| 144 | 147 |
| 145 // Version of URLFetcherTest that tests headers. | 148 // Version of URLFetcherTest that tests headers. |
| 146 class URLFetcherHeadersTest : public URLFetcherTest { | 149 class URLFetcherHeadersTest : public URLFetcherTest { |
| 147 public: | 150 public: |
| 148 // content::URLFetcherDelegate | 151 // content::URLFetcherDelegate |
| 149 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 152 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 150 }; | 153 }; |
| 151 | 154 |
| 152 // Version of URLFetcherTest that tests SocketAddress. | 155 // Version of URLFetcherTest that tests SocketAddress. |
| 153 class URLFetcherSocketAddressTest : public URLFetcherTest { | 156 class URLFetcherSocketAddressTest : public URLFetcherTest { |
| 154 public: | 157 public: |
| 155 // content::URLFetcherDelegate | 158 // content::URLFetcherDelegate |
| 156 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 159 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 157 protected: | 160 protected: |
| 158 std::string expected_host_; | 161 std::string expected_host_; |
| 159 uint16 expected_port_; | 162 uint16 expected_port_; |
| 160 }; | 163 }; |
| 161 | 164 |
| 162 // Version of URLFetcherTest that tests overload protection. | 165 // Version of URLFetcherTest that tests overload protection. |
| 163 class URLFetcherProtectTest : public URLFetcherTest { | 166 class URLFetcherProtectTest : public URLFetcherTest { |
| 164 public: | 167 public: |
| 165 virtual void CreateFetcher(const GURL& url); | 168 // URLFetcherTest override. |
| 169 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 166 // content::URLFetcherDelegate | 170 // content::URLFetcherDelegate |
| 167 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 171 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 168 private: | 172 private: |
| 169 Time start_time_; | 173 Time start_time_; |
| 170 }; | 174 }; |
| 171 | 175 |
| 172 // Version of URLFetcherTest that tests overload protection, when responses | 176 // Version of URLFetcherTest that tests overload protection, when responses |
| 173 // passed through. | 177 // passed through. |
| 174 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 178 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
| 175 public: | 179 public: |
| 176 virtual void CreateFetcher(const GURL& url); | 180 // URLFetcherTest override. |
| 181 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 177 // content::URLFetcherDelegate | 182 // content::URLFetcherDelegate |
| 178 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 183 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 179 private: | 184 private: |
| 180 Time start_time_; | 185 Time start_time_; |
| 181 }; | 186 }; |
| 182 | 187 |
| 183 // Version of URLFetcherTest that tests bad HTTPS requests. | 188 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 184 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 189 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 185 public: | 190 public: |
| 186 URLFetcherBadHTTPSTest(); | 191 URLFetcherBadHTTPSTest(); |
| 187 | 192 |
| 188 // content::URLFetcherDelegate | 193 // content::URLFetcherDelegate |
| 189 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 194 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 190 | 195 |
| 191 private: | 196 private: |
| 192 FilePath cert_dir_; | 197 FilePath cert_dir_; |
| 193 }; | 198 }; |
| 194 | 199 |
| 195 // Version of URLFetcherTest that tests request cancellation on shutdown. | 200 // Version of URLFetcherTest that tests request cancellation on shutdown. |
| 196 class URLFetcherCancelTest : public URLFetcherTest { | 201 class URLFetcherCancelTest : public URLFetcherTest { |
| 197 public: | 202 public: |
| 198 virtual void CreateFetcher(const GURL& url); | 203 // URLFetcherTest override. |
| 204 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 199 // content::URLFetcherDelegate | 205 // content::URLFetcherDelegate |
| 200 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 206 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 201 | 207 |
| 202 void CancelRequest(); | 208 void CancelRequest(); |
| 203 }; | 209 }; |
| 204 | 210 |
| 205 // Version of TestURLRequestContext that posts a Quit task to the IO | 211 // Version of TestURLRequestContext that posts a Quit task to the IO |
| 206 // thread once it is deleted. | 212 // thread once it is deleted. |
| 207 class CancelTestURLRequestContext : public TestURLRequestContext { | 213 class CancelTestURLRequestContext : public TestURLRequestContext { |
| 208 virtual ~CancelTestURLRequestContext() { | 214 virtual ~CancelTestURLRequestContext() { |
| 209 // The d'tor should execute in the IO thread. Post the quit task to the | 215 // The d'tor should execute in the IO thread. Post the quit task to the |
| 210 // current thread. | 216 // current thread. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 238 | 244 |
| 239 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 245 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 240 base::WaitableEvent context_created_; | 246 base::WaitableEvent context_created_; |
| 241 scoped_refptr<net::URLRequestContext> context_; | 247 scoped_refptr<net::URLRequestContext> context_; |
| 242 }; | 248 }; |
| 243 | 249 |
| 244 // Version of URLFetcherTest that tests retying the same request twice. | 250 // Version of URLFetcherTest that tests retying the same request twice. |
| 245 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 251 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
| 246 public: | 252 public: |
| 247 // content::URLFetcherDelegate | 253 // content::URLFetcherDelegate |
| 248 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 254 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 249 private: | 255 private: |
| 250 std::string data_; | 256 std::string data_; |
| 251 }; | 257 }; |
| 252 | 258 |
| 253 class URLFetcherTempFileTest : public URLFetcherTest { | 259 class URLFetcherTempFileTest : public URLFetcherTest { |
| 254 public: | 260 public: |
| 255 URLFetcherTempFileTest() | 261 URLFetcherTempFileTest() |
| 256 : take_ownership_of_temp_file_(false) { | 262 : take_ownership_of_temp_file_(false) { |
| 257 } | 263 } |
| 258 | 264 |
| 265 // URLFetcherTest override. |
| 266 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| 267 |
| 259 // content::URLFetcherDelegate | 268 // content::URLFetcherDelegate |
| 260 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 269 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 261 | |
| 262 virtual void CreateFetcher(const GURL& url); | |
| 263 | 270 |
| 264 protected: | 271 protected: |
| 265 FilePath expected_file_; | 272 FilePath expected_file_; |
| 266 FilePath temp_file_; | 273 FilePath temp_file_; |
| 267 | 274 |
| 268 // Set by the test. Used in OnURLFetchComplete() to decide if | 275 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 269 // the URLFetcher should own the temp file, so that we can test | 276 // the URLFetcher should own the temp file, so that we can test |
| 270 // disowning prevents the file from being deleted. | 277 // disowning prevents the file from being deleted. |
| 271 bool take_ownership_of_temp_file_; | 278 bool take_ownership_of_temp_file_; |
| 272 }; | 279 }; |
| 273 | 280 |
| 274 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | |
| 275 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | |
| 276 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | |
| 277 io_message_loop_proxy())); | |
| 278 | |
| 279 // Use the IO message loop to do the file operations in this test. | |
| 280 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | |
| 281 fetcher_->Start(); | |
| 282 } | |
| 283 | |
| 284 TEST_F(URLFetcherTempFileTest, SmallGet) { | |
| 285 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 286 net::TestServer::kLocalhost, | |
| 287 FilePath(kDocRoot)); | |
| 288 ASSERT_TRUE(test_server.Start()); | |
| 289 | |
| 290 // Get a small file. | |
| 291 static const char kFileToFetch[] = "simple.html"; | |
| 292 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 293 CreateFetcher( | |
| 294 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 295 | |
| 296 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 297 | |
| 298 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 299 << temp_file_.value() << " not removed."; | |
| 300 } | |
| 301 | |
| 302 TEST_F(URLFetcherTempFileTest, LargeGet) { | |
| 303 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 304 net::TestServer::kLocalhost, | |
| 305 FilePath(kDocRoot)); | |
| 306 ASSERT_TRUE(test_server.Start()); | |
| 307 | |
| 308 // Get a file large enough to require more than one read into | |
| 309 // URLFetcher::Core's IOBuffer. | |
| 310 static const char kFileToFetch[] = "animate1.gif"; | |
| 311 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 312 CreateFetcher(test_server.GetURL( | |
| 313 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 314 | |
| 315 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 316 } | |
| 317 | |
| 318 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { | |
| 319 net::TestServer test_server(net::TestServer::TYPE_HTTP, | |
| 320 net::TestServer::kLocalhost, | |
| 321 FilePath(kDocRoot)); | |
| 322 ASSERT_TRUE(test_server.Start()); | |
| 323 | |
| 324 // Get a small file. | |
| 325 static const char kFileToFetch[] = "simple.html"; | |
| 326 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | |
| 327 CreateFetcher(test_server.GetURL( | |
| 328 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 329 | |
| 330 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 331 | |
| 332 MessageLoop::current()->RunAllPending(); | |
| 333 ASSERT_FALSE(file_util::PathExists(temp_file_)) | |
| 334 << temp_file_.value() << " not removed."; | |
| 335 } | |
| 336 | |
| 337 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 281 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 338 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); | 282 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
| 339 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 283 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 340 io_message_loop_proxy())); | 284 io_message_loop_proxy())); |
| 341 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 285 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 342 "bobsyeruncle"); | 286 "bobsyeruncle"); |
| 343 fetcher_->Start(); | 287 fetcher_->Start(); |
| 344 } | 288 } |
| 345 | 289 |
| 346 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { | 290 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 delete fetcher_; // Have to delete this here and not in the destructor, | 499 delete fetcher_; // Have to delete this here and not in the destructor, |
| 556 // because the destructor won't necessarily run on the | 500 // because the destructor won't necessarily run on the |
| 557 // same thread that CreateFetcher() did. | 501 // same thread that CreateFetcher() did. |
| 558 | 502 |
| 559 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 503 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 560 // If the current message loop is not the IO loop, it will be shut down when | 504 // If the current message loop is not the IO loop, it will be shut down when |
| 561 // the main loop returns and this thread subsequently goes out of scope. | 505 // the main loop returns and this thread subsequently goes out of scope. |
| 562 } | 506 } |
| 563 } | 507 } |
| 564 | 508 |
| 509 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { |
| 510 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 511 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 512 io_message_loop_proxy())); |
| 513 |
| 514 // Use the IO message loop to do the file operations in this test. |
| 515 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
| 516 fetcher_->Start(); |
| 517 } |
| 518 |
| 565 void URLFetcherTempFileTest::OnURLFetchComplete( | 519 void URLFetcherTempFileTest::OnURLFetchComplete( |
| 566 const content::URLFetcher* source) { | 520 const content::URLFetcher* source) { |
| 567 EXPECT_TRUE(source->GetStatus().is_success()); | 521 EXPECT_TRUE(source->GetStatus().is_success()); |
| 568 EXPECT_EQ(source->GetResponseCode(), 200); | 522 EXPECT_EQ(source->GetResponseCode(), 200); |
| 569 | 523 |
| 570 EXPECT_TRUE(source->GetResponseAsFilePath( | 524 EXPECT_TRUE(source->GetResponseAsFilePath( |
| 571 take_ownership_of_temp_file_, &temp_file_)); | 525 take_ownership_of_temp_file_, &temp_file_)); |
| 572 | 526 |
| 573 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 527 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
| 574 | 528 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 ASSERT_TRUE(t.Start()); | 565 ASSERT_TRUE(t.Start()); |
| 612 t.message_loop()->PostTask( | 566 t.message_loop()->PostTask( |
| 613 FROM_HERE, | 567 FROM_HERE, |
| 614 base::Bind(&URLFetcherTest::CreateFetcher, | 568 base::Bind(&URLFetcherTest::CreateFetcher, |
| 615 base::Unretained(this), | 569 base::Unretained(this), |
| 616 test_server.GetURL("defaultresponse"))); | 570 test_server.GetURL("defaultresponse"))); |
| 617 | 571 |
| 618 MessageLoop::current()->Run(); | 572 MessageLoop::current()->Run(); |
| 619 } | 573 } |
| 620 | 574 |
| 575 void CancelAllOnIO() { |
| 576 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); |
| 577 URLFetcherImpl::CancelAll(); |
| 578 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); |
| 579 } |
| 580 |
| 581 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
| 582 TEST_F(URLFetcherTest, CancelAll) { |
| 583 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 584 net::TestServer::kLocalhost, |
| 585 FilePath(kDocRoot)); |
| 586 ASSERT_TRUE(test_server.Start()); |
| 587 EXPECT_EQ(0, GetNumFetcherCores()); |
| 588 |
| 589 CreateFetcher(test_server.GetURL("defaultresponse")); |
| 590 io_message_loop_proxy()->PostTaskAndReply( |
| 591 FROM_HERE, |
| 592 base::Bind(&CancelAllOnIO), |
| 593 MessageLoop::QuitClosure()); |
| 594 MessageLoop::current()->Run(); |
| 595 EXPECT_EQ(0, GetNumFetcherCores()); |
| 596 delete fetcher_; |
| 597 } |
| 598 |
| 621 #if defined(OS_MACOSX) | 599 #if defined(OS_MACOSX) |
| 622 // SIGSEGV on Mac: http://crbug.com/60426 | 600 // SIGSEGV on Mac: http://crbug.com/60426 |
| 623 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | 601 TEST_F(URLFetcherPostTest, DISABLED_Basic) { |
| 624 #else | 602 #else |
| 625 TEST_F(URLFetcherPostTest, Basic) { | 603 TEST_F(URLFetcherPostTest, Basic) { |
| 626 #endif | 604 #endif |
| 627 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 605 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 628 net::TestServer::kLocalhost, | 606 net::TestServer::kLocalhost, |
| 629 FilePath(kDocRoot)); | 607 FilePath(kDocRoot)); |
| 630 ASSERT_TRUE(test_server.Start()); | 608 ASSERT_TRUE(test_server.Start()); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 TEST_F(URLFetcherCancelTest, ReleasesContext) { | 767 TEST_F(URLFetcherCancelTest, ReleasesContext) { |
| 790 #endif | 768 #endif |
| 791 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 769 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 792 net::TestServer::kLocalhost, | 770 net::TestServer::kLocalhost, |
| 793 FilePath(kDocRoot)); | 771 FilePath(kDocRoot)); |
| 794 ASSERT_TRUE(test_server.Start()); | 772 ASSERT_TRUE(test_server.Start()); |
| 795 | 773 |
| 796 GURL url(test_server.GetURL("files/server-unavailable.html")); | 774 GURL url(test_server.GetURL("files/server-unavailable.html")); |
| 797 | 775 |
| 798 // Registers an entry for test url. The backoff time is calculated by: | 776 // Registers an entry for test url. The backoff time is calculated by: |
| 799 // new_backoff = 2.0 * old_backoff +0 | 777 // new_backoff = 2.0 * old_backoff + 0 |
| 800 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 778 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
| 801 // Maximum retries allowed is set to 2. | 779 // Maximum retries allowed is set to 2. |
| 802 net::URLRequestThrottlerManager* manager = | 780 net::URLRequestThrottlerManager* manager = |
| 803 net::URLRequestThrottlerManager::GetInstance(); | 781 net::URLRequestThrottlerManager::GetInstance(); |
| 804 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 782 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
| 805 new net::URLRequestThrottlerEntry( | 783 new net::URLRequestThrottlerEntry( |
| 806 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); | 784 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); |
| 807 manager->OverrideEntryForTests(url, entry); | 785 manager->OverrideEntryForTests(url, entry); |
| 808 | 786 |
| 809 // Create a separate thread that will create the URLFetcher. The current | 787 // Create a separate thread that will create the URLFetcher. The current |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 ASSERT_TRUE(test_server.Start()); | 842 ASSERT_TRUE(test_server.Start()); |
| 865 | 843 |
| 866 // Create the fetcher on the main thread. Since IO will happen on the main | 844 // Create the fetcher on the main thread. Since IO will happen on the main |
| 867 // thread, this will test URLFetcher's ability to do everything on one | 845 // thread, this will test URLFetcher's ability to do everything on one |
| 868 // thread. | 846 // thread. |
| 869 CreateFetcher(test_server.GetURL("defaultresponse")); | 847 CreateFetcher(test_server.GetURL("defaultresponse")); |
| 870 | 848 |
| 871 MessageLoop::current()->Run(); | 849 MessageLoop::current()->Run(); |
| 872 } | 850 } |
| 873 | 851 |
| 874 void CancelAllOnIO() { | 852 TEST_F(URLFetcherTempFileTest, SmallGet) { |
| 875 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | |
| 876 URLFetcherImpl::CancelAll(); | |
| 877 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | |
| 878 } | |
| 879 | |
| 880 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | |
| 881 TEST_F(URLFetcherTest, CancelAll) { | |
| 882 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 853 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 883 net::TestServer::kLocalhost, | 854 net::TestServer::kLocalhost, |
| 884 FilePath(kDocRoot)); | 855 FilePath(kDocRoot)); |
| 885 ASSERT_TRUE(test_server.Start()); | 856 ASSERT_TRUE(test_server.Start()); |
| 886 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 887 | 857 |
| 888 CreateFetcher(test_server.GetURL("defaultresponse")); | 858 // Get a small file. |
| 889 io_message_loop_proxy()->PostTaskAndReply( | 859 static const char kFileToFetch[] = "simple.html"; |
| 890 FROM_HERE, | 860 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 891 base::Bind(&CancelAllOnIO), | 861 CreateFetcher( |
| 892 MessageLoop::QuitClosure()); | 862 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 893 MessageLoop::current()->Run(); | 863 |
| 894 EXPECT_EQ(0, GetNumFetcherCores()); | 864 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 895 delete fetcher_; | 865 |
| 866 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
| 867 << temp_file_.value() << " not removed."; |
| 868 } |
| 869 |
| 870 TEST_F(URLFetcherTempFileTest, LargeGet) { |
| 871 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 872 net::TestServer::kLocalhost, |
| 873 FilePath(kDocRoot)); |
| 874 ASSERT_TRUE(test_server.Start()); |
| 875 |
| 876 // Get a file large enough to require more than one read into |
| 877 // URLFetcher::Core's IOBuffer. |
| 878 static const char kFileToFetch[] = "animate1.gif"; |
| 879 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 880 CreateFetcher(test_server.GetURL( |
| 881 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 882 |
| 883 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 884 } |
| 885 |
| 886 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { |
| 887 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 888 net::TestServer::kLocalhost, |
| 889 FilePath(kDocRoot)); |
| 890 ASSERT_TRUE(test_server.Start()); |
| 891 |
| 892 // Get a small file. |
| 893 static const char kFileToFetch[] = "simple.html"; |
| 894 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
| 895 CreateFetcher(test_server.GetURL( |
| 896 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 897 |
| 898 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 899 |
| 900 MessageLoop::current()->RunAllPending(); |
| 901 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
| 902 << temp_file_.value() << " not removed."; |
| 896 } | 903 } |
| 897 | 904 |
| 898 } // namespace. | 905 } // namespace. |
| OLD | NEW |