Skip to content

Commit

Permalink
Override None timeout with a default one
Browse files Browse the repository at this point in the history
Until all dependent libraries expose configurable timeouts, we do
not want to accept an accidentally passed None timeout.

Assumption: there are no good real-world use cases where a None
timeout would be desirable, as that can make a request to hang
indefinitely.
  • Loading branch information
plamut committed Jan 23, 2020
1 parent 849438c commit 495bbd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions google/auth/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ def request(
# thread-safety.
_credential_refresh_attempt = kwargs.pop("_credential_refresh_attempt", 0)

# Until all libraries expose explicit timeouts, a rouge None timeout
# might sneak in - for example, a default timeout value in one of the
# internal classes. Since it's desired to have at least some timeout,
# we override any `None` values for now.
if timeout is None:
timeout = _DEFAULT_TIMEOUT

# Make a copy of the headers. They will be modified by the credentials
# and we want to pass the original headers if we recurse.
request_headers = headers.copy() if headers is not None else {}
Expand Down
15 changes: 15 additions & 0 deletions tests/transport/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ def test_request_default_timeout(self):
expected_timeout = google.auth.transport.requests._DEFAULT_TIMEOUT
assert patched_request.call_args.kwargs.get("timeout") == expected_timeout

def test_request_overriding_none_timeouts(self):
credentials = mock.Mock(wraps=CredentialsStub())
response = make_response()
adapter = AdapterStub([response])

authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
authed_session.mount(self.TEST_URL, adapter)

patcher = mock.patch("google.auth.transport.requests.requests.Session.request")
with patcher as patched_request:
authed_session.request("GET", self.TEST_URL, timeout=None)

expected_timeout = google.auth.transport.requests._DEFAULT_TIMEOUT
assert patched_request.call_args.kwargs.get("timeout") == expected_timeout

def test_request_no_refresh(self):
credentials = mock.Mock(wraps=CredentialsStub())
response = make_response()
Expand Down

0 comments on commit 495bbd5

Please sign in to comment.