OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <map> | 6 #include <map> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 | 834 |
835 // 2) Upload the content to the upload URL. | 835 // 2) Upload the content to the upload URL. |
836 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); | 836 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); |
837 ResumeUploadParams resume_params( | 837 ResumeUploadParams resume_params( |
838 UPLOAD_NEW_FILE, | 838 UPLOAD_NEW_FILE, |
839 0, // start_position | 839 0, // start_position |
840 kUploadContent.size(), // end_position (exclusive) | 840 kUploadContent.size(), // end_position (exclusive) |
841 kUploadContent.size(), // content_length, | 841 kUploadContent.size(), // content_length, |
842 "text/plain", // content_type | 842 "text/plain", // content_type |
843 buffer, | 843 buffer, |
| 844 0, // buf_offset |
844 upload_url, | 845 upload_url, |
845 FilePath::FromUTF8Unsafe("drive/newfile.txt")); | 846 FilePath::FromUTF8Unsafe("drive/newfile.txt")); |
846 | 847 |
847 UploadRangeResponse response; | 848 UploadRangeResponse response; |
848 scoped_ptr<ResourceEntry> new_entry; | 849 scoped_ptr<ResourceEntry> new_entry; |
849 | 850 |
850 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( | 851 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
851 &operation_registry_, | 852 &operation_registry_, |
852 request_context_getter_.get(), | 853 request_context_getter_.get(), |
853 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | 854 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 const size_t end_position = start_position + payload.size(); | 950 const size_t end_position = start_position + payload.size(); |
950 | 951 |
951 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(payload); | 952 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(payload); |
952 ResumeUploadParams resume_params( | 953 ResumeUploadParams resume_params( |
953 UPLOAD_NEW_FILE, | 954 UPLOAD_NEW_FILE, |
954 start_position, | 955 start_position, |
955 end_position, | 956 end_position, |
956 kUploadContent.size(), // content_length, | 957 kUploadContent.size(), // content_length, |
957 "text/plain", // content_type | 958 "text/plain", // content_type |
958 buffer, | 959 buffer, |
| 960 0, // buf_offset |
959 upload_url, | 961 upload_url, |
960 FilePath::FromUTF8Unsafe("drive/newfile.txt")); | 962 FilePath::FromUTF8Unsafe("drive/newfile.txt")); |
961 | 963 |
962 UploadRangeResponse response; | 964 UploadRangeResponse response; |
963 scoped_ptr<ResourceEntry> new_entry; | 965 scoped_ptr<ResourceEntry> new_entry; |
964 | 966 |
965 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( | 967 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
966 &operation_registry_, | 968 &operation_registry_, |
967 request_context_getter_.get(), | 969 request_context_getter_.get(), |
968 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | 970 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1099 | 1101 |
1100 // 2) Upload the content to the upload URL. | 1102 // 2) Upload the content to the upload URL. |
1101 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); | 1103 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); |
1102 ResumeUploadParams resume_params( | 1104 ResumeUploadParams resume_params( |
1103 UPLOAD_NEW_FILE, | 1105 UPLOAD_NEW_FILE, |
1104 0, // start_position | 1106 0, // start_position |
1105 kUploadContent.size(), // end_position (exclusive) | 1107 kUploadContent.size(), // end_position (exclusive) |
1106 kUploadContent.size(), // content_length, | 1108 kUploadContent.size(), // content_length, |
1107 "text/plain", // content_type | 1109 "text/plain", // content_type |
1108 buffer, | 1110 buffer, |
| 1111 0, // buf_offset |
1109 upload_url, | 1112 upload_url, |
1110 FilePath::FromUTF8Unsafe("drive/newfile.txt")); | 1113 FilePath::FromUTF8Unsafe("drive/newfile.txt")); |
1111 | 1114 |
1112 UploadRangeResponse response; | 1115 UploadRangeResponse response; |
1113 scoped_ptr<ResourceEntry> new_entry; | 1116 scoped_ptr<ResourceEntry> new_entry; |
1114 | 1117 |
1115 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( | 1118 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
1116 &operation_registry_, | 1119 &operation_registry_, |
1117 request_context_getter_.get(), | 1120 request_context_getter_.get(), |
1118 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | 1121 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1196 | 1199 |
1197 // 2) Upload the content to the upload URL. | 1200 // 2) Upload the content to the upload URL. |
1198 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); | 1201 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); |
1199 ResumeUploadParams resume_params( | 1202 ResumeUploadParams resume_params( |
1200 UPLOAD_EXISTING_FILE, | 1203 UPLOAD_EXISTING_FILE, |
1201 0, // start_position | 1204 0, // start_position |
1202 kUploadContent.size(), // end_position (exclusive) | 1205 kUploadContent.size(), // end_position (exclusive) |
1203 kUploadContent.size(), // content_length, | 1206 kUploadContent.size(), // content_length, |
1204 "text/plain", // content_type | 1207 "text/plain", // content_type |
1205 buffer, | 1208 buffer, |
| 1209 0, // buf_offset |
1206 upload_url, | 1210 upload_url, |
1207 FilePath::FromUTF8Unsafe("drive/existingfile.txt")); | 1211 FilePath::FromUTF8Unsafe("drive/existingfile.txt")); |
1208 | 1212 |
1209 UploadRangeResponse response; | 1213 UploadRangeResponse response; |
1210 scoped_ptr<ResourceEntry> new_entry; | 1214 scoped_ptr<ResourceEntry> new_entry; |
1211 | 1215 |
1212 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( | 1216 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
1213 &operation_registry_, | 1217 &operation_registry_, |
1214 request_context_getter_.get(), | 1218 request_context_getter_.get(), |
1215 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | 1219 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1295 | 1299 |
1296 // 2) Upload the content to the upload URL. | 1300 // 2) Upload the content to the upload URL. |
1297 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); | 1301 scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); |
1298 ResumeUploadParams resume_params( | 1302 ResumeUploadParams resume_params( |
1299 UPLOAD_EXISTING_FILE, | 1303 UPLOAD_EXISTING_FILE, |
1300 0, // start_position | 1304 0, // start_position |
1301 kUploadContent.size(), // end_position (exclusive) | 1305 kUploadContent.size(), // end_position (exclusive) |
1302 kUploadContent.size(), // content_length, | 1306 kUploadContent.size(), // content_length, |
1303 "text/plain", // content_type | 1307 "text/plain", // content_type |
1304 buffer, | 1308 buffer, |
| 1309 0, // buf_offset |
1305 upload_url, | 1310 upload_url, |
1306 FilePath::FromUTF8Unsafe("drive/existingfile.txt")); | 1311 FilePath::FromUTF8Unsafe("drive/existingfile.txt")); |
1307 | 1312 |
1308 UploadRangeResponse response; | 1313 UploadRangeResponse response; |
1309 scoped_ptr<ResourceEntry> new_entry; | 1314 scoped_ptr<ResourceEntry> new_entry; |
1310 | 1315 |
1311 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( | 1316 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
1312 &operation_registry_, | 1317 &operation_registry_, |
1313 request_context_getter_.get(), | 1318 request_context_getter_.get(), |
1314 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | 1319 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1385 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); | 1390 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); |
1386 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), | 1391 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
1387 http_request_.headers["X-Upload-Content-Length"]); | 1392 http_request_.headers["X-Upload-Content-Length"]); |
1388 // For updating an existing file, an empty body should be attached (PUT | 1393 // For updating an existing file, an empty body should be attached (PUT |
1389 // requires a body) | 1394 // requires a body) |
1390 EXPECT_TRUE(http_request_.has_content); | 1395 EXPECT_TRUE(http_request_.has_content); |
1391 EXPECT_EQ("", http_request_.content); | 1396 EXPECT_EQ("", http_request_.content); |
1392 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); | 1397 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); |
1393 } | 1398 } |
1394 | 1399 |
| 1400 TEST_F(GDataWapiOperationsTest, ResumeUploadOperationBufOffset) { |
| 1401 const size_t kMaxNumBytes = 2; |
| 1402 // This is big enough to cause multiple requests of ResumeUploadOperation |
| 1403 // as we are going to send at most kMaxNumBytes at a time. |
| 1404 const std::string kUploadContent = "abcdefghi"; |
| 1405 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
| 1406 GURL upload_url; |
| 1407 |
| 1408 // 1) Get the upload URL for uploading a new file. |
| 1409 InitiateUploadParams initiate_params( |
| 1410 UPLOAD_NEW_FILE, |
| 1411 "New file", |
| 1412 "text/plain", |
| 1413 kUploadContent.size(), |
| 1414 test_server_.GetURL("/feeds/upload/create-session/default/private/full"), |
| 1415 FilePath::FromUTF8Unsafe("drive/newfile.txt"), |
| 1416 "" /* etag */); |
| 1417 |
| 1418 InitiateUploadOperation* initiate_operation = new InitiateUploadOperation( |
| 1419 &operation_registry_, |
| 1420 request_context_getter_.get(), |
| 1421 base::Bind(&CopyResultFromInitiateUploadCallbackAndQuit, |
| 1422 &result_code, |
| 1423 &upload_url), |
| 1424 initiate_params); |
| 1425 |
| 1426 initiate_operation->Start( |
| 1427 kTestGDataAuthToken, kTestUserAgent, |
| 1428 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); |
| 1429 MessageLoop::current()->Run(); |
| 1430 |
| 1431 EXPECT_EQ(HTTP_SUCCESS, result_code); |
| 1432 EXPECT_EQ(test_server_.GetURL("/upload_new_file"), upload_url); |
| 1433 EXPECT_EQ(test_server::METHOD_POST, http_request_.method); |
| 1434 // convert=false should be passed as files should be uploaded as-is. |
| 1435 EXPECT_EQ("/feeds/upload/create-session/default/private/full?convert=false" |
| 1436 "&v=3&alt=json", |
| 1437 http_request_.relative_url); |
| 1438 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); |
| 1439 EXPECT_EQ("application/atom+xml", http_request_.headers["Content-Type"]); |
| 1440 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
| 1441 http_request_.headers["X-Upload-Content-Length"]); |
| 1442 |
| 1443 EXPECT_TRUE(http_request_.has_content); |
| 1444 EXPECT_EQ("<?xml version=\"1.0\"?>\n" |
| 1445 "<entry xmlns=\"http://www.w3.org/2005/Atom\" " |
| 1446 "xmlns:docs=\"http://schemas.google.com/docs/2007\">\n" |
| 1447 " <title>New file</title>\n" |
| 1448 "</entry>\n", |
| 1449 http_request_.content); |
| 1450 |
| 1451 // Keep only one buffer instance and reuse it. The sent data range should |
| 1452 // be specified via buf_offset. |
| 1453 scoped_refptr<net::IOBuffer> buffer = |
| 1454 new net::StringIOBuffer(kUploadContent); |
| 1455 |
| 1456 // 2) Upload the content to the upload URL with multiple requests. |
| 1457 size_t num_bytes_consumed = 0; |
| 1458 for (size_t start_position = 0; start_position < kUploadContent.size(); |
| 1459 start_position += kMaxNumBytes) { |
| 1460 SCOPED_TRACE(testing::Message("start_position: ") << start_position); |
| 1461 |
| 1462 // The payload is at most kMaxNumBytes. |
| 1463 const size_t remaining_size = kUploadContent.size() - start_position; |
| 1464 const std::string payload = kUploadContent.substr( |
| 1465 start_position, std::min(kMaxNumBytes, remaining_size)); |
| 1466 num_bytes_consumed += payload.size(); |
| 1467 // The end position is exclusive. |
| 1468 const size_t end_position = start_position + payload.size(); |
| 1469 |
| 1470 ResumeUploadParams resume_params( |
| 1471 UPLOAD_NEW_FILE, |
| 1472 start_position, |
| 1473 end_position, |
| 1474 kUploadContent.size(), // content_length, |
| 1475 "text/plain", // content_type |
| 1476 buffer, |
| 1477 start_position, // buf_offset |
| 1478 upload_url, |
| 1479 FilePath::FromUTF8Unsafe("drive/newfile.txt")); |
| 1480 |
| 1481 UploadRangeResponse response; |
| 1482 scoped_ptr<ResourceEntry> new_entry; |
| 1483 |
| 1484 ResumeUploadOperation* resume_operation = new ResumeUploadOperation( |
| 1485 &operation_registry_, |
| 1486 request_context_getter_.get(), |
| 1487 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
| 1488 &response, |
| 1489 &new_entry), |
| 1490 resume_params); |
| 1491 |
| 1492 resume_operation->Start( |
| 1493 kTestGDataAuthToken, kTestUserAgent, |
| 1494 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); |
| 1495 MessageLoop::current()->Run(); |
| 1496 |
| 1497 // METHOD_PUT should be used to upload data. |
| 1498 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); |
| 1499 // Request should go to the upload URL. |
| 1500 EXPECT_EQ(upload_url.path(), http_request_.relative_url); |
| 1501 // Content-Range header should be added. |
| 1502 EXPECT_EQ("bytes " + |
| 1503 base::Int64ToString(start_position) + "-" + |
| 1504 base::Int64ToString(end_position - 1) + "/" + |
| 1505 base::Int64ToString(kUploadContent.size()), |
| 1506 http_request_.headers["Content-Range"]); |
| 1507 // The upload content should be set in the HTTP request. |
| 1508 EXPECT_TRUE(http_request_.has_content); |
| 1509 EXPECT_EQ(payload, http_request_.content); |
| 1510 |
| 1511 // Check the response. |
| 1512 if (payload.size() == remaining_size) { |
| 1513 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file. |
| 1514 // The start and end positions should be set to -1, if an upload is |
| 1515 // complete. |
| 1516 EXPECT_EQ(-1, response.start_position_received); |
| 1517 EXPECT_EQ(-1, response.end_position_received); |
| 1518 // The upload process is completed, so exit from the loop. |
| 1519 break; |
| 1520 } |
| 1521 |
| 1522 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); |
| 1523 EXPECT_EQ(static_cast<int64>(start_position), |
| 1524 response.start_position_received); |
| 1525 EXPECT_EQ(static_cast<int64>(end_position), |
| 1526 response.end_position_received); |
| 1527 |
| 1528 // Check the response by GetUploadStatusOperation. |
| 1529 GetUploadStatusOperation* get_upload_status_operation = |
| 1530 new GetUploadStatusOperation( |
| 1531 &operation_registry_, |
| 1532 request_context_getter_.get(), |
| 1533 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, |
| 1534 &response, |
| 1535 &new_entry), |
| 1536 UPLOAD_NEW_FILE, |
| 1537 FilePath::FromUTF8Unsafe("drive/newfile.txt"), |
| 1538 upload_url, |
| 1539 kUploadContent.size()); |
| 1540 get_upload_status_operation->Start( |
| 1541 kTestGDataAuthToken, kTestUserAgent, |
| 1542 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); |
| 1543 MessageLoop::current()->Run(); |
| 1544 |
| 1545 // METHOD_PUT should be used to upload data. |
| 1546 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); |
| 1547 // Request should go to the upload URL. |
| 1548 EXPECT_EQ(upload_url.path(), http_request_.relative_url); |
| 1549 // Content-Range header should be added. |
| 1550 EXPECT_EQ("bytes */" + base::Int64ToString(kUploadContent.size()), |
| 1551 http_request_.headers["Content-Range"]); |
| 1552 EXPECT_TRUE(http_request_.has_content); |
| 1553 EXPECT_TRUE(http_request_.content.empty()); |
| 1554 |
| 1555 // Check the response. |
| 1556 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); |
| 1557 EXPECT_EQ(static_cast<int64>(start_position), |
| 1558 response.start_position_received); |
| 1559 EXPECT_EQ(static_cast<int64>(end_position), |
| 1560 response.end_position_received); |
| 1561 } |
| 1562 |
| 1563 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed); |
| 1564 } |
| 1565 |
| 1566 |
1395 } // namespace google_apis | 1567 } // namespace google_apis |
OLD | NEW |