Skip to content

Commit 0c5fa4f

Browse files
authored
fix(rest): missing user-agent separator (#11480)
1 parent 63c4c44 commit 0c5fa4f

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ info, see [#7463] and [#5976].
119119

120120
</details>
121121

122+
## v2.10.1 - 2023-05
123+
124+
- fix(rest): missing user-agent separator (backport from [#11473](https://github.com/googleapis/google-cloud-cpp/pull/11473))
125+
122126
## v2.10.0 - 2023-05
123127

124128
### New Libraries

google/cloud/internal/curl_impl.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ CurlImpl::CurlImpl(CurlHandle handle,
212212
socket_options_.send_buffer_size_ =
213213
options.get<MaximumCurlSocketSendSizeOption>();
214214

215-
auto const& agents = options.get<UserAgentProductsOption>();
216-
user_agent_ = absl::StrCat(absl::StrJoin(agents, " "), UserAgentSuffix());
215+
auto agents = options.get<UserAgentProductsOption>();
216+
agents.push_back(UserAgentSuffix());
217+
user_agent_ = absl::StrJoin(agents, " ");
217218

218219
http_version_ = options.get<HttpVersionOption>();
219220

google/cloud/internal/curl_rest_client_integration_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "google/cloud/testing_util/contains_once.h"
2323
#include "google/cloud/testing_util/status_matchers.h"
2424
#include "absl/strings/match.h"
25+
#include "absl/strings/str_split.h"
2526
#include <gmock/gmock.h>
2627
#include <nlohmann/json.hpp>
2728

@@ -38,6 +39,7 @@ using ::testing::Eq;
3839
using ::testing::HasSubstr;
3940
using ::testing::Not;
4041
using ::testing::Pair;
42+
using ::testing::StartsWith;
4143

4244
class RestClientIntegrationTest : public ::testing::Test {
4345
protected:
@@ -554,8 +556,10 @@ TEST_F(RestClientIntegrationTest, PerRequestOptions) {
554556
ASSERT_TRUE(parsed_response.is_object()) << "body=" << *body;
555557
auto headers = parsed_response.find("headers");
556558
ASSERT_TRUE(headers != parsed_response.end()) << "body=" << *body;
557-
EXPECT_THAT(headers->value("User-Agent", ""), HasSubstr(p1));
558-
EXPECT_THAT(headers->value("User-Agent", ""), HasSubstr(p2));
559+
auto const products = std::vector<std::string>(
560+
absl::StrSplit(headers->value("User-Agent", ""), ' '));
561+
EXPECT_THAT(products, AllOf(Contains(p1), Contains(p2),
562+
Contains(StartsWith("gcloud-cpp/"))));
559563
}
560564

561565
} // namespace

google/cloud/storage/tests/curl_request_integration_test.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ TEST(CurlRequestTest, UserAgent) {
346346
HttpBinEndpoint() + "/headers",
347347
storage::internal::GetDefaultCurlHandleFactory());
348348
auto options = google::cloud::Options{}.set<UserAgentProductsOption>(
349-
{"test-user-agent-prefix"});
349+
{"test-user-agent-prefix-1", "test-user-agent-prefix-2"});
350350
builder.ApplyClientOptions(options);
351351
builder.AddHeader("Accept: application/json");
352352
builder.AddHeader("charsets: utf-8");
@@ -357,11 +357,13 @@ TEST(CurlRequestTest, UserAgent) {
357357
ASSERT_STATUS_OK(response);
358358
ASSERT_EQ(200, response->status_code) << "response=" << *response;
359359
auto payload = nlohmann::json::parse(response->payload);
360-
ASSERT_EQ(1U, payload.count("headers"));
361-
auto headers = payload["headers"];
362-
EXPECT_THAT(headers.value("User-Agent", ""),
363-
HasSubstr("test-user-agent-prefix"));
364-
EXPECT_THAT(headers.value("User-Agent", ""), HasSubstr("gcloud-cpp/"));
360+
ASSERT_TRUE(payload.is_object()) << "payload=" << response->payload;
361+
ASSERT_TRUE(payload.contains("headers")) << "payload=" << response->payload;
362+
auto const products = std::vector<std::string>(
363+
absl::StrSplit(payload["headers"].value("User-Agent", ""), ' '));
364+
EXPECT_THAT(products, AllOf(Contains("test-user-agent-prefix-1"),
365+
Contains("test-user-agent-prefix-2"),
366+
Contains(StartsWith("gcloud-cpp/"))));
365367
}
366368

367369
#if CURL_AT_LEAST_VERSION(7, 43, 0)

0 commit comments

Comments
 (0)