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

Side by Side Diff: google_apis/drive/drive_api_requests_unittest.cc

Issue 944413003: Add support for setting properties to requests uploading contents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « google_apis/drive/drive_api_requests.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 base::Unretained(this))); 102 base::Unretained(this)));
103 103
104 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); 104 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port());
105 url_generator_.reset(new DriveApiUrlGenerator( 105 url_generator_.reset(new DriveApiUrlGenerator(
106 test_base_url, test_base_url.Resolve(kTestDownloadPathPrefix))); 106 test_base_url, test_base_url.Resolve(kTestDownloadPathPrefix)));
107 107
108 // Reset the server's expected behavior just in case. 108 // Reset the server's expected behavior just in case.
109 ResetExpectedResponse(); 109 ResetExpectedResponse();
110 received_bytes_ = 0; 110 received_bytes_ = 0;
111 content_length_ = 0; 111 content_length_ = 0;
112
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");
117
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");
122
123 testing_properties_.clear();
124 testing_properties_.push_back(private_property);
125 testing_properties_.push_back(public_property);
112 } 126 }
113 127
114 base::MessageLoopForIO message_loop_; // Test server needs IO thread. 128 base::MessageLoopForIO message_loop_; // Test server needs IO thread.
115 net::test_server::EmbeddedTestServer test_server_; 129 net::test_server::EmbeddedTestServer test_server_;
116 scoped_ptr<RequestSender> request_sender_; 130 scoped_ptr<RequestSender> request_sender_;
117 scoped_ptr<DriveApiUrlGenerator> url_generator_; 131 scoped_ptr<DriveApiUrlGenerator> url_generator_;
118 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; 132 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
119 base::ScopedTempDir temp_dir_; 133 base::ScopedTempDir temp_dir_;
120 134
121 // This is a path to the file which contains expected response from 135 // This is a path to the file which contains expected response from
(...skipping 11 matching lines...) Expand all
133 // These are content and its type in the expected response from the server. 147 // These are content and its type in the expected response from the server.
134 // See also HandleContentResponse below. 148 // See also HandleContentResponse below.
135 std::string expected_content_type_; 149 std::string expected_content_type_;
136 std::string expected_content_; 150 std::string expected_content_;
137 151
138 // The incoming HTTP request is saved so tests can verify the request 152 // The incoming HTTP request is saved so tests can verify the request
139 // parameters like HTTP method (ex. some requests should use DELETE 153 // parameters like HTTP method (ex. some requests should use DELETE
140 // instead of GET). 154 // instead of GET).
141 net::test_server::HttpRequest http_request_; 155 net::test_server::HttpRequest http_request_;
142 156
157 // Testing properties used by multiple test cases.
158 drive::Properties testing_properties_;
159
143 private: 160 private:
144 void ResetExpectedResponse() { 161 void ResetExpectedResponse() {
145 expected_data_file_path_.clear(); 162 expected_data_file_path_.clear();
146 expected_upload_path_.clear(); 163 expected_upload_path_.clear();
147 expected_content_type_.clear(); 164 expected_content_type_.clear();
148 expected_content_.clear(); 165 expected_content_.clear();
149 } 166 }
150 167
151 // For "Children: delete" request, the server will return "204 No Content" 168 // For "Children: delete" request, the server will return "204 No Content"
152 // response meaning "success". 169 // response meaning "success".
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 *url_generator_, 461 *url_generator_,
445 test_util::CreateQuitCallback( 462 test_util::CreateQuitCallback(
446 &run_loop, 463 &run_loop,
447 test_util::CreateCopyResultCallback(&error, &file_resource))); 464 test_util::CreateCopyResultCallback(&error, &file_resource)));
448 request->set_last_viewed_by_me_date( 465 request->set_last_viewed_by_me_date(
449 base::Time::FromUTCExploded(kLastViewedByMeDate)); 466 base::Time::FromUTCExploded(kLastViewedByMeDate));
450 request->set_mime_type("application/vnd.google-apps.folder"); 467 request->set_mime_type("application/vnd.google-apps.folder");
451 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate)); 468 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
452 request->add_parent("root"); 469 request->add_parent("root");
453 request->set_title("new directory"); 470 request->set_title("new directory");
471 request->set_properties(testing_properties_);
454 request_sender_->StartRequestWithRetry(request); 472 request_sender_->StartRequestWithRetry(request);
455 run_loop.Run(); 473 run_loop.Run();
456 } 474 }
457 475
458 EXPECT_EQ(HTTP_SUCCESS, error); 476 EXPECT_EQ(HTTP_SUCCESS, error);
459 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method); 477 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
460 EXPECT_EQ("/drive/v2/files", http_request_.relative_url); 478 EXPECT_EQ("/drive/v2/files", http_request_.relative_url);
461 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]); 479 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
462 480
463 EXPECT_TRUE(http_request_.has_content); 481 EXPECT_TRUE(http_request_.has_content);
464 EXPECT_EQ("{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\"," 482 EXPECT_EQ(
465 "\"mimeType\":\"application/vnd.google-apps.folder\"," 483 "{\"lastViewedByMeDate\":\"2013-07-19T15:59:13.123Z\","
466 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\"," 484 "\"mimeType\":\"application/vnd.google-apps.folder\","
467 "\"parents\":[{\"id\":\"root\"}]," 485 "\"modifiedDate\":\"2012-07-19T15:59:13.123Z\","
468 "\"title\":\"new directory\"}", 486 "\"parents\":[{\"id\":\"root\"}],"
469 http_request_.content); 487 "\"properties\":["
488 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
489 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}],"
490 "\"title\":\"new directory\"}",
491 http_request_.content);
470 492
471 scoped_ptr<FileResource> expected( 493 scoped_ptr<FileResource> expected(
472 FileResource::CreateFrom( 494 FileResource::CreateFrom(
473 *test_util::LoadJSONFile("drive/directory_entry.json"))); 495 *test_util::LoadJSONFile("drive/directory_entry.json")));
474 496
475 // Sanity check. 497 // Sanity check.
476 ASSERT_TRUE(file_resource.get()); 498 ASSERT_TRUE(file_resource.get());
477 499
478 EXPECT_EQ(expected->file_id(), file_resource->file_id()); 500 EXPECT_EQ(expected->file_id(), file_resource->file_id());
479 EXPECT_EQ(expected->title(), file_resource->title()); 501 EXPECT_EQ(expected->title(), file_resource->title());
(...skipping 24 matching lines...) Expand all
504 request->set_file_id("resource_id"); 526 request->set_file_id("resource_id");
505 request->set_set_modified_date(true); 527 request->set_set_modified_date(true);
506 request->set_update_viewed_date(false); 528 request->set_update_viewed_date(false);
507 529
508 request->set_title("new title"); 530 request->set_title("new title");
509 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate)); 531 request->set_modified_date(base::Time::FromUTCExploded(kModifiedDate));
510 request->set_last_viewed_by_me_date( 532 request->set_last_viewed_by_me_date(
511 base::Time::FromUTCExploded(kLastViewedByMeDate)); 533 base::Time::FromUTCExploded(kLastViewedByMeDate));
512 request->add_parent("parent_resource_id"); 534 request->add_parent("parent_resource_id");
513 535
514 drive::Property private_property; 536 request->set_properties(testing_properties_);
515 private_property.set_key("key1");
516 private_property.set_value("value1");
517
518 drive::Property public_property;
519 public_property.set_visibility(drive::Property::VISIBILITY_PUBLIC);
520 public_property.set_key("key2");
521 public_property.set_value("value2");
522
523 drive::Properties properties;
524 properties.push_back(private_property);
525 properties.push_back(public_property);
526 request->set_properties(properties);
527
528 request_sender_->StartRequestWithRetry(request); 537 request_sender_->StartRequestWithRetry(request);
529 run_loop.Run(); 538 run_loop.Run();
530 } 539 }
531 540
532 EXPECT_EQ(HTTP_SUCCESS, error); 541 EXPECT_EQ(HTTP_SUCCESS, error);
533 EXPECT_EQ(net::test_server::METHOD_PATCH, http_request_.method); 542 EXPECT_EQ(net::test_server::METHOD_PATCH, http_request_.method);
534 EXPECT_EQ("/drive/v2/files/resource_id" 543 EXPECT_EQ("/drive/v2/files/resource_id"
535 "?setModifiedDate=true&updateViewedDate=false", 544 "?setModifiedDate=true&updateViewedDate=false",
536 http_request_.relative_url); 545 http_request_.relative_url);
537 546
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 new drive::InitiateUploadNewFileRequest( 966 new drive::InitiateUploadNewFileRequest(
958 request_sender_.get(), 967 request_sender_.get(),
959 *url_generator_, 968 *url_generator_,
960 kTestContentType, 969 kTestContentType,
961 kTestContent.size(), 970 kTestContent.size(),
962 "parent_resource_id", // The resource id of the parent directory. 971 "parent_resource_id", // The resource id of the parent directory.
963 "new file title", // The title of the file being uploaded. 972 "new file title", // The title of the file being uploaded.
964 test_util::CreateQuitCallback( 973 test_util::CreateQuitCallback(
965 &run_loop, 974 &run_loop,
966 test_util::CreateCopyResultCallback(&error, &upload_url))); 975 test_util::CreateCopyResultCallback(&error, &upload_url)));
976 request->set_properties(testing_properties_);
967 request_sender_->StartRequestWithRetry(request); 977 request_sender_->StartRequestWithRetry(request);
968 run_loop.Run(); 978 run_loop.Run();
969 } 979 }
970 980
971 EXPECT_EQ(HTTP_SUCCESS, error); 981 EXPECT_EQ(HTTP_SUCCESS, error);
972 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path()); 982 EXPECT_EQ(kTestUploadNewFilePath, upload_url.path());
973 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]); 983 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
974 EXPECT_EQ(base::Int64ToString(kTestContent.size()), 984 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
975 http_request_.headers["X-Upload-Content-Length"]); 985 http_request_.headers["X-Upload-Content-Length"]);
976 986
977 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method); 987 EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
978 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable", 988 EXPECT_EQ("/upload/drive/v2/files?uploadType=resumable",
979 http_request_.relative_url); 989 http_request_.relative_url);
980 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]); 990 EXPECT_EQ("application/json", http_request_.headers["Content-Type"]);
981 EXPECT_TRUE(http_request_.has_content); 991 EXPECT_TRUE(http_request_.has_content);
982 EXPECT_EQ("{\"parents\":[{" 992 EXPECT_EQ(
983 "\"id\":\"parent_resource_id\"," 993 "{\"parents\":[{"
984 "\"kind\":\"drive#fileLink\"" 994 "\"id\":\"parent_resource_id\","
985 "}]," 995 "\"kind\":\"drive#fileLink\""
986 "\"title\":\"new file title\"}", 996 "}],"
987 http_request_.content); 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);
988 1002
989 // Upload the content to the upload URL. 1003 // Upload the content to the upload URL.
990 UploadRangeResponse response; 1004 UploadRangeResponse response;
991 scoped_ptr<FileResource> new_entry; 1005 scoped_ptr<FileResource> new_entry;
992 1006
993 { 1007 {
994 base::RunLoop run_loop; 1008 base::RunLoop run_loop;
995 drive::ResumeUploadRequest* resume_request = 1009 drive::ResumeUploadRequest* resume_request =
996 new drive::ResumeUploadRequest( 1010 new drive::ResumeUploadRequest(
997 request_sender_.get(), 1011 request_sender_.get(),
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
1370 new drive::InitiateUploadExistingFileRequest( 1384 new drive::InitiateUploadExistingFileRequest(
1371 request_sender_.get(), 1385 request_sender_.get(),
1372 *url_generator_, 1386 *url_generator_,
1373 kTestContentType, 1387 kTestContentType,
1374 kTestContent.size(), 1388 kTestContent.size(),
1375 "resource_id", // The resource id of the file to be overwritten. 1389 "resource_id", // The resource id of the file to be overwritten.
1376 std::string(), // No etag. 1390 std::string(), // No etag.
1377 test_util::CreateQuitCallback( 1391 test_util::CreateQuitCallback(
1378 &run_loop, 1392 &run_loop,
1379 test_util::CreateCopyResultCallback(&error, &upload_url))); 1393 test_util::CreateCopyResultCallback(&error, &upload_url)));
1394 request->set_properties(testing_properties_);
1380 request_sender_->StartRequestWithRetry(request); 1395 request_sender_->StartRequestWithRetry(request);
1381 run_loop.Run(); 1396 run_loop.Run();
1382 } 1397 }
1383 1398
1384 EXPECT_EQ(HTTP_SUCCESS, error); 1399 EXPECT_EQ(HTTP_SUCCESS, error);
1385 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path()); 1400 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path());
1386 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]); 1401 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]);
1387 EXPECT_EQ(base::Int64ToString(kTestContent.size()), 1402 EXPECT_EQ(base::Int64ToString(kTestContent.size()),
1388 http_request_.headers["X-Upload-Content-Length"]); 1403 http_request_.headers["X-Upload-Content-Length"]);
1389 EXPECT_EQ("*", http_request_.headers["If-Match"]); 1404 EXPECT_EQ("*", http_request_.headers["If-Match"]);
1390 1405
1391 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); 1406 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1392 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", 1407 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable",
1393 http_request_.relative_url); 1408 http_request_.relative_url);
1394 EXPECT_TRUE(http_request_.has_content); 1409 EXPECT_TRUE(http_request_.has_content);
1395 EXPECT_TRUE(http_request_.content.empty()); 1410 EXPECT_EQ(
1411 "{\"properties\":["
1412 "{\"key\":\"key1\",\"value\":\"value1\",\"visibility\":\"PRIVATE\"},"
1413 "{\"key\":\"key2\",\"value\":\"value2\",\"visibility\":\"PUBLIC\"}]}",
1414 http_request_.content);
1396 1415
1397 // Upload the content to the upload URL. 1416 // Upload the content to the upload URL.
1398 UploadRangeResponse response; 1417 UploadRangeResponse response;
1399 scoped_ptr<FileResource> new_entry; 1418 scoped_ptr<FileResource> new_entry;
1400 1419
1401 { 1420 {
1402 base::RunLoop run_loop; 1421 base::RunLoop run_loop;
1403 drive::ResumeUploadRequest* resume_request = 1422 drive::ResumeUploadRequest* resume_request =
1404 new drive::ResumeUploadRequest( 1423 new drive::ResumeUploadRequest(
1405 request_sender_.get(), 1424 request_sender_.get(),
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
1863 expected.reset(base::JSONReader::Read( 1882 expected.reset(base::JSONReader::Read(
1864 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}")); 1883 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}"));
1865 ASSERT_TRUE(expected); 1884 ASSERT_TRUE(expected);
1866 1885
1867 result.reset(base::JSONReader::Read(http_request_.content)); 1886 result.reset(base::JSONReader::Read(http_request_.content));
1868 EXPECT_TRUE(http_request_.has_content); 1887 EXPECT_TRUE(http_request_.has_content);
1869 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get())); 1888 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));
1870 } 1889 }
1871 1890
1872 } // namespace google_apis 1891 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/drive_api_requests.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698