Skip to content
3 changes: 3 additions & 0 deletions google/cloud/storage/internal/rest/stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ StatusOr<ListBucketsResponse> 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<ListBucketsResponse>(
storage_rest_client_->Get(context, std::move(builder).BuildRequest()));
}
Expand Down
37 changes: 37 additions & 0 deletions google/cloud/storage/internal/rest/stub_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -193,6 +194,42 @@ TEST(RestStubTest, CreateBucket) {
StatusIs(PermanentError().code(), PermanentError().message()));
}

TEST(RestStubTest, ListBucketsIncludesPageTokenWhenPresentInRequest) {
auto mock = std::make_shared<MockRestClient>();
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<RestStub>(Options{}, mock, mock);
auto context = TestContext();
tested->ListBuckets(context, TestOptions(), request);
}

TEST(RestStubTest, ListBucketsOmitsPageTokenWhenEmptyInRequest) {
auto mock = std::make_shared<MockRestClient>();
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<RestStub>(Options{}, mock, mock);
auto context = TestContext();
tested->ListBuckets(context, TestOptions(), request);
}

TEST(RestStubTest, GetBucketMetadata) {
auto mock = std::make_shared<MockRestClient>();
EXPECT_CALL(*mock, Get(ExpectedContext(), ExpectedRequest()))
Expand Down
Loading