| 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" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "content/public/common/content_url_request_user_data.h" |
| 12 #include "content/public/common/url_fetcher_delegate.h" | 13 #include "content/public/common/url_fetcher_delegate.h" |
| 13 #include "crypto/nss_util.h" | 14 #include "crypto/nss_util.h" |
| 14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 15 #include "net/test/test_server.h" | 16 #include "net/test/test_server.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 18 #include "net/url_request/url_request_throttler_manager.h" | 19 #include "net/url_request/url_request_throttler_manager.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 #if defined(USE_NSS) | 22 #if defined(USE_NSS) |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 MessageLoopForIO io_loop_; | 103 MessageLoopForIO io_loop_; |
| 103 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 104 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 104 | 105 |
| 105 URLFetcherImpl* fetcher_; | 106 URLFetcherImpl* fetcher_; |
| 106 }; | 107 }; |
| 107 | 108 |
| 108 void URLFetcherTest::CreateFetcher(const GURL& url) { | 109 void URLFetcherTest::CreateFetcher(const GURL& url) { |
| 109 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 110 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 110 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 111 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 111 io_message_loop_proxy())); | 112 io_message_loop_proxy())); |
| 113 fetcher_->SetContentURLRequestUserData( |
| 114 new content::ContentURLRequestUserData()); |
| 112 fetcher_->Start(); | 115 fetcher_->Start(); |
| 113 } | 116 } |
| 114 | 117 |
| 115 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { | 118 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| 116 EXPECT_TRUE(source->GetStatus().is_success()); | 119 EXPECT_TRUE(source->GetStatus().is_success()); |
| 117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 120 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| 118 | 121 |
| 119 std::string data; | 122 std::string data; |
| 120 EXPECT_TRUE(source->GetResponseAsString(&data)); | 123 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 121 EXPECT_FALSE(data.empty()); | 124 EXPECT_FALSE(data.empty()); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 // Set by the test. Used in OnURLFetchComplete() to decide if | 295 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 293 // the URLFetcher should own the temp file, so that we can test | 296 // the URLFetcher should own the temp file, so that we can test |
| 294 // disowning prevents the file from being deleted. | 297 // disowning prevents the file from being deleted. |
| 295 bool take_ownership_of_temp_file_; | 298 bool take_ownership_of_temp_file_; |
| 296 }; | 299 }; |
| 297 | 300 |
| 298 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | 301 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { |
| 299 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 302 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 300 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 303 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 301 io_message_loop_proxy())); | 304 io_message_loop_proxy())); |
| 305 fetcher_->SetContentURLRequestUserData( |
| 306 new content::ContentURLRequestUserData()); |
| 302 | 307 |
| 303 // Use the IO message loop to do the file operations in this test. | 308 // Use the IO message loop to do the file operations in this test. |
| 304 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | 309 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
| 305 fetcher_->Start(); | 310 fetcher_->Start(); |
| 306 } | 311 } |
| 307 | 312 |
| 308 TEST_F(URLFetcherTempFileTest, SmallGet) { | 313 TEST_F(URLFetcherTempFileTest, SmallGet) { |
| 309 net::TestServer test_server(net::TestServer::TYPE_HTTP, | 314 net::TestServer test_server(net::TestServer::TYPE_HTTP, |
| 310 net::TestServer::kLocalhost, | 315 net::TestServer::kLocalhost, |
| 311 FilePath(kDocRoot)); | 316 FilePath(kDocRoot)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 360 |
| 356 MessageLoop::current()->RunAllPending(); | 361 MessageLoop::current()->RunAllPending(); |
| 357 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 362 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
| 358 << temp_file_.value() << " not removed."; | 363 << temp_file_.value() << " not removed."; |
| 359 } | 364 } |
| 360 | 365 |
| 361 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 366 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 362 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); | 367 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
| 363 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 368 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 364 io_message_loop_proxy())); | 369 io_message_loop_proxy())); |
| 370 fetcher_->SetContentURLRequestUserData( |
| 371 new content::ContentURLRequestUserData()); |
| 365 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 372 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 366 "bobsyeruncle"); | 373 "bobsyeruncle"); |
| 367 fetcher_->Start(); | 374 fetcher_->Start(); |
| 368 } | 375 } |
| 369 | 376 |
| 370 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { | 377 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| 371 std::string data; | 378 std::string data; |
| 372 EXPECT_TRUE(source->GetResponseAsString(&data)); | 379 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 373 EXPECT_EQ(std::string("bobsyeruncle"), data); | 380 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 374 URLFetcherTest::OnURLFetchComplete(source); | 381 URLFetcherTest::OnURLFetchComplete(source); |
| 375 } | 382 } |
| 376 | 383 |
| 377 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | 384 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
| 378 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 385 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 379 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 386 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 380 io_message_loop_proxy())); | 387 io_message_loop_proxy())); |
| 388 fetcher_->SetContentURLRequestUserData( |
| 389 new content::ContentURLRequestUserData()); |
| 381 previous_progress_ = 0; | 390 previous_progress_ = 0; |
| 382 fetcher_->Start(); | 391 fetcher_->Start(); |
| 383 } | 392 } |
| 384 | 393 |
| 385 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | 394 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
| 386 const content::URLFetcher* source, int64 current, int64 total) { | 395 const content::URLFetcher* source, int64 current, int64 total) { |
| 387 // Increasing between 0 and total. | 396 // Increasing between 0 and total. |
| 388 EXPECT_LE(0, current); | 397 EXPECT_LE(0, current); |
| 389 EXPECT_GE(total, current); | 398 EXPECT_GE(total, current); |
| 390 EXPECT_LE(previous_progress_, current); | 399 EXPECT_LE(previous_progress_, current); |
| 391 previous_progress_ = current; | 400 previous_progress_ = current; |
| 392 EXPECT_EQ(expected_total_, total); | 401 EXPECT_EQ(expected_total_, total); |
| 393 } | 402 } |
| 394 | 403 |
| 395 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | 404 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
| 396 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 405 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 397 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 406 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 398 io_message_loop_proxy())); | 407 io_message_loop_proxy())); |
| 408 fetcher_->SetContentURLRequestUserData( |
| 409 new content::ContentURLRequestUserData()); |
| 399 cancelled_ = false; | 410 cancelled_ = false; |
| 400 fetcher_->Start(); | 411 fetcher_->Start(); |
| 401 } | 412 } |
| 402 | 413 |
| 403 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( | 414 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( |
| 404 const content::URLFetcher* source, int64 current, int64 total) { | 415 const content::URLFetcher* source, int64 current, int64 total) { |
| 405 EXPECT_FALSE(cancelled_); | 416 EXPECT_FALSE(cancelled_); |
| 406 if (!cancelled_) { | 417 if (!cancelled_) { |
| 407 delete fetcher_; | 418 delete fetcher_; |
| 408 cancelled_ = true; | 419 cancelled_ = true; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 431 const content::URLFetcher* source) { | 442 const content::URLFetcher* source) { |
| 432 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); | 443 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); |
| 433 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); | 444 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); |
| 434 URLFetcherTest::OnURLFetchComplete(source); | 445 URLFetcherTest::OnURLFetchComplete(source); |
| 435 } | 446 } |
| 436 | 447 |
| 437 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 448 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
| 438 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 449 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 439 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 450 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 440 io_message_loop_proxy())); | 451 io_message_loop_proxy())); |
| 452 fetcher_->SetContentURLRequestUserData( |
| 453 new content::ContentURLRequestUserData()); |
| 441 start_time_ = Time::Now(); | 454 start_time_ = Time::Now(); |
| 442 fetcher_->SetMaxRetries(11); | 455 fetcher_->SetMaxRetries(11); |
| 443 fetcher_->Start(); | 456 fetcher_->Start(); |
| 444 } | 457 } |
| 445 | 458 |
| 446 void URLFetcherProtectTest::OnURLFetchComplete( | 459 void URLFetcherProtectTest::OnURLFetchComplete( |
| 447 const content::URLFetcher* source) { | 460 const content::URLFetcher* source) { |
| 448 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 461 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
| 449 if (source->GetResponseCode() >= 500) { | 462 if (source->GetResponseCode() >= 500) { |
| 450 // Now running ServerUnavailable test. | 463 // Now running ServerUnavailable test. |
| 451 // It takes more than 1 second to finish all 11 requests. | 464 // It takes more than 1 second to finish all 11 requests. |
| 452 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 465 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
| 453 EXPECT_TRUE(source->GetStatus().is_success()); | 466 EXPECT_TRUE(source->GetStatus().is_success()); |
| 454 std::string data; | 467 std::string data; |
| 455 EXPECT_TRUE(source->GetResponseAsString(&data)); | 468 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 456 EXPECT_FALSE(data.empty()); | 469 EXPECT_FALSE(data.empty()); |
| 457 delete fetcher_; | 470 delete fetcher_; |
| 458 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 471 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 459 } else { | 472 } else { |
| 460 // Now running Overload test. | 473 // Now running Overload test. |
| 461 static int count = 0; | 474 static int count = 0; |
| 462 count++; | 475 count++; |
| 463 if (count < 20) { | 476 if (count < 20) { |
| 464 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( | 477 fetcher_->StartWithRequestContextGetterAndUserData( |
| 465 io_message_loop_proxy())); | 478 new TestURLRequestContextGetter(io_message_loop_proxy()), |
| 479 new content::ContentURLRequestUserData()); |
| 466 } else { | 480 } else { |
| 467 // We have already sent 20 requests continuously. And we expect that | 481 // We have already sent 20 requests continuously. And we expect that |
| 468 // it takes more than 1 second due to the overload protection settings. | 482 // it takes more than 1 second due to the overload protection settings. |
| 469 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 483 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
| 470 URLFetcherTest::OnURLFetchComplete(source); | 484 URLFetcherTest::OnURLFetchComplete(source); |
| 471 } | 485 } |
| 472 } | 486 } |
| 473 } | 487 } |
| 474 | 488 |
| 475 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 489 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
| 476 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 490 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 477 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 491 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 478 io_message_loop_proxy())); | 492 io_message_loop_proxy())); |
| 493 fetcher_->SetContentURLRequestUserData( |
| 494 new content::ContentURLRequestUserData()); |
| 479 fetcher_->SetAutomaticallyRetryOn5xx(false); | 495 fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 480 start_time_ = Time::Now(); | 496 start_time_ = Time::Now(); |
| 481 fetcher_->SetMaxRetries(11); | 497 fetcher_->SetMaxRetries(11); |
| 482 fetcher_->Start(); | 498 fetcher_->Start(); |
| 483 } | 499 } |
| 484 | 500 |
| 485 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 501 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
| 486 const content::URLFetcher* source) { | 502 const content::URLFetcher* source) { |
| 487 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | 503 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); |
| 488 if (source->GetResponseCode() >= 500) { | 504 if (source->GetResponseCode() >= 500) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 549 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
| 534 delete fetcher_; | 550 delete fetcher_; |
| 535 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 551 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 536 } | 552 } |
| 537 | 553 |
| 538 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 554 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
| 539 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 555 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
| 540 CancelTestURLRequestContextGetter* context_getter = | 556 CancelTestURLRequestContextGetter* context_getter = |
| 541 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); | 557 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); |
| 542 fetcher_->SetRequestContext(context_getter); | 558 fetcher_->SetRequestContext(context_getter); |
| 559 fetcher_->SetContentURLRequestUserData( |
| 560 new content::ContentURLRequestUserData()); |
| 543 fetcher_->SetMaxRetries(2); | 561 fetcher_->SetMaxRetries(2); |
| 544 fetcher_->Start(); | 562 fetcher_->Start(); |
| 545 // We need to wait for the creation of the net::URLRequestContext, since we | 563 // We need to wait for the creation of the net::URLRequestContext, since we |
| 546 // rely on it being destroyed as a signal to end the test. | 564 // rely on it being destroyed as a signal to end the test. |
| 547 context_getter->WaitForContextCreation(); | 565 context_getter->WaitForContextCreation(); |
| 548 CancelRequest(); | 566 CancelRequest(); |
| 549 } | 567 } |
| 550 | 568 |
| 551 void URLFetcherCancelTest::OnURLFetchComplete( | 569 void URLFetcherCancelTest::OnURLFetchComplete( |
| 552 const content::URLFetcher* source) { | 570 const content::URLFetcher* source) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 565 | 583 |
| 566 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | 584 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( |
| 567 const content::URLFetcher* source) { | 585 const content::URLFetcher* source) { |
| 568 EXPECT_TRUE(source->GetStatus().is_success()); | 586 EXPECT_TRUE(source->GetStatus().is_success()); |
| 569 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK | 587 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| 570 std::string data; | 588 std::string data; |
| 571 EXPECT_TRUE(source->GetResponseAsString(&data)); | 589 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 572 EXPECT_FALSE(data.empty()); | 590 EXPECT_FALSE(data.empty()); |
| 573 if (!data.empty() && data_.empty()) { | 591 if (!data.empty() && data_.empty()) { |
| 574 data_ = data; | 592 data_ = data; |
| 575 fetcher_->StartWithRequestContextGetter( | 593 fetcher_->StartWithRequestContextGetterAndUserData( |
| 576 new TestURLRequestContextGetter(io_message_loop_proxy())); | 594 new TestURLRequestContextGetter(io_message_loop_proxy()), |
| 595 new content::ContentURLRequestUserData()); |
| 577 } else { | 596 } else { |
| 578 EXPECT_EQ(data, data_); | 597 EXPECT_EQ(data, data_); |
| 579 delete fetcher_; // Have to delete this here and not in the destructor, | 598 delete fetcher_; // Have to delete this here and not in the destructor, |
| 580 // because the destructor won't necessarily run on the | 599 // because the destructor won't necessarily run on the |
| 581 // same thread that CreateFetcher() did. | 600 // same thread that CreateFetcher() did. |
| 582 | 601 |
| 583 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 602 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 584 // If the current message loop is not the IO loop, it will be shut down when | 603 // If the current message loop is not the IO loop, it will be shut down when |
| 585 // the main loop returns and this thread subsequently goes out of scope. | 604 // the main loop returns and this thread subsequently goes out of scope. |
| 586 } | 605 } |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 io_message_loop_proxy()->PostTaskAndReply( | 932 io_message_loop_proxy()->PostTaskAndReply( |
| 914 FROM_HERE, | 933 FROM_HERE, |
| 915 base::Bind(&CancelAllOnIO), | 934 base::Bind(&CancelAllOnIO), |
| 916 MessageLoop::QuitClosure()); | 935 MessageLoop::QuitClosure()); |
| 917 MessageLoop::current()->Run(); | 936 MessageLoop::current()->Run(); |
| 918 EXPECT_EQ(0, GetNumFetcherCores()); | 937 EXPECT_EQ(0, GetNumFetcherCores()); |
| 919 delete fetcher_; | 938 delete fetcher_; |
| 920 } | 939 } |
| 921 | 940 |
| 922 } // namespace. | 941 } // namespace. |
| OLD | NEW |