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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not append duplicates to user agent string #570

Merged
merged 8 commits into from Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion google/cloud/storage/_http.py
Expand Up @@ -56,7 +56,9 @@ def __init__(self, client, client_info=None, api_endpoint=None):
# TODO: When metrics all use gccl, this should be removed #9552
if self._client_info.user_agent is None: # pragma: no branch
self._client_info.user_agent = ""
self._client_info.user_agent += " gcloud-python/{} ".format(__version__)
agent_version = "gcloud-python/{}".format(__version__)
if agent_version not in self._client_info.user_agent:
self._client_info.user_agent += " {} ".format(agent_version)

API_VERSION = "v1"
"""The version of the API, used in building the API call's URL."""
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test__http.py
Expand Up @@ -228,3 +228,18 @@ def test_mtls(self):
self.assertEqual(conn.ALLOW_AUTO_SWITCH_TO_MTLS_URL, False)
self.assertEqual(conn.API_BASE_URL, "http://foo")
self.assertEqual(conn.API_BASE_MTLS_URL, "https://storage.mtls.googleapis.com")

def test_duplicate_user_agent(self):
# Regression test for issue #565
from google.cloud._http import ClientInfo
from google.cloud.storage.client import Client
from google.cloud.storage.batch import Batch
from google.cloud.storage import __version__

client_info = ClientInfo(user_agent="test/123")
client = Client(project="test-project", client_info=client_info)
conn = self._make_one(client, client_info)
expected_user_agent = "test/123 gcloud-python/{} ".format(__version__)
self.assertEqual(conn._client_info.user_agent, expected_user_agent)
batch = Batch(client)
self.assertEqual(batch._client_info.user_agent, expected_user_agent)