Skip to content
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
Prev Previous commit
Next Next commit
Ensure 'X-Goog-API-Client' header is always passed.
  • Loading branch information
tseaver committed May 2, 2019
commit cfed70bdaa4b5a5cfd1811228a5eda5c87fa5013
4 changes: 3 additions & 1 deletion core/google/cloud/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def extra_headers(self):
:rtype: dict
:returns: header keys / values
"""
return self._extra_headers or {}
result = self._extra_headers.copy() if self._extra_headers else {}
result[CLIENT_INFO_HEADER] = self.user_agent
return result

@extra_headers.setter
def extra_headers(self, value):
Expand Down
59 changes: 53 additions & 6 deletions core/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ def test_user_agent_setter(self):
self.assertEqual(conn._client_info.user_agent, user_agent)

def test_extra_headers_all_caps_getter_deprecated(self):
from google.cloud._http import CLIENT_INFO_HEADER

client = object()
conn = self._make_one(client)
expected = conn._extra_headers = {"foo": "bar"}
conn._extra_headers = {"foo": "bar"}
expected = {
CLIENT_INFO_HEADER: conn._client_info.to_user_agent(),
"foo": "bar",
}

with mock.patch.object(warnings, "warn", autospec=True) as warn:
self.assertEqual(conn._EXTRA_HEADERS, expected)
Expand All @@ -95,12 +101,21 @@ def test_extra_headers_all_caps_setter_deprecated(self):
warn.assert_called_once_with(mock.ANY, DeprecationWarning, stacklevel=2)

def test_extra_headers_getter_default(self):
from google.cloud._http import CLIENT_INFO_HEADER

conn = self._make_one(object())
self.assertEqual(conn.extra_headers, {})
expected = {CLIENT_INFO_HEADER: conn._client_info.to_user_agent()}
self.assertEqual(conn.extra_headers, expected)

def test_extra_headers_getter_overridden(self):
from google.cloud._http import CLIENT_INFO_HEADER

conn = self._make_one(object())
expected = conn._extra_headers = {"foo": "bar"}
conn._extra_headers = {"foo": "bar"}
expected = {
CLIENT_INFO_HEADER: conn._client_info.to_user_agent(),
"foo": "bar",
}
self.assertEqual(conn.extra_headers, expected)

def test_extra_headers_setter(self):
Expand Down Expand Up @@ -192,6 +207,8 @@ def test_build_api_url_w_extra_query_params(self):
self.assertEqual(parms["qux"], ["quux", "corge"])

def test__make_request_no_data_no_content_type_no_headers(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([make_response()])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_one(client)
Expand All @@ -202,12 +219,18 @@ def test__make_request_no_data_no_content_type_no_headers(self):
self.assertEqual(response.status_code, http_client.OK)
self.assertEqual(response.content, b"")

expected_headers = {"Accept-Encoding": "gzip", "User-Agent": conn.user_agent}
expected_headers = {
"Accept-Encoding": "gzip",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=url, headers=expected_headers, data=None
)

def test__make_request_w_data_no_extra_headers(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([make_response()])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_one(client)
Expand All @@ -220,12 +243,15 @@ def test__make_request_w_data_no_extra_headers(self):
"Accept-Encoding": "gzip",
"Content-Type": "application/json",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=url, headers=expected_headers, data=data
)

def test__make_request_w_extra_headers(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([make_response()])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_one(client)
Expand All @@ -237,12 +263,15 @@ def test__make_request_w_extra_headers(self):
"Accept-Encoding": "gzip",
"X-Foo": "foo",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=url, headers=expected_headers, data=None
)

def test_api_request_defaults(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session(
[make_response(content=b"{}", headers=self.JSON_HEADERS)]
)
Expand All @@ -252,7 +281,11 @@ def test_api_request_defaults(self):

self.assertEqual(conn.api_request("GET", path), {})

expected_headers = {"Accept-Encoding": "gzip", "User-Agent": conn.user_agent}
expected_headers = {
"Accept-Encoding": "gzip",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}
expected_url = "{base}/mock/{version}{path}".format(
base=conn.API_BASE_URL, version=conn.API_VERSION, path=path
)
Expand Down Expand Up @@ -280,6 +313,7 @@ def test_api_request_wo_json_expected(self):
def test_api_request_w_query_params(self):
from six.moves.urllib.parse import parse_qs
from six.moves.urllib.parse import urlsplit
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([self.EMPTY_JSON_RESPONSE])
client = mock.Mock(_http=http, spec=["_http"])
Expand All @@ -289,7 +323,11 @@ def test_api_request_w_query_params(self):

self.assertEqual(result, {})

expected_headers = {"Accept-Encoding": "gzip", "User-Agent": conn.user_agent}
expected_headers = {
"Accept-Encoding": "gzip",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=mock.ANY, headers=expected_headers, data=None
)
Expand All @@ -305,6 +343,8 @@ def test_api_request_w_query_params(self):
self.assertEqual(parms["baz"], ["qux", "quux"])

def test_api_request_w_headers(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([self.EMPTY_JSON_RESPONSE])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_mock_one(client)
Expand All @@ -316,12 +356,15 @@ def test_api_request_w_headers(self):
"Accept-Encoding": "gzip",
"User-Agent": conn.user_agent,
"X-Foo": "bar",
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=mock.ANY, headers=expected_headers, data=None
)

def test_api_request_w_extra_headers(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([self.EMPTY_JSON_RESPONSE])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_mock_one(client)
Expand All @@ -339,12 +382,15 @@ def test_api_request_w_extra_headers(self):
"User-Agent": conn.user_agent,
"X-Foo": "not-bar", # The one passed-in is overridden.
"X-Baz": "dax-quux",
CLIENT_INFO_HEADER: conn.user_agent,
}
http.request.assert_called_once_with(
method="GET", url=mock.ANY, headers=expected_headers, data=None
)

def test_api_request_w_data(self):
from google.cloud._http import CLIENT_INFO_HEADER

http = make_requests_session([self.EMPTY_JSON_RESPONSE])
client = mock.Mock(_http=http, spec=["_http"])
conn = self._make_mock_one(client)
Expand All @@ -358,6 +404,7 @@ def test_api_request_w_data(self):
"Accept-Encoding": "gzip",
"Content-Type": "application/json",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: conn.user_agent,
}

http.request.assert_called_once_with(
Expand Down