Skip to content

Commit

Permalink
feat: add non-None default timeout to AuthorizedSession.request() (#435)
Browse files Browse the repository at this point in the history
Closes #434.

This PR adds a non-None default timeout to `AuthorizedSession.request()` to prevent requests from hanging indefinitely in a default case.

Should help with googleapis/google-cloud-python#10182
  • Loading branch information
plamut authored and busunkim96 committed Jan 23, 2020
1 parent 1b9de8d commit d274a3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions google/auth/transport/requests.py
Expand Up @@ -42,6 +42,8 @@

_LOGGER = logging.getLogger(__name__)

_DEFAULT_TIMEOUT = 120 # in seconds


class _Response(transport.Response):
"""Requests transport response adapter.
Expand Down Expand Up @@ -141,7 +143,13 @@ def __init__(self, session=None):
self.session = session

def __call__(
self, url, method="GET", body=None, headers=None, timeout=120, **kwargs
self,
url,
method="GET",
body=None,
headers=None,
timeout=_DEFAULT_TIMEOUT,
**kwargs
):
"""Make an HTTP request using requests.
Expand Down Expand Up @@ -246,7 +254,7 @@ def request(
data=None,
headers=None,
max_allowed_time=None,
timeout=None,
timeout=_DEFAULT_TIMEOUT,
**kwargs
):
"""Implementation of Requests' request.
Expand Down
15 changes: 15 additions & 0 deletions tests/transport/test_requests.py
Expand Up @@ -177,6 +177,21 @@ def test_constructor_with_auth_request(self):

assert authed_session._auth_request == auth_request

def test_request_default_timeout(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)

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 d274a3a

Please sign in to comment.