diff --git a/google/cloud/storage/internal/rest/stub.cc b/google/cloud/storage/internal/rest/stub.cc index 8cc9feeb8a9ca..39e0413092854 100644 --- a/google/cloud/storage/internal/rest/stub.cc +++ b/google/cloud/storage/internal/rest/stub.cc @@ -183,6 +183,9 @@ StatusOr RestStub::ListBuckets( if (!headers.ok()) return headers; request.AddOptionsToHttpRequest(builder); builder.AddQueryParameter("project", request.project_id()); + if (!request.page_token().empty()) { + builder.AddQueryParameter("pageToken", request.page_token()); + } return ParseFromRestResponse( storage_rest_client_->Get(context, std::move(builder).BuildRequest())); } diff --git a/google/cloud/storage/internal/rest/stub_test.cc b/google/cloud/storage/internal/rest/stub_test.cc index c3b8f612b3679..1dce05ec6dbbc 100644 --- a/google/cloud/storage/internal/rest/stub_test.cc +++ b/google/cloud/storage/internal/rest/stub_test.cc @@ -40,6 +40,7 @@ using ::testing::ElementsAre; using ::testing::Eq; using ::testing::HasSubstr; using ::testing::Matcher; +using ::testing::Not; using ::testing::Pair; using ::testing::ResultOf; using ::testing::Return; @@ -193,6 +194,42 @@ TEST(RestStubTest, CreateBucket) { StatusIs(PermanentError().code(), PermanentError().message())); } +TEST(RestStubTest, ListBucketsIncludesPageTokenWhenPresentInRequest) { + auto mock = std::make_shared(); + std::string expected_token = "test-page-token"; + ListBucketsRequest request("test-project-id"); + request.set_page_token(expected_token); + + EXPECT_CALL(*mock, + Get(ExpectedContext(), + ResultOf( + "request parameters contain 'pageToken'", + [](RestRequest const& r) { return r.parameters(); }, + Contains(Pair("pageToken", expected_token))))) + .WillOnce(Return(PermanentError())); + + auto tested = std::make_unique(Options{}, mock, mock); + auto context = TestContext(); + tested->ListBuckets(context, TestOptions(), request); +} + +TEST(RestStubTest, ListBucketsOmitsPageTokenWhenEmptyInRequest) { + auto mock = std::make_shared(); + ListBucketsRequest request("test-project-id"); + + EXPECT_CALL(*mock, + Get(ExpectedContext(), + ResultOf( + "request parameters do not contain 'pageToken'", + [](RestRequest const& r) { return r.parameters(); }, + Not(Contains(Pair("pageToken", _)))))) + .WillOnce(Return(PermanentError())); + + auto tested = std::make_unique(Options{}, mock, mock); + auto context = TestContext(); + tested->ListBuckets(context, TestOptions(), request); +} + TEST(RestStubTest, GetBucketMetadata) { auto mock = std::make_shared(); EXPECT_CALL(*mock, Get(ExpectedContext(), ExpectedRequest()))