| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 28 #include "crypto/nss_util.h" | 28 #include "crypto/nss_util.h" |
| 29 #include "net/base/elements_upload_data_stream.h" | 29 #include "net/base/elements_upload_data_stream.h" |
| 30 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
| 31 #include "net/base/upload_bytes_element_reader.h" | 31 #include "net/base/upload_bytes_element_reader.h" |
| 32 #include "net/base/upload_element_reader.h" | 32 #include "net/base/upload_element_reader.h" |
| 33 #include "net/base/upload_file_element_reader.h" | 33 #include "net/base/upload_file_element_reader.h" |
| 34 #include "net/dns/mock_host_resolver.h" | 34 #include "net/dns/mock_host_resolver.h" |
| 35 #include "net/http/http_response_headers.h" | 35 #include "net/http/http_response_headers.h" |
| 36 #include "net/test/spawned_test_server/spawned_test_server.h" | 36 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 37 #include "net/url_request/url_fetcher_delegate.h" | 37 #include "net/url_request/url_fetcher_delegate.h" |
| 38 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
| 39 #include "net/url_request/url_request_test_util.h" | 39 #include "net/url_request/url_request_test_util.h" |
| 40 #include "net/url_request/url_request_throttler_manager.h" | 40 #include "net/url_request/url_request_throttler_manager.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 42 | 42 |
| 43 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | 43 #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 44 #include "net/cert_net/nss_ocsp.h" | 44 #include "net/cert_net/nss_ocsp.h" |
| 45 #endif | 45 #endif |
| 46 | 46 |
| 47 namespace net { | 47 namespace net { |
| 48 | 48 |
| 49 using base::Time; | 49 using base::Time; |
| 50 using base::TimeDelta; | 50 using base::TimeDelta; |
| 51 | 51 |
| 52 // TODO(eroman): Add a regression test for http://crbug.com/40505. | 52 // TODO(eroman): Add a regression test for http://crbug.com/40505. |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 // TODO(akalin): Move all the test data to somewhere under net/. | 56 // TODO(akalin): Move all the test data to somewhere under net/. |
| 57 const base::FilePath::CharType kDocRoot[] = | 57 const base::FilePath::CharType kDocRoot[] = |
| 58 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); | 58 FILE_PATH_LITERAL("net/data/url_fetcher_impl_unittest"); |
| 59 const char kTestServerFilePrefix[] = "files/"; | 59 const char kTestServerFilePrefix[] = "/"; |
| 60 | 60 |
| 61 // Test server path and response body for the default URL used by many of the | 61 // Test server path and response body for the default URL used by many of the |
| 62 // tests. | 62 // tests. |
| 63 const char kDefaultResponsePath[] = "defaultresponse"; | 63 const char kDefaultResponsePath[] = "/defaultresponse"; |
| 64 const char kDefaultResponseBody[] = | 64 const char kDefaultResponseBody[] = |
| 65 "Default response given for path: /defaultresponse"; | 65 "Default response given for path: /defaultresponse"; |
| 66 | 66 |
| 67 // Request body for streams created by CreateUploadStream. | 67 // Request body for streams created by CreateUploadStream. |
| 68 const char kCreateUploadStreamBody[] = "rosebud"; | 68 const char kCreateUploadStreamBody[] = "rosebud"; |
| 69 | 69 |
| 70 base::FilePath GetUploadFileTestPath() { | 70 base::FilePath GetUploadFileTestPath() { |
| 71 base::FilePath path; | 71 base::FilePath path; |
| 72 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 72 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 73 return path.Append( | 73 return path.Append( |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); | 379 EXPECT_TRUE(delegate->fetcher()->GetStatus().is_success()); |
| 380 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); | 380 EXPECT_EQ(200, delegate->fetcher()->GetResponseCode()); |
| 381 | 381 |
| 382 base::FilePath out_path; | 382 base::FilePath out_path; |
| 383 EXPECT_TRUE( | 383 EXPECT_TRUE( |
| 384 delegate->fetcher()->GetResponseAsFilePath(take_ownership, &out_path)); | 384 delegate->fetcher()->GetResponseAsFilePath(take_ownership, &out_path)); |
| 385 if (!save_to_temporary_file) { | 385 if (!save_to_temporary_file) { |
| 386 EXPECT_EQ(requested_out_path, out_path); | 386 EXPECT_EQ(requested_out_path, out_path); |
| 387 } | 387 } |
| 388 | 388 |
| 389 base::FilePath server_root; |
| 390 PathService::Get(base::DIR_SOURCE_ROOT, &server_root); |
| 391 |
| 389 EXPECT_TRUE(base::ContentsEqual( | 392 EXPECT_TRUE(base::ContentsEqual( |
| 390 test_server_->GetDocumentRoot().AppendASCII(file_to_fetch), out_path)); | 393 server_root.Append(base::FilePath(kDocRoot)).AppendASCII(file_to_fetch), |
| 394 out_path)); |
| 391 | 395 |
| 392 // Delete the delegate and run the message loop to give the fetcher's | 396 // Delete the delegate and run the message loop to give the fetcher's |
| 393 // destructor a chance to delete the file. | 397 // destructor a chance to delete the file. |
| 394 delegate.reset(); | 398 delegate.reset(); |
| 395 base::RunLoop().RunUntilIdle(); | 399 base::RunLoop().RunUntilIdle(); |
| 396 | 400 |
| 397 // File should only exist if |take_ownership| was true. | 401 // File should only exist if |take_ownership| was true. |
| 398 EXPECT_EQ(take_ownership, base::PathExists(out_path)); | 402 EXPECT_EQ(take_ownership, base::PathExists(out_path)); |
| 399 | 403 |
| 400 // Cleanup. | 404 // Cleanup. |
| 401 if (base::PathExists(out_path)) | 405 if (base::PathExists(out_path)) |
| 402 base::DeleteFile(out_path, false); | 406 base::DeleteFile(out_path, false); |
| 403 } | 407 } |
| 404 | 408 |
| 405 // Returns a URL that hangs on DNS resolution when using a context created by | 409 // Returns a URL that hangs on DNS resolution when using a context created by |
| 406 // the test fixture. | 410 // the test fixture. |
| 407 const GURL& hanging_url() const { return hanging_url_; } | 411 const GURL& hanging_url() const { return hanging_url_; } |
| 408 | 412 |
| 409 // testing::Test: | 413 // testing::Test: |
| 410 void SetUp() override { | 414 void SetUp() override { |
| 411 SetUpServer(); | 415 SetUpServer(); |
| 412 ASSERT_TRUE(test_server_->Start()); | 416 ASSERT_TRUE(test_server_->Start()); |
| 413 | 417 |
| 414 // URL that will hang when lookups reach the host resolver. | 418 // URL that will hang when lookups reach the host resolver. |
| 415 hanging_url_ = GURL(base::StringPrintf( | 419 hanging_url_ = GURL(base::StringPrintf( |
| 416 "http://example.com:%d/%s", test_server_->host_port_pair().port(), | 420 "http://example.com:%d%s", test_server_->host_port_pair().port(), |
| 417 kDefaultResponsePath)); | 421 kDefaultResponsePath)); |
| 418 ASSERT_TRUE(hanging_url_.is_valid()); | 422 ASSERT_TRUE(hanging_url_.is_valid()); |
| 419 | 423 |
| 420 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | 424 #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 421 crypto::EnsureNSSInit(); | 425 crypto::EnsureNSSInit(); |
| 422 EnsureNSSHttpIOInit(); | 426 EnsureNSSHttpIOInit(); |
| 423 #endif | 427 #endif |
| 424 } | 428 } |
| 425 | 429 |
| 426 void TearDown() override { | 430 void TearDown() override { |
| 427 #if defined(USE_NSS_CERTS) || defined(OS_IOS) | 431 #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 428 ShutdownNSSHttpIO(); | 432 ShutdownNSSHttpIO(); |
| 429 #endif | 433 #endif |
| 430 } | 434 } |
| 431 | 435 |
| 432 // Initializes |test_server_| without starting it. Allows subclasses to use | 436 // Initializes |test_server_| without starting it. Allows subclasses to use |
| 433 // their own server configuration. | 437 // their own server configuration. |
| 434 virtual void SetUpServer() { | 438 virtual void SetUpServer() { |
| 435 test_server_.reset(new SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, | 439 test_server_.reset(new EmbeddedTestServer); |
| 436 SpawnedTestServer::kLocalhost, | 440 test_server_->AddDefaultHandlers(base::FilePath(kDocRoot)); |
| 437 base::FilePath(kDocRoot))); | |
| 438 } | 441 } |
| 439 | 442 |
| 440 // Network thread for cross-thread tests. Most threads just use the main | 443 // Network thread for cross-thread tests. Most threads just use the main |
| 441 // thread for network activity. | 444 // thread for network activity. |
| 442 scoped_ptr<base::Thread> network_thread_; | 445 scoped_ptr<base::Thread> network_thread_; |
| 443 | 446 |
| 444 scoped_ptr<SpawnedTestServer> test_server_; | 447 scoped_ptr<EmbeddedTestServer> test_server_; |
| 445 GURL hanging_url_; | 448 GURL hanging_url_; |
| 446 | 449 |
| 447 size_t num_upload_streams_created_; | 450 size_t num_upload_streams_created_; |
| 448 }; | 451 }; |
| 449 | 452 |
| 450 namespace { | 453 namespace { |
| 451 | 454 |
| 452 // Version of URLFetcherTest that tests bad HTTPS requests. | 455 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 453 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 456 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 454 public: | 457 public: |
| 455 URLFetcherBadHTTPSTest() {} | 458 URLFetcherBadHTTPSTest() {} |
| 456 | 459 |
| 457 // URLFetcherTest: | 460 // URLFetcherTest: |
| 458 void SetUpServer() override { | 461 void SetUpServer() override { |
| 459 SpawnedTestServer::SSLOptions ssl_options( | 462 test_server_.reset( |
| 460 SpawnedTestServer::SSLOptions::CERT_EXPIRED); | 463 new EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS)); |
| 461 test_server_.reset(new SpawnedTestServer( | 464 test_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 462 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath(kDocRoot))); | 465 test_server_->ServeFilesFromSourceDirectory("net/data/ssl"); |
| 463 } | 466 } |
| 464 }; | 467 }; |
| 465 | 468 |
| 466 // Create the fetcher on the main thread. Since network IO will happen on the | 469 // Create the fetcher on the main thread. Since network IO will happen on the |
| 467 // main thread, this will test URLFetcher's ability to do everything on one | 470 // main thread, this will test URLFetcher's ability to do everything on one |
| 468 // thread. | 471 // thread. |
| 469 TEST_F(URLFetcherTest, SameThreadTest) { | 472 TEST_F(URLFetcherTest, SameThreadTest) { |
| 470 WaitingURLFetcherDelegate delegate; | 473 WaitingURLFetcherDelegate delegate; |
| 471 delegate.CreateFetcher(test_server_->GetURL(kDefaultResponsePath), | 474 delegate.CreateFetcher(test_server_->GetURL(kDefaultResponsePath), |
| 472 URLFetcher::GET, CreateSameThreadContextGetter()); | 475 URLFetcher::GET, CreateSameThreadContextGetter()); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 | 656 |
| 654 std::string data; | 657 std::string data; |
| 655 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 658 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 656 EXPECT_EQ(kDefaultResponseBody, data); | 659 EXPECT_EQ(kDefaultResponseBody, data); |
| 657 } | 660 } |
| 658 | 661 |
| 659 TEST_F(URLFetcherTest, PostString) { | 662 TEST_F(URLFetcherTest, PostString) { |
| 660 const char kUploadData[] = "bobsyeruncle"; | 663 const char kUploadData[] = "bobsyeruncle"; |
| 661 | 664 |
| 662 WaitingURLFetcherDelegate delegate; | 665 WaitingURLFetcherDelegate delegate; |
| 663 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 666 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 664 CreateSameThreadContextGetter()); | 667 CreateSameThreadContextGetter()); |
| 665 delegate.fetcher()->SetUploadData("application/x-www-form-urlencoded", | 668 delegate.fetcher()->SetUploadData("application/x-www-form-urlencoded", |
| 666 kUploadData); | 669 kUploadData); |
| 667 delegate.StartFetcherAndWait(); | 670 delegate.StartFetcherAndWait(); |
| 668 | 671 |
| 669 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 672 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 670 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 673 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 671 std::string data; | 674 std::string data; |
| 672 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 675 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 673 EXPECT_EQ(kUploadData, data); | 676 EXPECT_EQ(kUploadData, data); |
| 674 } | 677 } |
| 675 | 678 |
| 676 TEST_F(URLFetcherTest, PostEmptyString) { | 679 TEST_F(URLFetcherTest, PostEmptyString) { |
| 677 const char kUploadData[] = ""; | 680 const char kUploadData[] = ""; |
| 678 | 681 |
| 679 WaitingURLFetcherDelegate delegate; | 682 WaitingURLFetcherDelegate delegate; |
| 680 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 683 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 681 CreateSameThreadContextGetter()); | 684 CreateSameThreadContextGetter()); |
| 682 delegate.fetcher()->SetUploadData("application/x-www-form-urlencoded", | 685 delegate.fetcher()->SetUploadData("application/x-www-form-urlencoded", |
| 683 kUploadData); | 686 kUploadData); |
| 684 delegate.StartFetcherAndWait(); | 687 delegate.StartFetcherAndWait(); |
| 685 | 688 |
| 686 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 689 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 687 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 690 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 688 std::string data; | 691 std::string data; |
| 689 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 692 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 690 EXPECT_EQ(kUploadData, data); | 693 EXPECT_EQ(kUploadData, data); |
| 691 } | 694 } |
| 692 | 695 |
| 693 TEST_F(URLFetcherTest, PostEntireFile) { | 696 TEST_F(URLFetcherTest, PostEntireFile) { |
| 694 base::FilePath upload_path = GetUploadFileTestPath(); | 697 base::FilePath upload_path = GetUploadFileTestPath(); |
| 695 | 698 |
| 696 WaitingURLFetcherDelegate delegate; | 699 WaitingURLFetcherDelegate delegate; |
| 697 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 700 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 698 CreateSameThreadContextGetter()); | 701 CreateSameThreadContextGetter()); |
| 699 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", | 702 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", |
| 700 upload_path, 0, kuint64max, | 703 upload_path, 0, kuint64max, |
| 701 base::ThreadTaskRunnerHandle::Get()); | 704 base::ThreadTaskRunnerHandle::Get()); |
| 702 delegate.StartFetcherAndWait(); | 705 delegate.StartFetcherAndWait(); |
| 703 | 706 |
| 704 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 707 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 705 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 708 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 706 | 709 |
| 707 std::string expected; | 710 std::string expected; |
| 708 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); | 711 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); |
| 709 std::string data; | 712 std::string data; |
| 710 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 713 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 711 EXPECT_EQ(expected, data); | 714 EXPECT_EQ(expected, data); |
| 712 } | 715 } |
| 713 | 716 |
| 714 TEST_F(URLFetcherTest, PostFileRange) { | 717 TEST_F(URLFetcherTest, PostFileRange) { |
| 715 const size_t kRangeStart = 30; | 718 const size_t kRangeStart = 30; |
| 716 const size_t kRangeLength = 100; | 719 const size_t kRangeLength = 100; |
| 717 base::FilePath upload_path = GetUploadFileTestPath(); | 720 base::FilePath upload_path = GetUploadFileTestPath(); |
| 718 | 721 |
| 719 WaitingURLFetcherDelegate delegate; | 722 WaitingURLFetcherDelegate delegate; |
| 720 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 723 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 721 CreateSameThreadContextGetter()); | 724 CreateSameThreadContextGetter()); |
| 722 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", | 725 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", |
| 723 upload_path, kRangeStart, kRangeLength, | 726 upload_path, kRangeStart, kRangeLength, |
| 724 base::ThreadTaskRunnerHandle::Get()); | 727 base::ThreadTaskRunnerHandle::Get()); |
| 725 delegate.StartFetcherAndWait(); | 728 delegate.StartFetcherAndWait(); |
| 726 | 729 |
| 727 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 730 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 728 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 731 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 729 | 732 |
| 730 std::string expected; | 733 std::string expected; |
| 731 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); | 734 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); |
| 732 std::string data; | 735 std::string data; |
| 733 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 736 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 734 EXPECT_EQ(expected.substr(kRangeStart, kRangeLength), data); | 737 EXPECT_EQ(expected.substr(kRangeStart, kRangeLength), data); |
| 735 } | 738 } |
| 736 | 739 |
| 737 TEST_F(URLFetcherTest, PostWithUploadStreamFactory) { | 740 TEST_F(URLFetcherTest, PostWithUploadStreamFactory) { |
| 738 WaitingURLFetcherDelegate delegate; | 741 WaitingURLFetcherDelegate delegate; |
| 739 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 742 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 740 CreateSameThreadContextGetter()); | 743 CreateSameThreadContextGetter()); |
| 741 delegate.fetcher()->SetUploadStreamFactory( | 744 delegate.fetcher()->SetUploadStreamFactory( |
| 742 "text/plain", | 745 "text/plain", |
| 743 base::Bind(&URLFetcherTest::CreateUploadStream, base::Unretained(this))); | 746 base::Bind(&URLFetcherTest::CreateUploadStream, base::Unretained(this))); |
| 744 delegate.StartFetcherAndWait(); | 747 delegate.StartFetcherAndWait(); |
| 745 | 748 |
| 746 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 749 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 747 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 750 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 748 std::string data; | 751 std::string data; |
| 749 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 752 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 750 EXPECT_EQ(kCreateUploadStreamBody, data); | 753 EXPECT_EQ(kCreateUploadStreamBody, data); |
| 751 EXPECT_EQ(1u, num_upload_streams_created()); | 754 EXPECT_EQ(1u, num_upload_streams_created()); |
| 752 } | 755 } |
| 753 | 756 |
| 754 TEST_F(URLFetcherTest, PostWithUploadStreamFactoryAndRetries) { | 757 TEST_F(URLFetcherTest, PostWithUploadStreamFactoryAndRetries) { |
| 755 WaitingURLFetcherDelegate delegate; | 758 WaitingURLFetcherDelegate delegate; |
| 756 delegate.CreateFetcher(test_server_->GetURL("echo?status=500"), | 759 delegate.CreateFetcher(test_server_->GetURL("/echo?status=500"), |
| 757 URLFetcher::POST, CreateSameThreadContextGetter()); | 760 URLFetcher::POST, CreateSameThreadContextGetter()); |
| 758 delegate.fetcher()->SetAutomaticallyRetryOn5xx(true); | 761 delegate.fetcher()->SetAutomaticallyRetryOn5xx(true); |
| 759 delegate.fetcher()->SetMaxRetriesOn5xx(1); | 762 delegate.fetcher()->SetMaxRetriesOn5xx(1); |
| 760 delegate.fetcher()->SetUploadStreamFactory( | 763 delegate.fetcher()->SetUploadStreamFactory( |
| 761 "text/plain", | 764 "text/plain", |
| 762 base::Bind(&URLFetcherTest::CreateUploadStream, base::Unretained(this))); | 765 base::Bind(&URLFetcherTest::CreateUploadStream, base::Unretained(this))); |
| 763 delegate.StartFetcherAndWait(); | 766 delegate.StartFetcherAndWait(); |
| 764 | 767 |
| 765 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 768 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 766 EXPECT_EQ(500, delegate.fetcher()->GetResponseCode()); | 769 EXPECT_EQ(500, delegate.fetcher()->GetResponseCode()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 const std::string chunk_; | 811 const std::string chunk_; |
| 809 | 812 |
| 810 int64 num_chunks_appended_; | 813 int64 num_chunks_appended_; |
| 811 int64 last_seen_progress_; | 814 int64 last_seen_progress_; |
| 812 | 815 |
| 813 DISALLOW_COPY_AND_ASSIGN(CheckUploadProgressDelegate); | 816 DISALLOW_COPY_AND_ASSIGN(CheckUploadProgressDelegate); |
| 814 }; | 817 }; |
| 815 | 818 |
| 816 TEST_F(URLFetcherTest, UploadProgress) { | 819 TEST_F(URLFetcherTest, UploadProgress) { |
| 817 CheckUploadProgressDelegate delegate; | 820 CheckUploadProgressDelegate delegate; |
| 818 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 821 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 819 CreateSameThreadContextGetter()); | 822 CreateSameThreadContextGetter()); |
| 820 // Use a chunked upload so that the upload can be paused after uploading data. | 823 // Use a chunked upload so that the upload can be paused after uploading data. |
| 821 // Since upload progress uses a timer, the delegate may not receive any | 824 // Since upload progress uses a timer, the delegate may not receive any |
| 822 // notification otherwise. | 825 // notification otherwise. |
| 823 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); | 826 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); |
| 824 | 827 |
| 825 delegate.fetcher()->Start(); | 828 delegate.fetcher()->Start(); |
| 826 // Append the first chunk. Others will be appended automatically in response | 829 // Append the first chunk. Others will be appended automatically in response |
| 827 // to OnURLFetchUploadProgress events. | 830 // to OnURLFetchUploadProgress events. |
| 828 delegate.MaybeAppendChunk(); | 831 delegate.MaybeAppendChunk(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 | 865 |
| 863 DISALLOW_COPY_AND_ASSIGN(CheckDownloadProgressDelegate); | 866 DISALLOW_COPY_AND_ASSIGN(CheckDownloadProgressDelegate); |
| 864 }; | 867 }; |
| 865 | 868 |
| 866 TEST_F(URLFetcherTest, DownloadProgress) { | 869 TEST_F(URLFetcherTest, DownloadProgress) { |
| 867 // Get a file large enough to require more than one read into | 870 // Get a file large enough to require more than one read into |
| 868 // URLFetcher::Core's IOBuffer. | 871 // URLFetcher::Core's IOBuffer. |
| 869 const char kFileToFetch[] = "animate1.gif"; | 872 const char kFileToFetch[] = "animate1.gif"; |
| 870 | 873 |
| 871 std::string file_contents; | 874 std::string file_contents; |
| 875 |
| 876 base::FilePath server_root; |
| 877 PathService::Get(base::DIR_SOURCE_ROOT, &server_root); |
| 878 |
| 872 ASSERT_TRUE(base::ReadFileToString( | 879 ASSERT_TRUE(base::ReadFileToString( |
| 873 test_server_->GetDocumentRoot().AppendASCII(kFileToFetch), | 880 server_root.Append(base::FilePath(kDocRoot)).AppendASCII(kFileToFetch), |
| 874 &file_contents)); | 881 &file_contents)); |
| 875 | 882 |
| 876 CheckDownloadProgressDelegate delegate(file_contents.size()); | 883 CheckDownloadProgressDelegate delegate(file_contents.size()); |
| 877 delegate.CreateFetcher( | 884 delegate.CreateFetcher( |
| 878 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 885 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 879 URLFetcher::GET, CreateSameThreadContextGetter()); | 886 URLFetcher::GET, CreateSameThreadContextGetter()); |
| 880 delegate.StartFetcherAndWait(); | 887 delegate.StartFetcherAndWait(); |
| 881 | 888 |
| 882 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 889 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 883 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 890 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 898 } | 905 } |
| 899 | 906 |
| 900 private: | 907 private: |
| 901 DISALLOW_COPY_AND_ASSIGN(CancelOnUploadProgressDelegate); | 908 DISALLOW_COPY_AND_ASSIGN(CancelOnUploadProgressDelegate); |
| 902 }; | 909 }; |
| 903 | 910 |
| 904 // Check that a fetch can be safely cancelled/deleted during an upload progress | 911 // Check that a fetch can be safely cancelled/deleted during an upload progress |
| 905 // callback. | 912 // callback. |
| 906 TEST_F(URLFetcherTest, CancelInUploadProgressCallback) { | 913 TEST_F(URLFetcherTest, CancelInUploadProgressCallback) { |
| 907 CancelOnUploadProgressDelegate delegate; | 914 CancelOnUploadProgressDelegate delegate; |
| 908 delegate.CreateFetcher(test_server_->GetURL("echo"), URLFetcher::POST, | 915 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
| 909 CreateSameThreadContextGetter()); | 916 CreateSameThreadContextGetter()); |
| 910 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); | 917 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); |
| 911 delegate.fetcher()->Start(); | 918 delegate.fetcher()->Start(); |
| 912 // Use a chunked upload so that the upload can be paused after uploading data. | 919 // Use a chunked upload so that the upload can be paused after uploading data. |
| 913 // Since uploads progress uses a timer, may not receive any notification, | 920 // Since uploads progress uses a timer, may not receive any notification, |
| 914 // otherwise. | 921 // otherwise. |
| 915 std::string upload_data(1 << 16, 'a'); | 922 std::string upload_data(1 << 16, 'a'); |
| 916 delegate.fetcher()->AppendChunkToUpload(upload_data, false); | 923 delegate.fetcher()->AppendChunkToUpload(upload_data, false); |
| 917 delegate.WaitForComplete(); | 924 delegate.WaitForComplete(); |
| 918 | 925 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 953 // Make sure there are no pending events that cause problems when run. | 960 // Make sure there are no pending events that cause problems when run. |
| 954 base::RunLoop().RunUntilIdle(); | 961 base::RunLoop().RunUntilIdle(); |
| 955 | 962 |
| 956 EXPECT_FALSE(delegate.did_complete()); | 963 EXPECT_FALSE(delegate.did_complete()); |
| 957 EXPECT_FALSE(delegate.fetcher()); | 964 EXPECT_FALSE(delegate.fetcher()); |
| 958 } | 965 } |
| 959 | 966 |
| 960 TEST_F(URLFetcherTest, Headers) { | 967 TEST_F(URLFetcherTest, Headers) { |
| 961 WaitingURLFetcherDelegate delegate; | 968 WaitingURLFetcherDelegate delegate; |
| 962 delegate.CreateFetcher( | 969 delegate.CreateFetcher( |
| 963 test_server_->GetURL("set-header?cache-control: private"), | 970 test_server_->GetURL("/set-header?cache-control: private"), |
| 964 URLFetcher::GET, CreateSameThreadContextGetter()); | 971 URLFetcher::GET, CreateSameThreadContextGetter()); |
| 965 delegate.StartFetcherAndWait(); | 972 delegate.StartFetcherAndWait(); |
| 966 | 973 |
| 967 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 974 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 968 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 975 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 969 std::string header; | 976 std::string header; |
| 970 ASSERT_TRUE(delegate.fetcher()->GetResponseHeaders()->GetNormalizedHeader( | 977 ASSERT_TRUE(delegate.fetcher()->GetResponseHeaders()->GetNormalizedHeader( |
| 971 "cache-control", &header)); | 978 "cache-control", &header)); |
| 972 EXPECT_EQ("private", header); | 979 EXPECT_EQ("private", header); |
| 973 } | 980 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 984 delegate.fetcher()->GetSocketAddress().port()); | 991 delegate.fetcher()->GetSocketAddress().port()); |
| 985 EXPECT_EQ(test_server_->host_port_pair().host(), | 992 EXPECT_EQ(test_server_->host_port_pair().host(), |
| 986 delegate.fetcher()->GetSocketAddress().host()); | 993 delegate.fetcher()->GetSocketAddress().host()); |
| 987 } | 994 } |
| 988 | 995 |
| 989 TEST_F(URLFetcherTest, StopOnRedirect) { | 996 TEST_F(URLFetcherTest, StopOnRedirect) { |
| 990 const char kRedirectTarget[] = "http://redirect.target.com"; | 997 const char kRedirectTarget[] = "http://redirect.target.com"; |
| 991 | 998 |
| 992 WaitingURLFetcherDelegate delegate; | 999 WaitingURLFetcherDelegate delegate; |
| 993 delegate.CreateFetcher( | 1000 delegate.CreateFetcher( |
| 994 test_server_->GetURL(std::string("server-redirect?") + kRedirectTarget), | 1001 test_server_->GetURL(std::string("/server-redirect?") + kRedirectTarget), |
| 995 URLFetcher::GET, CreateSameThreadContextGetter()); | 1002 URLFetcher::GET, CreateSameThreadContextGetter()); |
| 996 delegate.fetcher()->SetStopOnRedirect(true); | 1003 delegate.fetcher()->SetStopOnRedirect(true); |
| 997 delegate.StartFetcherAndWait(); | 1004 delegate.StartFetcherAndWait(); |
| 998 | 1005 |
| 999 EXPECT_EQ(GURL(kRedirectTarget), delegate.fetcher()->GetURL()); | 1006 EXPECT_EQ(GURL(kRedirectTarget), delegate.fetcher()->GetURL()); |
| 1000 EXPECT_EQ(URLRequestStatus::CANCELED, | 1007 EXPECT_EQ(URLRequestStatus::CANCELED, |
| 1001 delegate.fetcher()->GetStatus().status()); | 1008 delegate.fetcher()->GetStatus().status()); |
| 1002 EXPECT_EQ(ERR_ABORTED, delegate.fetcher()->GetStatus().error()); | 1009 EXPECT_EQ(ERR_ABORTED, delegate.fetcher()->GetStatus().error()); |
| 1003 EXPECT_EQ(301, delegate.fetcher()->GetResponseCode()); | 1010 EXPECT_EQ(301, delegate.fetcher()->GetResponseCode()); |
| 1004 } | 1011 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1028 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 1035 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 1029 } | 1036 } |
| 1030 | 1037 |
| 1031 // 20 requests were sent. Due to throttling, they should have collectively | 1038 // 20 requests were sent. Due to throttling, they should have collectively |
| 1032 // taken over 1 second. | 1039 // taken over 1 second. |
| 1033 EXPECT_GE(Time::Now() - start_time, base::TimeDelta::FromSeconds(1)); | 1040 EXPECT_GE(Time::Now() - start_time, base::TimeDelta::FromSeconds(1)); |
| 1034 } | 1041 } |
| 1035 | 1042 |
| 1036 TEST_F(URLFetcherTest, ThrottleOn5xxRetries) { | 1043 TEST_F(URLFetcherTest, ThrottleOn5xxRetries) { |
| 1037 base::Time start_time = Time::Now(); | 1044 base::Time start_time = Time::Now(); |
| 1038 GURL url(test_server_->GetURL("files/server-unavailable.html")); | 1045 GURL url(test_server_->GetURL("/server-unavailable.html")); |
| 1039 | 1046 |
| 1040 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 1047 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
| 1041 CreateSameThreadContextGetter()); | 1048 CreateSameThreadContextGetter()); |
| 1042 | 1049 |
| 1043 // Registers an entry for test url. The backoff time is calculated by: | 1050 // Registers an entry for test url. The backoff time is calculated by: |
| 1044 // new_backoff = 2.0 * old_backoff + 0 | 1051 // new_backoff = 2.0 * old_backoff + 0 |
| 1045 // and maximum backoff time is 256 milliseconds. | 1052 // and maximum backoff time is 256 milliseconds. |
| 1046 // Maximum retries allowed is set to 11. | 1053 // Maximum retries allowed is set to 11. |
| 1047 context_getter->AddThrottlerEntry( | 1054 context_getter->AddThrottlerEntry( |
| 1048 url, std::string() /* url_id */, 200 /* sliding_window_period_ms */, | 1055 url, std::string() /* url_id */, 200 /* sliding_window_period_ms */, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1065 | 1072 |
| 1066 // The request should have been retried 11 times (12 times including the first | 1073 // The request should have been retried 11 times (12 times including the first |
| 1067 // attempt). Due to throttling, they should have collectively taken over 1 | 1074 // attempt). Due to throttling, they should have collectively taken over 1 |
| 1068 // second. | 1075 // second. |
| 1069 EXPECT_GE(Time::Now() - start_time, base::TimeDelta::FromSeconds(1)); | 1076 EXPECT_GE(Time::Now() - start_time, base::TimeDelta::FromSeconds(1)); |
| 1070 } | 1077 } |
| 1071 | 1078 |
| 1072 // Tests overload protection, when responses passed through. | 1079 // Tests overload protection, when responses passed through. |
| 1073 TEST_F(URLFetcherTest, ProtectTestPassedThrough) { | 1080 TEST_F(URLFetcherTest, ProtectTestPassedThrough) { |
| 1074 base::Time start_time = Time::Now(); | 1081 base::Time start_time = Time::Now(); |
| 1075 GURL url(test_server_->GetURL("files/server-unavailable.html")); | 1082 GURL url(test_server_->GetURL("/server-unavailable.html")); |
| 1076 | 1083 |
| 1077 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( | 1084 scoped_refptr<FetcherTestURLRequestContextGetter> context_getter( |
| 1078 CreateSameThreadContextGetter()); | 1085 CreateSameThreadContextGetter()); |
| 1079 | 1086 |
| 1080 // Registers an entry for test url. The backoff time is calculated by: | 1087 // Registers an entry for test url. The backoff time is calculated by: |
| 1081 // new_backoff = 2.0 * old_backoff + 0 | 1088 // new_backoff = 2.0 * old_backoff + 0 |
| 1082 // and maximum backoff time is 150000 milliseconds. | 1089 // and maximum backoff time is 150000 milliseconds. |
| 1083 // Maximum retries allowed is set to 11. | 1090 // Maximum retries allowed is set to 11. |
| 1084 // Total time if *not* for not doing automatic backoff would be 150s. | 1091 // Total time if *not* for not doing automatic backoff would be 150s. |
| 1085 // In reality it should be "as soon as server responds". | 1092 // In reality it should be "as soon as server responds". |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1234 DISALLOW_COPY_AND_ASSIGN(ReuseFetcherDelegate); | 1241 DISALLOW_COPY_AND_ASSIGN(ReuseFetcherDelegate); |
| 1235 }; | 1242 }; |
| 1236 | 1243 |
| 1237 TEST_F(URLFetcherTest, ReuseFetcherForSameURL) { | 1244 TEST_F(URLFetcherTest, ReuseFetcherForSameURL) { |
| 1238 // TODO(mmenke): It's really weird that this is supported, particularly | 1245 // TODO(mmenke): It's really weird that this is supported, particularly |
| 1239 // some fields can be modified between requests, but some (Like upload body) | 1246 // some fields can be modified between requests, but some (Like upload body) |
| 1240 // cannot be. Can we get rid of support for this? | 1247 // cannot be. Can we get rid of support for this? |
| 1241 scoped_refptr<URLRequestContextGetter> context_getter( | 1248 scoped_refptr<URLRequestContextGetter> context_getter( |
| 1242 CreateSameThreadContextGetter()); | 1249 CreateSameThreadContextGetter()); |
| 1243 ReuseFetcherDelegate delegate(context_getter); | 1250 ReuseFetcherDelegate delegate(context_getter); |
| 1244 delegate.CreateFetcher(test_server_->GetURL("echoheader?test"), | 1251 delegate.CreateFetcher(test_server_->GetURL("/echoheader?test"), |
| 1245 URLFetcher::GET, context_getter); | 1252 URLFetcher::GET, context_getter); |
| 1246 delegate.fetcher()->SetExtraRequestHeaders("test: request1"); | 1253 delegate.fetcher()->SetExtraRequestHeaders("test: request1"); |
| 1247 delegate.StartFetcherAndWait(); | 1254 delegate.StartFetcherAndWait(); |
| 1248 | 1255 |
| 1249 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 1256 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 1250 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 1257 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
| 1251 std::string data; | 1258 std::string data; |
| 1252 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1259 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 1253 EXPECT_EQ("request2", data); | 1260 EXPECT_EQ("request2", data); |
| 1254 } | 1261 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1417 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1424 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
| 1418 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); | 1425 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); |
| 1419 std::string data; | 1426 std::string data; |
| 1420 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1427 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 1421 EXPECT_TRUE(data.empty()); | 1428 EXPECT_TRUE(data.empty()); |
| 1422 } | 1429 } |
| 1423 | 1430 |
| 1424 } // namespace | 1431 } // namespace |
| 1425 | 1432 |
| 1426 } // namespace net | 1433 } // namespace net |
| OLD | NEW |