Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest): missing user-agent separator #11473

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::StrCat(absl::StrJoin(agents, " "));
dbolduc marked this conversation as resolved.
Show resolved Hide resolved

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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: I don't know what gives nicer errors between AllOf(Contains(m1), Contains(m2), Contains(m3)) and IsSupersetOf({m1, m2, m3})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to fiddle with the type of {"foo", "bar", StartsWith("baz")}, or {Eq("foo"), Eq("bar"), StartsWith("baz")}. I think both are ambiguous (they need to be initializer_list<T> and T can be too many things.

Contains("test-user-agent-prefix-2"),
Contains(StartsWith("gcloud-cpp/"))));
}

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