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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 base::Unretained(this))); | 120 base::Unretained(this))); |
121 test_server_.RegisterRequestHandler( | 121 test_server_.RegisterRequestHandler( |
122 base::Bind(&GDataWapiOperationsTest::HandleCreateSessionRequest, | 122 base::Bind(&GDataWapiOperationsTest::HandleCreateSessionRequest, |
123 base::Unretained(this))); | 123 base::Unretained(this))); |
124 test_server_.RegisterRequestHandler( | 124 test_server_.RegisterRequestHandler( |
125 base::Bind(&GDataWapiOperationsTest::HandleUploadRequest, | 125 base::Bind(&GDataWapiOperationsTest::HandleUploadRequest, |
126 base::Unretained(this))); | 126 base::Unretained(this))); |
127 | 127 |
128 url_generator_.reset(new GDataWapiUrlGenerator( | 128 url_generator_.reset(new GDataWapiUrlGenerator( |
129 test_util::GetBaseUrlForTesting(test_server_.port()))); | 129 test_util::GetBaseUrlForTesting(test_server_.port()))); |
130 | |
131 start_position_ = 0; | |
132 end_position_ = 0; | |
133 content_length_ = 0; | |
130 } | 134 } |
131 | 135 |
132 virtual void TearDown() OVERRIDE { | 136 virtual void TearDown() OVERRIDE { |
133 test_server_.ShutdownAndWaitUntilComplete(); | 137 test_server_.ShutdownAndWaitUntilComplete(); |
134 request_context_getter_ = NULL; | 138 request_context_getter_ = NULL; |
135 } | 139 } |
136 | 140 |
137 protected: | 141 protected: |
138 // Handles a request for fetching a resource feed. | 142 // Handles a request for fetching a resource feed. |
139 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( | 143 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 const GURL absolute_url = test_server_.GetURL(request.relative_url); | 222 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
219 if (absolute_url.path() == | 223 if (absolute_url.path() == |
220 // This is an upload URL of the root directory. | 224 // This is an upload URL of the root directory. |
221 "/feeds/upload/create-session/default/private/full" || | 225 "/feeds/upload/create-session/default/private/full" || |
222 absolute_url.path() == | 226 absolute_url.path() == |
223 // This is an upload URL of an existing file. | 227 // This is an upload URL of an existing file. |
224 "/feeds/upload/create-session/default/private/full/file:foo") { | 228 "/feeds/upload/create-session/default/private/full/file:foo") { |
225 scoped_ptr<test_server::HttpResponse> http_response( | 229 scoped_ptr<test_server::HttpResponse> http_response( |
226 new test_server::HttpResponse); | 230 new test_server::HttpResponse); |
227 | 231 |
232 // Check an ETag. | |
228 std::map<std::string, std::string>::const_iterator found = | 233 std::map<std::string, std::string>::const_iterator found = |
229 request.headers.find("If-Match"); | 234 request.headers.find("If-Match"); |
230 if (found != request.headers.end() && | 235 if (found != request.headers.end() && |
231 found->second != "*" && | 236 found->second != "*" && |
232 found->second != kTestETag) { | 237 found->second != kTestETag) { |
233 http_response->set_code(test_server::PRECONDITION); | 238 http_response->set_code(test_server::PRECONDITION); |
234 return http_response.Pass(); | 239 return http_response.Pass(); |
235 } | 240 } |
236 | 241 |
242 // Check if the X-Upload-Content-Length is present. If yes, store the | |
243 // length of the file. | |
244 found = request.headers.find("X-Upload-Content-Length"); | |
245 if (found == request.headers.end() || | |
246 !base::StringToInt64(found->second, &content_length_)) { | |
247 return scoped_ptr<test_server::HttpResponse>(); | |
248 } | |
249 start_position_ = 0; | |
250 end_position_ = 0; | |
kinaba
2013/02/07 09:06:12
Theoretically, it should be end_position_ = -1, si
hidehiko
2013/02/08 09:17:00
Changed to use received_bytes_.
| |
251 | |
237 http_response->set_code(test_server::SUCCESS); | 252 http_response->set_code(test_server::SUCCESS); |
238 GURL upload_url; | 253 GURL upload_url; |
239 // POST is used for a new file, and PUT is used for an existing file. | 254 // POST is used for a new file, and PUT is used for an existing file. |
240 if (request.method == test_server::METHOD_POST) { | 255 if (request.method == test_server::METHOD_POST) { |
241 upload_url = test_server_.GetURL("/upload_new_file"); | 256 upload_url = test_server_.GetURL("/upload_new_file"); |
242 } else if (request.method == test_server::METHOD_PUT) { | 257 } else if (request.method == test_server::METHOD_PUT) { |
243 upload_url = test_server_.GetURL("/upload_existing_file"); | 258 upload_url = test_server_.GetURL("/upload_existing_file"); |
244 } else { | 259 } else { |
245 return scoped_ptr<test_server::HttpResponse>(); | 260 return scoped_ptr<test_server::HttpResponse>(); |
246 } | 261 } |
(...skipping 25 matching lines...) Expand all Loading... | |
272 if (absolute_url.path() == "/upload_new_file") | 287 if (absolute_url.path() == "/upload_new_file") |
273 response->set_code(test_server::CREATED); | 288 response->set_code(test_server::CREATED); |
274 | 289 |
275 // Check if the Content-Range header is present. This must be present if | 290 // Check if the Content-Range header is present. This must be present if |
276 // the request body is not empty. | 291 // the request body is not empty. |
277 if (!request.content.empty()) { | 292 if (!request.content.empty()) { |
278 std::map<std::string, std::string>::const_iterator iter = | 293 std::map<std::string, std::string>::const_iterator iter = |
279 request.headers.find("Content-Range"); | 294 request.headers.find("Content-Range"); |
280 if (iter == request.headers.end()) | 295 if (iter == request.headers.end()) |
281 return scoped_ptr<test_server::HttpResponse>(); | 296 return scoped_ptr<test_server::HttpResponse>(); |
282 int64 start_position = 0; | |
283 int64 end_position = 0; | |
284 int64 length = 0; | 297 int64 length = 0; |
285 if (!ParseContentRangeHeader(iter->second, | 298 if (!ParseContentRangeHeader(iter->second, |
286 &start_position, | 299 &start_position_, |
287 &end_position, | 300 &end_position_, |
288 &length)) { | 301 &length)) { |
289 return scoped_ptr<test_server::HttpResponse>(); | 302 return scoped_ptr<test_server::HttpResponse>(); |
290 } | 303 } |
304 EXPECT_EQ(length, content_length_); | |
305 } | |
291 | 306 |
292 // Add Range header to the response, based on the values of | 307 // Add Range header to the response, based on the values of |
293 // Content-Range header in the request. | 308 // Content-Range header in the request. |
294 response->AddCustomHeader( | 309 response->AddCustomHeader( |
295 "Range", | 310 "Range", |
296 "bytes=" + | 311 "bytes=" + |
297 base::Int64ToString(start_position) + "-" + | 312 base::Int64ToString(start_position_) + "-" + |
kinaba
2013/02/07 09:06:12
Correct me if I'm wrong,
When data is uploaded, t
hidehiko
2013/02/08 09:17:00
The server will return no Range header, if there i
| |
298 base::Int64ToString(end_position)); | 313 base::Int64ToString(end_position_)); |
299 | 314 |
300 // Change the code to RESUME_INCOMPLETE if upload is not complete. | 315 // Change the code to RESUME_INCOMPLETE if upload is not complete. |
301 if (end_position + 1 < length) | 316 if (end_position_ + 1 < content_length_) |
302 response->set_code(test_server::RESUME_INCOMPLETE); | 317 response->set_code(test_server::RESUME_INCOMPLETE); |
303 } | |
304 | 318 |
305 return response.Pass(); | 319 return response.Pass(); |
306 } | 320 } |
307 | 321 |
308 MessageLoopForUI message_loop_; | 322 MessageLoopForUI message_loop_; |
309 content::TestBrowserThread ui_thread_; | 323 content::TestBrowserThread ui_thread_; |
310 content::TestBrowserThread file_thread_; | 324 content::TestBrowserThread file_thread_; |
311 content::TestBrowserThread io_thread_; | 325 content::TestBrowserThread io_thread_; |
312 test_server::HttpServer test_server_; | 326 test_server::HttpServer test_server_; |
313 scoped_ptr<TestingProfile> profile_; | 327 scoped_ptr<TestingProfile> profile_; |
314 OperationRegistry operation_registry_; | 328 OperationRegistry operation_registry_; |
315 scoped_ptr<GDataWapiUrlGenerator> url_generator_; | 329 scoped_ptr<GDataWapiUrlGenerator> url_generator_; |
316 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 330 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
317 | 331 |
332 // These fields are used to keep the current upload state during a | |
333 // test case. These values are updated by the request from | |
334 // ResumeUploadOperation, and used to construct the response for | |
335 // both ResumeUploadOperation and GetUploadStatusOperation, to emulate | |
336 // the WAPI server. | |
337 int64 start_position_; | |
338 int64 end_position_; | |
339 int64 content_length_; | |
340 | |
318 // The incoming HTTP request is saved so tests can verify the request | 341 // The incoming HTTP request is saved so tests can verify the request |
319 // parameters like HTTP method (ex. some operations should use DELETE | 342 // parameters like HTTP method (ex. some operations should use DELETE |
320 // instead of GET). | 343 // instead of GET). |
321 test_server::HttpRequest http_request_; | 344 test_server::HttpRequest http_request_; |
322 }; | 345 }; |
323 | 346 |
324 } // namespace | 347 } // namespace |
325 | 348 |
326 TEST_F(GDataWapiOperationsTest, GetResourceListOperation_DefaultFeed) { | 349 TEST_F(GDataWapiOperationsTest, GetResourceListOperation_DefaultFeed) { |
327 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 350 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 | 882 |
860 // Check the response. | 883 // Check the response. |
861 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file | 884 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file |
862 // The start and end positions should be set to -1, if an upload is complete. | 885 // The start and end positions should be set to -1, if an upload is complete. |
863 EXPECT_EQ(-1, response.start_position_received); | 886 EXPECT_EQ(-1, response.start_position_received); |
864 EXPECT_EQ(-1, response.end_position_received); | 887 EXPECT_EQ(-1, response.end_position_received); |
865 } | 888 } |
866 | 889 |
867 // This test exercises InitiateUploadOperation and ResumeUploadOperation for | 890 // This test exercises InitiateUploadOperation and ResumeUploadOperation for |
868 // a scenario of uploading a new *large* file, which requires multiple requests | 891 // a scenario of uploading a new *large* file, which requires multiple requests |
869 // of ResumeUploadOperation. | 892 // of ResumeUploadOperation. GetUploadOperation is also tested in this test |
893 // case. | |
870 TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { | 894 TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { |
871 const size_t kMaxNumBytes = 10; | 895 const size_t kMaxNumBytes = 10; |
872 // This is big enough to cause multiple requests of ResumeUploadOperation | 896 // This is big enough to cause multiple requests of ResumeUploadOperation |
873 // as we are going to send at most kMaxNumBytes at a time. | 897 // as we are going to send at most kMaxNumBytes at a time. |
874 const std::string kUploadContent(kMaxNumBytes + 1, 'a'); | 898 const std::string kUploadContent(kMaxNumBytes + 1, 'a'); |
875 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 899 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
876 GURL upload_url; | 900 GURL upload_url; |
877 | 901 |
878 // 1) Get the upload URL for uploading a new file. | 902 // 1) Get the upload URL for uploading a new file. |
879 InitiateUploadParams initiate_params( | 903 InitiateUploadParams initiate_params( |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
973 EXPECT_TRUE(http_request_.has_content); | 997 EXPECT_TRUE(http_request_.has_content); |
974 EXPECT_EQ(payload, http_request_.content); | 998 EXPECT_EQ(payload, http_request_.content); |
975 | 999 |
976 // Check the response. | 1000 // Check the response. |
977 if (payload.size() == remaining_size) { | 1001 if (payload.size() == remaining_size) { |
978 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file. | 1002 EXPECT_EQ(HTTP_CREATED, response.code); // Because it's a new file. |
979 // The start and end positions should be set to -1, if an upload is | 1003 // The start and end positions should be set to -1, if an upload is |
980 // complete. | 1004 // complete. |
981 EXPECT_EQ(-1, response.start_position_received); | 1005 EXPECT_EQ(-1, response.start_position_received); |
982 EXPECT_EQ(-1, response.end_position_received); | 1006 EXPECT_EQ(-1, response.end_position_received); |
983 } else { | 1007 // The upload process is completed, so exit from the loop. |
984 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); | 1008 break; |
985 EXPECT_EQ(static_cast<int64>(start_position), | |
986 response.start_position_received); | |
987 EXPECT_EQ(static_cast<int64>(end_position), | |
988 response.end_position_received); | |
989 } | 1009 } |
1010 | |
1011 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); | |
1012 EXPECT_EQ(static_cast<int64>(start_position), | |
1013 response.start_position_received); | |
1014 EXPECT_EQ(static_cast<int64>(end_position), | |
1015 response.end_position_received); | |
1016 | |
1017 // Check the response by GetUploadStatusOperation. | |
1018 GetUploadStatusOperation* get_upload_status_operation = | |
1019 new GetUploadStatusOperation( | |
1020 &operation_registry_, | |
1021 request_context_getter_.get(), | |
1022 base::Bind(&CopyResultFromUploadRangeCallbackAndQuit, | |
1023 &response, | |
1024 &new_entry), | |
1025 UPLOAD_NEW_FILE, | |
1026 FilePath::FromUTF8Unsafe("drive/newfile.txt"), | |
1027 upload_url, | |
1028 kUploadContent.size()); | |
1029 get_upload_status_operation->Start( | |
1030 kTestGDataAuthToken, kTestUserAgent, | |
1031 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); | |
1032 MessageLoop::current()->Run(); | |
1033 | |
1034 // METHOD_PUT should be used to upload data. | |
1035 EXPECT_EQ(test_server::METHOD_PUT, http_request_.method); | |
1036 // Request should go to the upload URL. | |
1037 EXPECT_EQ(upload_url.path(), http_request_.relative_url); | |
1038 // Content-Range header should be added. | |
1039 EXPECT_EQ("bytes */" + base::Int64ToString(kUploadContent.size()), | |
1040 http_request_.headers["Content-Range"]); | |
1041 EXPECT_TRUE(http_request_.has_content); | |
1042 EXPECT_TRUE(http_request_.content.empty()); | |
1043 | |
1044 // Check the response. | |
1045 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); | |
1046 EXPECT_EQ(static_cast<int64>(start_position), | |
1047 response.start_position_received); | |
1048 EXPECT_EQ(static_cast<int64>(end_position), | |
1049 response.end_position_received); | |
990 } | 1050 } |
991 | 1051 |
992 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed); | 1052 EXPECT_EQ(kUploadContent.size(), num_bytes_consumed); |
993 } | 1053 } |
994 | 1054 |
995 // This test exercises InitiateUploadOperation and ResumeUploadOperation for | 1055 // This test exercises InitiateUploadOperation and ResumeUploadOperation for |
996 // a scenario of uploading a new *empty* file. | 1056 // a scenario of uploading a new *empty* file. |
997 // | 1057 // |
998 // The test is almost identical to UploadNewFile. The only difference is the | 1058 // The test is almost identical to UploadNewFile. The only difference is the |
999 // expectation for the Content-Range header. | 1059 // expectation for the Content-Range header. |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1334 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), | 1394 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
1335 http_request_.headers["X-Upload-Content-Length"]); | 1395 http_request_.headers["X-Upload-Content-Length"]); |
1336 // For updating an existing file, an empty body should be attached (PUT | 1396 // For updating an existing file, an empty body should be attached (PUT |
1337 // requires a body) | 1397 // requires a body) |
1338 EXPECT_TRUE(http_request_.has_content); | 1398 EXPECT_TRUE(http_request_.has_content); |
1339 EXPECT_EQ("", http_request_.content); | 1399 EXPECT_EQ("", http_request_.content); |
1340 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); | 1400 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); |
1341 } | 1401 } |
1342 | 1402 |
1343 } // namespace google_apis | 1403 } // namespace google_apis |
OLD | NEW |