| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/url_request/url_fetcher_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/message_loop/message_loop_proxy.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/synchronization/waitable_event.h" | |
| 17 #include "base/threading/thread.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "crypto/nss_util.h" | |
| 20 #include "net/base/elements_upload_data_stream.h" | |
| 21 #include "net/base/network_change_notifier.h" | |
| 22 #include "net/base/upload_bytes_element_reader.h" | |
| 23 #include "net/base/upload_element_reader.h" | |
| 24 #include "net/base/upload_file_element_reader.h" | |
| 25 #include "net/dns/mock_host_resolver.h" | |
| 26 #include "net/http/http_response_headers.h" | |
| 27 #include "net/test/spawned_test_server/spawned_test_server.h" | |
| 28 #include "net/url_request/url_fetcher_delegate.h" | |
| 29 #include "net/url_request/url_request_context_getter.h" | |
| 30 #include "net/url_request/url_request_test_util.h" | |
| 31 #include "net/url_request/url_request_throttler_manager.h" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | |
| 33 | |
| 34 #if defined(USE_NSS) || defined(OS_IOS) | |
| 35 #include "net/ocsp/nss_ocsp.h" | |
| 36 #endif | |
| 37 | |
| 38 namespace net { | |
| 39 | |
| 40 using base::Time; | |
| 41 using base::TimeDelta; | |
| 42 | |
| 43 // TODO(eroman): Add a regression test for http://crbug.com/40505. | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 // TODO(akalin): Move all the test data to somewhere under net/. | |
| 48 const base::FilePath::CharType kDocRoot[] = | |
| 49 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); | |
| 50 const char kTestServerFilePrefix[] = "files/"; | |
| 51 | |
| 52 class ThrottlingTestURLRequestContext : public TestURLRequestContext { | |
| 53 public: | |
| 54 ThrottlingTestURLRequestContext() : TestURLRequestContext(true) { | |
| 55 set_throttler_manager(&throttler_manager_); | |
| 56 Init(); | |
| 57 DCHECK(throttler_manager() != NULL); | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 URLRequestThrottlerManager throttler_manager_; | |
| 62 }; | |
| 63 | |
| 64 class ThrottlingTestURLRequestContextGetter | |
| 65 : public TestURLRequestContextGetter { | |
| 66 public: | |
| 67 ThrottlingTestURLRequestContextGetter( | |
| 68 base::MessageLoopProxy* io_message_loop_proxy, | |
| 69 TestURLRequestContext* request_context) | |
| 70 : TestURLRequestContextGetter(io_message_loop_proxy), | |
| 71 context_(request_context) { | |
| 72 } | |
| 73 | |
| 74 // TestURLRequestContextGetter: | |
| 75 TestURLRequestContext* GetURLRequestContext() override { return context_; } | |
| 76 | |
| 77 protected: | |
| 78 ~ThrottlingTestURLRequestContextGetter() override {} | |
| 79 | |
| 80 TestURLRequestContext* const context_; | |
| 81 }; | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 class URLFetcherTest : public testing::Test, | |
| 86 public URLFetcherDelegate { | |
| 87 public: | |
| 88 URLFetcherTest() : fetcher_(NULL), expected_status_code_(200) {} | |
| 89 | |
| 90 static int GetNumFetcherCores() { | |
| 91 return URLFetcherImpl::GetNumFetcherCores(); | |
| 92 } | |
| 93 | |
| 94 // Creates a URLFetcher, using the program's main thread to do IO. | |
| 95 virtual void CreateFetcher(const GURL& url); | |
| 96 | |
| 97 // URLFetcherDelegate: | |
| 98 // Subclasses that override this should either call this function or | |
| 99 // CleanupAfterFetchComplete() at the end of their processing, depending on | |
| 100 // whether they want to check for a non-empty HTTP 200 response or not. | |
| 101 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 102 | |
| 103 // Deletes |fetcher| and terminates the message loop. | |
| 104 void CleanupAfterFetchComplete(); | |
| 105 | |
| 106 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | |
| 107 return io_message_loop_proxy_; | |
| 108 } | |
| 109 | |
| 110 TestURLRequestContext* request_context() { | |
| 111 return context_.get(); | |
| 112 } | |
| 113 | |
| 114 protected: | |
| 115 // testing::Test: | |
| 116 void SetUp() override { | |
| 117 testing::Test::SetUp(); | |
| 118 | |
| 119 context_.reset(new ThrottlingTestURLRequestContext()); | |
| 120 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | |
| 121 | |
| 122 #if defined(USE_NSS) || defined(OS_IOS) | |
| 123 crypto::EnsureNSSInit(); | |
| 124 EnsureNSSHttpIOInit(); | |
| 125 #endif | |
| 126 } | |
| 127 | |
| 128 void TearDown() override { | |
| 129 #if defined(USE_NSS) || defined(OS_IOS) | |
| 130 ShutdownNSSHttpIO(); | |
| 131 #endif | |
| 132 } | |
| 133 | |
| 134 // 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 | |
| 136 // dispatches its requests to. When we wish to simulate being used from | |
| 137 // a UI thread, we dispatch a worker thread to do so. | |
| 138 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 139 | |
| 140 URLFetcherImpl* fetcher_; | |
| 141 scoped_ptr<TestURLRequestContext> context_; | |
| 142 int expected_status_code_; | |
| 143 }; | |
| 144 | |
| 145 // 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. | |
| 147 class URLFetcherMockDnsTest : public URLFetcherTest { | |
| 148 public: | |
| 149 // testing::Test: | |
| 150 void SetUp() override; | |
| 151 | |
| 152 // URLFetcherTest: | |
| 153 void CreateFetcher(const GURL& url) override; | |
| 154 | |
| 155 // URLFetcherDelegate: | |
| 156 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 157 | |
| 158 protected: | |
| 159 GURL test_url_; | |
| 160 scoped_ptr<SpawnedTestServer> test_server_; | |
| 161 MockHostResolver resolver_; | |
| 162 scoped_ptr<URLFetcher> completed_fetcher_; | |
| 163 }; | |
| 164 | |
| 165 void URLFetcherTest::CreateFetcher(const GURL& url) { | |
| 166 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 167 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 168 io_message_loop_proxy().get(), request_context())); | |
| 169 fetcher_->Start(); | |
| 170 } | |
| 171 | |
| 172 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 173 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 174 EXPECT_EQ(expected_status_code_, source->GetResponseCode()); // HTTP OK | |
| 175 | |
| 176 std::string data; | |
| 177 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 178 EXPECT_FALSE(data.empty()); | |
| 179 | |
| 180 CleanupAfterFetchComplete(); | |
| 181 } | |
| 182 | |
| 183 void URLFetcherTest::CleanupAfterFetchComplete() { | |
| 184 delete fetcher_; // Have to delete this here and not in the destructor, | |
| 185 // because the destructor won't necessarily run on the | |
| 186 // same thread that CreateFetcher() did. | |
| 187 | |
| 188 io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 189 base::MessageLoop::QuitClosure()); | |
| 190 // If the current message loop is not the IO loop, it will be shut down when | |
| 191 // the main loop returns and this thread subsequently goes out of scope. | |
| 192 } | |
| 193 | |
| 194 void URLFetcherMockDnsTest::SetUp() { | |
| 195 URLFetcherTest::SetUp(); | |
| 196 | |
| 197 resolver_.set_ondemand_mode(true); | |
| 198 resolver_.rules()->AddRule("example.com", "127.0.0.1"); | |
| 199 | |
| 200 context_.reset(new TestURLRequestContext(true)); | |
| 201 context_->set_host_resolver(&resolver_); | |
| 202 context_->Init(); | |
| 203 | |
| 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 | |
| 210 // immediately resolved by the MockHostResolver. Use a hostname instead to | |
| 211 // trigger an async resolve. | |
| 212 test_url_ = GURL( | |
| 213 base::StringPrintf("http://example.com:%d/defaultresponse", | |
| 214 test_server_->host_port_pair().port())); | |
| 215 ASSERT_TRUE(test_url_.is_valid()); | |
| 216 } | |
| 217 | |
| 218 void URLFetcherMockDnsTest::CreateFetcher(const GURL& url) { | |
| 219 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 220 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 221 io_message_loop_proxy().get(), request_context())); | |
| 222 } | |
| 223 | |
| 224 void URLFetcherMockDnsTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 225 io_message_loop_proxy()->PostTask(FROM_HERE, | |
| 226 base::MessageLoop::QuitClosure()); | |
| 227 ASSERT_EQ(fetcher_, source); | |
| 228 EXPECT_EQ(test_url_, source->GetOriginalURL()); | |
| 229 completed_fetcher_.reset(fetcher_); | |
| 230 } | |
| 231 | |
| 232 namespace { | |
| 233 | |
| 234 // Version of URLFetcherTest that does a POST instead | |
| 235 class URLFetcherPostTest : public URLFetcherTest { | |
| 236 public: | |
| 237 // URLFetcherTest: | |
| 238 void CreateFetcher(const GURL& url) override; | |
| 239 | |
| 240 // URLFetcherDelegate: | |
| 241 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 242 }; | |
| 243 | |
| 244 // Version of URLFetcherTest that does a POST of a file using | |
| 245 // SetUploadDataStream | |
| 246 class URLFetcherPostFileTest : public URLFetcherTest { | |
| 247 public: | |
| 248 URLFetcherPostFileTest(); | |
| 249 | |
| 250 void SetUploadRange(uint64 range_offset, uint64 range_length) { | |
| 251 range_offset_ = range_offset; | |
| 252 range_length_ = range_length; | |
| 253 } | |
| 254 | |
| 255 // URLFetcherTest: | |
| 256 void CreateFetcher(const GURL& url) override; | |
| 257 | |
| 258 // URLFetcherDelegate: | |
| 259 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 260 | |
| 261 private: | |
| 262 base::FilePath path_; | |
| 263 uint64 range_offset_; | |
| 264 uint64 range_length_; | |
| 265 }; | |
| 266 | |
| 267 class URLFetcherSetUploadFactoryTest : public URLFetcherTest { | |
| 268 public: | |
| 269 URLFetcherSetUploadFactoryTest() : create_stream_count_(0) {} | |
| 270 | |
| 271 // URLFetcherTest: | |
| 272 void CreateFetcher(const GURL& url) override; | |
| 273 | |
| 274 // URLFetcherDelegate: | |
| 275 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 276 | |
| 277 // Callback passed to URLFetcher to create upload stream. | |
| 278 scoped_ptr<UploadDataStream> CreateUploadStream() { | |
| 279 ++create_stream_count_; | |
| 280 const std::string str("bobsyeruncle\n"); | |
| 281 std::vector<char> buffer(str.begin(), str.end()); | |
| 282 return ElementsUploadDataStream::CreateWithReader( | |
| 283 scoped_ptr<UploadElementReader>( | |
| 284 new UploadOwnedBytesElementReader(&buffer)), | |
| 285 0); | |
| 286 } | |
| 287 | |
| 288 size_t create_stream_count() const { return create_stream_count_; } | |
| 289 | |
| 290 private: | |
| 291 // Count of calling CreateStream. | |
| 292 size_t create_stream_count_; | |
| 293 }; | |
| 294 | |
| 295 // Version of URLFetcherTest that does a POST instead with empty upload body | |
| 296 class URLFetcherEmptyPostTest : public URLFetcherTest { | |
| 297 public: | |
| 298 // URLFetcherTest: | |
| 299 void CreateFetcher(const GURL& url) override; | |
| 300 | |
| 301 // URLFetcherDelegate: | |
| 302 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 303 }; | |
| 304 | |
| 305 // Version of URLFetcherTest that tests download progress reports. | |
| 306 class URLFetcherDownloadProgressTest : public URLFetcherTest { | |
| 307 public: | |
| 308 URLFetcherDownloadProgressTest() | |
| 309 : previous_progress_(0), | |
| 310 expected_total_(0) { | |
| 311 } | |
| 312 | |
| 313 // URLFetcherTest: | |
| 314 void CreateFetcher(const GURL& url) override; | |
| 315 | |
| 316 // URLFetcherDelegate: | |
| 317 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 318 int64 current, | |
| 319 int64 total) override; | |
| 320 | |
| 321 protected: | |
| 322 // Download progress returned by the previous callback. | |
| 323 int64 previous_progress_; | |
| 324 // Size of the file being downloaded, known in advance (provided by each test | |
| 325 // case). | |
| 326 int64 expected_total_; | |
| 327 }; | |
| 328 | |
| 329 // Version of URLFetcherTest that tests progress reports at cancellation. | |
| 330 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | |
| 331 public: | |
| 332 // URLFetcherTest: | |
| 333 void CreateFetcher(const GURL& url) override; | |
| 334 | |
| 335 // URLFetcherDelegate: | |
| 336 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 337 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 338 int64 current, | |
| 339 int64 total) override; | |
| 340 | |
| 341 protected: | |
| 342 bool cancelled_; | |
| 343 }; | |
| 344 | |
| 345 // Version of URLFetcherTest that tests upload progress reports. | |
| 346 class URLFetcherUploadProgressTest : public URLFetcherTest { | |
| 347 public: | |
| 348 // URLFetcherTest: | |
| 349 void CreateFetcher(const GURL& url) override; | |
| 350 | |
| 351 // URLFetcherDelegate: | |
| 352 void OnURLFetchUploadProgress(const URLFetcher* source, | |
| 353 int64 current, | |
| 354 int64 total) override; | |
| 355 | |
| 356 protected: | |
| 357 int64 previous_progress_; | |
| 358 std::string chunk_; | |
| 359 int64 number_of_chunks_added_; | |
| 360 }; | |
| 361 | |
| 362 // Version of URLFetcherTest that tests headers. | |
| 363 class URLFetcherHeadersTest : public URLFetcherTest { | |
| 364 public: | |
| 365 // URLFetcherDelegate: | |
| 366 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 367 }; | |
| 368 | |
| 369 // Version of URLFetcherTest that tests SocketAddress. | |
| 370 class URLFetcherSocketAddressTest : public URLFetcherTest { | |
| 371 public: | |
| 372 // URLFetcherDelegate: | |
| 373 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 374 | |
| 375 protected: | |
| 376 std::string expected_host_; | |
| 377 uint16 expected_port_; | |
| 378 }; | |
| 379 | |
| 380 // Version of URLFetcherTest that tests stopping on a redirect. | |
| 381 class URLFetcherStopOnRedirectTest : public URLFetcherTest { | |
| 382 public: | |
| 383 URLFetcherStopOnRedirectTest(); | |
| 384 ~URLFetcherStopOnRedirectTest() override; | |
| 385 | |
| 386 // URLFetcherTest: | |
| 387 void CreateFetcher(const GURL& url) override; | |
| 388 | |
| 389 // URLFetcherDelegate: | |
| 390 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 391 | |
| 392 protected: | |
| 393 // The URL we should be redirected to. | |
| 394 static const char* kRedirectTarget; | |
| 395 | |
| 396 bool callback_called_; // Set to true in OnURLFetchComplete(). | |
| 397 }; | |
| 398 | |
| 399 // Version of URLFetcherTest that tests overload protection. | |
| 400 class URLFetcherProtectTest : public URLFetcherTest { | |
| 401 public: | |
| 402 // URLFetcherTest: | |
| 403 void CreateFetcher(const GURL& url) override; | |
| 404 | |
| 405 // URLFetcherDelegate: | |
| 406 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 407 | |
| 408 private: | |
| 409 Time start_time_; | |
| 410 }; | |
| 411 | |
| 412 // Version of URLFetcherTest that tests overload protection, when responses | |
| 413 // passed through. | |
| 414 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | |
| 415 public: | |
| 416 // URLFetcherTest: | |
| 417 void CreateFetcher(const GURL& url) override; | |
| 418 | |
| 419 // URLFetcherDelegate: | |
| 420 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 421 | |
| 422 private: | |
| 423 Time start_time_; | |
| 424 }; | |
| 425 | |
| 426 // Version of URLFetcherTest that tests bad HTTPS requests. | |
| 427 class URLFetcherBadHTTPSTest : public URLFetcherTest { | |
| 428 public: | |
| 429 URLFetcherBadHTTPSTest(); | |
| 430 | |
| 431 // URLFetcherDelegate: | |
| 432 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 433 | |
| 434 private: | |
| 435 base::FilePath cert_dir_; | |
| 436 }; | |
| 437 | |
| 438 // Version of URLFetcherTest that tests request cancellation on shutdown. | |
| 439 class URLFetcherCancelTest : public URLFetcherTest { | |
| 440 public: | |
| 441 // URLFetcherTest: | |
| 442 void CreateFetcher(const GURL& url) override; | |
| 443 | |
| 444 // URLFetcherDelegate: | |
| 445 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 446 | |
| 447 void CancelRequest(); | |
| 448 }; | |
| 449 | |
| 450 // Version of TestURLRequestContext that posts a Quit task to the IO | |
| 451 // thread once it is deleted. | |
| 452 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { | |
| 453 public: | |
| 454 explicit CancelTestURLRequestContext() { | |
| 455 } | |
| 456 | |
| 457 private: | |
| 458 ~CancelTestURLRequestContext() override { | |
| 459 // The d'tor should execute in the IO thread. Post the quit task to the | |
| 460 // current thread. | |
| 461 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 462 base::MessageLoop::QuitClosure()); | |
| 463 } | |
| 464 }; | |
| 465 | |
| 466 class CancelTestURLRequestContextGetter | |
| 467 : public TestURLRequestContextGetter { | |
| 468 public: | |
| 469 CancelTestURLRequestContextGetter( | |
| 470 base::MessageLoopProxy* io_message_loop_proxy, | |
| 471 const GURL& throttle_for_url) | |
| 472 : TestURLRequestContextGetter(io_message_loop_proxy), | |
| 473 io_message_loop_proxy_(io_message_loop_proxy), | |
| 474 context_created_(false, false), | |
| 475 throttle_for_url_(throttle_for_url) { | |
| 476 } | |
| 477 | |
| 478 // TestURLRequestContextGetter: | |
| 479 TestURLRequestContext* GetURLRequestContext() override { | |
| 480 if (!context_.get()) { | |
| 481 context_.reset(new CancelTestURLRequestContext()); | |
| 482 DCHECK(context_->throttler_manager()); | |
| 483 | |
| 484 // Registers an entry for test url. The backoff time is calculated by: | |
| 485 // new_backoff = 2.0 * old_backoff + 0 | |
| 486 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | |
| 487 // Maximum retries allowed is set to 2. | |
| 488 scoped_refptr<URLRequestThrottlerEntry> entry( | |
| 489 new URLRequestThrottlerEntry(context_->throttler_manager(), | |
| 490 std::string(), | |
| 491 200, | |
| 492 3, | |
| 493 2000, | |
| 494 2.0, | |
| 495 0.0, | |
| 496 4000)); | |
| 497 context_->throttler_manager() | |
| 498 ->OverrideEntryForTests(throttle_for_url_, entry.get()); | |
| 499 | |
| 500 context_created_.Signal(); | |
| 501 } | |
| 502 return context_.get(); | |
| 503 } | |
| 504 | |
| 505 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | |
| 506 return io_message_loop_proxy_; | |
| 507 } | |
| 508 | |
| 509 void WaitForContextCreation() { | |
| 510 context_created_.Wait(); | |
| 511 } | |
| 512 | |
| 513 protected: | |
| 514 ~CancelTestURLRequestContextGetter() override {} | |
| 515 | |
| 516 private: | |
| 517 scoped_ptr<TestURLRequestContext> context_; | |
| 518 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
| 519 base::WaitableEvent context_created_; | |
| 520 GURL throttle_for_url_; | |
| 521 }; | |
| 522 | |
| 523 // Version of URLFetcherTest that tests retying the same request twice. | |
| 524 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | |
| 525 public: | |
| 526 // URLFetcherDelegate: | |
| 527 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 528 | |
| 529 private: | |
| 530 std::string data_; | |
| 531 }; | |
| 532 | |
| 533 class URLFetcherFileTest : public URLFetcherTest { | |
| 534 public: | |
| 535 URLFetcherFileTest() : take_ownership_of_file_(false), | |
| 536 expected_file_error_(OK) {} | |
| 537 | |
| 538 void CreateFetcherForFile(const GURL& url, const base::FilePath& file_path); | |
| 539 void CreateFetcherForTempFile(const GURL& url); | |
| 540 | |
| 541 // URLFetcherDelegate: | |
| 542 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 543 | |
| 544 protected: | |
| 545 base::FilePath expected_file_; | |
| 546 base::FilePath file_path_; | |
| 547 | |
| 548 // Set by the test. Used in OnURLFetchComplete() to decide if | |
| 549 // the URLFetcher should own the temp file, so that we can test | |
| 550 // disowning prevents the file from being deleted. | |
| 551 bool take_ownership_of_file_; | |
| 552 | |
| 553 // Expected file error code for the test. OK when expecting success. | |
| 554 int expected_file_error_; | |
| 555 }; | |
| 556 | |
| 557 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | |
| 558 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 559 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 560 io_message_loop_proxy().get(), request_context())); | |
| 561 fetcher_->SetUploadData("application/x-www-form-urlencoded", "bobsyeruncle"); | |
| 562 fetcher_->Start(); | |
| 563 } | |
| 564 | |
| 565 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 566 std::string data; | |
| 567 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 568 EXPECT_EQ(std::string("bobsyeruncle"), data); | |
| 569 URLFetcherTest::OnURLFetchComplete(source); | |
| 570 } | |
| 571 | |
| 572 URLFetcherPostFileTest::URLFetcherPostFileTest() | |
| 573 : range_offset_(0), | |
| 574 range_length_(kuint64max) { | |
| 575 PathService::Get(base::DIR_SOURCE_ROOT, &path_); | |
| 576 path_ = path_.Append(FILE_PATH_LITERAL("net")); | |
| 577 path_ = path_.Append(FILE_PATH_LITERAL("data")); | |
| 578 path_ = path_.Append(FILE_PATH_LITERAL("url_request_unittest")); | |
| 579 path_ = path_.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); | |
| 580 } | |
| 581 | |
| 582 void URLFetcherPostFileTest::CreateFetcher(const GURL& url) { | |
| 583 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 584 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 585 io_message_loop_proxy().get(), request_context())); | |
| 586 fetcher_->SetUploadFilePath("application/x-www-form-urlencoded", | |
| 587 path_, | |
| 588 range_offset_, | |
| 589 range_length_, | |
| 590 base::MessageLoopProxy::current()); | |
| 591 fetcher_->Start(); | |
| 592 } | |
| 593 | |
| 594 void URLFetcherPostFileTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 595 std::string expected; | |
| 596 ASSERT_TRUE(base::ReadFileToString(path_, &expected)); | |
| 597 ASSERT_LE(range_offset_, expected.size()); | |
| 598 uint64 expected_size = | |
| 599 std::min(range_length_, expected.size() - range_offset_); | |
| 600 | |
| 601 std::string data; | |
| 602 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 603 EXPECT_EQ(expected.substr(range_offset_, expected_size), data); | |
| 604 URLFetcherTest::OnURLFetchComplete(source); | |
| 605 } | |
| 606 | |
| 607 void URLFetcherSetUploadFactoryTest::CreateFetcher(const GURL& url) { | |
| 608 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 609 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 610 io_message_loop_proxy().get(), request_context())); | |
| 611 fetcher_->SetUploadStreamFactory( | |
| 612 "text/plain", | |
| 613 base::Bind(&URLFetcherSetUploadFactoryTest::CreateUploadStream, | |
| 614 base::Unretained(this))); | |
| 615 fetcher_->SetAutomaticallyRetryOn5xx(true); | |
| 616 fetcher_->SetMaxRetriesOn5xx(1); | |
| 617 fetcher_->Start(); | |
| 618 } | |
| 619 | |
| 620 void URLFetcherSetUploadFactoryTest::OnURLFetchComplete( | |
| 621 const URLFetcher* source) { | |
| 622 std::string data; | |
| 623 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 624 EXPECT_EQ("bobsyeruncle\n", data); | |
| 625 URLFetcherTest::OnURLFetchComplete(source); | |
| 626 } | |
| 627 | |
| 628 void URLFetcherEmptyPostTest::CreateFetcher(const GURL& url) { | |
| 629 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 630 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | |
| 631 io_message_loop_proxy())); | |
| 632 fetcher_->SetUploadData("text/plain", std::string()); | |
| 633 fetcher_->Start(); | |
| 634 } | |
| 635 | |
| 636 void URLFetcherEmptyPostTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 637 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 638 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | |
| 639 | |
| 640 std::string data; | |
| 641 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 642 EXPECT_TRUE(data.empty()); | |
| 643 | |
| 644 CleanupAfterFetchComplete(); | |
| 645 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, | |
| 646 // since it expects a non-empty response. | |
| 647 } | |
| 648 | |
| 649 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | |
| 650 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 651 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 652 io_message_loop_proxy().get(), request_context())); | |
| 653 fetcher_->Start(); | |
| 654 } | |
| 655 | |
| 656 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | |
| 657 const URLFetcher* source, int64 progress, int64 total) { | |
| 658 // Increasing between 0 and total. | |
| 659 EXPECT_LE(0, progress); | |
| 660 EXPECT_GE(total, progress); | |
| 661 EXPECT_LE(previous_progress_, progress); | |
| 662 EXPECT_EQ(expected_total_, total); | |
| 663 previous_progress_ = progress; | |
| 664 } | |
| 665 | |
| 666 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | |
| 667 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 668 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 669 io_message_loop_proxy().get(), request_context())); | |
| 670 cancelled_ = false; | |
| 671 fetcher_->Start(); | |
| 672 } | |
| 673 | |
| 674 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( | |
| 675 const URLFetcher* source, int64 current, int64 total) { | |
| 676 EXPECT_FALSE(cancelled_); | |
| 677 if (!cancelled_) { | |
| 678 cancelled_ = true; | |
| 679 CleanupAfterFetchComplete(); | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( | |
| 684 const URLFetcher* source) { | |
| 685 // Should have been cancelled. | |
| 686 ADD_FAILURE(); | |
| 687 CleanupAfterFetchComplete(); | |
| 688 } | |
| 689 | |
| 690 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { | |
| 691 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 692 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 693 io_message_loop_proxy().get(), request_context())); | |
| 694 previous_progress_ = 0; | |
| 695 // Large enough data to require more than one read from UploadDataStream. | |
| 696 chunk_.assign(1<<16, 'a'); | |
| 697 // Use chunked upload to wait for a timer event of progress notification. | |
| 698 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); | |
| 699 fetcher_->Start(); | |
| 700 number_of_chunks_added_ = 1; | |
| 701 fetcher_->AppendChunkToUpload(chunk_, false); | |
| 702 } | |
| 703 | |
| 704 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress( | |
| 705 const URLFetcher* source, int64 current, int64 total) { | |
| 706 // Increasing between 0 and total. | |
| 707 EXPECT_LE(0, current); | |
| 708 EXPECT_GE(static_cast<int64>(chunk_.size()) * number_of_chunks_added_, | |
| 709 current); | |
| 710 EXPECT_LE(previous_progress_, current); | |
| 711 previous_progress_ = current; | |
| 712 EXPECT_EQ(-1, total); | |
| 713 | |
| 714 if (number_of_chunks_added_ < 2) { | |
| 715 number_of_chunks_added_ += 1; | |
| 716 fetcher_->AppendChunkToUpload(chunk_, true); | |
| 717 } | |
| 718 } | |
| 719 | |
| 720 void URLFetcherHeadersTest::OnURLFetchComplete( | |
| 721 const URLFetcher* source) { | |
| 722 std::string header; | |
| 723 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control", | |
| 724 &header)); | |
| 725 EXPECT_EQ("private", header); | |
| 726 URLFetcherTest::OnURLFetchComplete(source); | |
| 727 } | |
| 728 | |
| 729 void URLFetcherSocketAddressTest::OnURLFetchComplete( | |
| 730 const URLFetcher* source) { | |
| 731 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); | |
| 732 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); | |
| 733 URLFetcherTest::OnURLFetchComplete(source); | |
| 734 } | |
| 735 | |
| 736 // static | |
| 737 const char* URLFetcherStopOnRedirectTest::kRedirectTarget = | |
| 738 "http://redirect.target.com"; | |
| 739 | |
| 740 URLFetcherStopOnRedirectTest::URLFetcherStopOnRedirectTest() | |
| 741 : callback_called_(false) { | |
| 742 } | |
| 743 | |
| 744 URLFetcherStopOnRedirectTest::~URLFetcherStopOnRedirectTest() { | |
| 745 } | |
| 746 | |
| 747 void URLFetcherStopOnRedirectTest::CreateFetcher(const GURL& url) { | |
| 748 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 749 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 750 io_message_loop_proxy().get(), request_context())); | |
| 751 fetcher_->SetStopOnRedirect(true); | |
| 752 fetcher_->Start(); | |
| 753 } | |
| 754 | |
| 755 void URLFetcherStopOnRedirectTest::OnURLFetchComplete( | |
| 756 const URLFetcher* source) { | |
| 757 callback_called_ = true; | |
| 758 EXPECT_EQ(GURL(kRedirectTarget), source->GetURL()); | |
| 759 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); | |
| 760 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); | |
| 761 EXPECT_EQ(301, source->GetResponseCode()); | |
| 762 CleanupAfterFetchComplete(); | |
| 763 } | |
| 764 | |
| 765 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | |
| 766 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 767 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 768 io_message_loop_proxy().get(), request_context())); | |
| 769 start_time_ = Time::Now(); | |
| 770 fetcher_->SetMaxRetriesOn5xx(11); | |
| 771 fetcher_->Start(); | |
| 772 } | |
| 773 | |
| 774 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 775 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | |
| 776 if (source->GetResponseCode() >= 500) { | |
| 777 // Now running ServerUnavailable test. | |
| 778 // It takes more than 1 second to finish all 11 requests. | |
| 779 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | |
| 780 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 781 std::string data; | |
| 782 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 783 EXPECT_FALSE(data.empty()); | |
| 784 CleanupAfterFetchComplete(); | |
| 785 } else { | |
| 786 // Now running Overload test. | |
| 787 static int count = 0; | |
| 788 count++; | |
| 789 if (count < 20) { | |
| 790 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 791 io_message_loop_proxy().get(), request_context())); | |
| 792 fetcher_->Start(); | |
| 793 } else { | |
| 794 // We have already sent 20 requests continuously. And we expect that | |
| 795 // it takes more than 1 second due to the overload protection settings. | |
| 796 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | |
| 797 URLFetcherTest::OnURLFetchComplete(source); | |
| 798 } | |
| 799 } | |
| 800 } | |
| 801 | |
| 802 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | |
| 803 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 804 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 805 io_message_loop_proxy().get(), request_context())); | |
| 806 fetcher_->SetAutomaticallyRetryOn5xx(false); | |
| 807 start_time_ = Time::Now(); | |
| 808 fetcher_->SetMaxRetriesOn5xx(11); | |
| 809 fetcher_->Start(); | |
| 810 } | |
| 811 | |
| 812 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | |
| 813 const URLFetcher* source) { | |
| 814 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | |
| 815 if (source->GetResponseCode() >= 500) { | |
| 816 // Now running ServerUnavailable test. | |
| 817 // It should get here on the first attempt, so almost immediately and | |
| 818 // *not* to attempt to execute all 11 requests (2.5 minutes). | |
| 819 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); | |
| 820 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 821 // Check that suggested back off time is bigger than 0. | |
| 822 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0); | |
| 823 std::string data; | |
| 824 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 825 EXPECT_FALSE(data.empty()); | |
| 826 } else { | |
| 827 // We should not get here! | |
| 828 ADD_FAILURE(); | |
| 829 } | |
| 830 | |
| 831 CleanupAfterFetchComplete(); | |
| 832 } | |
| 833 | |
| 834 | |
| 835 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | |
| 836 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | |
| 837 cert_dir_ = cert_dir_.AppendASCII("chrome"); | |
| 838 cert_dir_ = cert_dir_.AppendASCII("test"); | |
| 839 cert_dir_ = cert_dir_.AppendASCII("data"); | |
| 840 cert_dir_ = cert_dir_.AppendASCII("ssl"); | |
| 841 cert_dir_ = cert_dir_.AppendASCII("certificates"); | |
| 842 } | |
| 843 | |
| 844 // The "server certificate expired" error should result in automatic | |
| 845 // cancellation of the request by | |
| 846 // URLRequest::Delegate::OnSSLCertificateError. | |
| 847 void URLFetcherBadHTTPSTest::OnURLFetchComplete( | |
| 848 const URLFetcher* source) { | |
| 849 // This part is different from URLFetcherTest::OnURLFetchComplete | |
| 850 // because this test expects the request to be cancelled. | |
| 851 EXPECT_EQ(URLRequestStatus::CANCELED, source->GetStatus().status()); | |
| 852 EXPECT_EQ(ERR_ABORTED, source->GetStatus().error()); | |
| 853 EXPECT_EQ(-1, source->GetResponseCode()); | |
| 854 EXPECT_TRUE(source->GetCookies().empty()); | |
| 855 std::string data; | |
| 856 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 857 EXPECT_TRUE(data.empty()); | |
| 858 CleanupAfterFetchComplete(); | |
| 859 } | |
| 860 | |
| 861 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | |
| 862 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 863 CancelTestURLRequestContextGetter* context_getter = | |
| 864 new CancelTestURLRequestContextGetter(io_message_loop_proxy().get(), url); | |
| 865 fetcher_->SetRequestContext(context_getter); | |
| 866 fetcher_->SetMaxRetriesOn5xx(2); | |
| 867 fetcher_->Start(); | |
| 868 // We need to wait for the creation of the URLRequestContext, since we | |
| 869 // rely on it being destroyed as a signal to end the test. | |
| 870 context_getter->WaitForContextCreation(); | |
| 871 CancelRequest(); | |
| 872 } | |
| 873 | |
| 874 void URLFetcherCancelTest::OnURLFetchComplete( | |
| 875 const URLFetcher* source) { | |
| 876 // We should have cancelled the request before completion. | |
| 877 ADD_FAILURE(); | |
| 878 CleanupAfterFetchComplete(); | |
| 879 } | |
| 880 | |
| 881 void URLFetcherCancelTest::CancelRequest() { | |
| 882 delete fetcher_; | |
| 883 // The URLFetcher's test context will post a Quit task once it is | |
| 884 // deleted. So if this test simply hangs, it means cancellation | |
| 885 // did not work. | |
| 886 } | |
| 887 | |
| 888 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | |
| 889 const URLFetcher* source) { | |
| 890 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 891 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | |
| 892 std::string data; | |
| 893 EXPECT_TRUE(source->GetResponseAsString(&data)); | |
| 894 EXPECT_FALSE(data.empty()); | |
| 895 if (!data.empty() && data_.empty()) { | |
| 896 data_ = data; | |
| 897 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 898 io_message_loop_proxy().get(), request_context())); | |
| 899 fetcher_->Start(); | |
| 900 } else { | |
| 901 EXPECT_EQ(data, data_); | |
| 902 CleanupAfterFetchComplete(); | |
| 903 } | |
| 904 } | |
| 905 | |
| 906 void URLFetcherFileTest::CreateFetcherForFile(const GURL& url, | |
| 907 const base::FilePath& file_path) { | |
| 908 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 909 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 910 io_message_loop_proxy().get(), request_context())); | |
| 911 | |
| 912 // Use the IO message loop to do the file operations in this test. | |
| 913 fetcher_->SaveResponseToFileAtPath(file_path, io_message_loop_proxy()); | |
| 914 fetcher_->Start(); | |
| 915 } | |
| 916 | |
| 917 void URLFetcherFileTest::CreateFetcherForTempFile(const GURL& url) { | |
| 918 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 919 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 920 io_message_loop_proxy().get(), request_context())); | |
| 921 | |
| 922 // Use the IO message loop to do the file operations in this test. | |
| 923 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | |
| 924 fetcher_->Start(); | |
| 925 } | |
| 926 | |
| 927 void URLFetcherFileTest::OnURLFetchComplete(const URLFetcher* source) { | |
| 928 if (expected_file_error_ == OK) { | |
| 929 EXPECT_TRUE(source->GetStatus().is_success()); | |
| 930 EXPECT_EQ(OK, source->GetStatus().error()); | |
| 931 EXPECT_EQ(200, source->GetResponseCode()); | |
| 932 | |
| 933 EXPECT_TRUE(source->GetResponseAsFilePath( | |
| 934 take_ownership_of_file_, &file_path_)); | |
| 935 | |
| 936 EXPECT_TRUE(base::ContentsEqual(expected_file_, file_path_)); | |
| 937 } else { | |
| 938 EXPECT_FALSE(source->GetStatus().is_success()); | |
| 939 EXPECT_EQ(expected_file_error_, source->GetStatus().error()); | |
| 940 } | |
| 941 CleanupAfterFetchComplete(); | |
| 942 } | |
| 943 | |
| 944 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 | |
| 951 // thread, this will test URLFetcher's ability to do everything on one | |
| 952 // thread. | |
| 953 CreateFetcher(test_server.GetURL("defaultresponse")); | |
| 954 | |
| 955 base::MessageLoop::current()->Run(); | |
| 956 } | |
| 957 | |
| 958 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 | |
| 965 // (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 | |
| 967 // message loop will be shut down automatically as the thread goes out of | |
| 968 // scope. | |
| 969 base::Thread t("URLFetcher test thread"); | |
| 970 ASSERT_TRUE(t.Start()); | |
| 971 t.message_loop()->PostTask( | |
| 972 FROM_HERE, | |
| 973 base::Bind(&URLFetcherTest::CreateFetcher, | |
| 974 base::Unretained(this), | |
| 975 test_server.GetURL("defaultresponse"))); | |
| 976 | |
| 977 base::MessageLoop::current()->Run(); | |
| 978 } | |
| 979 | |
| 980 void CancelAllOnIO() { | |
| 981 EXPECT_EQ(1, URLFetcherTest::GetNumFetcherCores()); | |
| 982 URLFetcherImpl::CancelAll(); | |
| 983 EXPECT_EQ(0, URLFetcherTest::GetNumFetcherCores()); | |
| 984 } | |
| 985 | |
| 986 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | |
| 987 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()); | |
| 993 | |
| 994 CreateFetcher(test_server.GetURL("defaultresponse")); | |
| 995 io_message_loop_proxy()->PostTaskAndReply( | |
| 996 FROM_HERE, base::Bind(&CancelAllOnIO), base::MessageLoop::QuitClosure()); | |
| 997 base::MessageLoop::current()->Run(); | |
| 998 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 999 delete fetcher_; | |
| 1000 } | |
| 1001 | |
| 1002 TEST_F(URLFetcherMockDnsTest, DontRetryOnNetworkChangedByDefault) { | |
| 1003 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1004 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1005 | |
| 1006 // This posts a task to start the fetcher. | |
| 1007 CreateFetcher(test_url_); | |
| 1008 fetcher_->Start(); | |
| 1009 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1010 base::MessageLoop::current()->RunUntilIdle(); | |
| 1011 | |
| 1012 // The fetcher is now running, but is pending the host resolve. | |
| 1013 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1014 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1015 ASSERT_FALSE(completed_fetcher_); | |
| 1016 | |
| 1017 // A network change notification aborts the connect job. | |
| 1018 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 1019 base::MessageLoop::current()->RunUntilIdle(); | |
| 1020 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1021 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1022 ASSERT_TRUE(completed_fetcher_); | |
| 1023 | |
| 1024 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
| 1025 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
| 1026 } | |
| 1027 | |
| 1028 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndFail) { | |
| 1029 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1030 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1031 | |
| 1032 // This posts a task to start the fetcher. | |
| 1033 CreateFetcher(test_url_); | |
| 1034 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 1035 fetcher_->Start(); | |
| 1036 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1037 base::MessageLoop::current()->RunUntilIdle(); | |
| 1038 | |
| 1039 // The fetcher is now running, but is pending the host resolve. | |
| 1040 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1041 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1042 ASSERT_FALSE(completed_fetcher_); | |
| 1043 | |
| 1044 // Make it fail 3 times. | |
| 1045 for (int i = 0; i < 3; ++i) { | |
| 1046 // A network change notification aborts the connect job. | |
| 1047 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 1048 base::MessageLoop::current()->RunUntilIdle(); | |
| 1049 | |
| 1050 // But the fetcher retries automatically. | |
| 1051 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1052 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1053 ASSERT_FALSE(completed_fetcher_); | |
| 1054 } | |
| 1055 | |
| 1056 // A 4th failure doesn't trigger another retry, and propagates the error | |
| 1057 // to the owner of the fetcher. | |
| 1058 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 1059 base::MessageLoop::current()->RunUntilIdle(); | |
| 1060 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1061 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1062 ASSERT_TRUE(completed_fetcher_); | |
| 1063 | |
| 1064 // And the owner of the fetcher gets the ERR_NETWORK_CHANGED error. | |
| 1065 EXPECT_EQ(ERR_NETWORK_CHANGED, completed_fetcher_->GetStatus().error()); | |
| 1066 } | |
| 1067 | |
| 1068 TEST_F(URLFetcherMockDnsTest, RetryOnNetworkChangedAndSucceed) { | |
| 1069 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1070 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1071 | |
| 1072 // This posts a task to start the fetcher. | |
| 1073 CreateFetcher(test_url_); | |
| 1074 fetcher_->SetAutomaticallyRetryOnNetworkChanges(3); | |
| 1075 fetcher_->Start(); | |
| 1076 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1077 base::MessageLoop::current()->RunUntilIdle(); | |
| 1078 | |
| 1079 // The fetcher is now running, but is pending the host resolve. | |
| 1080 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1081 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1082 ASSERT_FALSE(completed_fetcher_); | |
| 1083 | |
| 1084 // Make it fail 3 times. | |
| 1085 for (int i = 0; i < 3; ++i) { | |
| 1086 // A network change notification aborts the connect job. | |
| 1087 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
| 1088 base::MessageLoop::current()->RunUntilIdle(); | |
| 1089 | |
| 1090 // But the fetcher retries automatically. | |
| 1091 EXPECT_EQ(1, GetNumFetcherCores()); | |
| 1092 EXPECT_TRUE(resolver_.has_pending_requests()); | |
| 1093 ASSERT_FALSE(completed_fetcher_); | |
| 1094 } | |
| 1095 | |
| 1096 // Now let it succeed by resolving the pending request. | |
| 1097 resolver_.ResolveAllPending(); | |
| 1098 base::MessageLoop::current()->Run(); | |
| 1099 | |
| 1100 // URLFetcherMockDnsTest::OnURLFetchComplete() will quit the loop. | |
| 1101 EXPECT_EQ(0, GetNumFetcherCores()); | |
| 1102 EXPECT_FALSE(resolver_.has_pending_requests()); | |
| 1103 ASSERT_TRUE(completed_fetcher_); | |
| 1104 | |
| 1105 // This time the request succeeded. | |
| 1106 EXPECT_EQ(OK, completed_fetcher_->GetStatus().error()); | |
| 1107 EXPECT_EQ(200, completed_fetcher_->GetResponseCode()); | |
| 1108 } | |
| 1109 | |
| 1110 TEST_F(URLFetcherPostTest, Basic) { | |
| 1111 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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(); | |
| 1118 } | |
| 1119 | |
| 1120 TEST_F(URLFetcherPostFileTest, Basic) { | |
| 1121 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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(); | |
| 1128 } | |
| 1129 | |
| 1130 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); | |
| 1137 | |
| 1138 CreateFetcher(test_server.GetURL("echo")); | |
| 1139 base::MessageLoop::current()->Run(); | |
| 1140 } | |
| 1141 | |
| 1142 TEST_F(URLFetcherSetUploadFactoryTest, Basic) { | |
| 1143 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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(); | |
| 1150 ASSERT_EQ(1u, create_stream_count()); | |
| 1151 } | |
| 1152 | |
| 1153 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; | |
| 1159 CreateFetcher(test_server.GetURL("echo?status=500")); | |
| 1160 base::MessageLoop::current()->Run(); | |
| 1161 ASSERT_EQ(2u, create_stream_count()); | |
| 1162 } | |
| 1163 | |
| 1164 TEST_F(URLFetcherEmptyPostTest, Basic) { | |
| 1165 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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(); | |
| 1172 } | |
| 1173 | |
| 1174 TEST_F(URLFetcherUploadProgressTest, Basic) { | |
| 1175 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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(); | |
| 1182 } | |
| 1183 | |
| 1184 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 | |
| 1191 // URLFetcher::Core's IOBuffer. | |
| 1192 static const char kFileToFetch[] = "animate1.gif"; | |
| 1193 // Hardcoded file size - it cannot be easily fetched when a remote http server | |
| 1194 // is used for testing. | |
| 1195 static const int64 kFileSize = 19021; | |
| 1196 | |
| 1197 expected_total_ = kFileSize; | |
| 1198 | |
| 1199 CreateFetcher(test_server.GetURL( | |
| 1200 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 1201 | |
| 1202 base::MessageLoop::current()->Run(); | |
| 1203 } | |
| 1204 | |
| 1205 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 | |
| 1212 // URLFetcher::Core's IOBuffer. | |
| 1213 static const char kFileToFetch[] = "animate1.gif"; | |
| 1214 CreateFetcher(test_server.GetURL( | |
| 1215 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 1216 | |
| 1217 base::MessageLoop::current()->Run(); | |
| 1218 } | |
| 1219 | |
| 1220 TEST_F(URLFetcherHeadersTest, Headers) { | |
| 1221 SpawnedTestServer test_server( | |
| 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(); | |
| 1229 // The actual tests are in the URLFetcherHeadersTest fixture. | |
| 1230 } | |
| 1231 | |
| 1232 TEST_F(URLFetcherSocketAddressTest, SocketAddress) { | |
| 1233 SpawnedTestServer test_server( | |
| 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 | |
| 1240 // Reusing "with-headers.html" but doesn't really matter. | |
| 1241 CreateFetcher(test_server.GetURL("files/with-headers.html")); | |
| 1242 base::MessageLoop::current()->Run(); | |
| 1243 // The actual tests are in the URLFetcherSocketAddressTest fixture. | |
| 1244 } | |
| 1245 | |
| 1246 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( | |
| 1253 test_server.GetURL(std::string("server-redirect?") + kRedirectTarget)); | |
| 1254 base::MessageLoop::current()->Run(); | |
| 1255 EXPECT_TRUE(callback_called_); | |
| 1256 } | |
| 1257 | |
| 1258 TEST_F(URLFetcherProtectTest, Overload) { | |
| 1259 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 1260 SpawnedTestServer::kLocalhost, | |
| 1261 base::FilePath(kDocRoot)); | |
| 1262 ASSERT_TRUE(test_server.Start()); | |
| 1263 | |
| 1264 GURL url(test_server.GetURL("defaultresponse")); | |
| 1265 | |
| 1266 // Registers an entry for test url. It only allows 3 requests to be sent | |
| 1267 // in 200 milliseconds. | |
| 1268 scoped_refptr<URLRequestThrottlerEntry> entry( | |
| 1269 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | |
| 1270 std::string(), | |
| 1271 200, | |
| 1272 3, | |
| 1273 1, | |
| 1274 2.0, | |
| 1275 0.0, | |
| 1276 256)); | |
| 1277 request_context()->throttler_manager() | |
| 1278 ->OverrideEntryForTests(url, entry.get()); | |
| 1279 | |
| 1280 CreateFetcher(url); | |
| 1281 | |
| 1282 base::MessageLoop::current()->Run(); | |
| 1283 } | |
| 1284 | |
| 1285 TEST_F(URLFetcherProtectTest, ServerUnavailable) { | |
| 1286 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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 | |
| 1293 // Registers an entry for test url. The backoff time is calculated by: | |
| 1294 // new_backoff = 2.0 * old_backoff + 0 | |
| 1295 // and maximum backoff time is 256 milliseconds. | |
| 1296 // Maximum retries allowed is set to 11. | |
| 1297 scoped_refptr<URLRequestThrottlerEntry> entry( | |
| 1298 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | |
| 1299 std::string(), | |
| 1300 200, | |
| 1301 3, | |
| 1302 1, | |
| 1303 2.0, | |
| 1304 0.0, | |
| 1305 256)); | |
| 1306 request_context()->throttler_manager() | |
| 1307 ->OverrideEntryForTests(url, entry.get()); | |
| 1308 | |
| 1309 CreateFetcher(url); | |
| 1310 | |
| 1311 base::MessageLoop::current()->Run(); | |
| 1312 } | |
| 1313 | |
| 1314 TEST_F(URLFetcherProtectTestPassedThrough, ServerUnavailablePropagateResponse) { | |
| 1315 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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 | |
| 1322 // Registers an entry for test url. The backoff time is calculated by: | |
| 1323 // new_backoff = 2.0 * old_backoff + 0 | |
| 1324 // and maximum backoff time is 150000 milliseconds. | |
| 1325 // Maximum retries allowed is set to 11. | |
| 1326 scoped_refptr<URLRequestThrottlerEntry> entry( | |
| 1327 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | |
| 1328 std::string(), | |
| 1329 200, | |
| 1330 3, | |
| 1331 100, | |
| 1332 2.0, | |
| 1333 0.0, | |
| 1334 150000)); | |
| 1335 // Total time if *not* for not doing automatic backoff would be 150s. | |
| 1336 // In reality it should be "as soon as server responds". | |
| 1337 request_context()->throttler_manager() | |
| 1338 ->OverrideEntryForTests(url, entry.get()); | |
| 1339 | |
| 1340 CreateFetcher(url); | |
| 1341 | |
| 1342 base::MessageLoop::current()->Run(); | |
| 1343 } | |
| 1344 | |
| 1345 TEST_F(URLFetcherBadHTTPSTest, BadHTTPSTest) { | |
| 1346 SpawnedTestServer::SSLOptions ssl_options( | |
| 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(); | |
| 1355 } | |
| 1356 | |
| 1357 TEST_F(URLFetcherCancelTest, ReleasesContext) { | |
| 1358 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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 | |
| 1365 // 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 | |
| 1367 // 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 | |
| 1369 // scope. | |
| 1370 base::Thread t("URLFetcher test thread"); | |
| 1371 ASSERT_TRUE(t.Start()); | |
| 1372 t.message_loop()->PostTask( | |
| 1373 FROM_HERE, | |
| 1374 base::Bind(&URLFetcherCancelTest::CreateFetcher, | |
| 1375 base::Unretained(this), url)); | |
| 1376 | |
| 1377 base::MessageLoop::current()->Run(); | |
| 1378 } | |
| 1379 | |
| 1380 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | |
| 1381 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, | |
| 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 | |
| 1388 // Register an entry for test url. | |
| 1389 // 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. | |
| 1391 scoped_refptr<URLRequestThrottlerEntry> entry( | |
| 1392 new URLRequestThrottlerEntry(request_context()->throttler_manager(), | |
| 1393 std::string(), | |
| 1394 4000, | |
| 1395 1, | |
| 1396 2000, | |
| 1397 2.0, | |
| 1398 0.0, | |
| 1399 4000)); | |
| 1400 request_context()->throttler_manager() | |
| 1401 ->OverrideEntryForTests(url, entry.get()); | |
| 1402 // Fake that a request has just started. | |
| 1403 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); | |
| 1404 | |
| 1405 // The next request we try to send will be delayed by ~4 seconds. | |
| 1406 // The slower the test runs, the less the delay will be (since it takes the | |
| 1407 // time difference from now). | |
| 1408 | |
| 1409 base::Thread t("URLFetcher test thread"); | |
| 1410 ASSERT_TRUE(t.Start()); | |
| 1411 t.message_loop()->PostTask( | |
| 1412 FROM_HERE, | |
| 1413 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); | |
| 1414 | |
| 1415 base::MessageLoop::current()->Run(); | |
| 1416 } | |
| 1417 | |
| 1418 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 | |
| 1425 // thread, this will test URLFetcher's ability to do everything on one | |
| 1426 // thread. | |
| 1427 CreateFetcher(test_server.GetURL("defaultresponse")); | |
| 1428 | |
| 1429 base::MessageLoop::current()->Run(); | |
| 1430 } | |
| 1431 | |
| 1432 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; | |
| 1439 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1440 | |
| 1441 // Get a small file. | |
| 1442 static const char kFileToFetch[] = "simple.html"; | |
| 1443 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1444 CreateFetcherForFile( | |
| 1445 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 1446 temp_dir.path().AppendASCII(kFileToFetch)); | |
| 1447 | |
| 1448 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1449 | |
| 1450 ASSERT_FALSE(base::PathExists(file_path_)) | |
| 1451 << file_path_.value() << " not removed."; | |
| 1452 } | |
| 1453 | |
| 1454 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; | |
| 1461 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1462 | |
| 1463 // Get a file large enough to require more than one read into | |
| 1464 // URLFetcher::Core's IOBuffer. | |
| 1465 static const char kFileToFetch[] = "animate1.gif"; | |
| 1466 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1467 CreateFetcherForFile( | |
| 1468 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 1469 temp_dir.path().AppendASCII(kFileToFetch)); | |
| 1470 | |
| 1471 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1472 } | |
| 1473 | |
| 1474 TEST_F(URLFetcherFileTest, SavedOutputFileOwnerhisp) { | |
| 1475 // 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. | |
| 1477 const bool kTake[] = {false, true}; | |
| 1478 for (size_t i = 0; i < arraysize(kTake); ++i) { | |
| 1479 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; | |
| 1486 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1487 | |
| 1488 // Get a small file. | |
| 1489 static const char kFileToFetch[] = "simple.html"; | |
| 1490 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1491 CreateFetcherForFile( | |
| 1492 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 1493 temp_dir.path().AppendASCII(kFileToFetch)); | |
| 1494 | |
| 1495 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1496 | |
| 1497 base::MessageLoop::current()->RunUntilIdle(); | |
| 1498 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | |
| 1499 "FilePath: " << file_path_.value(); | |
| 1500 } | |
| 1501 } | |
| 1502 | |
| 1503 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; | |
| 1510 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1511 | |
| 1512 // Create a file before trying to fetch. | |
| 1513 static const char kFileToFetch[] = "simple.html"; | |
| 1514 std::string data(10000, '?'); // Meant to be larger than simple.html. | |
| 1515 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | |
| 1516 ASSERT_EQ(static_cast<int>(data.size()), | |
| 1517 base::WriteFile(file_path_, data.data(), data.size())); | |
| 1518 ASSERT_TRUE(base::PathExists(file_path_)); | |
| 1519 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1520 ASSERT_FALSE(base::ContentsEqual(file_path_, expected_file_)); | |
| 1521 | |
| 1522 // Get a small file. | |
| 1523 CreateFetcherForFile( | |
| 1524 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 1525 file_path_); | |
| 1526 | |
| 1527 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1528 } | |
| 1529 | |
| 1530 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; | |
| 1537 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 1538 | |
| 1539 // Create a directory before trying to fetch. | |
| 1540 static const char kFileToFetch[] = "simple.html"; | |
| 1541 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | |
| 1542 ASSERT_TRUE(base::CreateDirectory(file_path_)); | |
| 1543 ASSERT_TRUE(base::PathExists(file_path_)); | |
| 1544 | |
| 1545 // Get a small file. | |
| 1546 expected_file_error_ = ERR_ACCESS_DENIED; | |
| 1547 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1548 CreateFetcherForFile( | |
| 1549 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 1550 file_path_); | |
| 1551 | |
| 1552 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1553 | |
| 1554 base::MessageLoop::current()->RunUntilIdle(); | |
| 1555 } | |
| 1556 | |
| 1557 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. | |
| 1564 static const char kFileToFetch[] = "simple.html"; | |
| 1565 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1566 CreateFetcherForTempFile( | |
| 1567 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 1568 | |
| 1569 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1570 | |
| 1571 ASSERT_FALSE(base::PathExists(file_path_)) | |
| 1572 << file_path_.value() << " not removed."; | |
| 1573 } | |
| 1574 | |
| 1575 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 | |
| 1582 // URLFetcher::Core's IOBuffer. | |
| 1583 static const char kFileToFetch[] = "animate1.gif"; | |
| 1584 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1585 CreateFetcherForTempFile(test_server.GetURL( | |
| 1586 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 1587 | |
| 1588 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1589 } | |
| 1590 | |
| 1591 TEST_F(URLFetcherFileTest, SavedOutputTempFileOwnerhisp) { | |
| 1592 // 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. | |
| 1594 const bool kTake[] = {false, true}; | |
| 1595 for (size_t i = 0; i < arraysize(kTake); ++i) { | |
| 1596 take_ownership_of_file_ = kTake[i]; | |
| 1597 | |
| 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. | |
| 1604 static const char kFileToFetch[] = "simple.html"; | |
| 1605 expected_file_ = test_server.GetDocumentRoot().AppendASCII(kFileToFetch); | |
| 1606 CreateFetcherForTempFile(test_server.GetURL( | |
| 1607 std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 1608 | |
| 1609 base::MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | |
| 1610 | |
| 1611 base::MessageLoop::current()->RunUntilIdle(); | |
| 1612 ASSERT_EQ(kTake[i], base::PathExists(file_path_)) << | |
| 1613 "FilePath: " << file_path_.value(); | |
| 1614 } | |
| 1615 } | |
| 1616 | |
| 1617 } // namespace | |
| 1618 | |
| 1619 } // namespace net | |
| OLD | NEW |