Skip to content

Commit d274a3a

Browse files
plamutbusunkim96
authored andcommitted
feat: add non-None default timeout to AuthorizedSession.request() (#435)
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
1 parent 1b9de8d commit d274a3a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

google/auth/transport/requests.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
_LOGGER = logging.getLogger(__name__)
4444

45+
_DEFAULT_TIMEOUT = 120 # in seconds
46+
4547

4648
class _Response(transport.Response):
4749
"""Requests transport response adapter.
@@ -141,7 +143,13 @@ def __init__(self, session=None):
141143
self.session = session
142144

143145
def __call__(
144-
self, url, method="GET", body=None, headers=None, timeout=120, **kwargs
146+
self,
147+
url,
148+
method="GET",
149+
body=None,
150+
headers=None,
151+
timeout=_DEFAULT_TIMEOUT,
152+
**kwargs
145153
):
146154
"""Make an HTTP request using requests.
147155
@@ -246,7 +254,7 @@ def request(
246254
data=None,
247255
headers=None,
248256
max_allowed_time=None,
249-
timeout=None,
257+
timeout=_DEFAULT_TIMEOUT,
250258
**kwargs
251259
):
252260
"""Implementation of Requests' request.

tests/transport/test_requests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ def test_constructor_with_auth_request(self):
177177

178178
assert authed_session._auth_request == auth_request
179179

180+
def test_request_default_timeout(self):
181+
credentials = mock.Mock(wraps=CredentialsStub())
182+
response = make_response()
183+
adapter = AdapterStub([response])
184+
185+
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
186+
authed_session.mount(self.TEST_URL, adapter)
187+
188+
patcher = mock.patch("google.auth.transport.requests.requests.Session.request")
189+
with patcher as patched_request:
190+
authed_session.request("GET", self.TEST_URL)
191+
192+
expected_timeout = google.auth.transport.requests._DEFAULT_TIMEOUT
193+
assert patched_request.call_args.kwargs.get("timeout") == expected_timeout
194+
180195
def test_request_no_refresh(self):
181196
credentials = mock.Mock(wraps=CredentialsStub())
182197
response = make_response()

0 commit comments

Comments
 (0)