OLD | NEW |
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/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 61 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
62 | 62 |
63 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 63 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
64 test_server_.RegisterRequestHandler( | 64 test_server_.RegisterRequestHandler( |
65 base::Bind(&DriveApiRequestsTest::HandleChildrenDeleteRequest, | 65 base::Bind(&DriveApiRequestsTest::HandleChildrenDeleteRequest, |
66 base::Unretained(this))); | 66 base::Unretained(this))); |
67 test_server_.RegisterRequestHandler( | 67 test_server_.RegisterRequestHandler( |
68 base::Bind(&DriveApiRequestsTest::HandleDataFileRequest, | 68 base::Bind(&DriveApiRequestsTest::HandleDataFileRequest, |
69 base::Unretained(this))); | 69 base::Unretained(this))); |
70 test_server_.RegisterRequestHandler( | 70 test_server_.RegisterRequestHandler( |
| 71 base::Bind(&DriveApiRequestsTest::HandlePreconditionFailedRequest, |
| 72 base::Unretained(this))); |
| 73 test_server_.RegisterRequestHandler( |
71 base::Bind(&DriveApiRequestsTest::HandleResumeUploadRequest, | 74 base::Bind(&DriveApiRequestsTest::HandleResumeUploadRequest, |
72 base::Unretained(this))); | 75 base::Unretained(this))); |
73 test_server_.RegisterRequestHandler( | 76 test_server_.RegisterRequestHandler( |
74 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, | 77 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, |
75 base::Unretained(this))); | 78 base::Unretained(this))); |
76 test_server_.RegisterRequestHandler( | 79 test_server_.RegisterRequestHandler( |
77 base::Bind(&DriveApiRequestsTest::HandleContentResponse, | 80 base::Bind(&DriveApiRequestsTest::HandleContentResponse, |
78 base::Unretained(this))); | 81 base::Unretained(this))); |
79 test_server_.RegisterRequestHandler( | 82 test_server_.RegisterRequestHandler( |
80 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest, | 83 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest, |
(...skipping 17 matching lines...) Expand all Loading... |
98 base::ScopedTempDir temp_dir_; | 101 base::ScopedTempDir temp_dir_; |
99 | 102 |
100 // This is a path to the file which contains expected response from | 103 // This is a path to the file which contains expected response from |
101 // the server. See also HandleDataFileRequest below. | 104 // the server. See also HandleDataFileRequest below. |
102 base::FilePath expected_data_file_path_; | 105 base::FilePath expected_data_file_path_; |
103 | 106 |
104 // This is a path string in the expected response header from the server | 107 // This is a path string in the expected response header from the server |
105 // for initiating file uploading. | 108 // for initiating file uploading. |
106 std::string expected_upload_path_; | 109 std::string expected_upload_path_; |
107 | 110 |
| 111 // This is a path to the file which contains expected response for |
| 112 // PRECONDITION_FAILED response. |
| 113 base::FilePath expected_precondition_failed_file_path_; |
| 114 |
108 // These are content and its type in the expected response from the server. | 115 // These are content and its type in the expected response from the server. |
109 // See also HandleContentResponse below. | 116 // See also HandleContentResponse below. |
110 std::string expected_content_type_; | 117 std::string expected_content_type_; |
111 std::string expected_content_; | 118 std::string expected_content_; |
112 | 119 |
113 // The incoming HTTP request is saved so tests can verify the request | 120 // The incoming HTTP request is saved so tests can verify the request |
114 // parameters like HTTP method (ex. some requests should use DELETE | 121 // parameters like HTTP method (ex. some requests should use DELETE |
115 // instead of GET). | 122 // instead of GET). |
116 net::test_server::HttpRequest http_request_; | 123 net::test_server::HttpRequest http_request_; |
117 | 124 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 return scoped_ptr<net::test_server::HttpResponse>(); | 162 return scoped_ptr<net::test_server::HttpResponse>(); |
156 } | 163 } |
157 | 164 |
158 http_request_ = request; | 165 http_request_ = request; |
159 | 166 |
160 // Return the response from the data file. | 167 // Return the response from the data file. |
161 return test_util::CreateHttpResponseFromFile( | 168 return test_util::CreateHttpResponseFromFile( |
162 expected_data_file_path_).PassAs<net::test_server::HttpResponse>(); | 169 expected_data_file_path_).PassAs<net::test_server::HttpResponse>(); |
163 } | 170 } |
164 | 171 |
| 172 // Returns PRECONDITION_FAILED response for ETag mismatching with error JSON |
| 173 // content specified by |expected_precondition_failed_file_path_|. |
| 174 // To use this method, it is necessary to set the variable to the appropriate |
| 175 // file path before sending the request to the server. |
| 176 scoped_ptr<net::test_server::HttpResponse> HandlePreconditionFailedRequest( |
| 177 const net::test_server::HttpRequest& request) { |
| 178 if (expected_precondition_failed_file_path_.empty()) { |
| 179 // The file is not specified. Delegate the process to the next handler. |
| 180 return scoped_ptr<net::test_server::HttpResponse>(); |
| 181 } |
| 182 |
| 183 http_request_ = request; |
| 184 |
| 185 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 186 new net::test_server::BasicHttpResponse); |
| 187 response->set_code(net::HTTP_PRECONDITION_FAILED); |
| 188 |
| 189 std::string content; |
| 190 if (file_util::ReadFileToString(expected_precondition_failed_file_path_, |
| 191 &content)) { |
| 192 response->set_content(content); |
| 193 response->set_content_type("application/json"); |
| 194 } |
| 195 |
| 196 return response.PassAs<net::test_server::HttpResponse>(); |
| 197 } |
| 198 |
165 // Returns the response based on set expected upload url. | 199 // Returns the response based on set expected upload url. |
166 // The response contains the url in its "Location: " header. Also, it doesn't | 200 // The response contains the url in its "Location: " header. Also, it doesn't |
167 // have any content. | 201 // have any content. |
168 // To use this method, it is necessary to set |expected_upload_path_| | 202 // To use this method, it is necessary to set |expected_upload_path_| |
169 // to the string representation of the url to be returned. | 203 // to the string representation of the url to be returned. |
170 scoped_ptr<net::test_server::HttpResponse> HandleInitiateUploadRequest( | 204 scoped_ptr<net::test_server::HttpResponse> HandleInitiateUploadRequest( |
171 const net::test_server::HttpRequest& request) { | 205 const net::test_server::HttpRequest& request) { |
172 if (request.relative_url == expected_upload_path_ || | 206 if (request.relative_url == expected_upload_path_ || |
173 expected_upload_path_.empty()) { | 207 expected_upload_path_.empty()) { |
174 // The request is for resume uploading or the expected upload url is not | 208 // The request is for resume uploading or the expected upload url is not |
175 // set. Delegate the processing to the next handler. | 209 // set. Delegate the processing to the next handler. |
176 return scoped_ptr<net::test_server::HttpResponse>(); | 210 return scoped_ptr<net::test_server::HttpResponse>(); |
177 } | 211 } |
178 | 212 |
179 http_request_ = request; | 213 http_request_ = request; |
180 | 214 |
181 scoped_ptr<net::test_server::BasicHttpResponse> response( | 215 scoped_ptr<net::test_server::BasicHttpResponse> response( |
182 new net::test_server::BasicHttpResponse); | 216 new net::test_server::BasicHttpResponse); |
183 | 217 |
184 // Check an ETag. | |
185 std::map<std::string, std::string>::const_iterator found = | |
186 request.headers.find("If-Match"); | |
187 if (found != request.headers.end() && | |
188 found->second != "*" && | |
189 found->second != kTestETag) { | |
190 response->set_code(net::HTTP_PRECONDITION_FAILED); | |
191 return response.PassAs<net::test_server::HttpResponse>(); | |
192 } | |
193 | |
194 // Check if the X-Upload-Content-Length is present. If yes, store the | 218 // Check if the X-Upload-Content-Length is present. If yes, store the |
195 // length of the file. | 219 // length of the file. |
196 found = request.headers.find("X-Upload-Content-Length"); | 220 std::map<std::string, std::string>::const_iterator found = |
| 221 request.headers.find("X-Upload-Content-Length"); |
197 if (found == request.headers.end() || | 222 if (found == request.headers.end() || |
198 !base::StringToInt64(found->second, &content_length_)) { | 223 !base::StringToInt64(found->second, &content_length_)) { |
199 return scoped_ptr<net::test_server::HttpResponse>(); | 224 return scoped_ptr<net::test_server::HttpResponse>(); |
200 } | 225 } |
201 received_bytes_ = 0; | 226 received_bytes_ = 0; |
202 | 227 |
203 response->set_code(net::HTTP_OK); | 228 response->set_code(net::HTTP_OK); |
204 response->AddCustomHeader( | 229 response->AddCustomHeader( |
205 "Location", | 230 "Location", |
206 test_server_.base_url().Resolve(expected_upload_path_).spec()); | 231 test_server_.base_url().Resolve(expected_upload_path_).spec()); |
(...skipping 1100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1307 EXPECT_EQ(HTTP_SUCCESS, response.code); // Because it's an existing file | 1332 EXPECT_EQ(HTTP_SUCCESS, response.code); // Because it's an existing file |
1308 // The start and end positions should be set to -1, if an upload is complete. | 1333 // The start and end positions should be set to -1, if an upload is complete. |
1309 EXPECT_EQ(-1, response.start_position_received); | 1334 EXPECT_EQ(-1, response.start_position_received); |
1310 EXPECT_EQ(-1, response.end_position_received); | 1335 EXPECT_EQ(-1, response.end_position_received); |
1311 } | 1336 } |
1312 | 1337 |
1313 TEST_F(DriveApiRequestsTest, UploadExistingFileRequestWithETagConflicting) { | 1338 TEST_F(DriveApiRequestsTest, UploadExistingFileRequestWithETagConflicting) { |
1314 // Set an expected url for uploading. | 1339 // Set an expected url for uploading. |
1315 expected_upload_path_ = kTestUploadExistingFilePath; | 1340 expected_upload_path_ = kTestUploadExistingFilePath; |
1316 | 1341 |
| 1342 // If it turned out that the etag is conflicting, PRECONDITION_FAILED should |
| 1343 // be returned. |
| 1344 expected_precondition_failed_file_path_ = |
| 1345 test_util::GetTestFilePath("drive/error.json"); |
| 1346 |
1317 const char kTestContentType[] = "text/plain"; | 1347 const char kTestContentType[] = "text/plain"; |
1318 const std::string kTestContent(100, 'a'); | 1348 const std::string kTestContent(100, 'a'); |
1319 | 1349 |
1320 GDataErrorCode error = GDATA_OTHER_ERROR; | 1350 GDataErrorCode error = GDATA_OTHER_ERROR; |
1321 GURL upload_url; | 1351 GURL upload_url; |
1322 | 1352 |
1323 // Initiate uploading a new file to the directory with "parent_resource_id". | 1353 // Initiate uploading a new file to the directory with "parent_resource_id". |
1324 { | 1354 { |
1325 base::RunLoop run_loop; | 1355 base::RunLoop run_loop; |
1326 drive::InitiateUploadExistingFileRequest* request = | 1356 drive::InitiateUploadExistingFileRequest* request = |
(...skipping 17 matching lines...) Expand all Loading... |
1344 http_request_.headers["X-Upload-Content-Length"]); | 1374 http_request_.headers["X-Upload-Content-Length"]); |
1345 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); | 1375 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); |
1346 | 1376 |
1347 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); | 1377 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
1348 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", | 1378 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", |
1349 http_request_.relative_url); | 1379 http_request_.relative_url); |
1350 EXPECT_TRUE(http_request_.has_content); | 1380 EXPECT_TRUE(http_request_.has_content); |
1351 EXPECT_TRUE(http_request_.content.empty()); | 1381 EXPECT_TRUE(http_request_.content.empty()); |
1352 } | 1382 } |
1353 | 1383 |
| 1384 TEST_F(DriveApiRequestsTest, |
| 1385 UploadExistingFileRequestWithETagConflictOnResumeUpload) { |
| 1386 // Set an expected url for uploading. |
| 1387 expected_upload_path_ = kTestUploadExistingFilePath; |
| 1388 |
| 1389 const char kTestContentType[] = "text/plain"; |
| 1390 const std::string kTestContent(100, 'a'); |
| 1391 const base::FilePath kTestFilePath = |
| 1392 temp_dir_.path().AppendASCII("upload_file.txt"); |
| 1393 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); |
| 1394 |
| 1395 GDataErrorCode error = GDATA_OTHER_ERROR; |
| 1396 GURL upload_url; |
| 1397 |
| 1398 // Initiate uploading a new file to the directory with "parent_resource_id". |
| 1399 { |
| 1400 base::RunLoop run_loop; |
| 1401 drive::InitiateUploadExistingFileRequest* request = |
| 1402 new drive::InitiateUploadExistingFileRequest( |
| 1403 request_sender_.get(), |
| 1404 *url_generator_, |
| 1405 kTestContentType, |
| 1406 kTestContent.size(), |
| 1407 "resource_id", // The resource id of the file to be overwritten. |
| 1408 kTestETag, |
| 1409 test_util::CreateQuitCallback( |
| 1410 &run_loop, |
| 1411 test_util::CreateCopyResultCallback(&error, &upload_url))); |
| 1412 request_sender_->StartRequestWithRetry(request); |
| 1413 run_loop.Run(); |
| 1414 } |
| 1415 |
| 1416 EXPECT_EQ(HTTP_SUCCESS, error); |
| 1417 EXPECT_EQ(kTestUploadExistingFilePath, upload_url.path()); |
| 1418 EXPECT_EQ(kTestContentType, http_request_.headers["X-Upload-Content-Type"]); |
| 1419 EXPECT_EQ(base::Int64ToString(kTestContent.size()), |
| 1420 http_request_.headers["X-Upload-Content-Length"]); |
| 1421 EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]); |
| 1422 |
| 1423 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
| 1424 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", |
| 1425 http_request_.relative_url); |
| 1426 EXPECT_TRUE(http_request_.has_content); |
| 1427 EXPECT_TRUE(http_request_.content.empty()); |
| 1428 |
| 1429 // Set PRECONDITION_FAILED to the server. This is the emulation of the |
| 1430 // confliction during uploading. |
| 1431 expected_precondition_failed_file_path_ = |
| 1432 test_util::GetTestFilePath("drive/error.json"); |
| 1433 |
| 1434 // Upload the content to the upload URL. |
| 1435 UploadRangeResponse response; |
| 1436 scoped_ptr<FileResource> new_entry; |
| 1437 |
| 1438 { |
| 1439 base::RunLoop run_loop; |
| 1440 drive::ResumeUploadRequest* resume_request = |
| 1441 new drive::ResumeUploadRequest( |
| 1442 request_sender_.get(), |
| 1443 upload_url, |
| 1444 0, // start_position |
| 1445 kTestContent.size(), // end_position (exclusive) |
| 1446 kTestContent.size(), // content_length, |
| 1447 kTestContentType, |
| 1448 kTestFilePath, |
| 1449 test_util::CreateQuitCallback( |
| 1450 &run_loop, |
| 1451 test_util::CreateCopyResultCallback(&response, &new_entry)), |
| 1452 ProgressCallback()); |
| 1453 request_sender_->StartRequestWithRetry(resume_request); |
| 1454 run_loop.Run(); |
| 1455 } |
| 1456 |
| 1457 // METHOD_PUT should be used to upload data. |
| 1458 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
| 1459 // Request should go to the upload URL. |
| 1460 EXPECT_EQ(upload_url.path(), http_request_.relative_url); |
| 1461 // Content-Range header should be added. |
| 1462 EXPECT_EQ("bytes 0-" + |
| 1463 base::Int64ToString(kTestContent.size() - 1) + "/" + |
| 1464 base::Int64ToString(kTestContent.size()), |
| 1465 http_request_.headers["Content-Range"]); |
| 1466 // The upload content should be set in the HTTP request. |
| 1467 EXPECT_TRUE(http_request_.has_content); |
| 1468 EXPECT_EQ(kTestContent, http_request_.content); |
| 1469 |
| 1470 // Check the response. |
| 1471 EXPECT_EQ(HTTP_PRECONDITION, response.code); |
| 1472 // The start and end positions should be set to -1 for error. |
| 1473 EXPECT_EQ(-1, response.start_position_received); |
| 1474 EXPECT_EQ(-1, response.end_position_received); |
| 1475 |
| 1476 // New entry should be NULL. |
| 1477 EXPECT_FALSE(new_entry.get()); |
| 1478 } |
| 1479 |
1354 TEST_F(DriveApiRequestsTest, DownloadFileRequest) { | 1480 TEST_F(DriveApiRequestsTest, DownloadFileRequest) { |
1355 const base::FilePath kDownloadedFilePath = | 1481 const base::FilePath kDownloadedFilePath = |
1356 temp_dir_.path().AppendASCII("cache_file"); | 1482 temp_dir_.path().AppendASCII("cache_file"); |
1357 const std::string kTestId("dummyId"); | 1483 const std::string kTestId("dummyId"); |
1358 | 1484 |
1359 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 1485 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
1360 base::FilePath temp_file; | 1486 base::FilePath temp_file; |
1361 { | 1487 { |
1362 base::RunLoop run_loop; | 1488 base::RunLoop run_loop; |
1363 drive::DownloadFileRequest* request = new drive::DownloadFileRequest( | 1489 drive::DownloadFileRequest* request = new drive::DownloadFileRequest( |
(...skipping 17 matching lines...) Expand all Loading... |
1381 EXPECT_EQ(HTTP_SUCCESS, result_code); | 1507 EXPECT_EQ(HTTP_SUCCESS, result_code); |
1382 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 1508 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
1383 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url); | 1509 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url); |
1384 EXPECT_EQ(kDownloadedFilePath, temp_file); | 1510 EXPECT_EQ(kDownloadedFilePath, temp_file); |
1385 | 1511 |
1386 const std::string expected_contents = kTestId + kTestId + kTestId; | 1512 const std::string expected_contents = kTestId + kTestId + kTestId; |
1387 EXPECT_EQ(expected_contents, contents); | 1513 EXPECT_EQ(expected_contents, contents); |
1388 } | 1514 } |
1389 | 1515 |
1390 } // namespace google_apis | 1516 } // namespace google_apis |
OLD | NEW |