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

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

Issue 1132693006: Drive API: Simplify lifetime management of child requests in BatchUploadRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve comment. Created 5 years, 7 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 const char kTestDownloadPathPrefix[] = "/host/"; 51 const char kTestDownloadPathPrefix[] = "/host/";
52 52
53 // Used as a GetContentCallback. 53 // Used as a GetContentCallback.
54 void AppendContent(std::string* out, 54 void AppendContent(std::string* out,
55 DriveApiErrorCode error, 55 DriveApiErrorCode error,
56 scoped_ptr<std::string> content) { 56 scoped_ptr<std::string> content) {
57 EXPECT_EQ(HTTP_SUCCESS, error); 57 EXPECT_EQ(HTTP_SUCCESS, error);
58 out->append(*content); 58 out->append(*content);
59 } 59 }
60 60
61 class TestBatchableRequest : public BatchableRequestBase { 61 class TestBatchableDelegate : public BatchableDelegate {
62 public: 62 public:
63 TestBatchableRequest(RequestSender* sender, 63 TestBatchableDelegate(const GURL url,
64 const GURL url, 64 const std::string& content_type,
65 const std::string& content_type, 65 const std::string& content_data,
66 const std::string& content_data, 66 const base::Closure& callback)
67 const base::Closure& callback) 67 : url_(url),
68 : BatchableRequestBase(sender),
69 url_(url),
70 content_type_(content_type), 68 content_type_(content_type),
71 content_data_(content_data), 69 content_data_(content_data),
72 callback_(callback) {} 70 callback_(callback) {}
73 GURL GetURL() const override { return url_; } 71 GURL GetURL() const override { return url_; }
74 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override {
75 callback_.Run();
76 }
77 void ProcessURLFetchResults(DriveApiErrorCode code,
78 const std::string& body) override {
79 callback_.Run();
80 }
81 net::URLFetcher::RequestType GetRequestType() const override { 72 net::URLFetcher::RequestType GetRequestType() const override {
82 return net::URLFetcher::PUT; 73 return net::URLFetcher::PUT;
83 } 74 }
75 std::vector<std::string> GetExtraRequestHeaders() const override {
76 return std::vector<std::string>();
77 }
78 void Prepare(const PrepareCallback& callback) override {
79 callback.Run(HTTP_SUCCESS);
80 }
84 bool GetContentData(std::string* upload_content_type, 81 bool GetContentData(std::string* upload_content_type,
85 std::string* upload_content) override { 82 std::string* upload_content) override {
86 upload_content_type->assign(content_type_); 83 upload_content_type->assign(content_type_);
87 upload_content->assign(content_data_); 84 upload_content->assign(content_data_);
88 return true; 85 return true;
89 } 86 }
90 void OnURLFetchUploadProgress(const net::URLFetcher* source, 87 void NotifyError(DriveApiErrorCode code) override { callback_.Run(); }
91 int64 current, 88 void NotifyResult(DriveApiErrorCode code,
92 int64 total) override { 89 const std::string& body,
90 const base::Closure& closure) override {
91 callback_.Run();
92 closure.Run();
93 }
94 void NotifyUploadProgress(const net::URLFetcher* source,
95 int64 current,
96 int64 total) override {
93 progress_values_.push_back(current); 97 progress_values_.push_back(current);
94 } 98 }
95 const std::vector<int64>& progress_values() const { return progress_values_; } 99 const std::vector<int64>& progress_values() const { return progress_values_; }
96 100
97 private: 101 private:
98 GURL url_; 102 GURL url_;
99 std::string content_type_; 103 std::string content_type_;
100 std::string content_data_; 104 std::string content_data_;
101 base::Closure callback_; 105 base::Closure callback_;
102 std::vector<int64> progress_values_; 106 std::vector<int64> progress_values_;
(...skipping 1892 matching lines...) Expand 10 before | Expand all | Expand 10 after
1995 request_sender_->StartRequestWithRetry(request); 1999 request_sender_->StartRequestWithRetry(request);
1996 2000
1997 // Create child request. 2001 // Create child request.
1998 DriveApiErrorCode errors[] = {DRIVE_OTHER_ERROR, DRIVE_OTHER_ERROR}; 2002 DriveApiErrorCode errors[] = {DRIVE_OTHER_ERROR, DRIVE_OTHER_ERROR};
1999 scoped_ptr<FileResource> file_resources[2]; 2003 scoped_ptr<FileResource> file_resources[2];
2000 base::RunLoop run_loop[2]; 2004 base::RunLoop run_loop[2];
2001 for (int i = 0; i < 2; ++i) { 2005 for (int i = 0; i < 2; ++i) {
2002 const FileResourceCallback callback = test_util::CreateQuitCallback( 2006 const FileResourceCallback callback = test_util::CreateQuitCallback(
2003 &run_loop[i], 2007 &run_loop[i],
2004 test_util::CreateCopyResultCallback(&errors[i], &file_resources[i])); 2008 test_util::CreateCopyResultCallback(&errors[i], &file_resources[i]));
2005 drive::MultipartUploadNewFileRequest* const child_request = 2009 drive::MultipartUploadNewFileDelegate* const child_request =
2006 new drive::MultipartUploadNewFileRequest( 2010 new drive::MultipartUploadNewFileDelegate(
2007 request_sender_.get(), base::StringPrintf("new file title %d", i), 2011 request_sender_.get(), base::StringPrintf("new file title %d", i),
2008 "parent_resource_id", kTestContentType, kTestContent.size(), 2012 "parent_resource_id", kTestContentType, kTestContent.size(),
2009 base::Time(), base::Time(), kTestFilePath, drive::Properties(), 2013 base::Time(), base::Time(), kTestFilePath, drive::Properties(),
2010 *url_generator_, callback, ProgressCallback()); 2014 *url_generator_, callback, ProgressCallback());
2011 child_request->SetBoundaryForTesting("INNERBOUNDARY"); 2015 child_request->SetBoundaryForTesting("INNERBOUNDARY");
2012 request->AddRequest(child_request); 2016 request->AddRequest(child_request);
2013 } 2017 }
2014 request->Commit(); 2018 request->Commit();
2015 run_loop[0].Run(); 2019 run_loop[0].Run();
2016 run_loop[1].Run(); 2020 run_loop[1].Run();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2055 "Content-Type: text/plain\n" 2059 "Content-Type: text/plain\n"
2056 "\n" 2060 "\n"
2057 "aaaaaaaaaa\n" 2061 "aaaaaaaaaa\n"
2058 "--INNERBOUNDARY--\n" 2062 "--INNERBOUNDARY--\n"
2059 "--OUTERBOUNDARY--", 2063 "--OUTERBOUNDARY--",
2060 http_request_.content); 2064 http_request_.content);
2061 EXPECT_EQ(HTTP_SUCCESS, errors[0]); 2065 EXPECT_EQ(HTTP_SUCCESS, errors[0]);
2062 ASSERT_TRUE(file_resources[0]); 2066 ASSERT_TRUE(file_resources[0]);
2063 EXPECT_EQ("file_id_1", file_resources[0]->file_id()); 2067 EXPECT_EQ("file_id_1", file_resources[0]->file_id());
2064 ASSERT_FALSE(file_resources[1]); 2068 ASSERT_FALSE(file_resources[1]);
2065 EXPECT_EQ(DRIVE_PARSE_ERROR, errors[1]); 2069 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE, errors[1]);
2066 } 2070 }
2067 2071
2068 TEST_F(DriveApiRequestsTest, EmptyBatchUploadRequest) { 2072 TEST_F(DriveApiRequestsTest, EmptyBatchUploadRequest) {
2069 drive::BatchUploadRequest* const request = 2073 drive::BatchUploadRequest* const request =
2070 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); 2074 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_);
2071 base::WeakPtr<drive::BatchUploadRequest> weak_ptr = 2075 base::WeakPtr<drive::BatchUploadRequest> weak_ptr =
2072 request->GetWeakPtrAsBatchUploadRequest(); 2076 request->GetWeakPtrAsBatchUploadRequest();
2073 request->Commit(); 2077 request->Commit();
2074 ASSERT_FALSE(weak_ptr.get()); 2078 ASSERT_FALSE(weak_ptr.get());
2075 } 2079 }
2076 2080
2077 TEST_F(DriveApiRequestsTest, BatchUploadRequestWithBodyIncludingZero) { 2081 TEST_F(DriveApiRequestsTest, BatchUploadRequestWithBodyIncludingZero) {
2078 // Create batch request. 2082 // Create batch request.
2079 drive::BatchUploadRequest* const request = 2083 drive::BatchUploadRequest* const request =
2080 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); 2084 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_);
2081 request->SetBoundaryForTesting("OUTERBOUNDARY"); 2085 request->SetBoundaryForTesting("OUTERBOUNDARY");
2082 request_sender_->StartRequestWithRetry(request); 2086 request_sender_->StartRequestWithRetry(request);
2083 2087
2084 // Create child request. 2088 // Create child request.
2085 { 2089 {
2086 base::RunLoop loop; 2090 base::RunLoop loop;
2087 TestBatchableRequest* child_request = new TestBatchableRequest( 2091 TestBatchableDelegate* const child_request = new TestBatchableDelegate(
2088 request_sender_.get(), GURL("http://example.com/test"), 2092 GURL("http://example.com/test"), "application/binary",
2089 "application/binary", std::string("Apple\0Orange\0", 13), 2093 std::string("Apple\0Orange\0", 13), loop.QuitClosure());
2090 loop.QuitClosure());
2091 request->AddRequest(child_request); 2094 request->AddRequest(child_request);
2092 request->Commit(); 2095 request->Commit();
2093 loop.Run(); 2096 loop.Run();
2094 } 2097 }
2095 2098
2096 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); 2099 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
2097 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]); 2100 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]);
2098 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY", 2101 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY",
2099 http_request_.headers["Content-Type"]); 2102 http_request_.headers["Content-Type"]);
2100 EXPECT_EQ( 2103 EXPECT_EQ(
2101 "--OUTERBOUNDARY\n" 2104 "--OUTERBOUNDARY\n"
2102 "Content-Type: application/http\n" 2105 "Content-Type: application/http\n"
2103 "\n" 2106 "\n"
2104 "PUT /test HTTP/1.1\n" 2107 "PUT /test HTTP/1.1\n"
2105 "Host: 127.0.0.1\n" 2108 "Host: 127.0.0.1\n"
2106 "X-Goog-Upload-Protocol: multipart\n" 2109 "X-Goog-Upload-Protocol: multipart\n"
2107 "Content-Type: application/binary\n" 2110 "Content-Type: application/binary\n"
2108 "\n" + 2111 "\n" +
2109 std::string("Apple\0Orange\0", 13) + 2112 std::string("Apple\0Orange\0", 13) +
2110 "\n" 2113 "\n"
2111 "--OUTERBOUNDARY--", 2114 "--OUTERBOUNDARY--",
2112 http_request_.content); 2115 http_request_.content);
2113 } 2116 }
2114 2117
2115 TEST_F(DriveApiRequestsTest, BatchUploadRequestProgress) { 2118 TEST_F(DriveApiRequestsTest, BatchUploadRequestProgress) {
2116 // Create batch request. 2119 // Create batch request.
2117 drive::BatchUploadRequest* const request = 2120 drive::BatchUploadRequest* const request =
2118 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_); 2121 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_);
2119 TestBatchableRequest* requests[] = { 2122 TestBatchableDelegate* requests[] = {
2120 new TestBatchableRequest(request_sender_.get(), 2123 new TestBatchableDelegate(GURL("http://example.com/test"),
2121 GURL("http://example.com/test"), 2124 "application/binary", std::string(100, 'a'),
2122 "application/binary", std::string(100, 'a'), 2125 base::Bind(&EmptyClosure)),
2123 base::Bind(&EmptyClosure)), 2126 new TestBatchableDelegate(GURL("http://example.com/test"),
2124 new TestBatchableRequest(request_sender_.get(), 2127 "application/binary", std::string(50, 'b'),
2125 GURL("http://example.com/test"), 2128 base::Bind(&EmptyClosure)),
2126 "application/binary", std::string(50, 'b'), 2129 new TestBatchableDelegate(GURL("http://example.com/test"),
2127 base::Bind(&EmptyClosure)), 2130 "application/binary", std::string(0, 'c'),
2128 new TestBatchableRequest(request_sender_.get(), 2131 base::Bind(&EmptyClosure))};
2129 GURL("http://example.com/test"),
2130 "application/binary", std::string(0, 'c'),
2131 base::Bind(&EmptyClosure))};
2132 const size_t kExpectedUploadDataPosition[] = {208, 517, 776}; 2132 const size_t kExpectedUploadDataPosition[] = {208, 517, 776};
2133 const size_t kExpectedUploadDataSize = 851; 2133 const size_t kExpectedUploadDataSize = 851;
2134 request->AddRequest(requests[0]); 2134 request->AddRequest(requests[0]);
2135 request->AddRequest(requests[1]); 2135 request->AddRequest(requests[1]);
2136 request->AddRequest(requests[2]); 2136 request->AddRequest(requests[2]);
2137 request->Commit(); 2137 request->Commit();
2138 request->Prepare(base::Bind(&EmptyPreapreCallback)); 2138 request->Prepare(base::Bind(&EmptyPreapreCallback));
2139 2139
2140 request->OnURLFetchUploadProgress(nullptr, 0, kExpectedUploadDataSize); 2140 request->OnURLFetchUploadProgress(nullptr, 0, kExpectedUploadDataSize);
2141 request->OnURLFetchUploadProgress(nullptr, 150, kExpectedUploadDataSize); 2141 request->OnURLFetchUploadProgress(nullptr, 150, kExpectedUploadDataSize);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
2278 "Header: value\r\n" 2278 "Header: value\r\n"
2279 "\r\n" 2279 "\r\n"
2280 "BODY\r\n" 2280 "BODY\r\n"
2281 "--BOUNDARY-- \t", 2281 "--BOUNDARY-- \t",
2282 &parts)); 2282 &parts));
2283 ASSERT_EQ(1u, parts.size()); 2283 ASSERT_EQ(1u, parts.size());
2284 EXPECT_EQ(HTTP_SUCCESS, parts[0].code); 2284 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2285 EXPECT_EQ("BODY", parts[0].body); 2285 EXPECT_EQ("BODY", parts[0].body);
2286 } 2286 }
2287 } // namespace google_apis 2287 } // 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