drive_api_service_unittest.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/drive/service/drive_api_service.h"
  5. #include <utility>
  6. #include "base/test/test_simple_task_runner.h"
  7. #include "google_apis/common/dummy_auth_service.h"
  8. #include "google_apis/common/request_sender.h"
  9. #include "google_apis/common/test_util.h"
  10. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  11. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  12. #include "services/network/test/test_url_loader_factory.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace drive {
  15. namespace {
  16. const char kTestUserAgent[] = "test-user-agent";
  17. }
  18. class TestAuthService : public google_apis::DummyAuthService {
  19. public:
  20. void StartAuthentication(google_apis::AuthStatusCallback callback) override {
  21. callback_ = std::move(callback);
  22. }
  23. bool HasAccessToken() const override { return false; }
  24. void SendHttpError() {
  25. ASSERT_FALSE(callback_.is_null());
  26. std::move(callback_).Run(google_apis::HTTP_UNAUTHORIZED, "");
  27. }
  28. private:
  29. google_apis::AuthStatusCallback callback_;
  30. };
  31. TEST(DriveAPIServiceTest, BatchRequestConfiguratorWithAuthFailure) {
  32. const GURL test_base_url("http://localhost/");
  33. google_apis::DriveApiUrlGenerator url_generator(test_base_url, test_base_url);
  34. scoped_refptr<base::TestSimpleTaskRunner> task_runner =
  35. new base::TestSimpleTaskRunner();
  36. network::TestURLLoaderFactory test_url_loader_factory;
  37. scoped_refptr<network::WeakWrapperSharedURLLoaderFactory>
  38. test_shared_loader_factory =
  39. base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
  40. &test_url_loader_factory);
  41. google_apis::RequestSender sender(
  42. std::make_unique<TestAuthService>(), test_shared_loader_factory,
  43. task_runner.get(), kTestUserAgent, TRAFFIC_ANNOTATION_FOR_TESTS);
  44. std::unique_ptr<google_apis::drive::BatchUploadRequest> request =
  45. std::make_unique<google_apis::drive::BatchUploadRequest>(&sender,
  46. url_generator);
  47. google_apis::drive::BatchUploadRequest* request_ptr = request.get();
  48. sender.StartRequestWithAuthRetry(std::move(request));
  49. BatchRequestConfigurator configurator(
  50. request_ptr->GetWeakPtrAsBatchUploadRequest(), task_runner.get(),
  51. url_generator, google_apis::CancelCallbackRepeating());
  52. static_cast<TestAuthService*>(sender.auth_service())->SendHttpError();
  53. {
  54. google_apis::ApiErrorCode error = google_apis::HTTP_SUCCESS;
  55. std::unique_ptr<google_apis::FileResource> file_resource;
  56. configurator.MultipartUploadNewFile(
  57. "text/plain", 10, "", "title",
  58. base::FilePath(FILE_PATH_LITERAL("/file")), UploadNewFileOptions(),
  59. google_apis::test_util::CreateCopyResultCallback(&error,
  60. &file_resource),
  61. google_apis::ProgressCallback());
  62. EXPECT_EQ(google_apis::OTHER_ERROR, error);
  63. }
  64. {
  65. google_apis::ApiErrorCode error = google_apis::HTTP_SUCCESS;
  66. std::unique_ptr<google_apis::FileResource> file_resource;
  67. configurator.MultipartUploadExistingFile(
  68. "text/plain", 10, "resource_id",
  69. base::FilePath(FILE_PATH_LITERAL("/file")), UploadExistingFileOptions(),
  70. google_apis::test_util::CreateCopyResultCallback(&error,
  71. &file_resource),
  72. google_apis::ProgressCallback());
  73. EXPECT_EQ(google_apis::OTHER_ERROR, error);
  74. }
  75. }
  76. } // namespace drive