3.8.0-rc 3.7.0 (latest) 3.6.0 3.5.0 3.4.0 3.3.0 3.2.0 2.48.0-rc 2.47.1 2.46.0 2.45.0 2.44.0 2.43.0 2.42.0 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.1 2.24.0 2.23.0 2.22.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.14.0 2.13.0 2.12.0 2.11.0

### Mocking GCS bucket read and write

It is possible to mock GCS bucket read and write using gmock framework, this is specially useful for developer using GCS C++ client library.

#### Mocking Successful call of ReadObject()

Mocking of ReadObject can be achieved by mocking `ObjectReadSource`. The following code snippet explains this

    TEST(StorageMockingSamples, MockReadObject) {
      namespace gcs = ::google::cloud::storage;

      std::shared_ptr<gcs::testing::MockClient> mock =
          std::make_shared<gcs::testing::MockClient>();
      auto client = gcs::testing::UndecoratedClientFromMock(mock);

      std::string const text = "this is a mock http response";
      std::size_t offset = 0;
      // Simulate a Read() call in the MockObjectReadSource object created below
      auto simulate_read = [&text, &offset](void* buf, std::size_t n) {
        auto const l = (std::min)(n, text.size() - offset);
        std::memcpy(buf, text.data() + offset, l);
        offset += l;
        return gcs::internal::ReadSourceResult{
            l, gcs::internal::HttpResponse{200, {}, {}}};
      };
      EXPECT_CALL(*mock, ReadObject)
          .WillOnce([&](gcs::internal::ReadObjectRangeRequest const& request) {
            EXPECT_EQ(request.bucket_name(), "mock-bucket-name") << request;
            std::unique_ptr<gcs::testing::MockObjectReadSource> mock_source(
                new gcs::testing::MockObjectReadSource);
            ::testing::InSequence seq;
            EXPECT_CALL(*mock_source, IsOpen()).WillRepeatedly(Return(true));
            EXPECT_CALL(*mock_source, Read).WillOnce(simulate_read);
            EXPECT_CALL(*mock_source, IsOpen()).WillRepeatedly(Return(false));

            return google::cloud::make_status_or(
                std::unique_ptr<gcs::internal::ObjectReadSource>(
                    std::move(mock_source)));
          });

      gcs::ObjectReadStream stream =
          client.ReadObject("mock-bucket-name", "mock-object-name");

      // Reading the payload of http response stored in stream
      std::string actual{std::istreambuf_iterator<char>(stream), {}};
      EXPECT_EQ(actual, text);
      stream.Close();
    }

#### Mocking Failed call of ReadObject()

Mocking of ReadObject can be achieved by mocking `ObjectReadSource`. The following code snippet explains this

    TEST(StorageMockingSamples, MockReadObjectFailure) {
      namespace gcs = ::google::cloud::storage;

      std::shared_ptr<gcs::testing::MockClient> mock =
          std::make_shared<gcs::testing::MockClient>();
      auto client = gcs::testing::UndecoratedClientFromMock(mock);

      std::string text = "this is a mock http response";
      EXPECT_CALL(*mock, ReadObject)
          .WillOnce([](gcs::internal::ReadObjectRangeRequest const& request) {
            EXPECT_EQ(request.bucket_name(), "mock-bucket-name") << request;
            auto* mock_source = new gcs::testing::MockObjectReadSource;
            ::testing::InSequence seq;
            EXPECT_CALL(*mock_source, IsOpen).WillRepeatedly(Return(true));
            EXPECT_CALL(*mock_source, Read)
                .WillOnce(Return(google::cloud::Status(
                    google::cloud::StatusCode::kInvalidArgument,
                    "Invalid Argument")));
            EXPECT_CALL(*mock_source, IsOpen).WillRepeatedly(Return(false));

            std::unique_ptr<gcs::internal::ObjectReadSource> result(mock_source);

            return google::cloud::make_status_or(std::move(result));
          });

      gcs::ObjectReadStream stream =
          client.ReadObject("mock-bucket-name", "mock-object-name");
      EXPECT_TRUE(stream.bad());
      stream.Close();
    }

