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

Unified Diff: chrome/browser/google_apis/gdata_wapi_operations_unittest.cc

Issue 12209035: Implement retry flow on DriveUploader. (Closed) Base URL: http://git.chromium.org/chromium/src.git@b148632_wapi_get_upload_status_operation_impl
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
diff --git a/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc b/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
index 8dae695de0824c2c8be839d17a1e65b92e7de507..c1b10094de3edf4b07d25d67dd9e4ea6e3824ac6 100644
--- a/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
+++ b/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
@@ -841,6 +841,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewFile) {
kUploadContent.size(), // content_length,
"text/plain", // content_type
buffer,
+ 0, // buf_offset
upload_url,
FilePath::FromUTF8Unsafe("drive/newfile.txt"));
@@ -956,6 +957,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
kUploadContent.size(), // content_length,
"text/plain", // content_type
buffer,
+ 0, // buf_offset
upload_url,
FilePath::FromUTF8Unsafe("drive/newfile.txt"));
@@ -1106,6 +1108,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) {
kUploadContent.size(), // content_length,
"text/plain", // content_type
buffer,
+ 0, // buf_offset
upload_url,
FilePath::FromUTF8Unsafe("drive/newfile.txt"));
@@ -1203,6 +1206,7 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFile) {
kUploadContent.size(), // content_length,
"text/plain", // content_type
buffer,
+ 0, // buf_offset
upload_url,
FilePath::FromUTF8Unsafe("drive/existingfile.txt"));
@@ -1302,6 +1306,7 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFileWithETag) {
kUploadContent.size(), // content_length,
"text/plain", // content_type
buffer,
+ 0, // buf_offset
upload_url,
FilePath::FromUTF8Unsafe("drive/existingfile.txt"));
@@ -1392,4 +1397,171 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFileWithETagConflict) {
EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]);
}
+TEST_F(GDataWapiOperationsTest, ResumeUploadOperationBufOffset) {
+ const size_t kMaxNumBytes = 2;
+ // This is big enough to cause multiple requests of ResumeUploadOperation
+ // as we are going to send at most kMaxNumBytes at a time.
+ const std::string kUploadContent = "abcdefghi";
+ GDataErrorCode result_code = GDATA_OTHER_ERROR;
+ GURL upload_url;
+
+ // 1) Get the upload URL for uploading a new file.
+ InitiateUploadParams initiate_params(
+ UPLOAD_NEW_FILE,
+ "New file",
+ "text/plain",
+ kUploadContent.size(),
+ test_server_.GetURL("/feeds/upload/create-session/default/private/full"),
+ FilePath::FromUTF8Unsafe("drive/newfile.txt"),
+ "" /* etag */);
+
+ InitiateUploadOperation* initiate_operation = new InitiateUploadOperation(
+ &operation_registry_,
+ request_context_getter_.get(),
+ base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit,
+ &result_code,
+ &upload_url),
+ initiate_params);
+
+ initiate_operation->Start(
+ kTestGDataAuthToken, kTestUserAgent,
+ base::Bind(&test_util::DoNothingForReAuthenticateCallback));
+ MessageLoop::current()->Run();
+
+ EXPECT_EQ(HTTP_SUCCESS, result_code);
+ EXPECT_EQ(test_server_.GetURL("/upload_new_file"), upload_url);
+ EXPECT_EQ(test_server::METHOD_POST, http_request_.method);
+ // convert=false should be passed as files should be uploaded as-is.
+ EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false"
+ "&v=3&alt=json",
+ http_request_.relative_url);
+ EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]);
+ EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]);
+ EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
+ http_request_.headers["X-Upload-Content-Length"]);
+
+ EXPECT_TRUE(http_request_.has_content);
+ EXPECT_EQ("<?xml version=\"1.0\"?>\n"
+ "<entry xmlns=\"http://www.w3.org/2005/Atom\" "
+ "xmlns:docs=\"http://schemas.google.com/docs/2007\">\n"
+ " <title>New file</title>\n"
+ "</entry>\n",
+ http_request_.content);
+
+ // Keep only one buffer instance and reuse it. The sent data range should
+ // be specified via buf_offset.
+ scoped_refptr<net::IOBuffer> buffer =
+ new net::StringIOBuffer(kUploadContent);
+
+ // 2) Upload the content to the upload URL with multiple requests.
+ size_t num_bytes_consumed = 0;
+ for (size_t start_position = 0; start_position < kUploadContent.size();
+ start_position += kMaxNumBytes) {
+ SCOPED_TRACE(testing::Message("start_position: ") << start_position);
+
+ // The payload is at most kMaxNumBytes.
+ const size_t remaining_size = kUploadContent.size() - start_position;
+ const std::string payload = kUploadContent.substr(
+ start_position, std::min(kMaxNumBytes, remaining_size));
+ num_bytes_consumed += payload.size();
+ // The end position is exclusive.
+ const size_t end_position = start_position + payload.size();
+
+ ResumeUploadParams resume_params(
+ UPLOAD_NEW_FILE,
+ start_position,
+ end_position,
+ kUploadContent.size(), // content_length,
+ "text/plain", // content_type
+ buffer,
+ start_position, // buf_offset
+ upload_url,
+ FilePath::FromUTF8Unsafe("drive/newfile.txt"));
+
+ UploadRangeResponse response;
+ scoped_ptr<ResourceEntry> new_entry;
+
+ ResumeUploadOperation* resume_operation = new ResumeUploadOperation(
+ &operation_registry_,
+ request_context_getter_.get(),
+ base::Bind(&CopyResultFromUploadRangeCallbackAndQuit,
+ &response,
+ &new_entry),
+ resume_params);
+
+ resume_operation->Start(
+ kTestGDataAuthToken, kTestUserAgent,
+ base::Bind(&test_util::DoNothingForReAuthenticateCallback));
+ MessageLoop::current()->Run();
+
+ // METHOD_PUT should be used to upload data.
+ EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
+ // Request should go to the upload URL.
+ EXPECT_EQ(upload_url.path(), http_request_.relative_url);
+ // Content-Range header should be added.
+ EXPECT_EQ("bytes " +
+ base::Int64ToString(start_position) + "-" +
+ base::Int64ToString(end_position - 1) + "/" +
+ base::Int64ToString(kUploadContent.size()),
+ http_request_.headers["Content-Range"]);
+ // The upload content should be set in the HTTP request.
+ EXPECT_TRUE(http_request_.has_content);
+ EXPECT_EQ(payload, http_request_.content);
+
+ // Check the response.
+ if (payload.size() == remaining_size) {
+ EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file.
+ // The start and end positions should be set to -1, if an upload is
+ // complete.
+ EXPECT_EQ(-1, response.start_position_received);
+ EXPECT_EQ(-1, response.end_position_received);
+ // The upload process is completed, so exit from the loop.
+ break;
+ }
+
+ EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
+ EXPECT_EQ(static_cast<int64>(start_position),
+ response.start_position_received);
+ EXPECT_EQ(static_cast<int64>(end_position),
+ response.end_position_received);
+
+ // Check the response by GetUploadStatusOperation.
+ GetUploadStatusOperation* get_upload_status_operation =
+ new GetUploadStatusOperation(
+ &operation_registry_,
+ request_context_getter_.get(),
+ base::Bind(&CopyResultFromUploadRangeCallbackAndQuit,
+ &response,
+ &new_entry),
+ UPLOAD_NEW_FILE,
+ FilePath::FromUTF8Unsafe("drive/newfile.txt"),
+ upload_url,
+ kUploadContent.size());
+ get_upload_status_operation->Start(
+ kTestGDataAuthToken, kTestUserAgent,
+ base::Bind(&test_util::DoNothingForReAuthenticateCallback));
+ MessageLoop::current()->Run();
+
+ // METHOD_PUT should be used to upload data.
+ EXPECT_EQ(test_server::METHOD_PUT, http_request_.method);
+ // Request should go to the upload URL.
+ EXPECT_EQ(upload_url.path(), http_request_.relative_url);
+ // Content-Range header should be added.
+ EXPECT_EQ("bytes */" + base::Int64ToString(kUploadContent.size()),
+ http_request_.headers["Content-Range"]);
+ EXPECT_TRUE(http_request_.has_content);
+ EXPECT_TRUE(http_request_.content.empty());
+
+ // Check the response.
+ EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
+ EXPECT_EQ(static_cast<int64>(start_position),
+ response.start_position_received);
+ EXPECT_EQ(static_cast<int64>(end_position),
+ response.end_position_received);
+ }
+
+ EXPECT_EQ(kUploadContent.size(), num_bytes_consumed);
+}
+
+
} // namespace google_apis

Powered by Google App Engine
This is Rietveld 408576698