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 17 matching lines...) Expand all Loading... |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 | 29 |
30 namespace google_apis { | 30 namespace google_apis { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 const char kTestGDataAuthToken[] = "testtoken"; | 34 const char kTestGDataAuthToken[] = "testtoken"; |
35 const char kTestUserAgent[] = "test-user-agent"; | 35 const char kTestUserAgent[] = "test-user-agent"; |
36 const char kTestETag[] = "test_etag"; | 36 const char kTestETag[] = "test_etag"; |
37 | 37 |
38 // Copies the results from DownloadActionCallback and quit the message loop. | |
39 // The contents of the download cache file are copied to a string, and the | |
40 // file is removed. | |
41 void CopyResultsFromDownloadActionCallbackAndQuit( | |
42 GDataErrorCode* out_result_code, | |
43 std::string* contents, | |
44 GDataErrorCode result_code, | |
45 const FilePath& cache_file_path) { | |
46 *out_result_code = result_code; | |
47 file_util::ReadFileToString(cache_file_path, contents); | |
48 file_util::Delete(cache_file_path, false); | |
49 MessageLoop::current()->Quit(); | |
50 } | |
51 | |
52 // Copies the result from InitiateUploadCallback and quit the message loop. | 38 // Copies the result from InitiateUploadCallback and quit the message loop. |
53 void CopyResultFromInitiateUploadCallbackAndQuit( | 39 void CopyResultFromInitiateUploadCallbackAndQuit( |
54 GDataErrorCode* out_result_code, | 40 GDataErrorCode* out_result_code, |
55 GURL* out_upload_url, | 41 GURL* out_upload_url, |
56 GDataErrorCode result_code, | 42 GDataErrorCode result_code, |
57 const GURL& upload_url) { | 43 const GURL& upload_url) { |
58 *out_result_code = result_code; | 44 *out_result_code = result_code; |
59 *out_upload_url = upload_url; | 45 *out_upload_url = upload_url; |
60 MessageLoop::current()->Quit(); | 46 MessageLoop::current()->Quit(); |
61 } | 47 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 url_generator_.reset(new GDataWapiUrlGenerator( | 128 url_generator_.reset(new GDataWapiUrlGenerator( |
143 test_util::GetBaseUrlForTesting(test_server_.port()))); | 129 test_util::GetBaseUrlForTesting(test_server_.port()))); |
144 } | 130 } |
145 | 131 |
146 virtual void TearDown() OVERRIDE { | 132 virtual void TearDown() OVERRIDE { |
147 test_server_.ShutdownAndWaitUntilComplete(); | 133 test_server_.ShutdownAndWaitUntilComplete(); |
148 request_context_getter_ = NULL; | 134 request_context_getter_ = NULL; |
149 } | 135 } |
150 | 136 |
151 protected: | 137 protected: |
152 // Returns a temporary file path suitable for storing the cache file. | |
153 FilePath GetTestCachedFilePath(const FilePath& file_name) { | |
154 return profile_->GetPath().Append(file_name); | |
155 } | |
156 | |
157 // Handles a request for fetching a resource feed. | 138 // Handles a request for fetching a resource feed. |
158 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( | 139 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( |
159 const test_server::HttpRequest& request) { | 140 const test_server::HttpRequest& request) { |
160 http_request_ = request; | 141 http_request_ = request; |
161 | 142 |
162 const GURL absolute_url = test_server_.GetURL(request.relative_url); | 143 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
163 std::string remaining_path; | 144 std::string remaining_path; |
164 if (absolute_url.path() == "/feeds/default/private/full" && | 145 if (absolute_url.path() == "/feeds/default/private/full" && |
165 request.method == test_server::METHOD_POST) { | 146 request.method == test_server::METHOD_POST) { |
166 // This is a request for copying a document. | 147 // This is a request for copying a document. |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 | 479 |
499 EXPECT_EQ(HTTP_SUCCESS, result_code); | 480 EXPECT_EQ(HTTP_SUCCESS, result_code); |
500 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); | 481 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); |
501 EXPECT_EQ("/feeds/metadata/default?v=3&alt=json&include-installed-apps=true", | 482 EXPECT_EQ("/feeds/metadata/default?v=3&alt=json&include-installed-apps=true", |
502 http_request_.relative_url); | 483 http_request_.relative_url); |
503 EXPECT_TRUE(test_util::VerifyJsonData( | 484 EXPECT_TRUE(test_util::VerifyJsonData( |
504 test_util::GetTestFilePath("gdata/account_metadata.json"), | 485 test_util::GetTestFilePath("gdata/account_metadata.json"), |
505 result_data.get())); | 486 result_data.get())); |
506 } | 487 } |
507 | 488 |
508 TEST_F(GDataWapiOperationsTest, DownloadFileOperation_ValidFile) { | |
509 GDataErrorCode result_code = GDATA_OTHER_ERROR; | |
510 std::string contents; | |
511 DownloadFileOperation* operation = new DownloadFileOperation( | |
512 &operation_registry_, | |
513 request_context_getter_.get(), | |
514 base::Bind(&CopyResultsFromDownloadActionCallbackAndQuit, | |
515 &result_code, | |
516 &contents), | |
517 GetContentCallback(), | |
518 test_server_.GetURL("/files/gdata/testfile.txt"), | |
519 FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"), | |
520 GetTestCachedFilePath(FilePath::FromUTF8Unsafe("cached_testfile.txt"))); | |
521 operation->Start(kTestGDataAuthToken, kTestUserAgent, | |
522 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); | |
523 MessageLoop::current()->Run(); | |
524 | |
525 EXPECT_EQ(HTTP_SUCCESS, result_code); | |
526 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); | |
527 EXPECT_EQ("/files/gdata/testfile.txt", http_request_.relative_url); | |
528 | |
529 const FilePath expected_path = | |
530 test_util::GetTestFilePath("gdata/testfile.txt"); | |
531 std::string expected_contents; | |
532 file_util::ReadFileToString(expected_path, &expected_contents); | |
533 EXPECT_EQ(expected_contents, contents); | |
534 } | |
535 | |
536 // http://crbug.com/169588 | |
537 TEST_F(GDataWapiOperationsTest, | |
538 DISABLED_DownloadFileOperation_NonExistentFile) { | |
539 GDataErrorCode result_code = GDATA_OTHER_ERROR; | |
540 std::string contents; | |
541 DownloadFileOperation* operation = new DownloadFileOperation( | |
542 &operation_registry_, | |
543 request_context_getter_.get(), | |
544 base::Bind(&CopyResultsFromDownloadActionCallbackAndQuit, | |
545 &result_code, | |
546 &contents), | |
547 GetContentCallback(), | |
548 test_server_.GetURL("/files/gdata/no-such-file.txt"), | |
549 FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"), | |
550 GetTestCachedFilePath( | |
551 FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); | |
552 operation->Start(kTestGDataAuthToken, kTestUserAgent, | |
553 base::Bind(&test_util::DoNothingForReAuthenticateCallback)); | |
554 MessageLoop::current()->Run(); | |
555 | |
556 EXPECT_EQ(HTTP_NOT_FOUND, result_code); | |
557 EXPECT_EQ(test_server::METHOD_GET, http_request_.method); | |
558 EXPECT_EQ("/files/gdata/no-such-file.txt", http_request_.relative_url); | |
559 // Do not verify the not found message. | |
560 } | |
561 | |
562 TEST_F(GDataWapiOperationsTest, DeleteResourceOperation) { | 489 TEST_F(GDataWapiOperationsTest, DeleteResourceOperation) { |
563 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 490 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
564 | 491 |
565 DeleteResourceOperation* operation = new DeleteResourceOperation( | 492 DeleteResourceOperation* operation = new DeleteResourceOperation( |
566 &operation_registry_, | 493 &operation_registry_, |
567 request_context_getter_.get(), | 494 request_context_getter_.get(), |
568 *url_generator_, | 495 *url_generator_, |
569 base::Bind(&test_util::CopyResultFromEntryActionCallbackAndQuit, | 496 base::Bind(&test_util::CopyResultFromEntryActionCallbackAndQuit, |
570 &result_code), | 497 &result_code), |
571 "file:2_file_resource_id", | 498 "file:2_file_resource_id", |
(...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1407 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), | 1334 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
1408 http_request_.headers["X-Upload-Content-Length"]); | 1335 http_request_.headers["X-Upload-Content-Length"]); |
1409 // For updating an existing file, an empty body should be attached (PUT | 1336 // For updating an existing file, an empty body should be attached (PUT |
1410 // requires a body) | 1337 // requires a body) |
1411 EXPECT_TRUE(http_request_.has_content); | 1338 EXPECT_TRUE(http_request_.has_content); |
1412 EXPECT_EQ("", http_request_.content); | 1339 EXPECT_EQ("", http_request_.content); |
1413 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); | 1340 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); |
1414 } | 1341 } |
1415 | 1342 |
1416 } // namespace google_apis | 1343 } // namespace google_apis |
OLD | NEW |