Enable ServiceWorkerPerfTest (service_worker.service_worker)
[chromium-blink-merge.git] / google_apis / drive / drive_api_requests_unittest.cc
blob3f56e4e0de1022393a5a3a916f1606b1652f0b0a
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_reader.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h"
14 #include "google_apis/drive/drive_api_parser.h"
15 #include "google_apis/drive/drive_api_requests.h"
16 #include "google_apis/drive/drive_api_url_generator.h"
17 #include "google_apis/drive/dummy_auth_service.h"
18 #include "google_apis/drive/request_sender.h"
19 #include "google_apis/drive/test_util.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/test/embedded_test_server/http_request.h"
22 #include "net/test/embedded_test_server/http_response.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace google_apis {
28 namespace {
30 const char kTestETag[] = "test_etag";
31 const char kTestUserAgent[] = "test-user-agent";
33 const char kTestChildrenResponse[] =
34 "{\n"
35 "\"kind\": \"drive#childReference\",\n"
36 "\"id\": \"resource_id\",\n"
37 "\"selfLink\": \"self_link\",\n"
38 "\"childLink\": \"child_link\",\n"
39 "}\n";
41 const char kTestPermissionResponse[] =
42 "{\n"
43 "\"kind\": \"drive#permission\",\n"
44 "\"id\": \"resource_id\",\n"
45 "\"selfLink\": \"self_link\",\n"
46 "}\n";
48 const char kTestUploadExistingFilePath[] = "/upload/existingfile/path";
49 const char kTestUploadNewFilePath[] = "/upload/newfile/path";
50 const char kTestDownloadPathPrefix[] = "/download/";
52 // Used as a GetContentCallback.
53 void AppendContent(std::string* out,
54 DriveApiErrorCode error,
55 scoped_ptr<std::string> content) {
56 EXPECT_EQ(HTTP_SUCCESS, error);
57 out->append(*content);
60 } // namespace
62 class DriveApiRequestsTest : public testing::Test {
63 public:
64 DriveApiRequestsTest() {
67 void SetUp() override {
68 request_context_getter_ = new net::TestURLRequestContextGetter(
69 message_loop_.message_loop_proxy());
71 request_sender_.reset(new RequestSender(new DummyAuthService,
72 request_context_getter_.get(),
73 message_loop_.message_loop_proxy(),
74 kTestUserAgent));
76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
78 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
79 test_server_.RegisterRequestHandler(
80 base::Bind(&DriveApiRequestsTest::HandleChildrenDeleteRequest,
81 base::Unretained(this)));
82 test_server_.RegisterRequestHandler(
83 base::Bind(&DriveApiRequestsTest::HandleDataFileRequest,
84 base::Unretained(this)));
85 test_server_.RegisterRequestHandler(
86 base::Bind(&DriveApiRequestsTest::HandleDeleteRequest,
87 base::Unretained(this)));
88 test_server_.RegisterRequestHandler(
89 base::Bind(&DriveApiRequestsTest::HandlePreconditionFailedRequest,
90 base::Unretained(this)));
91 test_server_.RegisterRequestHandler(
92 base::Bind(&DriveApiRequestsTest::HandleResumeUploadRequest,
93 base::Unretained(this)));
94 test_server_.RegisterRequestHandler(
95 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest,
96 base::Unretained(this)));
97 test_server_.RegisterRequestHandler(
98 base::Bind(&DriveApiRequestsTest::HandleContentResponse,
99 base::Unretained(this)));
100 test_server_.RegisterRequestHandler(
101 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest,
102 base::Unretained(this)));
104 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port());
105 url_generator_.reset(new DriveApiUrlGenerator(
106 test_base_url, test_base_url.Resolve(kTestDownloadPathPrefix)));
108 // Reset the server's expected behavior just in case.
109 ResetExpectedResponse();
110 received_bytes_ = 0;
111 content_length_ = 0;
113 // Testing properties used by multiple test cases.
114 drive::Property private_property;
115 private_property.set_key("key1");
116 private_property.set_value("value1");
118 drive::Property public_property;
119 public_property.set_visibility(drive::Property::VISIBILITY_PUBLIC);
120 public_property.set_key("key2");
121 public_property.set_value("value2");
123 testing_properties_.clear();
124 testing_properties_.push_back(private_property);
125 testing_properties_.push_back(public_property);
128 base::MessageLoopForIO message_loop_; // Test server needs IO thread.
129 net::test_server::EmbeddedTestServer test_server_;
130 scoped_ptr<RequestSender> request_sender_;
131 scoped_ptr<DriveApiUrlGenerator> url_generator_;
132 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
133 base::ScopedTempDir temp_dir_;
135 // This is a path to the file which contains expected response from
136 // the server. See also HandleDataFileRequest below.
137 base::FilePath expected_data_file_path_;
139 // This is a path string in the expected response header from the server
140 // for initiating file uploading.
141 std::string expected_upload_path_;
143 // This is a path to the file which contains expected response for
144 // PRECONDITION_FAILED response.
145 base::FilePath expected_precondition_failed_file_path_;
147 // These are content and its type in the expected response from the server.
148 // See also HandleContentResponse below.
149 std::string expected_content_type_;
150 std::string expected_content_;
152 // The incoming HTTP request is saved so tests can verify the request
153 // parameters like HTTP method (ex. some requests should use DELETE
154 // instead of GET).
155 net::test_server::HttpRequest http_request_;
157 // Testing properties used by multiple test cases.
158 drive::Properties testing_properties_;
160 private:
161 void ResetExpectedResponse() {
162 expected_data_file_path_.clear();
163 expected_upload_path_.clear();
164 expected_content_type_.clear();
165 expected_content_.clear();
168 // For "Children: delete" request, the server will return "204 No Content"
169 // response meaning "success".
170 scoped_ptr<net::test_server::HttpResponse> HandleChildrenDeleteRequest(
171 const net::test_server::HttpRequest& request) {
172 if (request.method != net::test_server::METHOD_DELETE ||
173 request.relative_url.find("/children/") == std::string::npos) {
174 // The request is not the "Children: delete" request. Delegate the
175 // processing to the next handler.
176 return scoped_ptr<net::test_server::HttpResponse>();
179 http_request_ = request;
181 // Return the response with just "204 No Content" status code.
182 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
183 new net::test_server::BasicHttpResponse);
184 http_response->set_code(net::HTTP_NO_CONTENT);
185 return http_response.Pass();
188 // Reads the data file of |expected_data_file_path_| and returns its content
189 // for the request.
190 // To use this method, it is necessary to set |expected_data_file_path_|
191 // to the appropriate file path before sending the request to the server.
192 scoped_ptr<net::test_server::HttpResponse> HandleDataFileRequest(
193 const net::test_server::HttpRequest& request) {
194 if (expected_data_file_path_.empty()) {
195 // The file is not specified. Delegate the processing to the next
196 // handler.
197 return scoped_ptr<net::test_server::HttpResponse>();
200 http_request_ = request;
202 // Return the response from the data file.
203 return test_util::CreateHttpResponseFromFile(expected_data_file_path_);
206 // Deletes the resource and returns no content with HTTP_NO_CONTENT status
207 // code.
208 scoped_ptr<net::test_server::HttpResponse> HandleDeleteRequest(
209 const net::test_server::HttpRequest& request) {
210 if (request.method != net::test_server::METHOD_DELETE ||
211 request.relative_url.find("/files/") == std::string::npos) {
212 // The file is not file deletion request. Delegate the processing to the
213 // next handler.
214 return scoped_ptr<net::test_server::HttpResponse>();
217 http_request_ = request;
219 scoped_ptr<net::test_server::BasicHttpResponse> response(
220 new net::test_server::BasicHttpResponse);
221 response->set_code(net::HTTP_NO_CONTENT);
223 return response.Pass();
226 // Returns PRECONDITION_FAILED response for ETag mismatching with error JSON
227 // content specified by |expected_precondition_failed_file_path_|.
228 // To use this method, it is necessary to set the variable to the appropriate
229 // file path before sending the request to the server.
230 scoped_ptr<net::test_server::HttpResponse> HandlePreconditionFailedRequest(
231 const net::test_server::HttpRequest& request) {
232 if (expected_precondition_failed_file_path_.empty()) {
233 // The file is not specified. Delegate the process to the next handler.
234 return scoped_ptr<net::test_server::HttpResponse>();
237 http_request_ = request;
239 scoped_ptr<net::test_server::BasicHttpResponse> response(
240 new net::test_server::BasicHttpResponse);
241 response->set_code(net::HTTP_PRECONDITION_FAILED);
243 std::string content;
244 if (base::ReadFileToString(expected_precondition_failed_file_path_,
245 &content)) {
246 response->set_content(content);
247 response->set_content_type("application/json");
250 return response.Pass();
253 // Returns the response based on set expected upload url.
254 // The response contains the url in its "Location: " header. Also, it doesn't
255 // have any content.
256 // To use this method, it is necessary to set |expected_upload_path_|
257 // to the string representation of the url to be returned.
258 scoped_ptr<net::test_server::HttpResponse> HandleInitiateUploadRequest(
259 const net::test_server::HttpRequest& request) {
260 if (request.relative_url == expected_upload_path_ ||
261 expected_upload_path_.empty()) {
262 // The request is for resume uploading or the expected upload url is not
263 // set. Delegate the processing to the next handler.
264 return scoped_ptr<net::test_server::HttpResponse>();
267 http_request_ = request;
269 scoped_ptr<net::test_server::BasicHttpResponse> response(
270 new net::test_server::BasicHttpResponse);
272 // Check if the X-Upload-Content-Length is present. If yes, store the
273 // length of the file.
274 std::map<std::string, std::string>::const_iterator found =
275 request.headers.find("X-Upload-Content-Length");
276 if (found == request.headers.end() ||
277 !base::StringToInt64(found->second, &content_length_)) {
278 return scoped_ptr<net::test_server::HttpResponse>();
280 received_bytes_ = 0;
282 response->set_code(net::HTTP_OK);
283 response->AddCustomHeader(
284 "Location",
285 test_server_.base_url().Resolve(expected_upload_path_).spec());
286 return response.Pass();
289 scoped_ptr<net::test_server::HttpResponse> HandleResumeUploadRequest(
290 const net::test_server::HttpRequest& request) {
291 if (request.relative_url != expected_upload_path_) {
292 // The request path is different from the expected path for uploading.
293 // Delegate the processing to the next handler.
294 return scoped_ptr<net::test_server::HttpResponse>();
297 http_request_ = request;
299 if (!request.content.empty()) {
300 std::map<std::string, std::string>::const_iterator iter =
301 request.headers.find("Content-Range");
302 if (iter == request.headers.end()) {
303 // The range must be set.
304 return scoped_ptr<net::test_server::HttpResponse>();
307 int64 length = 0;
308 int64 start_position = 0;
309 int64 end_position = 0;
310 if (!test_util::ParseContentRangeHeader(
311 iter->second, &start_position, &end_position, &length)) {
312 // Invalid "Content-Range" value.
313 return scoped_ptr<net::test_server::HttpResponse>();
316 EXPECT_EQ(start_position, received_bytes_);
317 EXPECT_EQ(length, content_length_);
319 // end_position is inclusive, but so +1 to change the range to byte size.
320 received_bytes_ = end_position + 1;
323 if (received_bytes_ < content_length_) {
324 scoped_ptr<net::test_server::BasicHttpResponse> response(
325 new net::test_server::BasicHttpResponse);
326 // Set RESUME INCOMPLETE (308) status code.
327 response->set_code(static_cast<net::HttpStatusCode>(308));
329 // Add Range header to the response, based on the values of
330 // Content-Range header in the request.
331 // The header is annotated only when at least one byte is received.
332 if (received_bytes_ > 0) {
333 response->AddCustomHeader(
334 "Range", "bytes=0-" + base::Int64ToString(received_bytes_ - 1));
337 return response.Pass();
340 // All bytes are received. Return the "success" response with the file's
341 // (dummy) metadata.
342 scoped_ptr<net::test_server::BasicHttpResponse> response =
343 test_util::CreateHttpResponseFromFile(
344 test_util::GetTestFilePath("drive/file_entry.json"));
346 // The response code is CREATED if it is new file uploading.
347 if (http_request_.relative_url == kTestUploadNewFilePath) {
348 response->set_code(net::HTTP_CREATED);
351 return response.Pass();
354 // Returns the response based on set expected content and its type.
355 // To use this method, both |expected_content_type_| and |expected_content_|
356 // must be set in advance.
357 scoped_ptr<net::test_server::HttpResponse> HandleContentResponse(
358 const net::test_server::HttpRequest& request) {
359 if (expected_content_type_.empty() || expected_content_.empty()) {
360 // Expected content is not set. Delegate the processing to the next
361 // handler.
362 return scoped_ptr<net::test_server::HttpResponse>();
365 http_request_ = request;
367 scoped_ptr<net::test_server::BasicHttpResponse> response(
368 new net::test_server::BasicHttpResponse);
369 response->set_code(net::HTTP_OK);
370 response->set_content_type(expected_content_type_);
371 response->set_content(expected_content_);
372 return response.Pass();
375 // Handles a request for downloading a file.
376 scoped_ptr<net::test_server::HttpResponse> HandleDownloadRequest(
377 const net::test_server::HttpRequest& request) {
378 http_request_ = request;
380 const GURL absolute_url = test_server_.GetURL(request.relative_url);
381 std::string id;
382 if (!test_util::RemovePrefix(absolute_url.path(),
383 kTestDownloadPathPrefix,
384 &id)) {
385 return scoped_ptr<net::test_server::HttpResponse>();
388 // For testing, returns a text with |id| repeated 3 times.
389 scoped_ptr<net::test_server::BasicHttpResponse> response(
390 new net::test_server::BasicHttpResponse);
391 response->set_code(net::HTTP_OK);
392 response->set_content(id + id + id);
393 response->set_content_type("text/plain");
394 return response.Pass();
397 // These are for the current upload file status.
398 int64 received_bytes_;
399 int64 content_length_;
402 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) {
403 // Make sure that "fields" query param is supported by using its subclass,
404 // AboutGetRequest.
406 // Set an expected data file containing valid result.
407 expected_data_file_path_ = test_util::GetTestFilePath(
408 "drive/about.json");
410 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
411 scoped_ptr<AboutResource> about_resource;
414 base::RunLoop run_loop;
415 drive::AboutGetRequest* request = new drive::AboutGetRequest(
416 request_sender_.get(),
417 *url_generator_,
418 test_util::CreateQuitCallback(
419 &run_loop,
420 test_util::CreateCopyResultCallback(&error, &about_resource)));
421 request->set_fields(
422 "kind,quotaBytesTotal,quotaBytesUsed,largestChangeId,rootFolderId");
423 request_sender_->StartRequestWithRetry(request);
424 run_loop.Run();
427 EXPECT_EQ(HTTP_SUCCESS, error);
428 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
429 EXPECT_EQ("/drive/v2/about?"
430 "fields=kind%2CquotaBytesTotal%2CquotaBytesUsed%2C"
431 "largestChangeId%2CrootFolderId",
432 http_request_.relative_url);
434 scoped_ptr<AboutResource> expected(
435 AboutResource::CreateFrom(
436 *test_util::LoadJSONFile("drive/about.json")));
437 ASSERT_TRUE(about_resource.get());
438 EXPECT_EQ(expected->largest_change_id(), about_resource->largest_change_id());
439 EXPECT_EQ(expected->quota_bytes_total(), about_resource->quota_bytes_total());
440 EXPECT_EQ(expected->quota_bytes_used(), about_resource->quota_bytes_used());
441 EXPECT_EQ(expected->root_folder_id(), about_resource->root_folder_id());
444 TEST_F(DriveApiRequestsTest, FilesInsertRequest) {
445 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
446 const base::Time::Exploded kLastViewedByMeDate =
447 {2013, 7, 0, 19, 15, 59, 13, 123};
449 // Set an expected data file containing the directory's entry data.
450 expected_data_file_path_ =
451 test_util::GetTestFilePath("drive/directory_entry.json");
453 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
454 scoped_ptr<FileResource> file_resource;
456 // Create "new directory" in the root directory.
458 base::RunLoop run_loop;
459 drive::FilesInsertRequest* request = new drive::FilesInsertRequest(
460 request_sender_.get(),
461 *url_generator_,
462 test_util::CreateQuitCallback(
463 &run_loop,
464 test_util::CreateCopyResultCallback(&error, &file_resource)));
465 request->set_last_viewed_by_me_date(
466 base::Time::FromUTCExploded(kLastViewedByMeDate));
467 request->set_mime_type("application/vnd.google-apps.folder");
468 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
469 request->add_parent("root");
470 request->set_title("new directory");
471 request->set_properties(testing_properties_);
472 request_sender_->StartRequestWithRetry(request);
473 run_loop.Run();
476 EXPECT_EQ(HTTP_SUCCESS, error);
477 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
478 EXPECT_EQ("/drive/v2/files", http_request_.relative_url);
479 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
481 EXPECT_TRUE(http_request_.has_content);
482 EXPECT_EQ(
483 "{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\","
484 "\"mimeType\":\"application/vnd.google-apps.folder\","
485 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
486 "\"parents\":[{\"id\":\"root\"}],"
487 "\"properties\":["
488 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
489 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}],"
490 "\"title\":\"new directory\"}",
491 http_request_.content);
493 scoped_ptr<FileResource> expected(
494 FileResource::CreateFrom(
495 *test_util::LoadJSONFile("drive/directory_entry.json")));
497 // Sanity check.
498 ASSERT_TRUE(file_resource.get());
500 EXPECT_EQ(expected->file_id(), file_resource->file_id());
501 EXPECT_EQ(expected->title(), file_resource->title());
502 EXPECT_EQ(expected->mime_type(), file_resource->mime_type());
503 EXPECT_EQ(expected->parents().size(), file_resource->parents().size());
506 TEST_F(DriveApiRequestsTest, FilesPatchRequest) {
507 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
508 const base::Time::Exploded kLastViewedByMeDate =
509 {2013, 7, 0, 19, 15, 59, 13, 123};
511 // Set an expected data file containing valid result.
512 expected_data_file_path_ =
513 test_util::GetTestFilePath("drive/file_entry.json");
515 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
516 scoped_ptr<FileResource> file_resource;
519 base::RunLoop run_loop;
520 drive::FilesPatchRequest* request = new drive::FilesPatchRequest(
521 request_sender_.get(),
522 *url_generator_,
523 test_util::CreateQuitCallback(
524 &run_loop,
525 test_util::CreateCopyResultCallback(&error, &file_resource)));
526 request->set_file_id("resource_id");
527 request->set_set_modified_date(true);
528 request->set_update_viewed_date(false);
530 request->set_title("new title");
531 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
532 request->set_last_viewed_by_me_date(
533 base::Time::FromUTCExploded(kLastViewedByMeDate));
534 request->add_parent("parent_resource_id");
536 request->set_properties(testing_properties_);
537 request_sender_->StartRequestWithRetry(request);
538 run_loop.Run();
541 EXPECT_EQ(HTTP_SUCCESS, error);
542 EXPECT_EQ(net::test_server::METHOD_PATCH, http_request_.method);
543 EXPECT_EQ("/drive/v2/files/resource_id"
544 "?setModifiedDate=true&updateViewedDate=false",
545 http_request_.relative_url);
547 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
548 EXPECT_TRUE(http_request_.has_content);
549 EXPECT_EQ(
550 "{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\","
551 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
552 "\"parents\":[{\"id\":\"parent_resource_id\"}],"
553 "\"properties\":["
554 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
555 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}],"
556 "\"title\":\"new title\"}",
557 http_request_.content);
558 EXPECT_TRUE(file_resource);
561 TEST_F(DriveApiRequestsTest, AboutGetRequest_ValidJson) {
562 // Set an expected data file containing valid result.
563 expected_data_file_path_ = test_util::GetTestFilePath(
564 "drive/about.json");
566 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
567 scoped_ptr<AboutResource> about_resource;
570 base::RunLoop run_loop;
571 drive::AboutGetRequest* request = new drive::AboutGetRequest(
572 request_sender_.get(),
573 *url_generator_,
574 test_util::CreateQuitCallback(
575 &run_loop,
576 test_util::CreateCopyResultCallback(&error, &about_resource)));
577 request_sender_->StartRequestWithRetry(request);
578 run_loop.Run();
581 EXPECT_EQ(HTTP_SUCCESS, error);
582 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
583 EXPECT_EQ("/drive/v2/about", http_request_.relative_url);
585 scoped_ptr<AboutResource> expected(
586 AboutResource::CreateFrom(
587 *test_util::LoadJSONFile("drive/about.json")));
588 ASSERT_TRUE(about_resource.get());
589 EXPECT_EQ(expected->largest_change_id(), about_resource->largest_change_id());
590 EXPECT_EQ(expected->quota_bytes_total(), about_resource->quota_bytes_total());
591 EXPECT_EQ(expected->quota_bytes_used(), about_resource->quota_bytes_used());
592 EXPECT_EQ(expected->root_folder_id(), about_resource->root_folder_id());
595 TEST_F(DriveApiRequestsTest, AboutGetRequest_InvalidJson) {
596 // Set an expected data file containing invalid result.
597 expected_data_file_path_ = test_util::GetTestFilePath(
598 "drive/testfile.txt");
600 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
601 scoped_ptr<AboutResource> about_resource;
604 base::RunLoop run_loop;
605 drive::AboutGetRequest* request = new drive::AboutGetRequest(
606 request_sender_.get(),
607 *url_generator_,
608 test_util::CreateQuitCallback(
609 &run_loop,
610 test_util::CreateCopyResultCallback(&error, &about_resource)));
611 request_sender_->StartRequestWithRetry(request);
612 run_loop.Run();
615 // "parse error" should be returned, and the about resource should be NULL.
616 EXPECT_EQ(DRIVE_PARSE_ERROR, error);
617 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
618 EXPECT_EQ("/drive/v2/about", http_request_.relative_url);
619 EXPECT_FALSE(about_resource);
622 TEST_F(DriveApiRequestsTest, AppsListRequest) {
623 // Set an expected data file containing valid result.
624 expected_data_file_path_ = test_util::GetTestFilePath(
625 "drive/applist.json");
627 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
628 scoped_ptr<AppList> app_list;
631 base::RunLoop run_loop;
632 drive::AppsListRequest* request = new drive::AppsListRequest(
633 request_sender_.get(),
634 *url_generator_,
635 false, // use_internal_endpoint
636 test_util::CreateQuitCallback(
637 &run_loop,
638 test_util::CreateCopyResultCallback(&error, &app_list)));
639 request_sender_->StartRequestWithRetry(request);
640 run_loop.Run();
643 EXPECT_EQ(HTTP_SUCCESS, error);
644 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
645 EXPECT_EQ("/drive/v2/apps", http_request_.relative_url);
646 EXPECT_TRUE(app_list);
649 TEST_F(DriveApiRequestsTest, ChangesListRequest) {
650 // Set an expected data file containing valid result.
651 expected_data_file_path_ = test_util::GetTestFilePath(
652 "drive/changelist.json");
654 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
655 scoped_ptr<ChangeList> result;
658 base::RunLoop run_loop;
659 drive::ChangesListRequest* request = new drive::ChangesListRequest(
660 request_sender_.get(), *url_generator_,
661 test_util::CreateQuitCallback(
662 &run_loop,
663 test_util::CreateCopyResultCallback(&error, &result)));
664 request->set_include_deleted(true);
665 request->set_start_change_id(100);
666 request->set_max_results(500);
667 request_sender_->StartRequestWithRetry(request);
668 run_loop.Run();
671 EXPECT_EQ(HTTP_SUCCESS, error);
672 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
673 EXPECT_EQ("/drive/v2/changes?maxResults=500&startChangeId=100",
674 http_request_.relative_url);
675 EXPECT_TRUE(result);
678 TEST_F(DriveApiRequestsTest, ChangesListNextPageRequest) {
679 // Set an expected data file containing valid result.
680 expected_data_file_path_ = test_util::GetTestFilePath(
681 "drive/changelist.json");
683 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
684 scoped_ptr<ChangeList> result;
687 base::RunLoop run_loop;
688 drive::ChangesListNextPageRequest* request =
689 new drive::ChangesListNextPageRequest(
690 request_sender_.get(),
691 test_util::CreateQuitCallback(
692 &run_loop,
693 test_util::CreateCopyResultCallback(&error, &result)));
694 request->set_next_link(test_server_.GetURL("/continue/get/change/list"));
695 request_sender_->StartRequestWithRetry(request);
696 run_loop.Run();
699 EXPECT_EQ(HTTP_SUCCESS, error);
700 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
701 EXPECT_EQ("/continue/get/change/list", http_request_.relative_url);
702 EXPECT_TRUE(result);
705 TEST_F(DriveApiRequestsTest, FilesCopyRequest) {
706 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
708 // Set an expected data file containing the dummy file entry data.
709 // It'd be returned if we copy a file.
710 expected_data_file_path_ =
711 test_util::GetTestFilePath("drive/file_entry.json");
713 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
714 scoped_ptr<FileResource> file_resource;
716 // Copy the file to a new file named "new title".
718 base::RunLoop run_loop;
719 drive::FilesCopyRequest* request = new drive::FilesCopyRequest(
720 request_sender_.get(),
721 *url_generator_,
722 test_util::CreateQuitCallback(
723 &run_loop,
724 test_util::CreateCopyResultCallback(&error, &file_resource)));
725 request->set_file_id("resource_id");
726 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
727 request->add_parent("parent_resource_id");
728 request->set_title("new title");
729 request_sender_->StartRequestWithRetry(request);
730 run_loop.Run();
733 EXPECT_EQ(HTTP_SUCCESS, error);
734 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
735 EXPECT_EQ("/drive/v2/files/resource_id/copy", http_request_.relative_url);
736 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
738 EXPECT_TRUE(http_request_.has_content);
739 EXPECT_EQ(
740 "{\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
741 "\"parents\":[{\"id\":\"parent_resource_id\"}],\"title\":\"new title\"}",
742 http_request_.content);
743 EXPECT_TRUE(file_resource);
746 TEST_F(DriveApiRequestsTest, FilesCopyRequest_EmptyParentResourceId) {
747 // Set an expected data file containing the dummy file entry data.
748 // It'd be returned if we copy a file.
749 expected_data_file_path_ =
750 test_util::GetTestFilePath("drive/file_entry.json");
752 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
753 scoped_ptr<FileResource> file_resource;
755 // Copy the file to a new file named "new title".
757 base::RunLoop run_loop;
758 drive::FilesCopyRequest* request = new drive::FilesCopyRequest(
759 request_sender_.get(),
760 *url_generator_,
761 test_util::CreateQuitCallback(
762 &run_loop,
763 test_util::CreateCopyResultCallback(&error, &file_resource)));
764 request->set_file_id("resource_id");
765 request->set_title("new title");
766 request_sender_->StartRequestWithRetry(request);
767 run_loop.Run();
770 EXPECT_EQ(HTTP_SUCCESS, error);
771 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
772 EXPECT_EQ("/drive/v2/files/resource_id/copy", http_request_.relative_url);
773 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
775 EXPECT_TRUE(http_request_.has_content);
776 EXPECT_EQ("{\"title\":\"new title\"}", http_request_.content);
777 EXPECT_TRUE(file_resource);
780 TEST_F(DriveApiRequestsTest, FilesListRequest) {
781 // Set an expected data file containing valid result.
782 expected_data_file_path_ = test_util::GetTestFilePath(
783 "drive/filelist.json");
785 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
786 scoped_ptr<FileList> result;
789 base::RunLoop run_loop;
790 drive::FilesListRequest* request = new drive::FilesListRequest(
791 request_sender_.get(), *url_generator_,
792 test_util::CreateQuitCallback(
793 &run_loop,
794 test_util::CreateCopyResultCallback(&error, &result)));
795 request->set_max_results(50);
796 request->set_q("\"abcde\" in parents");
797 request_sender_->StartRequestWithRetry(request);
798 run_loop.Run();
801 EXPECT_EQ(HTTP_SUCCESS, error);
802 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
803 EXPECT_EQ("/drive/v2/files?maxResults=50&q=%22abcde%22+in+parents",
804 http_request_.relative_url);
805 EXPECT_TRUE(result);
808 TEST_F(DriveApiRequestsTest, FilesListNextPageRequest) {
809 // Set an expected data file containing valid result.
810 expected_data_file_path_ = test_util::GetTestFilePath(
811 "drive/filelist.json");
813 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
814 scoped_ptr<FileList> result;
817 base::RunLoop run_loop;
818 drive::FilesListNextPageRequest* request =
819 new drive::FilesListNextPageRequest(
820 request_sender_.get(),
821 test_util::CreateQuitCallback(
822 &run_loop,
823 test_util::CreateCopyResultCallback(&error, &result)));
824 request->set_next_link(test_server_.GetURL("/continue/get/file/list"));
825 request_sender_->StartRequestWithRetry(request);
826 run_loop.Run();
829 EXPECT_EQ(HTTP_SUCCESS, error);
830 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
831 EXPECT_EQ("/continue/get/file/list", http_request_.relative_url);
832 EXPECT_TRUE(result);
835 TEST_F(DriveApiRequestsTest, FilesDeleteRequest) {
836 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
838 // Delete a resource with the given resource id.
840 base::RunLoop run_loop;
841 drive::FilesDeleteRequest* request = new drive::FilesDeleteRequest(
842 request_sender_.get(),
843 *url_generator_,
844 test_util::CreateQuitCallback(
845 &run_loop, test_util::CreateCopyResultCallback(&error)));
846 request->set_file_id("resource_id");
847 request->set_etag(kTestETag);
848 request_sender_->StartRequestWithRetry(request);
849 run_loop.Run();
852 EXPECT_EQ(HTTP_NO_CONTENT, error);
853 EXPECT_EQ(net::test_server::METHOD_DELETE, http_request_.method);
854 EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]);
855 EXPECT_EQ("/drive/v2/files/resource_id", http_request_.relative_url);
856 EXPECT_FALSE(http_request_.has_content);
859 TEST_F(DriveApiRequestsTest, FilesTrashRequest) {
860 // Set data for the expected result. Directory entry should be returned
861 // if the trashing entry is a directory, so using it here should be fine.
862 expected_data_file_path_ =
863 test_util::GetTestFilePath("drive/directory_entry.json");
865 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
866 scoped_ptr<FileResource> file_resource;
868 // Trash a resource with the given resource id.
870 base::RunLoop run_loop;
871 drive::FilesTrashRequest* request = new drive::FilesTrashRequest(
872 request_sender_.get(),
873 *url_generator_,
874 test_util::CreateQuitCallback(
875 &run_loop,
876 test_util::CreateCopyResultCallback(&error, &file_resource)));
877 request->set_file_id("resource_id");
878 request_sender_->StartRequestWithRetry(request);
879 run_loop.Run();
882 EXPECT_EQ(HTTP_SUCCESS, error);
883 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
884 EXPECT_EQ("/drive/v2/files/resource_id/trash", http_request_.relative_url);
885 EXPECT_TRUE(http_request_.has_content);
886 EXPECT_TRUE(http_request_.content.empty());
889 TEST_F(DriveApiRequestsTest, ChildrenInsertRequest) {
890 // Set an expected data file containing the children entry.
891 expected_content_type_ = "application/json";
892 expected_content_ = kTestChildrenResponse;
894 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
896 // Add a resource with "resource_id" to a directory with
897 // "parent_resource_id".
899 base::RunLoop run_loop;
900 drive::ChildrenInsertRequest* request = new drive::ChildrenInsertRequest(
901 request_sender_.get(),
902 *url_generator_,
903 test_util::CreateQuitCallback(
904 &run_loop,
905 test_util::CreateCopyResultCallback(&error)));
906 request->set_folder_id("parent_resource_id");
907 request->set_id("resource_id");
908 request_sender_->StartRequestWithRetry(request);
909 run_loop.Run();
912 EXPECT_EQ(HTTP_SUCCESS, error);
913 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
914 EXPECT_EQ("/drive/v2/files/parent_resource_id/children",
915 http_request_.relative_url);
916 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
918 EXPECT_TRUE(http_request_.has_content);
919 EXPECT_EQ("{\"id\":\"resource_id\"}", http_request_.content);
922 TEST_F(DriveApiRequestsTest, ChildrenDeleteRequest) {
923 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
925 // Remove a resource with "resource_id" from a directory with
926 // "parent_resource_id".
928 base::RunLoop run_loop;
929 drive::ChildrenDeleteRequest* request = new drive::ChildrenDeleteRequest(
930 request_sender_.get(),
931 *url_generator_,
932 test_util::CreateQuitCallback(
933 &run_loop,
934 test_util::CreateCopyResultCallback(&error)));
935 request->set_child_id("resource_id");
936 request->set_folder_id("parent_resource_id");
937 request_sender_->StartRequestWithRetry(request);
938 run_loop.Run();
941 EXPECT_EQ(HTTP_NO_CONTENT, error);
942 EXPECT_EQ(net::test_server::METHOD_DELETE, http_request_.method);
943 EXPECT_EQ("/drive/v2/files/parent_resource_id/children/resource_id",
944 http_request_.relative_url);
945 EXPECT_FALSE(http_request_.has_content);
948 TEST_F(DriveApiRequestsTest, UploadNewFileRequest) {
949 // Set an expected url for uploading.
950 expected_upload_path_ = kTestUploadNewFilePath;
952 const char kTestContentType[] = "text/plain";
953 const std::string kTestContent(100, 'a');
954 const base::FilePath kTestFilePath =
955 temp_dir_.path().AppendASCII("upload_file.txt");
956 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
958 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
959 GURL upload_url;
961 // Initiate uploading a new file to the directory with
962 // "parent_resource_id".
964 base::RunLoop run_loop;
965 drive::InitiateUploadNewFileRequest* request =
966 new drive::InitiateUploadNewFileRequest(
967 request_sender_.get(),
968 *url_generator_,
969 kTestContentType,
970 kTestContent.size(),
971 "parent_resource_id", // The resource id of the parent directory.
972 "new file title", // The title of the file being uploaded.
973 test_util::CreateQuitCallback(
974 &run_loop,
975 test_util::CreateCopyResultCallback(&error, &upload_url)));
976 request->set_properties(testing_properties_);
977 request_sender_->StartRequestWithRetry(request);
978 run_loop.Run();
981 EXPECT_EQ(HTTP_SUCCESS, error);
982 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path());
983 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
984 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
985 http_request_.headers["X-Upload-Content-Length"]);
987 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
988 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable",
989 http_request_.relative_url);
990 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
991 EXPECT_TRUE(http_request_.has_content);
992 EXPECT_EQ(
993 "{\"parents\":[{"
994 "\"id\":\"parent_resource_id\","
995 "\"kind\":\"drive#fileLink\""
996 "}],"
997 "\"properties\":["
998 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
999 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}],"
1000 "\"title\":\"new file title\"}",
1001 http_request_.content);
1003 // Upload the content to the upload URL.
1004 UploadRangeResponse response;
1005 scoped_ptr<FileResource> new_entry;
1008 base::RunLoop run_loop;
1009 drive::ResumeUploadRequest* resume_request =
1010 new drive::ResumeUploadRequest(
1011 request_sender_.get(),
1012 upload_url,
1013 0, // start_position
1014 kTestContent.size(), // end_position (exclusive)
1015 kTestContent.size(), // content_length,
1016 kTestContentType,
1017 kTestFilePath,
1018 test_util::CreateQuitCallback(
1019 &run_loop,
1020 test_util::CreateCopyResultCallback(&response, &new_entry)),
1021 ProgressCallback());
1022 request_sender_->StartRequestWithRetry(resume_request);
1023 run_loop.Run();
1026 // METHOD_PUT should be used to upload data.
1027 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1028 // Request should go to the upload URL.
1029 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1030 // Content-Range header should be added.
1031 EXPECT_EQ("bytes 0-" +
1032 base::Int64ToString(kTestContent.size() - 1) + "/" +
1033 base::Int64ToString(kTestContent.size()),
1034 http_request_.headers["Content-Range"]);
1035 // The upload content should be set in the HTTP request.
1036 EXPECT_TRUE(http_request_.has_content);
1037 EXPECT_EQ(kTestContent, http_request_.content);
1039 // Check the response.
1040 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file
1041 // The start and end positions should be set to -1, if an upload is complete.
1042 EXPECT_EQ(-1, response.start_position_received);
1043 EXPECT_EQ(-1, response.end_position_received);
1046 TEST_F(DriveApiRequestsTest, UploadNewEmptyFileRequest) {
1047 // Set an expected url for uploading.
1048 expected_upload_path_ = kTestUploadNewFilePath;
1050 const char kTestContentType[] = "text/plain";
1051 const char kTestContent[] = "";
1052 const base::FilePath kTestFilePath =
1053 temp_dir_.path().AppendASCII("empty_file.txt");
1054 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1056 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1057 GURL upload_url;
1059 // Initiate uploading a new file to the directory with "parent_resource_id".
1061 base::RunLoop run_loop;
1062 drive::InitiateUploadNewFileRequest* request =
1063 new drive::InitiateUploadNewFileRequest(
1064 request_sender_.get(),
1065 *url_generator_,
1066 kTestContentType,
1068 "parent_resource_id", // The resource id of the parent directory.
1069 "new file title", // The title of the file being uploaded.
1070 test_util::CreateQuitCallback(
1071 &run_loop,
1072 test_util::CreateCopyResultCallback(&error, &upload_url)));
1073 request_sender_->StartRequestWithRetry(request);
1074 run_loop.Run();
1077 EXPECT_EQ(HTTP_SUCCESS, error);
1078 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path());
1079 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1080 EXPECT_EQ("0", http_request_.headers["X-Upload-Content-Length"]);
1082 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
1083 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable",
1084 http_request_.relative_url);
1085 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1086 EXPECT_TRUE(http_request_.has_content);
1087 EXPECT_EQ("{\"parents\":[{"
1088 "\"id\":\"parent_resource_id\","
1089 "\"kind\":\"drive#fileLink\""
1090 "}],"
1091 "\"title\":\"new file title\"}",
1092 http_request_.content);
1094 // Upload the content to the upload URL.
1095 UploadRangeResponse response;
1096 scoped_ptr<FileResource> new_entry;
1099 base::RunLoop run_loop;
1100 drive::ResumeUploadRequest* resume_request =
1101 new drive::ResumeUploadRequest(
1102 request_sender_.get(),
1103 upload_url,
1104 0, // start_position
1105 0, // end_position (exclusive)
1106 0, // content_length,
1107 kTestContentType,
1108 kTestFilePath,
1109 test_util::CreateQuitCallback(
1110 &run_loop,
1111 test_util::CreateCopyResultCallback(&response, &new_entry)),
1112 ProgressCallback());
1113 request_sender_->StartRequestWithRetry(resume_request);
1114 run_loop.Run();
1117 // METHOD_PUT should be used to upload data.
1118 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1119 // Request should go to the upload URL.
1120 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1121 // Content-Range header should NOT be added.
1122 EXPECT_EQ(0U, http_request_.headers.count("Content-Range"));
1123 // The upload content should be set in the HTTP request.
1124 EXPECT_TRUE(http_request_.has_content);
1125 EXPECT_EQ(kTestContent, http_request_.content);
1127 // Check the response.
1128 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file
1129 // The start and end positions should be set to -1, if an upload is complete.
1130 EXPECT_EQ(-1, response.start_position_received);
1131 EXPECT_EQ(-1, response.end_position_received);
1134 TEST_F(DriveApiRequestsTest, UploadNewLargeFileRequest) {
1135 // Set an expected url for uploading.
1136 expected_upload_path_ = kTestUploadNewFilePath;
1138 const char kTestContentType[] = "text/plain";
1139 const size_t kNumChunkBytes = 10; // Num bytes in a chunk.
1140 const std::string kTestContent(100, 'a');
1141 const base::FilePath kTestFilePath =
1142 temp_dir_.path().AppendASCII("upload_file.txt");
1143 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1145 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1146 GURL upload_url;
1148 // Initiate uploading a new file to the directory with "parent_resource_id".
1150 base::RunLoop run_loop;
1151 drive::InitiateUploadNewFileRequest* request =
1152 new drive::InitiateUploadNewFileRequest(
1153 request_sender_.get(),
1154 *url_generator_,
1155 kTestContentType,
1156 kTestContent.size(),
1157 "parent_resource_id", // The resource id of the parent directory.
1158 "new file title", // The title of the file being uploaded.
1159 test_util::CreateQuitCallback(
1160 &run_loop,
1161 test_util::CreateCopyResultCallback(&error, &upload_url)));
1162 request_sender_->StartRequestWithRetry(request);
1163 run_loop.Run();
1166 EXPECT_EQ(HTTP_SUCCESS, error);
1167 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path());
1168 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1169 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1170 http_request_.headers["X-Upload-Content-Length"]);
1172 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
1173 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable",
1174 http_request_.relative_url);
1175 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1176 EXPECT_TRUE(http_request_.has_content);
1177 EXPECT_EQ("{\"parents\":[{"
1178 "\"id\":\"parent_resource_id\","
1179 "\"kind\":\"drive#fileLink\""
1180 "}],"
1181 "\"title\":\"new file title\"}",
1182 http_request_.content);
1184 // Before sending any data, check the current status.
1185 // This is an edge case test for GetUploadStatusRequest.
1187 UploadRangeResponse response;
1188 scoped_ptr<FileResource> new_entry;
1190 // Check the response by GetUploadStatusRequest.
1192 base::RunLoop run_loop;
1193 drive::GetUploadStatusRequest* get_upload_status_request =
1194 new drive::GetUploadStatusRequest(
1195 request_sender_.get(),
1196 upload_url,
1197 kTestContent.size(),
1198 test_util::CreateQuitCallback(
1199 &run_loop,
1200 test_util::CreateCopyResultCallback(&response, &new_entry)));
1201 request_sender_->StartRequestWithRetry(get_upload_status_request);
1202 run_loop.Run();
1205 // METHOD_PUT should be used to upload data.
1206 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1207 // Request should go to the upload URL.
1208 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1209 // Content-Range header should be added.
1210 EXPECT_EQ("bytes */" + base::Int64ToString(kTestContent.size()),
1211 http_request_.headers["Content-Range"]);
1212 EXPECT_TRUE(http_request_.has_content);
1213 EXPECT_TRUE(http_request_.content.empty());
1215 // Check the response.
1216 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1217 EXPECT_EQ(0, response.start_position_received);
1218 EXPECT_EQ(0, response.end_position_received);
1221 // Upload the content to the upload URL.
1222 for (size_t start_position = 0; start_position < kTestContent.size();
1223 start_position += kNumChunkBytes) {
1224 const std::string payload = kTestContent.substr(
1225 start_position,
1226 std::min(kNumChunkBytes, kTestContent.size() - start_position));
1227 const size_t end_position = start_position + payload.size();
1229 UploadRangeResponse response;
1230 scoped_ptr<FileResource> new_entry;
1233 base::RunLoop run_loop;
1234 drive::ResumeUploadRequest* resume_request =
1235 new drive::ResumeUploadRequest(
1236 request_sender_.get(),
1237 upload_url,
1238 start_position,
1239 end_position,
1240 kTestContent.size(), // content_length,
1241 kTestContentType,
1242 kTestFilePath,
1243 test_util::CreateQuitCallback(
1244 &run_loop,
1245 test_util::CreateCopyResultCallback(&response, &new_entry)),
1246 ProgressCallback());
1247 request_sender_->StartRequestWithRetry(resume_request);
1248 run_loop.Run();
1251 // METHOD_PUT should be used to upload data.
1252 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1253 // Request should go to the upload URL.
1254 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1255 // Content-Range header should be added.
1256 EXPECT_EQ("bytes " +
1257 base::Int64ToString(start_position) + "-" +
1258 base::Int64ToString(end_position - 1) + "/" +
1259 base::Int64ToString(kTestContent.size()),
1260 http_request_.headers["Content-Range"]);
1261 // The upload content should be set in the HTTP request.
1262 EXPECT_TRUE(http_request_.has_content);
1263 EXPECT_EQ(payload, http_request_.content);
1265 if (end_position == kTestContent.size()) {
1266 // Check the response.
1267 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file
1268 // The start and end positions should be set to -1, if an upload is
1269 // complete.
1270 EXPECT_EQ(-1, response.start_position_received);
1271 EXPECT_EQ(-1, response.end_position_received);
1272 break;
1275 // Check the response.
1276 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1277 EXPECT_EQ(0, response.start_position_received);
1278 EXPECT_EQ(static_cast<int64>(end_position), response.end_position_received);
1280 // Check the response by GetUploadStatusRequest.
1282 base::RunLoop run_loop;
1283 drive::GetUploadStatusRequest* get_upload_status_request =
1284 new drive::GetUploadStatusRequest(
1285 request_sender_.get(),
1286 upload_url,
1287 kTestContent.size(),
1288 test_util::CreateQuitCallback(
1289 &run_loop,
1290 test_util::CreateCopyResultCallback(&response, &new_entry)));
1291 request_sender_->StartRequestWithRetry(get_upload_status_request);
1292 run_loop.Run();
1295 // METHOD_PUT should be used to upload data.
1296 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1297 // Request should go to the upload URL.
1298 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1299 // Content-Range header should be added.
1300 EXPECT_EQ("bytes */" + base::Int64ToString(kTestContent.size()),
1301 http_request_.headers["Content-Range"]);
1302 EXPECT_TRUE(http_request_.has_content);
1303 EXPECT_TRUE(http_request_.content.empty());
1305 // Check the response.
1306 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1307 EXPECT_EQ(0, response.start_position_received);
1308 EXPECT_EQ(static_cast<int64>(end_position),
1309 response.end_position_received);
1313 TEST_F(DriveApiRequestsTest, UploadNewFileWithMetadataRequest) {
1314 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
1315 const base::Time::Exploded kLastViewedByMeDate =
1316 {2013, 7, 0, 19, 15, 59, 13, 123};
1318 // Set an expected url for uploading.
1319 expected_upload_path_ = kTestUploadNewFilePath;
1321 const char kTestContentType[] = "text/plain";
1322 const std::string kTestContent(100, 'a');
1324 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1325 GURL upload_url;
1327 // Initiate uploading a new file to the directory with "parent_resource_id".
1329 base::RunLoop run_loop;
1330 drive::InitiateUploadNewFileRequest* request =
1331 new drive::InitiateUploadNewFileRequest(
1332 request_sender_.get(),
1333 *url_generator_,
1334 kTestContentType,
1335 kTestContent.size(),
1336 "parent_resource_id", // The resource id of the parent directory.
1337 "new file title", // The title of the file being uploaded.
1338 test_util::CreateQuitCallback(
1339 &run_loop,
1340 test_util::CreateCopyResultCallback(&error, &upload_url)));
1341 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
1342 request->set_last_viewed_by_me_date(
1343 base::Time::FromUTCExploded(kLastViewedByMeDate));
1344 request_sender_->StartRequestWithRetry(request);
1345 run_loop.Run();
1348 EXPECT_EQ(HTTP_SUCCESS, error);
1349 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path());
1350 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1351 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1352 http_request_.headers["X-Upload-Content-Length"]);
1354 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
1355 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable&setModifiedDate=true",
1356 http_request_.relative_url);
1357 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1358 EXPECT_TRUE(http_request_.has_content);
1359 EXPECT_EQ("{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\","
1360 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
1361 "\"parents\":[{\"id\":\"parent_resource_id\","
1362 "\"kind\":\"drive#fileLink\"}],"
1363 "\"title\":\"new file title\"}",
1364 http_request_.content);
1367 TEST_F(DriveApiRequestsTest, UploadExistingFileRequest) {
1368 // Set an expected url for uploading.
1369 expected_upload_path_ = kTestUploadExistingFilePath;
1371 const char kTestContentType[] = "text/plain";
1372 const std::string kTestContent(100, 'a');
1373 const base::FilePath kTestFilePath =
1374 temp_dir_.path().AppendASCII("upload_file.txt");
1375 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1377 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1378 GURL upload_url;
1380 // Initiate uploading a new file to the directory with "parent_resource_id".
1382 base::RunLoop run_loop;
1383 drive::InitiateUploadExistingFileRequest* request =
1384 new drive::InitiateUploadExistingFileRequest(
1385 request_sender_.get(),
1386 *url_generator_,
1387 kTestContentType,
1388 kTestContent.size(),
1389 "resource_id", // The resource id of the file to be overwritten.
1390 std::string(), // No etag.
1391 test_util::CreateQuitCallback(
1392 &run_loop,
1393 test_util::CreateCopyResultCallback(&error, &upload_url)));
1394 request->set_properties(testing_properties_);
1395 request_sender_->StartRequestWithRetry(request);
1396 run_loop.Run();
1399 EXPECT_EQ(HTTP_SUCCESS, error);
1400 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path());
1401 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1402 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1403 http_request_.headers["X-Upload-Content-Length"]);
1404 EXPECT_EQ("*", http_request_.headers["If-Match"]);
1406 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1407 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable",
1408 http_request_.relative_url);
1409 EXPECT_TRUE(http_request_.has_content);
1410 EXPECT_EQ(
1411 "{\"properties\":["
1412 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
1413 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}]}",
1414 http_request_.content);
1416 // Upload the content to the upload URL.
1417 UploadRangeResponse response;
1418 scoped_ptr<FileResource> new_entry;
1421 base::RunLoop run_loop;
1422 drive::ResumeUploadRequest* resume_request =
1423 new drive::ResumeUploadRequest(
1424 request_sender_.get(),
1425 upload_url,
1426 0, // start_position
1427 kTestContent.size(), // end_position (exclusive)
1428 kTestContent.size(), // content_length,
1429 kTestContentType,
1430 kTestFilePath,
1431 test_util::CreateQuitCallback(
1432 &run_loop,
1433 test_util::CreateCopyResultCallback(&response, &new_entry)),
1434 ProgressCallback());
1435 request_sender_->StartRequestWithRetry(resume_request);
1436 run_loop.Run();
1439 // METHOD_PUT should be used to upload data.
1440 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1441 // Request should go to the upload URL.
1442 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1443 // Content-Range header should be added.
1444 EXPECT_EQ("bytes 0-" +
1445 base::Int64ToString(kTestContent.size() - 1) + "/" +
1446 base::Int64ToString(kTestContent.size()),
1447 http_request_.headers["Content-Range"]);
1448 // The upload content should be set in the HTTP request.
1449 EXPECT_TRUE(http_request_.has_content);
1450 EXPECT_EQ(kTestContent, http_request_.content);
1452 // Check the response.
1453 EXPECT_EQ(HTTP_SUCCESS, response.code); // Because it's an existing file
1454 // The start and end positions should be set to -1, if an upload is complete.
1455 EXPECT_EQ(-1, response.start_position_received);
1456 EXPECT_EQ(-1, response.end_position_received);
1459 TEST_F(DriveApiRequestsTest, UploadExistingFileRequestWithETag) {
1460 // Set an expected url for uploading.
1461 expected_upload_path_ = kTestUploadExistingFilePath;
1463 const char kTestContentType[] = "text/plain";
1464 const std::string kTestContent(100, 'a');
1465 const base::FilePath kTestFilePath =
1466 temp_dir_.path().AppendASCII("upload_file.txt");
1467 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1469 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1470 GURL upload_url;
1472 // Initiate uploading a new file to the directory with "parent_resource_id".
1474 base::RunLoop run_loop;
1475 drive::InitiateUploadExistingFileRequest* request =
1476 new drive::InitiateUploadExistingFileRequest(
1477 request_sender_.get(),
1478 *url_generator_,
1479 kTestContentType,
1480 kTestContent.size(),
1481 "resource_id", // The resource id of the file to be overwritten.
1482 kTestETag,
1483 test_util::CreateQuitCallback(
1484 &run_loop,
1485 test_util::CreateCopyResultCallback(&error, &upload_url)));
1486 request_sender_->StartRequestWithRetry(request);
1487 run_loop.Run();
1490 EXPECT_EQ(HTTP_SUCCESS, error);
1491 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path());
1492 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1493 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1494 http_request_.headers["X-Upload-Content-Length"]);
1495 EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]);
1497 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1498 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable",
1499 http_request_.relative_url);
1500 EXPECT_TRUE(http_request_.has_content);
1501 EXPECT_TRUE(http_request_.content.empty());
1503 // Upload the content to the upload URL.
1504 UploadRangeResponse response;
1505 scoped_ptr<FileResource> new_entry;
1508 base::RunLoop run_loop;
1509 drive::ResumeUploadRequest* resume_request =
1510 new drive::ResumeUploadRequest(
1511 request_sender_.get(),
1512 upload_url,
1513 0, // start_position
1514 kTestContent.size(), // end_position (exclusive)
1515 kTestContent.size(), // content_length,
1516 kTestContentType,
1517 kTestFilePath,
1518 test_util::CreateQuitCallback(
1519 &run_loop,
1520 test_util::CreateCopyResultCallback(&response, &new_entry)),
1521 ProgressCallback());
1522 request_sender_->StartRequestWithRetry(resume_request);
1523 run_loop.Run();
1526 // METHOD_PUT should be used to upload data.
1527 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1528 // Request should go to the upload URL.
1529 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1530 // Content-Range header should be added.
1531 EXPECT_EQ("bytes 0-" +
1532 base::Int64ToString(kTestContent.size() - 1) + "/" +
1533 base::Int64ToString(kTestContent.size()),
1534 http_request_.headers["Content-Range"]);
1535 // The upload content should be set in the HTTP request.
1536 EXPECT_TRUE(http_request_.has_content);
1537 EXPECT_EQ(kTestContent, http_request_.content);
1539 // Check the response.
1540 EXPECT_EQ(HTTP_SUCCESS, response.code); // Because it's an existing file
1541 // The start and end positions should be set to -1, if an upload is complete.
1542 EXPECT_EQ(-1, response.start_position_received);
1543 EXPECT_EQ(-1, response.end_position_received);
1546 TEST_F(DriveApiRequestsTest, UploadExistingFileRequestWithETagConflicting) {
1547 // Set an expected url for uploading.
1548 expected_upload_path_ = kTestUploadExistingFilePath;
1550 // If it turned out that the etag is conflicting, PRECONDITION_FAILED should
1551 // be returned.
1552 expected_precondition_failed_file_path_ =
1553 test_util::GetTestFilePath("drive/error.json");
1555 const char kTestContentType[] = "text/plain";
1556 const std::string kTestContent(100, 'a');
1558 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1559 GURL upload_url;
1561 // Initiate uploading a new file to the directory with "parent_resource_id".
1563 base::RunLoop run_loop;
1564 drive::InitiateUploadExistingFileRequest* request =
1565 new drive::InitiateUploadExistingFileRequest(
1566 request_sender_.get(),
1567 *url_generator_,
1568 kTestContentType,
1569 kTestContent.size(),
1570 "resource_id", // The resource id of the file to be overwritten.
1571 "Conflicting-etag",
1572 test_util::CreateQuitCallback(
1573 &run_loop,
1574 test_util::CreateCopyResultCallback(&error, &upload_url)));
1575 request_sender_->StartRequestWithRetry(request);
1576 run_loop.Run();
1579 EXPECT_EQ(HTTP_PRECONDITION, error);
1580 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1581 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1582 http_request_.headers["X-Upload-Content-Length"]);
1583 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]);
1585 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1586 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable",
1587 http_request_.relative_url);
1588 EXPECT_TRUE(http_request_.has_content);
1589 EXPECT_TRUE(http_request_.content.empty());
1592 TEST_F(DriveApiRequestsTest,
1593 UploadExistingFileRequestWithETagConflictOnResumeUpload) {
1594 // Set an expected url for uploading.
1595 expected_upload_path_ = kTestUploadExistingFilePath;
1597 const char kTestContentType[] = "text/plain";
1598 const std::string kTestContent(100, 'a');
1599 const base::FilePath kTestFilePath =
1600 temp_dir_.path().AppendASCII("upload_file.txt");
1601 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1603 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1604 GURL upload_url;
1606 // Initiate uploading a new file to the directory with "parent_resource_id".
1608 base::RunLoop run_loop;
1609 drive::InitiateUploadExistingFileRequest* request =
1610 new drive::InitiateUploadExistingFileRequest(
1611 request_sender_.get(),
1612 *url_generator_,
1613 kTestContentType,
1614 kTestContent.size(),
1615 "resource_id", // The resource id of the file to be overwritten.
1616 kTestETag,
1617 test_util::CreateQuitCallback(
1618 &run_loop,
1619 test_util::CreateCopyResultCallback(&error, &upload_url)));
1620 request_sender_->StartRequestWithRetry(request);
1621 run_loop.Run();
1624 EXPECT_EQ(HTTP_SUCCESS, error);
1625 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path());
1626 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1627 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1628 http_request_.headers["X-Upload-Content-Length"]);
1629 EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]);
1631 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1632 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable",
1633 http_request_.relative_url);
1634 EXPECT_TRUE(http_request_.has_content);
1635 EXPECT_TRUE(http_request_.content.empty());
1637 // Set PRECONDITION_FAILED to the server. This is the emulation of the
1638 // confliction during uploading.
1639 expected_precondition_failed_file_path_ =
1640 test_util::GetTestFilePath("drive/error.json");
1642 // Upload the content to the upload URL.
1643 UploadRangeResponse response;
1644 scoped_ptr<FileResource> new_entry;
1647 base::RunLoop run_loop;
1648 drive::ResumeUploadRequest* resume_request =
1649 new drive::ResumeUploadRequest(
1650 request_sender_.get(),
1651 upload_url,
1652 0, // start_position
1653 kTestContent.size(), // end_position (exclusive)
1654 kTestContent.size(), // content_length,
1655 kTestContentType,
1656 kTestFilePath,
1657 test_util::CreateQuitCallback(
1658 &run_loop,
1659 test_util::CreateCopyResultCallback(&response, &new_entry)),
1660 ProgressCallback());
1661 request_sender_->StartRequestWithRetry(resume_request);
1662 run_loop.Run();
1665 // METHOD_PUT should be used to upload data.
1666 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1667 // Request should go to the upload URL.
1668 EXPECT_EQ(upload_url.path(), http_request_.relative_url);
1669 // Content-Range header should be added.
1670 EXPECT_EQ("bytes 0-" +
1671 base::Int64ToString(kTestContent.size() - 1) + "/" +
1672 base::Int64ToString(kTestContent.size()),
1673 http_request_.headers["Content-Range"]);
1674 // The upload content should be set in the HTTP request.
1675 EXPECT_TRUE(http_request_.has_content);
1676 EXPECT_EQ(kTestContent, http_request_.content);
1678 // Check the response.
1679 EXPECT_EQ(HTTP_PRECONDITION, response.code);
1680 // The start and end positions should be set to -1 for error.
1681 EXPECT_EQ(-1, response.start_position_received);
1682 EXPECT_EQ(-1, response.end_position_received);
1684 // New entry should be NULL.
1685 EXPECT_FALSE(new_entry.get());
1688 TEST_F(DriveApiRequestsTest, UploadExistingFileWithMetadataRequest) {
1689 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
1690 const base::Time::Exploded kLastViewedByMeDate =
1691 {2013, 7, 0, 19, 15, 59, 13, 123};
1693 // Set an expected url for uploading.
1694 expected_upload_path_ = kTestUploadExistingFilePath;
1696 const char kTestContentType[] = "text/plain";
1697 const std::string kTestContent(100, 'a');
1699 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1700 GURL upload_url;
1702 // Initiate uploading a new file to the directory with "parent_resource_id".
1704 base::RunLoop run_loop;
1705 drive::InitiateUploadExistingFileRequest* request =
1706 new drive::InitiateUploadExistingFileRequest(
1707 request_sender_.get(),
1708 *url_generator_,
1709 kTestContentType,
1710 kTestContent.size(),
1711 "resource_id", // The resource id of the file to be overwritten.
1712 kTestETag,
1713 test_util::CreateQuitCallback(
1714 &run_loop,
1715 test_util::CreateCopyResultCallback(&error, &upload_url)));
1716 request->set_parent_resource_id("new_parent_resource_id");
1717 request->set_title("new file title");
1718 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
1719 request->set_last_viewed_by_me_date(
1720 base::Time::FromUTCExploded(kLastViewedByMeDate));
1722 request_sender_->StartRequestWithRetry(request);
1723 run_loop.Run();
1726 EXPECT_EQ(HTTP_SUCCESS, error);
1727 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path());
1728 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1729 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1730 http_request_.headers["X-Upload-Content-Length"]);
1731 EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]);
1733 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1734 EXPECT_EQ("/upload/drive/v2/files/resource_id?"
1735 "uploadType=resumable&setModifiedDate=true",
1736 http_request_.relative_url);
1737 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1738 EXPECT_TRUE(http_request_.has_content);
1739 EXPECT_EQ("{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\","
1740 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
1741 "\"parents\":[{\"id\":\"new_parent_resource_id\","
1742 "\"kind\":\"drive#fileLink\"}],"
1743 "\"title\":\"new file title\"}",
1744 http_request_.content);
1747 TEST_F(DriveApiRequestsTest, DownloadFileRequest) {
1748 const base::FilePath kDownloadedFilePath =
1749 temp_dir_.path().AppendASCII("cache_file");
1750 const std::string kTestId("dummyId");
1752 DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
1753 base::FilePath temp_file;
1755 base::RunLoop run_loop;
1756 drive::DownloadFileRequest* request = new drive::DownloadFileRequest(
1757 request_sender_.get(),
1758 *url_generator_,
1759 kTestId,
1760 kDownloadedFilePath,
1761 test_util::CreateQuitCallback(
1762 &run_loop,
1763 test_util::CreateCopyResultCallback(&result_code, &temp_file)),
1764 GetContentCallback(),
1765 ProgressCallback());
1766 request_sender_->StartRequestWithRetry(request);
1767 run_loop.Run();
1770 std::string contents;
1771 base::ReadFileToString(temp_file, &contents);
1772 base::DeleteFile(temp_file, false);
1774 EXPECT_EQ(HTTP_SUCCESS, result_code);
1775 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
1776 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url);
1777 EXPECT_EQ(kDownloadedFilePath, temp_file);
1779 const std::string expected_contents = kTestId + kTestId + kTestId;
1780 EXPECT_EQ(expected_contents, contents);
1783 TEST_F(DriveApiRequestsTest, DownloadFileRequest_GetContentCallback) {
1784 const base::FilePath kDownloadedFilePath =
1785 temp_dir_.path().AppendASCII("cache_file");
1786 const std::string kTestId("dummyId");
1788 DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
1789 base::FilePath temp_file;
1790 std::string contents;
1792 base::RunLoop run_loop;
1793 drive::DownloadFileRequest* request = new drive::DownloadFileRequest(
1794 request_sender_.get(),
1795 *url_generator_,
1796 kTestId,
1797 kDownloadedFilePath,
1798 test_util::CreateQuitCallback(
1799 &run_loop,
1800 test_util::CreateCopyResultCallback(&result_code, &temp_file)),
1801 base::Bind(&AppendContent, &contents),
1802 ProgressCallback());
1803 request_sender_->StartRequestWithRetry(request);
1804 run_loop.Run();
1807 base::DeleteFile(temp_file, false);
1809 EXPECT_EQ(HTTP_SUCCESS, result_code);
1810 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
1811 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url);
1812 EXPECT_EQ(kDownloadedFilePath, temp_file);
1814 const std::string expected_contents = kTestId + kTestId + kTestId;
1815 EXPECT_EQ(expected_contents, contents);
1818 TEST_F(DriveApiRequestsTest, PermissionsInsertRequest) {
1819 expected_content_type_ = "application/json";
1820 expected_content_ = kTestPermissionResponse;
1822 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1824 // Add comment permission to the user "user@example.com".
1826 base::RunLoop run_loop;
1827 drive::PermissionsInsertRequest* request =
1828 new drive::PermissionsInsertRequest(
1829 request_sender_.get(),
1830 *url_generator_,
1831 test_util::CreateQuitCallback(
1832 &run_loop,
1833 test_util::CreateCopyResultCallback(&error)));
1834 request->set_id("resource_id");
1835 request->set_role(drive::PERMISSION_ROLE_COMMENTER);
1836 request->set_type(drive::PERMISSION_TYPE_USER);
1837 request->set_value("user@example.com");
1838 request_sender_->StartRequestWithRetry(request);
1839 run_loop.Run();
1842 EXPECT_EQ(HTTP_SUCCESS, error);
1843 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
1844 EXPECT_EQ("/drive/v2/files/resource_id/permissions",
1845 http_request_.relative_url);
1846 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1848 scoped_ptr<base::Value> expected(base::JSONReader::Read(
1849 "{\"additionalRoles\":[\"commenter\"], \"role\":\"reader\", "
1850 "\"type\":\"user\",\"value\":\"user@example.com\"}"));
1851 ASSERT_TRUE(expected);
1853 scoped_ptr<base::Value> result(base::JSONReader::Read(http_request_.content));
1854 EXPECT_TRUE(http_request_.has_content);
1855 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));
1857 // Add "can edit" permission to users in "example.com".
1858 error = DRIVE_OTHER_ERROR;
1860 base::RunLoop run_loop;
1861 drive::PermissionsInsertRequest* request =
1862 new drive::PermissionsInsertRequest(
1863 request_sender_.get(),
1864 *url_generator_,
1865 test_util::CreateQuitCallback(
1866 &run_loop,
1867 test_util::CreateCopyResultCallback(&error)));
1868 request->set_id("resource_id2");
1869 request->set_role(drive::PERMISSION_ROLE_WRITER);
1870 request->set_type(drive::PERMISSION_TYPE_DOMAIN);
1871 request->set_value("example.com");
1872 request_sender_->StartRequestWithRetry(request);
1873 run_loop.Run();
1876 EXPECT_EQ(HTTP_SUCCESS, error);
1877 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
1878 EXPECT_EQ("/drive/v2/files/resource_id2/permissions",
1879 http_request_.relative_url);
1880 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
1882 expected.reset(base::JSONReader::Read(
1883 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}"));
1884 ASSERT_TRUE(expected);
1886 result.reset(base::JSONReader::Read(http_request_.content));
1887 EXPECT_TRUE(http_request_.has_content);
1888 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));
1891 } // namespace google_apis