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

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

Issue 11308315: google_apis: Add a test for uploading a large file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 8 years 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 | Annotate | Revision Log
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/message_loop_proxy.h" 9 #include "base/message_loop_proxy.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/string_split.h"
11 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
12 #include "chrome/browser/google_apis/gdata_wapi_operations.h" 13 #include "chrome/browser/google_apis/gdata_wapi_operations.h"
13 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 14 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
14 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" 15 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h"
15 #include "chrome/browser/google_apis/operation_registry.h" 16 #include "chrome/browser/google_apis/operation_registry.h"
16 #include "chrome/browser/google_apis/test_server/http_server.h" 17 #include "chrome/browser/google_apis/test_server/http_server.h"
17 #include "chrome/browser/google_apis/test_util.h" 18 #include "chrome/browser/google_apis/test_util.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/test/base/testing_browser_process.h" 20 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 bool RemovePrefix(const std::string& input, 125 bool RemovePrefix(const std::string& input,
125 const std::string& prefix, 126 const std::string& prefix,
126 std::string* output) { 127 std::string* output) {
127 if (!StartsWithASCII(input, prefix, true /* case sensitive */)) 128 if (!StartsWithASCII(input, prefix, true /* case sensitive */))
128 return false; 129 return false;
129 130
130 *output = input.substr(prefix.size()); 131 *output = input.substr(prefix.size());
131 return true; 132 return true;
132 } 133 }
133 134
135 // Parses a value of Content-Range header, which looks like
136 // "bytes <start_position>-<end_position>/<length>".
137 // Returns true on success.
138 bool ParseContentRangeHeader(const std::string& value,
139 int64* start_position,
140 int64* end_position,
141 int64* length) {
142 DCHECK(start_position);
143 DCHECK(end_position);
144 DCHECK(length);
145
146 std::string remaining;
147 if (!RemovePrefix(value, "bytes ", &remaining))
148 return false;
149
150 std::vector<std::string> parts;
151 base::SplitString(remaining, '/', &parts);
152 if (parts.size() != 2U)
153 return false;
154
155 const std::string range = parts[0];
156 if (!base::StringToInt64(parts[1], length))
157 return false;
158
159 parts.clear();
160 base::SplitString(range, '-', &parts);
161 if (parts.size() != 2U)
162 return false;
163
164 return (base::StringToInt64(parts[0], start_position) &&
165 base::StringToInt64(parts[1], end_position));
166 }
167
134 // This class sets a request context getter for testing in 168 // This class sets a request context getter for testing in
135 // |testing_browser_process| and then clears the state when an instance of it 169 // |testing_browser_process| and then clears the state when an instance of it
136 // goes out of scope. 170 // goes out of scope.
137 class ScopedRequestContextGetterForTesting { 171 class ScopedRequestContextGetterForTesting {
138 public: 172 public:
139 ScopedRequestContextGetterForTesting( 173 ScopedRequestContextGetterForTesting(
140 TestingBrowserProcess* testing_browser_process) 174 TestingBrowserProcess* testing_browser_process)
141 : testing_browser_process_(testing_browser_process) { 175 : testing_browser_process_(testing_browser_process) {
142 context_getter_ = new net::TestURLRequestContextGetter( 176 context_getter_ = new net::TestURLRequestContextGetter(
143 content::BrowserThread::GetMessageLoopProxyForThread( 177 content::BrowserThread::GetMessageLoopProxyForThread(
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 const GURL absolute_url = test_server_.GetURL(request.relative_url); 337 const GURL absolute_url = test_server_.GetURL(request.relative_url);
304 if (absolute_url.path() == 338 if (absolute_url.path() ==
305 // This is an upload URL of the root directory. 339 // This is an upload URL of the root directory.
306 "/feeds/upload/create-session/default/private/full" || 340 "/feeds/upload/create-session/default/private/full" ||
307 absolute_url.path() == 341 absolute_url.path() ==
308 // This is an upload URL of an existing file. 342 // This is an upload URL of an existing file.
309 "/feeds/upload/create-session/default/private/full/file:foo") { 343 "/feeds/upload/create-session/default/private/full/file:foo") {
310 scoped_ptr<test_server::HttpResponse> http_response( 344 scoped_ptr<test_server::HttpResponse> http_response(
311 new test_server::HttpResponse); 345 new test_server::HttpResponse);
312 http_response->set_code(test_server::SUCCESS); 346 http_response->set_code(test_server::SUCCESS);
313 http_response->AddCustomHeader("Location", 347 GURL upload_url;
314 test_server_.GetURL("/upload_url").spec()); 348 // POST is used for a new file, and PUT is used for an existing file.
349 if (request.method == test_server::METHOD_POST) {
350 upload_url = test_server_.GetURL("/upload_new_file");
351 } else if (request.method == test_server::METHOD_PUT) {
352 upload_url = test_server_.GetURL("/upload_existing_file");
353 } else {
354 return scoped_ptr<test_server::HttpResponse>();
355 }
356 http_response->AddCustomHeader("Location", upload_url.spec());
315 return http_response.Pass(); 357 return http_response.Pass();
316 } 358 }
317 359
318 return scoped_ptr<test_server::HttpResponse>(); 360 return scoped_ptr<test_server::HttpResponse>();
319 } 361 }
320 362
321 // Handles a request for uploading content. 363 // Handles a request for uploading content.
322 scoped_ptr<test_server::HttpResponse> HandleUploadRequest( 364 scoped_ptr<test_server::HttpResponse> HandleUploadRequest(
323 const test_server::HttpRequest& request) { 365 const test_server::HttpRequest& request) {
324 http_request_ = request; 366 http_request_ = request;
325 367
326 const GURL absolute_url = test_server_.GetURL(request.relative_url); 368 const GURL absolute_url = test_server_.GetURL(request.relative_url);
327 if (absolute_url.path() != "/upload_url") 369 if (absolute_url.path() != "/upload_new_file" &&
370 absolute_url.path() != "/upload_existing_file") {
328 return scoped_ptr<test_server::HttpResponse>(); 371 return scoped_ptr<test_server::HttpResponse>();
372 }
329 373
330 // TODO(satorux): We should create a correct entry for the uploaded file, 374 // TODO(satorux): We should create a correct entry for the uploaded file,
331 // but for now, just return entry.xml. 375 // but for now, just return entry.xml.
332 return CreateHttpResponseFromFile( 376 scoped_ptr<test_server::HttpResponse> response =
333 test_util::GetTestFilePath("gdata/entry.xml")); 377 CreateHttpResponseFromFile(
378 test_util::GetTestFilePath("gdata/entry.xml"));
379 // response.code() is set to SUCCESS. Change it to CREATED if it's a new
380 // file.
381 if (absolute_url.path() == "/upload_new_file")
382 response->set_code(test_server::CREATED);
383
384 // Check if the Content-Range header is present. This must be present if
385 // the request body is not empty.
386 if (!request.content.empty()) {
387 std::map<std::string, std::string>::const_iterator iter =
388 request.headers.find("Content-Range");
389 if (iter == request.headers.end())
390 return scoped_ptr<test_server::HttpResponse>();
391 int64 start_position = 0;
392 int64 end_position = 0;
393 int64 length = 0;
394 if (!ParseContentRangeHeader(iter->second,
395 &start_position,
396 &end_position,
397 &length)) {
398 return scoped_ptr<test_server::HttpResponse>();
399 }
400
401 // Add Range header to the response, based on the values of
402 // Content-Range header in the request.
403 response->AddCustomHeader(
404 "Range",
405 "bytes=" +
406 base::Int64ToString(start_position) + "-" +
407 base::Int64ToString(end_position));
408
409 // Change the code to RESUME_INCOMPLETE if upload is not complete.
410 if (end_position + 1 < length)
411 response->set_code(test_server::RESUME_INCOMPLETE);
412 }
413
414 return response.Pass();
334 } 415 }
335 416
336 MessageLoopForUI message_loop_; 417 MessageLoopForUI message_loop_;
337 content::TestBrowserThread ui_thread_; 418 content::TestBrowserThread ui_thread_;
338 content::TestBrowserThread file_thread_; 419 content::TestBrowserThread file_thread_;
339 content::TestBrowserThread io_thread_; 420 content::TestBrowserThread io_thread_;
340 test_server::HttpServer test_server_; 421 test_server::HttpServer test_server_;
341 scoped_ptr<TestingProfile> profile_; 422 scoped_ptr<TestingProfile> profile_;
342 OperationRegistry operation_registry_; 423 OperationRegistry operation_registry_;
343 scoped_ptr<GDataWapiUrlGenerator> url_generator_; 424 scoped_ptr<GDataWapiUrlGenerator> url_generator_;
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 &operation_registry_, 892 &operation_registry_,
812 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit, 893 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit,
813 &result_code, 894 &result_code,
814 &upload_url), 895 &upload_url),
815 initiate_params); 896 initiate_params);
816 897
817 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent); 898 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent);
818 MessageLoop::current()->Run(); 899 MessageLoop::current()->Run();
819 900
820 EXPECT_EQ(HTTP_SUCCESS, result_code); 901 EXPECT_EQ(HTTP_SUCCESS, result_code);
821 EXPECT_EQ(test_server_.GetURL("/upload_url"), upload_url); 902 EXPECT_EQ(test_server_.GetURL("/upload_new_file"), upload_url);
822 EXPECT_EQ(test_server::METHOD_POST, http_request_.method); 903 EXPECT_EQ(test_server::METHOD_POST, http_request_.method);
823 // convert=false should be passed as files should be uploaded as-is. 904 // convert=false should be passed as files should be uploaded as-is.
824 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false", 905 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false",
825 http_request_.relative_url); 906 http_request_.relative_url);
826 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); 907 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]);
827 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]); 908 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]);
828 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), 909 EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
829 http_request_.headers["X-Upload-Content-Length"]); 910 http_request_.headers["X-Upload-Content-Length"]);
830 911
831 EXPECT_TRUE(http_request_.has_content); 912 EXPECT_TRUE(http_request_.has_content);
(...skipping 23 matching lines...) Expand all
855 new ResumeUploadOperation( 936 new ResumeUploadOperation(
856 &operation_registry_, 937 &operation_registry_,
857 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit, 938 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit,
858 &response, 939 &response,
859 &new_entry), 940 &new_entry),
860 resume_params); 941 resume_params);
861 942
862 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent); 943 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent);
863 MessageLoop::current()->Run(); 944 MessageLoop::current()->Run();
864 945
865 EXPECT_EQ(HTTP_SUCCESS, result_code);
866 // METHOD_PUT should be used to upload data. 946 // METHOD_PUT should be used to upload data.
867 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); 947 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
868 // Request should go to the upload URL. 948 // Request should go to the upload URL.
869 EXPECT_EQ(upload_url.path(), http_request_.relative_url); 949 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
870 // Content-Range header should be added. 950 // Content-Range header should be added.
871 EXPECT_EQ("bytes 0-" + 951 EXPECT_EQ("bytes 0-" +
872 base::Int64ToString(kUploadContent.size() -1) + "/" + 952 base::Int64ToString(kUploadContent.size() -1) + "/" +
873 base::Int64ToString(kUploadContent.size()), 953 base::Int64ToString(kUploadContent.size()),
874 http_request_.headers["Content-Range"]); 954 http_request_.headers["Content-Range"]);
875 // The upload content should be set in the HTTP request. 955 // The upload content should be set in the HTTP request.
876 EXPECT_TRUE(http_request_.has_content); 956 EXPECT_TRUE(http_request_.has_content);
877 EXPECT_EQ(kUploadContent, http_request_.content); 957 EXPECT_EQ(kUploadContent, http_request_.content);
958
959 // Check the response.
960 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file
961 // The start and end positions should be set to -1, if an upload is complete.
962 EXPECT_EQ(-1, response.start_range_received);
963 EXPECT_EQ(-1, response.end_range_received);
878 } 964 }
879 965
880 // This test exercises InitiateUploadOperation and ResumeUploadOperation for 966 // This test exercises InitiateUploadOperation and ResumeUploadOperation for
967 // a scenario of uploading a new *large* file, which requires mutiple requests
968 // of ResumeUploadOperation.
969 TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
970 const size_t kMaxNumBytes = 10;
971 // This is big enough to cause multiple requests of ResumeUploadOperation
972 // as we are gonig to send at most kMaxNumBytes at a time.
973 const std::string kUploadContent(kMaxNumBytes + 1, 'a');
974 GDataErrorCode result_code = GDATA_OTHER_ERROR;
975 GURL upload_url;
976
977 // 1) Get the upload URL for uploading a new file.
978 InitiateUploadParams initiate_params(
979 UPLOAD_NEW_FILE,
980 "New file",
981 "text/plain",
982 kUploadContent.size(),
983 test_server_.GetURL("/feeds/upload/create-session/default/private/full"),
984 FilePath::FromUTF8Unsafe("drive/newfile.txt"));
985
986 InitiateUploadOperation* initiate_operation =
987 new InitiateUploadOperation(
988 &operation_registry_,
989 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit,
990 &result_code,
991 &upload_url),
992 initiate_params);
993
994 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent);
995 MessageLoop::current()->Run();
996
997 EXPECT_EQ(HTTP_SUCCESS, result_code);
998 EXPECT_EQ(test_server_.GetURL("/upload_new_file"), upload_url);
999 EXPECT_EQ(test_server::METHOD_POST, http_request_.method);
1000 // convert=false should be passed as files should be uploaded as-is.
1001 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false",
1002 http_request_.relative_url);
1003 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]);
1004 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]);
1005 EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
1006 http_request_.headers["X-Upload-Content-Length"]);
1007
1008 EXPECT_TRUE(http_request_.has_content);
1009 EXPECT_EQ("<?xml version=\"1.0\"?>\n"
1010 "<entry xmlns=\"http://www.w3.org/2005/Atom\" "
1011 "xmlns:docs=\"http://schemas.google.com/docs/2007\">\n"
1012 " <title>New file</title>\n"
1013 "</entry>\n",
1014 http_request_.content);
1015
1016 // 2) Upload the content to the upload URL with multiple requests.
1017 size_t num_bytes_consumed = 0;
1018 for (size_t start_position = 0; start_position < kUploadContent.size();
1019 start_position += kMaxNumBytes) {
1020 SCOPED_TRACE(testing::Message("start_position: ") << start_position);
1021
1022 // The payload is at most kMaxNumBytes.
1023 const size_t remaining_size = kUploadContent.size() - start_position;
1024 const std::string payload = kUploadContent.substr(
1025 start_position, std::min(kMaxNumBytes, remaining_size));
1026 num_bytes_consumed += payload.size();
1027 // The end position is inclusive, hence -1.
1028 const size_t end_position = start_position + payload.size() - 1;
1029
1030 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(payload);
1031 ResumeUploadParams resume_params(
1032 UPLOAD_NEW_FILE,
1033 start_position, // start_range
1034 end_position, // end_range
1035 kUploadContent.size(), // content_length,
1036 "text/plain", // content_type
1037 buffer,
1038 upload_url,
1039 FilePath::FromUTF8Unsafe("drive/newfile.txt"));
1040
1041 ResumeUploadResponse response;
1042 scoped_ptr<DocumentEntry> new_entry;
1043
1044 ResumeUploadOperation* resume_operation =
1045 new ResumeUploadOperation(
1046 &operation_registry_,
1047 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit,
1048 &response,
1049 &new_entry),
1050 resume_params);
1051
1052 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent);
1053 MessageLoop::current()->Run();
1054
1055 // METHOD_PUT should be used to upload data.
1056 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
1057 // Request should go to the upload URL.
1058 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1059 // Content-Range header should be added.
1060 EXPECT_EQ("bytes " +
1061 base::Int64ToString(start_position) + "-" +
1062 base::Int64ToString(end_position) + "/" +
1063 base::Int64ToString(kUploadContent.size()),
1064 http_request_.headers["Content-Range"]);
1065 // The upload content should be set in the HTTP request.
1066 EXPECT_TRUE(http_request_.has_content);
1067 EXPECT_EQ(payload, http_request_.content);
1068
1069 // Check the response.
1070 if (payload.size() == remaining_size) {
1071 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file.
1072 // The start and end positions should be set to -1, if an upload is
1073 // complete.
1074 EXPECT_EQ(-1, response.start_range_received);
1075 EXPECT_EQ(-1, response.end_range_received);
1076 } else {
1077 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1078 EXPECT_EQ(static_cast<int64>(start_position),
1079 response.start_range_received);
1080 EXPECT_EQ(static_cast<int64>(end_position),
1081 response.end_range_received);
1082 }
1083 }
1084
1085 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed);
1086 }
1087
1088 // This test exercises InitiateUploadOperation and ResumeUploadOperation for
881 // a scenario of uploading a new *empty* file. 1089 // a scenario of uploading a new *empty* file.
882 // 1090 //
883 // The test is almost identical to UploadNewFile. The only difference is the 1091 // The test is almost identical to UploadNewFile. The only difference is the
884 // expectation for the Content-Range header. 1092 // expectation for the Content-Range header.
885 TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) { 1093 TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) {
886 const std::string kUploadContent = ""; 1094 const std::string kUploadContent = "";
887 GDataErrorCode result_code = GDATA_OTHER_ERROR; 1095 GDataErrorCode result_code = GDATA_OTHER_ERROR;
888 GURL upload_url; 1096 GURL upload_url;
889 1097
890 // 1) Get the upload URL for uploading a new file. 1098 // 1) Get the upload URL for uploading a new file.
(...skipping 10 matching lines...) Expand all
901 &operation_registry_, 1109 &operation_registry_,
902 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit, 1110 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit,
903 &result_code, 1111 &result_code,
904 &upload_url), 1112 &upload_url),
905 initiate_params); 1113 initiate_params);
906 1114
907 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent); 1115 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent);
908 MessageLoop::current()->Run(); 1116 MessageLoop::current()->Run();
909 1117
910 EXPECT_EQ(HTTP_SUCCESS, result_code); 1118 EXPECT_EQ(HTTP_SUCCESS, result_code);
911 EXPECT_EQ(test_server_.GetURL("/upload_url"), upload_url); 1119 EXPECT_EQ(test_server_.GetURL("/upload_new_file"), upload_url);
912 EXPECT_EQ(test_server::METHOD_POST, http_request_.method); 1120 EXPECT_EQ(test_server::METHOD_POST, http_request_.method);
913 // convert=false should be passed as files should be uploaded as-is. 1121 // convert=false should be passed as files should be uploaded as-is.
914 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false", 1122 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false",
915 http_request_.relative_url); 1123 http_request_.relative_url);
916 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); 1124 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]);
917 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]); 1125 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]);
918 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), 1126 EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
919 http_request_.headers["X-Upload-Content-Length"]); 1127 http_request_.headers["X-Upload-Content-Length"]);
920 1128
921 EXPECT_TRUE(http_request_.has_content); 1129 EXPECT_TRUE(http_request_.has_content);
(...skipping 23 matching lines...) Expand all
945 new ResumeUploadOperation( 1153 new ResumeUploadOperation(
946 &operation_registry_, 1154 &operation_registry_,
947 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit, 1155 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit,
948 &response, 1156 &response,
949 &new_entry), 1157 &new_entry),
950 resume_params); 1158 resume_params);
951 1159
952 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent); 1160 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent);
953 MessageLoop::current()->Run(); 1161 MessageLoop::current()->Run();
954 1162
955 EXPECT_EQ(HTTP_SUCCESS, result_code);
956 // METHOD_PUT should be used to upload data. 1163 // METHOD_PUT should be used to upload data.
957 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); 1164 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
958 // Request should go to the upload URL. 1165 // Request should go to the upload URL.
959 EXPECT_EQ(upload_url.path(), http_request_.relative_url); 1166 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
960 // Content-Range header should not exit if the content is empty. 1167 // Content-Range header should not exit if the content is empty.
961 // We should not generate the header with an invalid value "bytes 0--1/0". 1168 // We should not generate the header with an invalid value "bytes 0--1/0".
962 EXPECT_EQ(0U, http_request_.headers.count("Content-Range")); 1169 EXPECT_EQ(0U, http_request_.headers.count("Content-Range"));
963 // The upload content should be set in the HTTP request. 1170 // The upload content should be set in the HTTP request.
964 EXPECT_TRUE(http_request_.has_content); 1171 EXPECT_TRUE(http_request_.has_content);
965 EXPECT_EQ(kUploadContent, http_request_.content); 1172 EXPECT_EQ(kUploadContent, http_request_.content);
1173
1174 // Check the response.
1175 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file.
1176 // The start and end positions should be set to -1, if an upload is complete.
1177 EXPECT_EQ(-1, response.start_range_received);
1178 EXPECT_EQ(-1, response.end_range_received);
966 } 1179 }
967 1180
968 // This test exercises InitiateUploadOperation and ResumeUploadOperation for 1181 // This test exercises InitiateUploadOperation and ResumeUploadOperation for
969 // a scenario of updating an existing file. 1182 // a scenario of updating an existing file.
970 TEST_F(GDataWapiOperationsTest, UploadExistingFile) { 1183 TEST_F(GDataWapiOperationsTest, UploadExistingFile) {
971 const std::string kUploadContent = "hello"; 1184 const std::string kUploadContent = "hello";
972 GDataErrorCode result_code = GDATA_OTHER_ERROR; 1185 GDataErrorCode result_code = GDATA_OTHER_ERROR;
973 GURL upload_url; 1186 GURL upload_url;
974 1187
975 // 1) Get the upload URL for uploading an existing file. 1188 // 1) Get the upload URL for uploading an existing file.
(...skipping 11 matching lines...) Expand all
987 &operation_registry_, 1200 &operation_registry_,
988 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit, 1201 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit,
989 &result_code, 1202 &result_code,
990 &upload_url), 1203 &upload_url),
991 initiate_params); 1204 initiate_params);
992 1205
993 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent); 1206 initiate_operation->Start(kTestGDataAuthToken, kTestUserAgent);
994 MessageLoop::current()->Run(); 1207 MessageLoop::current()->Run();
995 1208
996 EXPECT_EQ(HTTP_SUCCESS, result_code); 1209 EXPECT_EQ(HTTP_SUCCESS, result_code);
997 EXPECT_EQ(test_server_.GetURL("/upload_url"), upload_url); 1210 EXPECT_EQ(test_server_.GetURL("/upload_existing_file"), upload_url);
998 // For updating an existing file, METHOD_PUT should be used. 1211 // For updating an existing file, METHOD_PUT should be used.
999 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); 1212 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
1000 // convert=false should be passed as files should be uploaded as-is. 1213 // convert=false should be passed as files should be uploaded as-is.
1001 EXPECT_EQ("/feeds/upload/create-session/default/private/full/file:foo" 1214 EXPECT_EQ("/feeds/upload/create-session/default/private/full/file:foo"
1002 "?convert=false", 1215 "?convert=false",
1003 http_request_.relative_url); 1216 http_request_.relative_url);
1004 // Even though the body is empty, the content type should be set to 1217 // Even though the body is empty, the content type should be set to
1005 // "text/plain". 1218 // "text/plain".
1006 EXPECT_EQ("text/plain", http_request_.headers["Content-Type"]); 1219 EXPECT_EQ("text/plain", http_request_.headers["Content-Type"]);
1007 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); 1220 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]);
(...skipping 23 matching lines...) Expand all
1031 new ResumeUploadOperation( 1244 new ResumeUploadOperation(
1032 &operation_registry_, 1245 &operation_registry_,
1033 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit, 1246 base::Bind(&CopyResultFromResumeUploadCallbackAndQuit,
1034 &response, 1247 &response,
1035 &new_entry), 1248 &new_entry),
1036 resume_params); 1249 resume_params);
1037 1250
1038 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent); 1251 resume_operation->Start(kTestGDataAuthToken, kTestUserAgent);
1039 MessageLoop::current()->Run(); 1252 MessageLoop::current()->Run();
1040 1253
1041 EXPECT_EQ(HTTP_SUCCESS, result_code);
1042 // METHOD_PUT should be used to upload data. 1254 // METHOD_PUT should be used to upload data.
1043 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); 1255 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
1044 // Request should go to the upload URL. 1256 // Request should go to the upload URL.
1045 EXPECT_EQ(upload_url.path(), http_request_.relative_url); 1257 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1046 // Content-Range header should be added. 1258 // Content-Range header should be added.
1047 EXPECT_EQ("bytes 0-" + 1259 EXPECT_EQ("bytes 0-" +
1048 base::Int64ToString(kUploadContent.size() -1) + "/" + 1260 base::Int64ToString(kUploadContent.size() -1) + "/" +
1049 base::Int64ToString(kUploadContent.size()), 1261 base::Int64ToString(kUploadContent.size()),
1050 http_request_.headers["Content-Range"]); 1262 http_request_.headers["Content-Range"]);
1051 // The upload content should be set in the HTTP request. 1263 // The upload content should be set in the HTTP request.
1052 EXPECT_TRUE(http_request_.has_content); 1264 EXPECT_TRUE(http_request_.has_content);
1053 EXPECT_EQ(kUploadContent, http_request_.content); 1265 EXPECT_EQ(kUploadContent, http_request_.content);
1266
1267 // Check the response.
1268 EXPECT_EQ(HTTP_SUCCESS, response.code); // Because it's an existing file.
1269 // The start and end positions should be set to -1, if an upload is complete.
1270 EXPECT_EQ(-1, response.start_range_received);
1271 EXPECT_EQ(-1, response.end_range_received);
1054 } 1272 }
1055 1273
1056 } // namespace google_apis 1274 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/gdata_wapi_operations.h ('k') | chrome/browser/google_apis/test_server/http_response.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698