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

Unified 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: Fix mock server's behavior, as well as empty Range header handling. 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 26397d9d35e9337fefcb1fa5c76aa04e66845132..244b539e3224248fad7ad92f04788b020b8fdd26 100644
--- a/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
+++ b/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
@@ -127,6 +127,9 @@ class GDataWapiOperationsTest : public testing::Test {
url_generator_.reset(new GDataWapiUrlGenerator(
test_util::GetBaseUrlForTesting(test_server_.port())));
+
+ received_bytes_ = 0;
+ content_length_ = 0;
}
virtual void TearDown() OVERRIDE {
@@ -225,6 +228,7 @@ class GDataWapiOperationsTest : public testing::Test {
scoped_ptr<test_server::HttpResponse> http_response(
new test_server::HttpResponse);
+ // Check an ETag.
std::map<std::string, std::string>::const_iterator found =
request.headers.find("If-Match");
if (found != request.headers.end() &&
@@ -234,6 +238,15 @@ class GDataWapiOperationsTest : public testing::Test {
return http_response.Pass();
}
+ // Check if the X-Upload-Content-Length is present. If yes, store the
+ // length of the file.
+ found = request.headers.find("X-Upload-Content-Length");
+ if (found == request.headers.end() ||
+ !base::StringToInt64(found->second, &content_length_)) {
+ return scoped_ptr<test_server::HttpResponse>();
+ }
+ received_bytes_ = 0;
+
http_response->set_code(test_server::SUCCESS);
GURL upload_url;
// POST is used for a new file, and PUT is used for an existing file.
@@ -279,29 +292,34 @@ class GDataWapiOperationsTest : public testing::Test {
request.headers.find("Content-Range");
if (iter == request.headers.end())
return scoped_ptr<test_server::HttpResponse>();
+ int64 length = 0;
int64 start_position = 0;
int64 end_position = 0;
- int64 length = 0;
if (!ParseContentRangeHeader(iter->second,
&start_position,
&end_position,
&length)) {
return scoped_ptr<test_server::HttpResponse>();
}
+ EXPECT_EQ(start_position, received_bytes_);
+ EXPECT_EQ(length, content_length_);
+ // end_position is inclusive, but so +1 to change the range to byte size.
+ received_bytes_ = end_position + 1;
+ }
- // Add Range header to the response, based on the values of
- // Content-Range header in the request.
+ // Add Range header to the response, based on the values of
+ // Content-Range header in the request.
+ // The header is annotated only when at least one byte is received.
+ if (received_bytes_ > 0) {
response->AddCustomHeader(
"Range",
- "bytes=" +
- base::Int64ToString(start_position) + "-" +
- base::Int64ToString(end_position));
-
- // Change the code to RESUME_INCOMPLETE if upload is not complete.
- if (end_position + 1 < length)
- response->set_code(test_server::RESUME_INCOMPLETE);
+ "bytes=0-" + base::Int64ToString(received_bytes_ - 1));
kinaba 2013/02/08 10:19:43 Is 0- the expected response when we have non-empty
hidehiko 2013/02/08 11:53:10 The actual server returns 0-end if there is (one o
}
+ // Change the code to RESUME_INCOMPLETE if upload is not complete.
+ if (received_bytes_ < content_length_)
+ response->set_code(test_server::RESUME_INCOMPLETE);
+
return response.Pass();
}
@@ -315,6 +333,14 @@ class GDataWapiOperationsTest : public testing::Test {
scoped_ptr<GDataWapiUrlGenerator> url_generator_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
+ // These fields are used to keep the current upload state during a
+ // test case. These values are updated by the request from
+ // ResumeUploadOperation, and used to construct the response for
+ // both ResumeUploadOperation and GetUploadStatusOperation, to emulate
+ // the WAPI server.
+ int64 received_bytes_;
+ int64 content_length_;
+
// The incoming HTTP request is saved so tests can verify the request
// parameters like HTTP method (ex. some operations should use DELETE
// instead of GET).
@@ -866,7 +892,8 @@ TEST_F(GDataWapiOperationsTest, UploadNewFile) {
// This test exercises InitiateUploadOperation and ResumeUploadOperation for
// a scenario of uploading a new *large* file, which requires multiple requests
-// of ResumeUploadOperation.
+// of ResumeUploadOperation. GetUploadOperation is also tested in this test
+// case.
TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
const size_t kMaxNumBytes = 10;
// This is big enough to cause multiple requests of ResumeUploadOperation
@@ -918,7 +945,44 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
"</entry>\n",
http_request_.content);
- // 2) Upload the content to the upload URL with multiple requests.
+ // 2) Before sending any data, check the current status.
hidehiko 2013/02/08 09:17:00 Hmm, the diff looks not good. What I did is insert
+ // This is an edge case test for GetUploadStatusOperation
+ // (UploadRangeOperationBase).
+ {
+ // 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(0, response.start_position_received);
+ EXPECT_EQ(0, response.end_position_received);
+ }
+
+ // 3) 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) {
@@ -980,13 +1044,49 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) {
// complete.
EXPECT_EQ(-1, response.start_position_received);
EXPECT_EQ(-1, response.end_position_received);
- } else {
- 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);
+ // 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);
« no previous file with comments | « chrome/browser/google_apis/gdata_wapi_operations.cc ('k') | chrome/browser/google_apis/gdata_wapi_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698