Skip to content

Commit

Permalink
fix(rest): missing user-agent separator (#11473)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan authored May 3, 2023
1 parent b373bb5 commit bcdce2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions google/cloud/internal/curl_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ CurlImpl::CurlImpl(CurlHandle handle,
socket_options_.send_buffer_size_ =
options.get<MaximumCurlSocketSendSizeOption>();

auto const& agents = options.get<UserAgentProductsOption>();
user_agent_ = absl::StrCat(absl::StrJoin(agents, " "), UserAgentSuffix());
auto agents = options.get<UserAgentProductsOption>();
agents.push_back(UserAgentSuffix());
user_agent_ = absl::StrJoin(agents, " ");

http_version_ = options.get<HttpVersionOption>();

Expand Down
8 changes: 6 additions & 2 deletions google/cloud/internal/curl_rest_client_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "google/cloud/testing_util/contains_once.h"
#include "google/cloud/testing_util/status_matchers.h"
#include "absl/strings/match.h"
#include "absl/strings/str_split.h"
#include <gmock/gmock.h>
#include <nlohmann/json.hpp>

Expand All @@ -38,6 +39,7 @@ using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Pair;
using ::testing::StartsWith;

class RestClientIntegrationTest : public ::testing::Test {
protected:
Expand Down Expand Up @@ -554,8 +556,10 @@ TEST_F(RestClientIntegrationTest, PerRequestOptions) {
ASSERT_TRUE(parsed_response.is_object()) << "body=" << *body;
auto headers = parsed_response.find("headers");
ASSERT_TRUE(headers != parsed_response.end()) << "body=" << *body;
EXPECT_THAT(headers->value("User-Agent", ""), HasSubstr(p1));
EXPECT_THAT(headers->value("User-Agent", ""), HasSubstr(p2));
auto const products = std::vector<std::string>(
absl::StrSplit(headers->value("User-Agent", ""), ' '));
EXPECT_THAT(products, AllOf(Contains(p1), Contains(p2),
Contains(StartsWith("gcloud-cpp/"))));
}

} // namespace
Expand Down
14 changes: 8 additions & 6 deletions google/cloud/storage/tests/curl_request_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ TEST(CurlRequestTest, UserAgent) {
HttpBinEndpoint() + "/headers",
storage::internal::GetDefaultCurlHandleFactory());
auto options = google::cloud::Options{}.set<UserAgentProductsOption>(
{"test-user-agent-prefix"});
{"test-user-agent-prefix-1", "test-user-agent-prefix-2"});
builder.ApplyClientOptions(options);
builder.AddHeader("Accept: application/json");
builder.AddHeader("charsets: utf-8");
Expand All @@ -357,11 +357,13 @@ TEST(CurlRequestTest, UserAgent) {
ASSERT_STATUS_OK(response);
ASSERT_EQ(200, response->status_code) << "response=" << *response;
auto payload = nlohmann::json::parse(response->payload);
ASSERT_EQ(1U, payload.count("headers"));
auto headers = payload["headers"];
EXPECT_THAT(headers.value("User-Agent", ""),
HasSubstr("test-user-agent-prefix"));
EXPECT_THAT(headers.value("User-Agent", ""), HasSubstr("gcloud-cpp/"));
ASSERT_TRUE(payload.is_object()) << "payload=" << response->payload;
ASSERT_TRUE(payload.contains("headers")) << "payload=" << response->payload;
auto const products = std::vector<std::string>(
absl::StrSplit(payload["headers"].value("User-Agent", ""), ' '));
EXPECT_THAT(products, AllOf(Contains("test-user-agent-prefix-1"),
Contains("test-user-agent-prefix-2"),
Contains(StartsWith("gcloud-cpp/"))));
}

#if CURL_AT_LEAST_VERSION(7, 43, 0)
Expand Down

0 comments on commit bcdce2f

Please sign in to comment.