Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/google_apis/gdata_wapi_operations_unittest.cc

Issue 12246002: Implement GetUploadStatusOperation on GData WAPI. (Closed) Base URL: http://git.chromium.org/chromium/src.git@b148632_create_base_operation
Patch Set: Update comments. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <map> 6 #include <map>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 const GURL absolute_url = test_server_.GetURL(request.relative_url); 218 const GURL absolute_url = test_server_.GetURL(request.relative_url);
219 if (absolute_url.path() == 219 if (absolute_url.path() ==
220 // This is an upload URL of the root directory. 220 // This is an upload URL of the root directory.
221 "/feeds/upload/create-session/default/private/full" || 221 "/feeds/upload/create-session/default/private/full" ||
222 absolute_url.path() == 222 absolute_url.path() ==
223 // This is an upload URL of an existing file. 223 // This is an upload URL of an existing file.
224 "/feeds/upload/create-session/default/private/full/file:foo") { 224 "/feeds/upload/create-session/default/private/full/file:foo") {
225 scoped_ptr<test_server::HttpResponse> http_response( 225 scoped_ptr<test_server::HttpResponse> http_response(
226 new test_server::HttpResponse); 226 new test_server::HttpResponse);
227 227
228 // Check an ETag.
228 std::map<std::string, std::string>::const_iterator found = 229 std::map<std::string, std::string>::const_iterator found =
229 request.headers.find("If-Match"); 230 request.headers.find("If-Match");
230 if (found != request.headers.end() && 231 if (found != request.headers.end() &&
231 found->second != "*" && 232 found->second != "*" &&
232 found->second != kTestETag) { 233 found->second != kTestETag) {
233 http_response->set_code(test_server::PRECONDITION); 234 http_response->set_code(test_server::PRECONDITION);
234 return http_response.Pass(); 235 return http_response.Pass();
235 } 236 }
236 237
238 // Check if the X-Upload-Content-Length is present. If yes, store the
239 // length of the file.
240 found = request.headers.find("X-Upload-Content-Length");
241 if (found == request.headers.end() ||
242 !base::StringToInt64(found->second, &content_length_)) {
243 return scoped_ptr<test_server::HttpResponse>();
244 }
245 start_position_ = 0;
246 end_position_ = 0;
247
237 http_response->set_code(test_server::SUCCESS); 248 http_response->set_code(test_server::SUCCESS);
238 GURL upload_url; 249 GURL upload_url;
239 // POST is used for a new file, and PUT is used for an existing file. 250 // POST is used for a new file, and PUT is used for an existing file.
240 if (request.method == test_server::METHOD_POST) { 251 if (request.method == test_server::METHOD_POST) {
241 upload_url = test_server_.GetURL("/upload_new_file"); 252 upload_url = test_server_.GetURL("/upload_new_file");
242 } else if (request.method == test_server::METHOD_PUT) { 253 } else if (request.method == test_server::METHOD_PUT) {
243 upload_url = test_server_.GetURL("/upload_existing_file"); 254 upload_url = test_server_.GetURL("/upload_existing_file");
244 } else { 255 } else {
245 return scoped_ptr<test_server::HttpResponse>(); 256 return scoped_ptr<test_server::HttpResponse>();
246 } 257 }
(...skipping 25 matching lines...) Expand all
272 if (absolute_url.path() == "/upload_new_file") 283 if (absolute_url.path() == "/upload_new_file")
273 response->set_code(test_server::CREATED); 284 response->set_code(test_server::CREATED);
274 285
275 // Check if the Content-Range header is present. This must be present if 286 // Check if the Content-Range header is present. This must be present if
276 // the request body is not empty. 287 // the request body is not empty.
277 if (!request.content.empty()) { 288 if (!request.content.empty()) {
278 std::map<std::string, std::string>::const_iterator iter = 289 std::map<std::string, std::string>::const_iterator iter =
279 request.headers.find("Content-Range"); 290 request.headers.find("Content-Range");
280 if (iter == request.headers.end()) 291 if (iter == request.headers.end())
281 return scoped_ptr<test_server::HttpResponse>(); 292 return scoped_ptr<test_server::HttpResponse>();
282 int64 start_position = 0;
283 int64 end_position = 0;
284 int64 length = 0; 293 int64 length = 0;
285 if (!ParseContentRangeHeader(iter->second, 294 if (!ParseContentRangeHeader(iter->second,
286 &start_position, 295 &start_position_,
287 &end_position, 296 &end_position_,
288 &length)) { 297 &length)) {
289 return scoped_ptr<test_server::HttpResponse>(); 298 return scoped_ptr<test_server::HttpResponse>();
290 } 299 }
300 EXPECT_EQ(length, content_length_);
301 }
291 302
292 // Add Range header to the response, based on the values of 303 // Add Range header to the response, based on the values of
293 // Content-Range header in the request. 304 // Content-Range header in the request.
294 response->AddCustomHeader( 305 response->AddCustomHeader(
295 "Range", 306 "Range",
296 "bytes=" + 307 "bytes=" +
297 base::Int64ToString(start_position) + "-" + 308 base::Int64ToString(start_position_) + "-" +
298 base::Int64ToString(end_position)); 309 base::Int64ToString(end_position_));
299 310
300 // Change the code to RESUME_INCOMPLETE if upload is not complete. 311 // Change the code to RESUME_INCOMPLETE if upload is not complete.
301 if (end_position + 1 < length) 312 if (end_position_ + 1 < content_length_)
302 response->set_code(test_server::RESUME_INCOMPLETE); 313 response->set_code(test_server::RESUME_INCOMPLETE);
303 }
304 314
305 return response.Pass(); 315 return response.Pass();
306 } 316 }
307 317
308 MessageLoopForUI message_loop_; 318 MessageLoopForUI message_loop_;
309 content::TestBrowserThread ui_thread_; 319 content::TestBrowserThread ui_thread_;
310 content::TestBrowserThread file_thread_; 320 content::TestBrowserThread file_thread_;
311 content::TestBrowserThread io_thread_; 321 content::TestBrowserThread io_thread_;
312 test_server::HttpServer test_server_; 322 test_server::HttpServer test_server_;
313 scoped_ptr<TestingProfile> profile_; 323 scoped_ptr<TestingProfile> profile_;
314 OperationRegistry operation_registry_; 324 OperationRegistry operation_registry_;
315 scoped_ptr<GDataWapiUrlGenerator> url_generator_; 325 scoped_ptr<GDataWapiUrlGenerator> url_generator_;
316 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; 326 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
317 327
328 // These fields are used to keep the current upload state during a
329 // test case.
330 int64 start_position_;
331 int64 end_position_;
332 int64 content_length_;
hashimoto 2013/02/07 06:56:13 Do we need to keep the values of start_position an
hidehiko 2013/02/07 07:44:07 Yes, it is necessary to keep them. This class is n
hashimoto 2013/02/07 07:56:10 I see, thanks.
333
318 // The incoming HTTP request is saved so tests can verify the request 334 // The incoming HTTP request is saved so tests can verify the request
319 // parameters like HTTP method (ex. some operations should use DELETE 335 // parameters like HTTP method (ex. some operations should use DELETE
320 // instead of GET). 336 // instead of GET).
321 test_server::HttpRequest http_request_; 337 test_server::HttpRequest http_request_;
322 }; 338 };
323 339
324 } // namespace 340 } // namespace
325 341
326 TEST_F(GDataWapiOperationsTest, GetResourceListOperation_DefaultFeed) { 342 TEST_F(GDataWapiOperationsTest, GetResourceListOperation_DefaultFeed) {
327 GDataErrorCode result_code = GDATA_OTHER_ERROR; 343 GDataErrorCode result_code = GDATA_OTHER_ERROR;
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 875
860 // Check the response. 876 // Check the response.
861 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file 877 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file
862 // The start and end positions should be set to -1, if an upload is complete. 878 // The start and end positions should be set to -1, if an upload is complete.
863 EXPECT_EQ(-1, response.start_position_received); 879 EXPECT_EQ(-1, response.start_position_received);
864 EXPECT_EQ(-1, response.end_position_received); 880 EXPECT_EQ(-1, response.end_position_received);
865 } 881 }
866 882
867 // This test exercises InitiateUploadOperation and ResumeUploadOperation for 883 // This test exercises InitiateUploadOperation and ResumeUploadOperation for
868 // a scenario of uploading a new *large* file, which requires multiple requests 884 // a scenario of uploading a new *large* file, which requires multiple requests
869 // of ResumeUploadOperation. 885 // of ResumeUploadOperation. GetUploadOperation is also testes in this test
hashimoto 2013/02/07 06:56:13 nit: s/testes/tested/?
hidehiko 2013/02/07 07:44:07 Good catch. Fixed.
886 // case.
870 TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { 887 TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
871 const size_t kMaxNumBytes = 10; 888 const size_t kMaxNumBytes = 10;
872 // This is big enough to cause multiple requests of ResumeUploadOperation 889 // This is big enough to cause multiple requests of ResumeUploadOperation
873 // as we are going to send at most kMaxNumBytes at a time. 890 // as we are going to send at most kMaxNumBytes at a time.
874 const std::string kUploadContent(kMaxNumBytes + 1, 'a'); 891 const std::string kUploadContent(kMaxNumBytes + 1, 'a');
875 GDataErrorCode result_code = GDATA_OTHER_ERROR; 892 GDataErrorCode result_code = GDATA_OTHER_ERROR;
876 GURL upload_url; 893 GURL upload_url;
877 894
878 // 1) Get the upload URL for uploading a new file. 895 // 1) Get the upload URL for uploading a new file.
879 InitiateUploadParams initiate_params( 896 InitiateUploadParams initiate_params(
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 EXPECT_TRUE(http_request_.has_content); 990 EXPECT_TRUE(http_request_.has_content);
974 EXPECT_EQ(payload, http_request_.content); 991 EXPECT_EQ(payload, http_request_.content);
975 992
976 // Check the response. 993 // Check the response.
977 if (payload.size() == remaining_size) { 994 if (payload.size() == remaining_size) {
978 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file. 995 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file.
979 // The start and end positions should be set to -1, if an upload is 996 // The start and end positions should be set to -1, if an upload is
980 // complete. 997 // complete.
981 EXPECT_EQ(-1, response.start_position_received); 998 EXPECT_EQ(-1, response.start_position_received);
982 EXPECT_EQ(-1, response.end_position_received); 999 EXPECT_EQ(-1, response.end_position_received);
983 } else { 1000 // The upload process is completed, so exit from the loop.
984 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); 1001 break;
985 EXPECT_EQ(static_cast<int64>(start_position),
986 response.start_position_received);
987 EXPECT_EQ(static_cast<int64>(end_position),
988 response.end_position_received);
989 } 1002 }
1003
1004 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1005 EXPECT_EQ(static_cast<int64>(start_position),
1006 response.start_position_received);
1007 EXPECT_EQ(static_cast<int64>(end_position),
1008 response.end_position_received);
1009
1010 // Check the response by GetUploadStatusOperation.
1011 GetUploadStatusOperation* get_upload_status_operation =
1012 new GetUploadStatusOperation(
1013 &operation_registry_,
1014 request_context_getter_.get(),
1015 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit,
1016 &response,
1017 &new_entry),
1018 UPLOAD_NEW_FILE,
1019 FilePath::FromUTF8Unsafe("drive/newfile.txt"),
1020 upload_url,
1021 kUploadContent.size());
hashimoto 2013/02/07 06:56:13 Could you also test the content_length==1 case?
hidehiko 2013/02/07 07:44:07 Sorry, I don't understand your request now... The
hashimoto 2013/02/07 07:56:10 Sorry, not 1, -1.
kinaba 2013/02/07 08:01:10 The case has gone away when the streamed uploading
hidehiko 2013/02/07 08:37:56 So, I've just removed -1 support from ResumeUpload
1022 get_upload_status_operation->Start(
1023 kTestGDataAuthToken, kTestUserAgent,
1024 base::Bind(&test_util::DoNothingForReAuthenticateCallback));
1025 MessageLoop::current()->Run();
1026
1027 // METHOD_PUT should be used to upload data.
1028 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
1029 // Request should go to the upload URL.
1030 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1031 // Content-Range header should be added.
1032 EXPECT_EQ("bytes */" + base::Int64ToString(kUploadContent.size()),
1033 http_request_.headers["Content-Range"]);
1034 EXPECT_TRUE(http_request_.has_content);
1035 EXPECT_TRUE(http_request_.content.empty());
1036
1037 // Check the response.
1038 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1039 EXPECT_EQ(static_cast<int64>(start_position),
1040 response.start_position_received);
1041 EXPECT_EQ(static_cast<int64>(end_position),
1042 response.end_position_received);
990 } 1043 }
991 1044
992 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed); 1045 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed);
993 } 1046 }
994 1047
995 // This test exercises InitiateUploadOperation and ResumeUploadOperation for 1048 // This test exercises InitiateUploadOperation and ResumeUploadOperation for
996 // a scenario of uploading a new *empty* file. 1049 // a scenario of uploading a new *empty* file.
997 // 1050 //
998 // The test is almost identical to UploadNewFile. The only difference is the 1051 // The test is almost identical to UploadNewFile. The only difference is the
999 // expectation for the Content-Range header. 1052 // expectation for the Content-Range header.
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), 1387 EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
1335 http_request_.headers["X-Upload-Content-Length"]); 1388 http_request_.headers["X-Upload-Content-Length"]);
1336 // For updating an existing file, an empty body should be attached (PUT 1389 // For updating an existing file, an empty body should be attached (PUT
1337 // requires a body) 1390 // requires a body)
1338 EXPECT_TRUE(http_request_.has_content); 1391 EXPECT_TRUE(http_request_.has_content);
1339 EXPECT_EQ("", http_request_.content); 1392 EXPECT_EQ("", http_request_.content);
1340 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); 1393 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]);
1341 } 1394 }
1342 1395
1343 } // namespace google_apis 1396 } // namespace google_apis
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698