#### Mocking Successful call of WriteObject()

Mocking of ReadObject can be achieved by mocking `ObjectWriteStreambuf`. The following code snippet explains this

    TEST(StorageMockingSamples, MockWriteObject) {
      namespace gcs = ::google::cloud::storage;

      std::shared_ptr<gcs::testing::MockClient> mock =
          std::make_shared<gcs::testing::MockClient>();
      auto client = gcs::testing::UndecoratedClientFromMock(mock);

      gcs::ObjectMetadata expected_metadata;

      using gcs::internal::CreateResumableUploadResponse;
      using gcs::internal::QueryResumableUploadResponse;
      EXPECT_CALL(*mock, CreateResumableUpload)
          .WillOnce(Return(CreateResumableUploadResponse{"test-only-upload-id"}));
      EXPECT_CALL(*mock, UploadChunk)
          .WillOnce(Return(QueryResumableUploadResponse{
              /*.committed_size=*/std::nullopt,
              /*.object_metadata=*/expected_metadata}));

      auto stream = client.WriteObject("mock-bucket-name", "mock-object-name");
      stream << "Hello World!";
      stream.Close();

#### Mocking Failed call of WriteObject()

Mocking of ReadObject can be achieved by mocking `ObjectWriteStreambuf`. The following code snippet explains this

    TEST(StorageMockingSamples, MockWriteObjectFailure) {
      namespace gcs = ::google::cloud::storage;

      std::shared_ptr<gcs::testing::MockClient> mock =
          std::make_shared<gcs::testing::MockClient>();
      auto client = gcs::testing::UndecoratedClientFromMock(mock);

      using gcs::internal::CreateResumableUploadResponse;
      using gcs::internal::QueryResumableUploadResponse;
      EXPECT_CALL(*mock, CreateResumableUpload)
          .WillOnce(Return(CreateResumableUploadResponse{"test-only-upload-id"}));
      EXPECT_CALL(*mock, UploadChunk)
          .WillOnce(Return(google::cloud::Status{
              google::cloud::StatusCode::kInvalidArgument, "Invalid Argument"}));

      auto stream = client.WriteObject("mock-bucket-name", "mock-object-name");
      stream << "Hello World!";
      stream.Close();

      EXPECT_TRUE(stream.bad());
    }

The documentation for each member function in the [Client](https://docs.cloud.google.com/cpp/docs/reference/storage/latest/classgoogle_1_1cloud_1_1storage_1_1Client) class includes short snippets on how to call the function. The snippets for [ReadObject()](https://docs.cloud.google.com/cpp/docs/reference/storage/latest/classgoogle_1_1cloud_1_1storage_1_1Client#classgoogle_1_1cloud_1_1storage_1_1Client_1a8b0575846b68475f6b1f7ef950b49323), [WriteObject()](https://docs.cloud.google.com/cpp/docs/reference/storage/latest/classgoogle_1_1cloud_1_1storage_1_1Client#classgoogle_1_1cloud_1_1storage_1_1Client_1a8918b5f338d91607b06d5107965ed35e), [ListObjects()](https://docs.cloud.google.com/cpp/docs/reference/storage/latest/classgoogle_1_1cloud_1_1storage_1_1Client#classgoogle_1_1cloud_1_1storage_1_1Client_1ac632b30c61f6447eb35ce393a63070f5), and [DeleteObject()](https://docs.cloud.google.com/cpp/docs/reference/storage/latest/classgoogle_1_1cloud_1_1storage_1_1Client#classgoogle_1_1cloud_1_1storage_1_1Client_1a57e64a4e994c07e6d360c647ca57e96f) are good places to continue learning about the library.