OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 80 } |
81 net::URLFetcher::RequestType GetRequestType() const override { | 81 net::URLFetcher::RequestType GetRequestType() const override { |
82 return net::URLFetcher::PUT; | 82 return net::URLFetcher::PUT; |
83 } | 83 } |
84 bool GetContentData(std::string* upload_content_type, | 84 bool GetContentData(std::string* upload_content_type, |
85 std::string* upload_content) override { | 85 std::string* upload_content) override { |
86 upload_content_type->assign(content_type_); | 86 upload_content_type->assign(content_type_); |
87 upload_content->assign(content_data_); | 87 upload_content->assign(content_data_); |
88 return true; | 88 return true; |
89 } | 89 } |
| 90 void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 91 int64 current, |
| 92 int64 total) override { |
| 93 progress_values_.push_back(current); |
| 94 } |
| 95 const std::vector<int64>& progress_values() const { return progress_values_; } |
90 | 96 |
91 private: | 97 private: |
92 GURL url_; | 98 GURL url_; |
93 std::string content_type_; | 99 std::string content_type_; |
94 std::string content_data_; | 100 std::string content_data_; |
95 base::Closure callback_; | 101 base::Closure callback_; |
| 102 std::vector<int64> progress_values_; |
96 }; | 103 }; |
97 | 104 |
98 } // namespace | 105 } // namespace |
99 | 106 |
100 class DriveApiRequestsTest : public testing::Test { | 107 class DriveApiRequestsTest : public testing::Test { |
101 public: | 108 public: |
102 DriveApiRequestsTest() { | 109 DriveApiRequestsTest() { |
103 } | 110 } |
104 | 111 |
105 void SetUp() override { | 112 void SetUp() override { |
(...skipping 1959 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2065 drive::BatchUploadRequest* const request = | 2072 drive::BatchUploadRequest* const request = |
2066 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); | 2073 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); |
2067 request->SetBoundaryForTesting("OUTERBOUNDARY"); | 2074 request->SetBoundaryForTesting("OUTERBOUNDARY"); |
2068 request_sender_->StartRequestWithRetry(request); | 2075 request_sender_->StartRequestWithRetry(request); |
2069 | 2076 |
2070 // Create child request. | 2077 // Create child request. |
2071 { | 2078 { |
2072 base::RunLoop loop; | 2079 base::RunLoop loop; |
2073 TestBatchableRequest* child_request = new TestBatchableRequest( | 2080 TestBatchableRequest* child_request = new TestBatchableRequest( |
2074 request_sender_.get(), GURL("http://example.com/test"), | 2081 request_sender_.get(), GURL("http://example.com/test"), |
2075 "application/binary", | 2082 "application/binary", std::string("Apple\0Orange\0", 13), |
2076 std::string("Apple\0Orange\0", 13), | |
2077 loop.QuitClosure()); | 2083 loop.QuitClosure()); |
2078 request->AddRequest(child_request); | 2084 request->AddRequest(child_request); |
2079 request->Commit(); | 2085 request->Commit(); |
2080 loop.Run(); | 2086 loop.Run(); |
2081 } | 2087 } |
2082 | 2088 |
2083 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); | 2089 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
2084 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]); | 2090 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]); |
2085 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY", | 2091 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY", |
2086 http_request_.headers["Content-Type"]); | 2092 http_request_.headers["Content-Type"]); |
2087 EXPECT_EQ( | 2093 EXPECT_EQ( |
2088 "--OUTERBOUNDARY\n" | 2094 "--OUTERBOUNDARY\n" |
2089 "Content-Type: application/http\n" | 2095 "Content-Type: application/http\n" |
2090 "\n" | 2096 "\n" |
2091 "PUT /test HTTP/1.1\n" | 2097 "PUT /test HTTP/1.1\n" |
2092 "Host: 127.0.0.1\n" | 2098 "Host: 127.0.0.1\n" |
2093 "X-Goog-Upload-Protocol: multipart\n" | 2099 "X-Goog-Upload-Protocol: multipart\n" |
2094 "Content-Type: application/binary\n" | 2100 "Content-Type: application/binary\n" |
2095 "\n" | 2101 "\n" + |
2096 + std::string("Apple\0Orange\0", 13) + "\n" | 2102 std::string("Apple\0Orange\0", 13) + |
2097 "--OUTERBOUNDARY--", | 2103 "\n" |
| 2104 "--OUTERBOUNDARY--", |
2098 http_request_.content); | 2105 http_request_.content); |
2099 } | 2106 } |
2100 | 2107 |
| 2108 void EmptyPreapreCallback(DriveApiErrorCode) { |
| 2109 } |
| 2110 |
| 2111 TEST_F(DriveApiRequestsTest, BatchUploadRequestProgress) { |
| 2112 // Create batch request. |
| 2113 drive::BatchUploadRequest* const request = |
| 2114 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); |
| 2115 TestBatchableRequest* requests[] = { |
| 2116 new TestBatchableRequest( |
| 2117 request_sender_.get(), GURL("http://example.com/test"), |
| 2118 "application/binary", std::string(100, 'a'), base::Closure()), |
| 2119 new TestBatchableRequest( |
| 2120 request_sender_.get(), GURL("http://example.com/test"), |
| 2121 "application/binary", std::string(50, 'b'), base::Closure()), |
| 2122 new TestBatchableRequest( |
| 2123 request_sender_.get(), GURL("http://example.com/test"), |
| 2124 "application/binary", std::string(0, 'c'), base::Closure())}; |
| 2125 const size_t kExpectedUploadDataPosition[] = {208, 517, 776}; |
| 2126 const size_t kExpectedUploadDataSize = 851; |
| 2127 request->AddRequest(requests[0]); |
| 2128 request->AddRequest(requests[1]); |
| 2129 request->AddRequest(requests[2]); |
| 2130 request->Commit(); |
| 2131 request->Prepare(base::Bind(&EmptyPreapreCallback)); |
| 2132 |
| 2133 request->OnURLFetchUploadProgress(nullptr, 0, kExpectedUploadDataSize); |
| 2134 request->OnURLFetchUploadProgress(nullptr, 150, kExpectedUploadDataSize); |
| 2135 EXPECT_EQ(0u, requests[0]->progress_values().size()); |
| 2136 EXPECT_EQ(0u, requests[1]->progress_values().size()); |
| 2137 EXPECT_EQ(0u, requests[2]->progress_values().size()); |
| 2138 request->OnURLFetchUploadProgress(nullptr, kExpectedUploadDataPosition[0], |
| 2139 kExpectedUploadDataSize); |
| 2140 EXPECT_EQ(1u, requests[0]->progress_values().size()); |
| 2141 EXPECT_EQ(0u, requests[1]->progress_values().size()); |
| 2142 EXPECT_EQ(0u, requests[2]->progress_values().size()); |
| 2143 request->OnURLFetchUploadProgress( |
| 2144 nullptr, kExpectedUploadDataPosition[0] + 50, kExpectedUploadDataSize); |
| 2145 EXPECT_EQ(2u, requests[0]->progress_values().size()); |
| 2146 EXPECT_EQ(0u, requests[1]->progress_values().size()); |
| 2147 EXPECT_EQ(0u, requests[2]->progress_values().size()); |
| 2148 request->OnURLFetchUploadProgress( |
| 2149 nullptr, kExpectedUploadDataPosition[1] + 20, kExpectedUploadDataSize); |
| 2150 EXPECT_EQ(3u, requests[0]->progress_values().size()); |
| 2151 EXPECT_EQ(1u, requests[1]->progress_values().size()); |
| 2152 EXPECT_EQ(0u, requests[2]->progress_values().size()); |
| 2153 request->OnURLFetchUploadProgress(nullptr, kExpectedUploadDataPosition[2], |
| 2154 kExpectedUploadDataSize); |
| 2155 EXPECT_EQ(3u, requests[0]->progress_values().size()); |
| 2156 EXPECT_EQ(2u, requests[1]->progress_values().size()); |
| 2157 EXPECT_EQ(1u, requests[2]->progress_values().size()); |
| 2158 request->OnURLFetchUploadProgress(nullptr, kExpectedUploadDataSize, |
| 2159 kExpectedUploadDataSize); |
| 2160 ASSERT_EQ(3u, requests[0]->progress_values().size()); |
| 2161 EXPECT_EQ(0, requests[0]->progress_values()[0]); |
| 2162 EXPECT_EQ(50, requests[0]->progress_values()[1]); |
| 2163 EXPECT_EQ(100, requests[0]->progress_values()[2]); |
| 2164 ASSERT_EQ(2u, requests[1]->progress_values().size()); |
| 2165 EXPECT_EQ(20, requests[1]->progress_values()[0]); |
| 2166 EXPECT_EQ(50, requests[1]->progress_values()[1]); |
| 2167 ASSERT_EQ(1u, requests[2]->progress_values().size()); |
| 2168 EXPECT_EQ(0, requests[2]->progress_values()[0]); |
| 2169 } |
| 2170 |
2101 TEST(ParseMultipartResponseTest, Empty) { | 2171 TEST(ParseMultipartResponseTest, Empty) { |
2102 std::vector<drive::MultipartHttpResponse> parts; | 2172 std::vector<drive::MultipartHttpResponse> parts; |
2103 EXPECT_FALSE(drive::ParseMultipartResponse( | 2173 EXPECT_FALSE(drive::ParseMultipartResponse( |
2104 "multipart/mixed; boundary=BOUNDARY", "", &parts)); | 2174 "multipart/mixed; boundary=BOUNDARY", "", &parts)); |
2105 EXPECT_FALSE(drive::ParseMultipartResponse("multipart/mixed; boundary=", | 2175 EXPECT_FALSE(drive::ParseMultipartResponse("multipart/mixed; boundary=", |
2106 "CONTENT", &parts)); | 2176 "CONTENT", &parts)); |
2107 } | 2177 } |
2108 | 2178 |
2109 TEST(ParseMultipartResponseTest, Basic) { | 2179 TEST(ParseMultipartResponseTest, Basic) { |
2110 std::vector<drive::MultipartHttpResponse> parts; | 2180 std::vector<drive::MultipartHttpResponse> parts; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2199 "Header: value\r\n" | 2269 "Header: value\r\n" |
2200 "\r\n" | 2270 "\r\n" |
2201 "BODY\r\n" | 2271 "BODY\r\n" |
2202 "--BOUNDARY-- \t", | 2272 "--BOUNDARY-- \t", |
2203 &parts)); | 2273 &parts)); |
2204 ASSERT_EQ(1u, parts.size()); | 2274 ASSERT_EQ(1u, parts.size()); |
2205 EXPECT_EQ(HTTP_SUCCESS, parts[0].code); | 2275 EXPECT_EQ(HTTP_SUCCESS, parts[0].code); |
2206 EXPECT_EQ("BODY", parts[0].body); | 2276 EXPECT_EQ("BODY", parts[0].body); |
2207 } | 2277 } |
2208 } // namespace google_apis | 2278 } // namespace google_apis |
OLD | NEW |