Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 void CreateFetcherWithContextGetter( | 85 void CreateFetcherWithContextGetter( |
| 86 const GURL& url, | 86 const GURL& url, |
| 87 URLFetcher::RequestType request_type, | 87 URLFetcher::RequestType request_type, |
| 88 net::URLRequestContextGetter* context_getter) { | 88 net::URLRequestContextGetter* context_getter) { |
| 89 fetcher_.reset(new URLFetcherImpl(url, request_type, this)); | 89 fetcher_.reset(new URLFetcherImpl(url, request_type, this)); |
| 90 fetcher_->SetRequestContext(context_getter); | 90 fetcher_->SetRequestContext(context_getter); |
| 91 } | 91 } |
| 92 | 92 |
| 93 URLFetcher* fetcher() const { return fetcher_.get(); } | 93 URLFetcher* fetcher() const { return fetcher_.get(); } |
| 94 | 94 |
| 95 // Wait until the request has completed or been canceled. | |
| 95 void StartFetcherAndWait() { | 96 void StartFetcherAndWait() { |
| 96 fetcher_->Start(); | 97 fetcher_->Start(); |
| 97 WaitForComplete(); | 98 WaitForComplete(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 // Wait until the request has completed, if it's already been started. | 101 // Wait until the request has completed or been canceled. Does not start the |
| 102 // request. | |
| 101 void WaitForComplete() { run_loop_.Run(); } | 103 void WaitForComplete() { run_loop_.Run(); } |
| 102 | 104 |
| 105 // Cancels the fetch by deleting the fetcher. | |
| 106 void CancelFetch() { | |
| 107 EXPECT_FALSE(did_complete_); | |
|
davidben
2015/04/16 20:55:35
Optional: I think it might be marginally better to
mmenke
2015/04/16 21:26:07
The tests are already checking that, actually, so
| |
| 108 EXPECT_TRUE(fetcher_); | |
| 109 fetcher_.reset(); | |
| 110 run_loop_.Quit(); | |
| 111 } | |
| 112 | |
| 113 // URLFetcherDelegate: | |
| 103 void OnURLFetchComplete(const URLFetcher* source) override { | 114 void OnURLFetchComplete(const URLFetcher* source) override { |
| 115 EXPECT_FALSE(did_complete_); | |
| 116 EXPECT_TRUE(fetcher_); | |
| 104 EXPECT_EQ(fetcher_.get(), source); | 117 EXPECT_EQ(fetcher_.get(), source); |
| 105 did_complete_ = true; | 118 did_complete_ = true; |
| 106 run_loop_.Quit(); | 119 run_loop_.Quit(); |
| 107 } | 120 } |
| 108 | 121 |
| 122 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 123 int64 current, | |
| 124 int64 total) override { | |
| 125 // Can't check that current progress is greater than previous progress, | |
| 126 // because some tests retry on 5xx responses. | |
|
davidben
2015/04/16 20:55:35
Nit: I'd maybe say
Note that the current progre
mmenke
2015/04/16 21:26:07
Done.
| |
| 127 EXPECT_FALSE(did_complete_); | |
| 128 EXPECT_TRUE(fetcher_); | |
| 129 EXPECT_EQ(source, fetcher_.get()); | |
| 130 | |
| 131 EXPECT_LE(0, current); | |
| 132 // If file size is not known, |total| is -1. | |
| 133 if (total >= 0) { | |
| 134 EXPECT_LE(current, total); | |
| 135 } | |
|
davidben
2015/04/16 20:55:35
Nit: much as I prefer putting in curlies, I think
mmenke
2015/04/16 21:26:07
Done.
I've had people suggest using them for macr
| |
| 136 } | |
| 137 | |
| 138 void OnURLFetchUploadProgress(const URLFetcher* source, | |
| 139 int64 current, | |
| 140 int64 total) override { | |
| 141 // Can't check that current progress is greater than previous progress, | |
| 142 // because some tests retry uploads on 5xx responses. | |
|
davidben
2015/04/16 20:55:35
Ditto.
mmenke
2015/04/16 21:26:07
Done.
| |
| 143 EXPECT_FALSE(did_complete_); | |
| 144 EXPECT_TRUE(fetcher_); | |
| 145 EXPECT_EQ(source, fetcher_.get()); | |
| 146 | |
| 147 EXPECT_LE(0, current); | |
| 148 // If file size is not known, |total| is -1. | |
| 149 if (total >= 0) { | |
| 150 EXPECT_LE(current, total); | |
| 151 } | |
| 152 } | |
| 153 | |
| 109 bool did_complete() const { return did_complete_; } | 154 bool did_complete() const { return did_complete_; } |
| 110 | 155 |
| 111 private: | 156 private: |
| 112 bool did_complete_; | 157 bool did_complete_; |
| 113 | 158 |
| 114 scoped_ptr<URLFetcherImpl> fetcher_; | 159 scoped_ptr<URLFetcherImpl> fetcher_; |
| 115 base::RunLoop run_loop_; | 160 base::RunLoop run_loop_; |
| 116 | 161 |
| 117 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); | 162 DISALLOW_COPY_AND_ASSIGN(WaitingURLFetcherDelegate); |
| 118 }; | 163 }; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 // same thread that CreateFetcher() did. | 387 // same thread that CreateFetcher() did. |
| 343 | 388 |
| 344 io_message_loop_proxy()->PostTask(FROM_HERE, | 389 io_message_loop_proxy()->PostTask(FROM_HERE, |
| 345 base::MessageLoop::QuitClosure()); | 390 base::MessageLoop::QuitClosure()); |
| 346 // If the current message loop is not the IO loop, it will be shut down when | 391 // If the current message loop is not the IO loop, it will be shut down when |
| 347 // the main loop returns and this thread subsequently goes out of scope. | 392 // the main loop returns and this thread subsequently goes out of scope. |
| 348 } | 393 } |
| 349 | 394 |
| 350 namespace { | 395 namespace { |
| 351 | 396 |
| 352 // Version of URLFetcherTest that tests download progress reports. | |
| 353 class URLFetcherDownloadProgressTest : public URLFetcherTest { | |
| 354 public: | |
| 355 URLFetcherDownloadProgressTest() | |
| 356 : previous_progress_(0), | |
| 357 expected_total_(0) { | |
| 358 } | |
| 359 | |
| 360 // URLFetcherTest: | |
| 361 void CreateFetcher(const GURL& url) override; | |
| 362 | |
| 363 // URLFetcherDelegate: | |
| 364 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 365 int64 current, | |
| 366 int64 total) override; | |
| 367 | |
| 368 protected: | |
| 369 // Download progress returned by the previous callback. | |
| 370 int64 previous_progress_; | |
| 371 // Size of the file being downloaded, known in advance (provided by each test | |
| 372 // case). | |
| 373 int64 expected_total_; | |
| 374 }; | |
| 375 | |
| 376 // Version of URLFetcherTest that tests progress reports at cancellation. | |
| 377 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | |
| 378 public: | |
| 379 // URLFetcherTest: | |
| 380 void CreateFetcher(const GURL& url) override; | |
| 381 | |
| 382 // URLFetcherDelegate: | |
| 383 void OnURLFetchComplete(const URLFetcher* source) override; | |
| 384 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 385 int64 current, | |
| 386 int64 total) override; | |
| 387 | |
| 388 protected: | |
| 389 bool cancelled_; | |
| 390 }; | |
| 391 | |
| 392 // Version of URLFetcherTest that tests upload progress reports. | |
| 393 class URLFetcherUploadProgressTest : public URLFetcherTest { | |
| 394 public: | |
| 395 // URLFetcherTest: | |
| 396 void CreateFetcher(const GURL& url) override; | |
| 397 | |
| 398 // URLFetcherDelegate: | |
| 399 void OnURLFetchUploadProgress(const URLFetcher* source, | |
| 400 int64 current, | |
| 401 int64 total) override; | |
| 402 | |
| 403 protected: | |
| 404 int64 previous_progress_; | |
| 405 std::string chunk_; | |
| 406 int64 number_of_chunks_added_; | |
| 407 }; | |
| 408 | |
| 409 // Version of URLFetcherTest that tests bad HTTPS requests. | 397 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 410 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 398 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 411 public: | 399 public: |
| 412 URLFetcherBadHTTPSTest(); | 400 URLFetcherBadHTTPSTest(); |
| 413 | 401 |
| 414 // URLFetcherTest: | 402 // URLFetcherTest: |
| 415 void SetUpServer() override; | 403 void SetUpServer() override; |
| 416 | 404 |
| 417 // URLFetcherDelegate: | 405 // URLFetcherDelegate: |
| 418 void OnURLFetchComplete(const URLFetcher* source) override; | 406 void OnURLFetchComplete(const URLFetcher* source) override; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 // Version of URLFetcherTest that tests retying the same request twice. | 497 // Version of URLFetcherTest that tests retying the same request twice. |
| 510 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 498 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
| 511 public: | 499 public: |
| 512 // URLFetcherDelegate: | 500 // URLFetcherDelegate: |
| 513 void OnURLFetchComplete(const URLFetcher* source) override; | 501 void OnURLFetchComplete(const URLFetcher* source) override; |
| 514 | 502 |
| 515 private: | 503 private: |
| 516 std::string data_; | 504 std::string data_; |
| 517 }; | 505 }; |
| 518 | 506 |
| 519 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | |
| 520 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 521 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 522 io_message_loop_proxy().get(), request_context())); | |
| 523 fetcher_->Start(); | |
| 524 } | |
| 525 | |
| 526 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | |
| 527 const URLFetcher* source, int64 progress, int64 total) { | |
| 528 // Increasing between 0 and total. | |
| 529 EXPECT_LE(0, progress); | |
| 530 EXPECT_GE(total, progress); | |
| 531 EXPECT_LE(previous_progress_, progress); | |
| 532 EXPECT_EQ(expected_total_, total); | |
| 533 previous_progress_ = progress; | |
| 534 } | |
| 535 | |
| 536 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | |
| 537 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | |
| 538 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 539 io_message_loop_proxy().get(), request_context())); | |
| 540 cancelled_ = false; | |
| 541 fetcher_->Start(); | |
| 542 } | |
| 543 | |
| 544 void URLFetcherDownloadProgressCancelTest::OnURLFetchDownloadProgress( | |
| 545 const URLFetcher* source, int64 current, int64 total) { | |
| 546 EXPECT_FALSE(cancelled_); | |
| 547 if (!cancelled_) { | |
| 548 cancelled_ = true; | |
| 549 CleanupAfterFetchComplete(); | |
| 550 } | |
| 551 } | |
| 552 | |
| 553 void URLFetcherDownloadProgressCancelTest::OnURLFetchComplete( | |
| 554 const URLFetcher* source) { | |
| 555 // Should have been cancelled. | |
| 556 ADD_FAILURE(); | |
| 557 CleanupAfterFetchComplete(); | |
| 558 } | |
| 559 | |
| 560 void URLFetcherUploadProgressTest::CreateFetcher(const GURL& url) { | |
| 561 fetcher_ = new URLFetcherImpl(url, URLFetcher::POST, this); | |
| 562 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | |
| 563 io_message_loop_proxy().get(), request_context())); | |
| 564 previous_progress_ = 0; | |
| 565 // Large enough data to require more than one read from UploadDataStream. | |
| 566 chunk_.assign(1<<16, 'a'); | |
| 567 // Use chunked upload to wait for a timer event of progress notification. | |
| 568 fetcher_->SetChunkedUpload("application/x-www-form-urlencoded"); | |
| 569 fetcher_->Start(); | |
| 570 number_of_chunks_added_ = 1; | |
| 571 fetcher_->AppendChunkToUpload(chunk_, false); | |
| 572 } | |
| 573 | |
| 574 void URLFetcherUploadProgressTest::OnURLFetchUploadProgress( | |
| 575 const URLFetcher* source, int64 current, int64 total) { | |
| 576 // Increasing between 0 and total. | |
| 577 EXPECT_LE(0, current); | |
| 578 EXPECT_GE(static_cast<int64>(chunk_.size()) * number_of_chunks_added_, | |
| 579 current); | |
| 580 EXPECT_LE(previous_progress_, current); | |
| 581 previous_progress_ = current; | |
| 582 EXPECT_EQ(-1, total); | |
| 583 | |
| 584 if (number_of_chunks_added_ < 2) { | |
| 585 number_of_chunks_added_ += 1; | |
| 586 fetcher_->AppendChunkToUpload(chunk_, true); | |
| 587 } | |
| 588 } | |
| 589 | |
| 590 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | 507 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { |
| 591 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 508 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
| 592 cert_dir_ = cert_dir_.AppendASCII("chrome"); | 509 cert_dir_ = cert_dir_.AppendASCII("chrome"); |
| 593 cert_dir_ = cert_dir_.AppendASCII("test"); | 510 cert_dir_ = cert_dir_.AppendASCII("test"); |
| 594 cert_dir_ = cert_dir_.AppendASCII("data"); | 511 cert_dir_ = cert_dir_.AppendASCII("data"); |
| 595 cert_dir_ = cert_dir_.AppendASCII("ssl"); | 512 cert_dir_ = cert_dir_.AppendASCII("ssl"); |
| 596 cert_dir_ = cert_dir_.AppendASCII("certificates"); | 513 cert_dir_ = cert_dir_.AppendASCII("certificates"); |
| 597 } | 514 } |
| 598 | 515 |
| 599 void URLFetcherBadHTTPSTest::SetUpServer() { | 516 void URLFetcherBadHTTPSTest::SetUpServer() { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 940 delegate.StartFetcherAndWait(); | 857 delegate.StartFetcherAndWait(); |
| 941 | 858 |
| 942 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 859 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| 943 EXPECT_EQ(500, delegate.fetcher()->GetResponseCode()); | 860 EXPECT_EQ(500, delegate.fetcher()->GetResponseCode()); |
| 944 std::string data; | 861 std::string data; |
| 945 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 862 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
| 946 EXPECT_EQ(kCreateUploadStreamBody, data); | 863 EXPECT_EQ(kCreateUploadStreamBody, data); |
| 947 EXPECT_EQ(2u, num_upload_streams_created()); | 864 EXPECT_EQ(2u, num_upload_streams_created()); |
| 948 } | 865 } |
| 949 | 866 |
| 950 TEST_F(URLFetcherUploadProgressTest, Basic) { | 867 // Checks that upload progress increases over time, never exceeds what's already |
| 951 CreateFetcher(test_server_->GetURL("echo")); | 868 // been sent, and adds a chunk whenever all previously appended chunks have |
| 952 base::MessageLoop::current()->Run(); | 869 // been uploaded. |
| 870 class CheckUploadProgressDelegate : public WaitingURLFetcherDelegate { | |
| 871 public: | |
| 872 CheckUploadProgressDelegate() | |
| 873 : chunk_(1 << 16, 'a'), num_chunks_appended_(0), last_seen_progress_(0) {} | |
| 874 ~CheckUploadProgressDelegate() override {} | |
| 875 | |
| 876 void OnURLFetchUploadProgress(const URLFetcher* source, | |
| 877 int64 current, | |
| 878 int64 total) override { | |
| 879 // Run default checks. | |
| 880 WaitingURLFetcherDelegate::OnURLFetchUploadProgress(source, current, total); | |
| 881 | |
| 882 EXPECT_LE(last_seen_progress_, current); | |
| 883 EXPECT_LE(current, bytes_sent()); | |
| 884 last_seen_progress_ = current; | |
| 885 MaybeAppendChunk(); | |
|
davidben
2015/04/16 20:55:35
The old test didn't require that OnURLFetchUploadP
mmenke
2015/04/16 21:26:07
Done.
I agree it's weird, I just noticed it worke
| |
| 886 } | |
| 887 | |
| 888 void MaybeAppendChunk() { | |
| 889 const int kNumChunks = 5; | |
| 890 if (last_seen_progress_ == bytes_sent() && | |
| 891 num_chunks_appended_ < kNumChunks) { | |
|
davidben
2015/04/16 20:55:35
I'm assuming doing 5 chunks was an intentional cha
mmenke
2015/04/16 21:26:07
Yea, sending just 2 seemed insufficient.
| |
| 892 ++num_chunks_appended_; | |
| 893 fetcher()->AppendChunkToUpload(chunk_, | |
| 894 num_chunks_appended_ == kNumChunks); | |
| 895 } | |
| 896 } | |
| 897 | |
| 898 int64 bytes_sent() const { return num_chunks_appended_ * chunk_.size(); } | |
|
davidben
2015/04/16 20:55:35
Nit: s/bytes_sent/bytes_appended/? Or bytes_prepar
mmenke
2015/04/16 21:26:07
Done, absolutely right!
| |
| 899 | |
| 900 private: | |
| 901 const std::string chunk_; | |
| 902 | |
| 903 int64 num_chunks_appended_; | |
| 904 int64 last_seen_progress_; | |
| 905 | |
| 906 DISALLOW_COPY_AND_ASSIGN(CheckUploadProgressDelegate); | |
| 907 }; | |
| 908 | |
| 909 TEST_F(URLFetcherTest, UploadProgress) { | |
| 910 CheckUploadProgressDelegate delegate; | |
| 911 delegate.CreateFetcherWithContext(test_server_->GetURL("echo"), | |
| 912 URLFetcher::POST, request_context()); | |
| 913 // Use a chunked upload so that the upload can be paused after uploading data. | |
| 914 // Since uploads progress uses a timer, may not receive any notification, | |
|
davidben
2015/04/16 20:55:35
Nit: Since upload progress -> Since upload progres
mmenke
2015/04/16 21:26:07
Done.
| |
| 915 // otherwise. | |
| 916 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); | |
| 917 delegate.StartFetcherAndWait(); | |
| 918 | |
| 919 // Make sure there are no pending events that cause problems when run. | |
| 920 base::RunLoop().RunUntilIdle(); | |
| 921 | |
| 922 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | |
| 923 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | |
| 924 EXPECT_TRUE(delegate.did_complete()); | |
| 953 } | 925 } |
| 954 | 926 |
| 955 TEST_F(URLFetcherDownloadProgressTest, Basic) { | 927 // Checks that download progress never decreases, never exceeds file size, and |
| 928 // that file size is correctly reported. | |
| 929 class CheckDownloadProgressDelegate : public WaitingURLFetcherDelegate { | |
| 930 public: | |
| 931 CheckDownloadProgressDelegate(int64 file_size) | |
| 932 : file_size_(file_size), last_seen_progress_(0) {} | |
| 933 ~CheckDownloadProgressDelegate() override {} | |
| 934 | |
| 935 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 936 int64 current, | |
| 937 int64 total) override { | |
| 938 // Run default checks. | |
| 939 WaitingURLFetcherDelegate::OnURLFetchDownloadProgress(source, current, | |
| 940 total); | |
| 941 | |
| 942 EXPECT_LE(last_seen_progress_, current); | |
| 943 EXPECT_EQ(file_size_, total); | |
| 944 last_seen_progress_ = current; | |
| 945 } | |
| 946 | |
| 947 private: | |
| 948 int64 file_size_; | |
| 949 int64 last_seen_progress_; | |
| 950 | |
| 951 DISALLOW_COPY_AND_ASSIGN(CheckDownloadProgressDelegate); | |
| 952 }; | |
| 953 | |
| 954 TEST_F(URLFetcherTest, DownloadProgress) { | |
| 955 // Get a file large enough to require more than one read into | |
| 956 // URLFetcher::Core's IOBuffer. | |
| 957 const char kFileToFetch[] = "animate1.gif"; | |
| 958 | |
| 959 std::string file_contents; | |
| 960 ASSERT_TRUE(base::ReadFileToString( | |
| 961 test_server_->GetDocumentRoot().AppendASCII(kFileToFetch), | |
| 962 &file_contents)); | |
| 963 | |
| 964 CheckDownloadProgressDelegate delegate(file_contents.size()); | |
| 965 delegate.CreateFetcherWithContext( | |
| 966 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | |
| 967 URLFetcher::GET, request_context()); | |
| 968 delegate.StartFetcherAndWait(); | |
| 969 | |
| 970 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | |
| 971 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | |
| 972 std::string data; | |
| 973 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | |
| 974 EXPECT_EQ(file_contents, data); | |
| 975 } | |
| 976 | |
| 977 class CancelOnUploadProgressDelegate : public WaitingURLFetcherDelegate { | |
| 978 public: | |
| 979 CancelOnUploadProgressDelegate() {} | |
| 980 ~CancelOnUploadProgressDelegate() override {} | |
| 981 | |
| 982 void OnURLFetchUploadProgress(const URLFetcher* source, | |
| 983 int64 current, | |
| 984 int64 total) override { | |
| 985 CancelFetch(); | |
| 986 } | |
| 987 | |
| 988 private: | |
| 989 DISALLOW_COPY_AND_ASSIGN(CancelOnUploadProgressDelegate); | |
| 990 }; | |
| 991 | |
| 992 // Check that a fetch can be safely cancelled/deleted during an upload progress | |
| 993 // callback. | |
| 994 TEST_F(URLFetcherTest, CancelInUploadProgressCallback) { | |
| 995 CancelOnUploadProgressDelegate delegate; | |
| 996 delegate.CreateFetcherWithContext(test_server_->GetURL("echo"), | |
| 997 URLFetcher::POST, request_context()); | |
| 998 delegate.fetcher()->SetChunkedUpload("application/x-www-form-urlencoded"); | |
| 999 delegate.fetcher()->Start(); | |
| 1000 // Use a chunked upload so that the upload can be paused after uploading data. | |
| 1001 // Since uploads progress uses a timer, may not receive any notification, | |
| 1002 // otherwise. | |
| 1003 std::string upload_data(1 << 16, 'a'); | |
| 1004 delegate.fetcher()->AppendChunkToUpload(upload_data, false); | |
| 1005 delegate.WaitForComplete(); | |
| 1006 | |
| 1007 // Make sure there are no pending events that cause problems when run. | |
| 1008 base::RunLoop().RunUntilIdle(); | |
| 1009 | |
| 1010 EXPECT_FALSE(delegate.did_complete()); | |
| 1011 EXPECT_FALSE(delegate.fetcher()); | |
| 1012 } | |
| 1013 | |
| 1014 class CancelOnDownloadProgressDelegate : public WaitingURLFetcherDelegate { | |
| 1015 public: | |
| 1016 CancelOnDownloadProgressDelegate() {} | |
| 1017 ~CancelOnDownloadProgressDelegate() override {} | |
| 1018 | |
| 1019 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
| 1020 int64 current, | |
| 1021 int64 total) override { | |
| 1022 CancelFetch(); | |
| 1023 } | |
| 1024 | |
| 1025 private: | |
| 1026 DISALLOW_COPY_AND_ASSIGN(CancelOnDownloadProgressDelegate); | |
| 1027 }; | |
| 1028 | |
| 1029 // Check that a fetch can be safely cancelled/deleted during a download progress | |
| 1030 // callback. | |
| 1031 TEST_F(URLFetcherTest, CancelInDownloadProgressCallback) { | |
| 956 // Get a file large enough to require more than one read into | 1032 // Get a file large enough to require more than one read into |
| 957 // URLFetcher::Core's IOBuffer. | 1033 // URLFetcher::Core's IOBuffer. |
| 958 static const char kFileToFetch[] = "animate1.gif"; | 1034 static const char kFileToFetch[] = "animate1.gif"; |
| 959 // Hardcoded file size - it cannot be easily fetched when a remote http server | 1035 CancelOnDownloadProgressDelegate delegate; |
| 960 // is used for testing. | 1036 delegate.CreateFetcherWithContext( |
| 961 static const int64 kFileSize = 19021; | 1037 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
| 1038 URLFetcher::GET, request_context()); | |
| 1039 delegate.StartFetcherAndWait(); | |
| 962 | 1040 |
| 963 expected_total_ = kFileSize; | 1041 // Make sure there are no pending events that cause problems when run. |
| 1042 base::RunLoop().RunUntilIdle(); | |
| 964 | 1043 |
| 965 CreateFetcher( | 1044 EXPECT_FALSE(delegate.did_complete()); |
| 966 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1045 EXPECT_FALSE(delegate.fetcher()); |
| 967 | |
| 968 base::MessageLoop::current()->Run(); | |
| 969 } | |
| 970 | |
| 971 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { | |
| 972 // Get a file large enough to require more than one read into | |
| 973 // URLFetcher::Core's IOBuffer. | |
| 974 static const char kFileToFetch[] = "animate1.gif"; | |
| 975 CreateFetcher( | |
| 976 test_server_->GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | |
| 977 | |
| 978 base::MessageLoop::current()->Run(); | |
| 979 } | 1046 } |
| 980 | 1047 |
| 981 TEST_F(URLFetcherTest, Headers) { | 1048 TEST_F(URLFetcherTest, Headers) { |
| 982 WaitingURLFetcherDelegate delegate; | 1049 WaitingURLFetcherDelegate delegate; |
| 983 delegate.CreateFetcherWithContext( | 1050 delegate.CreateFetcherWithContext( |
| 984 test_server_->GetURL("set-header?cache-control: private"), | 1051 test_server_->GetURL("set-header?cache-control: private"), |
| 985 URLFetcher::GET, request_context()); | 1052 URLFetcher::GET, request_context()); |
| 986 delegate.StartFetcherAndWait(); | 1053 delegate.StartFetcherAndWait(); |
| 987 | 1054 |
| 988 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 1055 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1277 | 1344 |
| 1278 // If the caller takes the ownership of the temp file, check that the file | 1345 // If the caller takes the ownership of the temp file, check that the file |
| 1279 // persists even after URLFetcher is gone. | 1346 // persists even after URLFetcher is gone. |
| 1280 TEST_F(URLFetcherTest, TempFileTestTakeOwnership) { | 1347 TEST_F(URLFetcherTest, TempFileTestTakeOwnership) { |
| 1281 SaveFileTest("simple.html", true, base::FilePath(), true); | 1348 SaveFileTest("simple.html", true, base::FilePath(), true); |
| 1282 } | 1349 } |
| 1283 | 1350 |
| 1284 } // namespace | 1351 } // namespace |
| 1285 | 1352 |
| 1286 } // namespace net | 1353 } // namespace net |
| OLD | NEW |