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> |
| 11 #include <limits> |
11 #include <string> | 12 #include <string> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
16 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
17 #include "base/location.h" | 18 #include "base/location.h" |
18 #include "base/macros.h" | 19 #include "base/macros.h" |
19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
20 #include "base/path_service.h" | 21 #include "base/path_service.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // URLFetcherDelegate: | 112 // URLFetcherDelegate: |
112 void OnURLFetchComplete(const URLFetcher* source) override { | 113 void OnURLFetchComplete(const URLFetcher* source) override { |
113 EXPECT_FALSE(did_complete_); | 114 EXPECT_FALSE(did_complete_); |
114 EXPECT_TRUE(fetcher_); | 115 EXPECT_TRUE(fetcher_); |
115 EXPECT_EQ(fetcher_.get(), source); | 116 EXPECT_EQ(fetcher_.get(), source); |
116 did_complete_ = true; | 117 did_complete_ = true; |
117 run_loop_.Quit(); | 118 run_loop_.Quit(); |
118 } | 119 } |
119 | 120 |
120 void OnURLFetchDownloadProgress(const URLFetcher* source, | 121 void OnURLFetchDownloadProgress(const URLFetcher* source, |
121 int64 current, | 122 int64_t current, |
122 int64 total) override { | 123 int64_t total) override { |
123 // Note that the current progress may be greater than the previous progress, | 124 // Note that the current progress may be greater than the previous progress, |
124 // in the case of retrying the request. | 125 // in the case of retrying the request. |
125 EXPECT_FALSE(did_complete_); | 126 EXPECT_FALSE(did_complete_); |
126 EXPECT_TRUE(fetcher_); | 127 EXPECT_TRUE(fetcher_); |
127 EXPECT_EQ(source, fetcher_.get()); | 128 EXPECT_EQ(source, fetcher_.get()); |
128 | 129 |
129 EXPECT_LE(0, current); | 130 EXPECT_LE(0, current); |
130 // If file size is not known, |total| is -1. | 131 // If file size is not known, |total| is -1. |
131 if (total >= 0) | 132 if (total >= 0) |
132 EXPECT_LE(current, total); | 133 EXPECT_LE(current, total); |
133 } | 134 } |
134 | 135 |
135 void OnURLFetchUploadProgress(const URLFetcher* source, | 136 void OnURLFetchUploadProgress(const URLFetcher* source, |
136 int64 current, | 137 int64_t current, |
137 int64 total) override { | 138 int64_t total) override { |
138 // Note that the current progress may be greater than the previous progress, | 139 // Note that the current progress may be greater than the previous progress, |
139 // in the case of retrying the request. | 140 // in the case of retrying the request. |
140 EXPECT_FALSE(did_complete_); | 141 EXPECT_FALSE(did_complete_); |
141 EXPECT_TRUE(fetcher_); | 142 EXPECT_TRUE(fetcher_); |
142 EXPECT_EQ(source, fetcher_.get()); | 143 EXPECT_EQ(source, fetcher_.get()); |
143 | 144 |
144 EXPECT_LE(0, current); | 145 EXPECT_LE(0, current); |
145 // If file size is not known, |total| is -1. | 146 // If file size is not known, |total| is -1. |
146 if (total >= 0) | 147 if (total >= 0) |
147 EXPECT_LE(current, total); | 148 EXPECT_LE(current, total); |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 EXPECT_EQ(kUploadData, data); | 694 EXPECT_EQ(kUploadData, data); |
694 } | 695 } |
695 | 696 |
696 TEST_F(URLFetcherTest, PostEntireFile) { | 697 TEST_F(URLFetcherTest, PostEntireFile) { |
697 base::FilePath upload_path = GetUploadFileTestPath(); | 698 base::FilePath upload_path = GetUploadFileTestPath(); |
698 | 699 |
699 WaitingURLFetcherDelegate delegate; | 700 WaitingURLFetcherDelegate delegate; |
700 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, | 701 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
701 CreateSameThreadContextGetter()); | 702 CreateSameThreadContextGetter()); |
702 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", | 703 delegate.fetcher()->SetUploadFilePath("application/x-www-form-urlencoded", |
703 upload_path, 0, kuint64max, | 704 upload_path, 0, |
| 705 std::numeric_limits<uint64_t>::max(), |
704 base::ThreadTaskRunnerHandle::Get()); | 706 base::ThreadTaskRunnerHandle::Get()); |
705 delegate.StartFetcherAndWait(); | 707 delegate.StartFetcherAndWait(); |
706 | 708 |
707 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 709 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
708 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 710 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
709 | 711 |
710 std::string expected; | 712 std::string expected; |
711 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); | 713 ASSERT_TRUE(base::ReadFileToString(upload_path, &expected)); |
712 std::string data; | 714 std::string data; |
713 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 715 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 // Checks that upload progress increases over time, never exceeds what's already | 778 // Checks that upload progress increases over time, never exceeds what's already |
777 // been sent, and adds a chunk whenever all previously appended chunks have | 779 // been sent, and adds a chunk whenever all previously appended chunks have |
778 // been uploaded. | 780 // been uploaded. |
779 class CheckUploadProgressDelegate : public WaitingURLFetcherDelegate { | 781 class CheckUploadProgressDelegate : public WaitingURLFetcherDelegate { |
780 public: | 782 public: |
781 CheckUploadProgressDelegate() | 783 CheckUploadProgressDelegate() |
782 : chunk_(1 << 16, 'a'), num_chunks_appended_(0), last_seen_progress_(0) {} | 784 : chunk_(1 << 16, 'a'), num_chunks_appended_(0), last_seen_progress_(0) {} |
783 ~CheckUploadProgressDelegate() override {} | 785 ~CheckUploadProgressDelegate() override {} |
784 | 786 |
785 void OnURLFetchUploadProgress(const URLFetcher* source, | 787 void OnURLFetchUploadProgress(const URLFetcher* source, |
786 int64 current, | 788 int64_t current, |
787 int64 total) override { | 789 int64_t total) override { |
788 // Run default checks. | 790 // Run default checks. |
789 WaitingURLFetcherDelegate::OnURLFetchUploadProgress(source, current, total); | 791 WaitingURLFetcherDelegate::OnURLFetchUploadProgress(source, current, total); |
790 | 792 |
791 EXPECT_LE(last_seen_progress_, current); | 793 EXPECT_LE(last_seen_progress_, current); |
792 EXPECT_LE(current, bytes_appended()); | 794 EXPECT_LE(current, bytes_appended()); |
793 last_seen_progress_ = current; | 795 last_seen_progress_ = current; |
794 MaybeAppendChunk(); | 796 MaybeAppendChunk(); |
795 } | 797 } |
796 | 798 |
797 // Append the next chunk if all previously appended chunks have been sent. | 799 // Append the next chunk if all previously appended chunks have been sent. |
798 void MaybeAppendChunk() { | 800 void MaybeAppendChunk() { |
799 const int kNumChunks = 5; | 801 const int kNumChunks = 5; |
800 if (last_seen_progress_ == bytes_appended() && | 802 if (last_seen_progress_ == bytes_appended() && |
801 num_chunks_appended_ < kNumChunks) { | 803 num_chunks_appended_ < kNumChunks) { |
802 ++num_chunks_appended_; | 804 ++num_chunks_appended_; |
803 fetcher()->AppendChunkToUpload(chunk_, | 805 fetcher()->AppendChunkToUpload(chunk_, |
804 num_chunks_appended_ == kNumChunks); | 806 num_chunks_appended_ == kNumChunks); |
805 } | 807 } |
806 } | 808 } |
807 | 809 |
808 private: | 810 private: |
809 int64 bytes_appended() const { return num_chunks_appended_ * chunk_.size(); } | 811 int64_t bytes_appended() const { |
| 812 return num_chunks_appended_ * chunk_.size(); |
| 813 } |
810 | 814 |
811 const std::string chunk_; | 815 const std::string chunk_; |
812 | 816 |
813 int64 num_chunks_appended_; | 817 int64_t num_chunks_appended_; |
814 int64 last_seen_progress_; | 818 int64_t last_seen_progress_; |
815 | 819 |
816 DISALLOW_COPY_AND_ASSIGN(CheckUploadProgressDelegate); | 820 DISALLOW_COPY_AND_ASSIGN(CheckUploadProgressDelegate); |
817 }; | 821 }; |
818 | 822 |
819 TEST_F(URLFetcherTest, UploadProgress) { | 823 TEST_F(URLFetcherTest, UploadProgress) { |
820 CheckUploadProgressDelegate delegate; | 824 CheckUploadProgressDelegate delegate; |
821 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, | 825 delegate.CreateFetcher(test_server_->GetURL("/echo"), URLFetcher::POST, |
822 CreateSameThreadContextGetter()); | 826 CreateSameThreadContextGetter()); |
823 // Use a chunked upload so that the upload can be paused after uploading data. | 827 // Use a chunked upload so that the upload can be paused after uploading data. |
824 // Since upload progress uses a timer, the delegate may not receive any | 828 // Since upload progress uses a timer, the delegate may not receive any |
(...skipping 11 matching lines...) Expand all Loading... |
836 | 840 |
837 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); | 841 EXPECT_TRUE(delegate.fetcher()->GetStatus().is_success()); |
838 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); | 842 EXPECT_EQ(200, delegate.fetcher()->GetResponseCode()); |
839 EXPECT_TRUE(delegate.did_complete()); | 843 EXPECT_TRUE(delegate.did_complete()); |
840 } | 844 } |
841 | 845 |
842 // Checks that download progress never decreases, never exceeds file size, and | 846 // Checks that download progress never decreases, never exceeds file size, and |
843 // that file size is correctly reported. | 847 // that file size is correctly reported. |
844 class CheckDownloadProgressDelegate : public WaitingURLFetcherDelegate { | 848 class CheckDownloadProgressDelegate : public WaitingURLFetcherDelegate { |
845 public: | 849 public: |
846 CheckDownloadProgressDelegate(int64 file_size) | 850 CheckDownloadProgressDelegate(int64_t file_size) |
847 : file_size_(file_size), last_seen_progress_(0) {} | 851 : file_size_(file_size), last_seen_progress_(0) {} |
848 ~CheckDownloadProgressDelegate() override {} | 852 ~CheckDownloadProgressDelegate() override {} |
849 | 853 |
850 void OnURLFetchDownloadProgress(const URLFetcher* source, | 854 void OnURLFetchDownloadProgress(const URLFetcher* source, |
851 int64 current, | 855 int64_t current, |
852 int64 total) override { | 856 int64_t total) override { |
853 // Run default checks. | 857 // Run default checks. |
854 WaitingURLFetcherDelegate::OnURLFetchDownloadProgress(source, current, | 858 WaitingURLFetcherDelegate::OnURLFetchDownloadProgress(source, current, |
855 total); | 859 total); |
856 | 860 |
857 EXPECT_LE(last_seen_progress_, current); | 861 EXPECT_LE(last_seen_progress_, current); |
858 EXPECT_EQ(file_size_, total); | 862 EXPECT_EQ(file_size_, total); |
859 last_seen_progress_ = current; | 863 last_seen_progress_ = current; |
860 } | 864 } |
861 | 865 |
862 private: | 866 private: |
863 int64 file_size_; | 867 int64_t file_size_; |
864 int64 last_seen_progress_; | 868 int64_t last_seen_progress_; |
865 | 869 |
866 DISALLOW_COPY_AND_ASSIGN(CheckDownloadProgressDelegate); | 870 DISALLOW_COPY_AND_ASSIGN(CheckDownloadProgressDelegate); |
867 }; | 871 }; |
868 | 872 |
869 TEST_F(URLFetcherTest, DownloadProgress) { | 873 TEST_F(URLFetcherTest, DownloadProgress) { |
870 // Get a file large enough to require more than one read into | 874 // Get a file large enough to require more than one read into |
871 // URLFetcher::Core's IOBuffer. | 875 // URLFetcher::Core's IOBuffer. |
872 const char kFileToFetch[] = "animate1.gif"; | 876 const char kFileToFetch[] = "animate1.gif"; |
873 | 877 |
874 std::string file_contents; | 878 std::string file_contents; |
(...skipping 16 matching lines...) Expand all Loading... |
891 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 895 ASSERT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
892 EXPECT_EQ(file_contents, data); | 896 EXPECT_EQ(file_contents, data); |
893 } | 897 } |
894 | 898 |
895 class CancelOnUploadProgressDelegate : public WaitingURLFetcherDelegate { | 899 class CancelOnUploadProgressDelegate : public WaitingURLFetcherDelegate { |
896 public: | 900 public: |
897 CancelOnUploadProgressDelegate() {} | 901 CancelOnUploadProgressDelegate() {} |
898 ~CancelOnUploadProgressDelegate() override {} | 902 ~CancelOnUploadProgressDelegate() override {} |
899 | 903 |
900 void OnURLFetchUploadProgress(const URLFetcher* source, | 904 void OnURLFetchUploadProgress(const URLFetcher* source, |
901 int64 current, | 905 int64_t current, |
902 int64 total) override { | 906 int64_t total) override { |
903 CancelFetch(); | 907 CancelFetch(); |
904 } | 908 } |
905 | 909 |
906 private: | 910 private: |
907 DISALLOW_COPY_AND_ASSIGN(CancelOnUploadProgressDelegate); | 911 DISALLOW_COPY_AND_ASSIGN(CancelOnUploadProgressDelegate); |
908 }; | 912 }; |
909 | 913 |
910 // Check that a fetch can be safely cancelled/deleted during an upload progress | 914 // Check that a fetch can be safely cancelled/deleted during an upload progress |
911 // callback. | 915 // callback. |
912 TEST_F(URLFetcherTest, CancelInUploadProgressCallback) { | 916 TEST_F(URLFetcherTest, CancelInUploadProgressCallback) { |
(...skipping 15 matching lines...) Expand all Loading... |
928 EXPECT_FALSE(delegate.did_complete()); | 932 EXPECT_FALSE(delegate.did_complete()); |
929 EXPECT_FALSE(delegate.fetcher()); | 933 EXPECT_FALSE(delegate.fetcher()); |
930 } | 934 } |
931 | 935 |
932 class CancelOnDownloadProgressDelegate : public WaitingURLFetcherDelegate { | 936 class CancelOnDownloadProgressDelegate : public WaitingURLFetcherDelegate { |
933 public: | 937 public: |
934 CancelOnDownloadProgressDelegate() {} | 938 CancelOnDownloadProgressDelegate() {} |
935 ~CancelOnDownloadProgressDelegate() override {} | 939 ~CancelOnDownloadProgressDelegate() override {} |
936 | 940 |
937 void OnURLFetchDownloadProgress(const URLFetcher* source, | 941 void OnURLFetchDownloadProgress(const URLFetcher* source, |
938 int64 current, | 942 int64_t current, |
939 int64 total) override { | 943 int64_t total) override { |
940 CancelFetch(); | 944 CancelFetch(); |
941 } | 945 } |
942 | 946 |
943 private: | 947 private: |
944 DISALLOW_COPY_AND_ASSIGN(CancelOnDownloadProgressDelegate); | 948 DISALLOW_COPY_AND_ASSIGN(CancelOnDownloadProgressDelegate); |
945 }; | 949 }; |
946 | 950 |
947 // Check that a fetch can be safely cancelled/deleted during a download progress | 951 // Check that a fetch can be safely cancelled/deleted during a download progress |
948 // callback. | 952 // callback. |
949 TEST_F(URLFetcherTest, CancelInDownloadProgressCallback) { | 953 TEST_F(URLFetcherTest, CancelInDownloadProgressCallback) { |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1423 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); | 1427 EXPECT_EQ(-1, delegate.fetcher()->GetResponseCode()); |
1424 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); | 1428 EXPECT_TRUE(delegate.fetcher()->GetCookies().empty()); |
1425 std::string data; | 1429 std::string data; |
1426 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); | 1430 EXPECT_TRUE(delegate.fetcher()->GetResponseAsString(&data)); |
1427 EXPECT_TRUE(data.empty()); | 1431 EXPECT_TRUE(data.empty()); |
1428 } | 1432 } |
1429 | 1433 |
1430 } // namespace | 1434 } // namespace |
1431 | 1435 |
1432 } // namespace net | 1436 } // namespace net |
OLD | NEW |