Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit e87abfb

Browse files
committed
adds unit test, alters error handling code
1 parent 32f811b commit e87abfb

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

google_auth_oauthlib/flow.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,21 @@ def run_local_server(
458458
local_server.timeout = timeout_seconds
459459
local_server.handle_request()
460460

461-
if wsgi_app.last_request_uri is None:
462-
# Timeout occurred
463-
raise WSGITimeout("Timed out waiting for response from authorization server")
461+
# if wsgi_app.last_request_uri is None:
462+
# # Timeout occurred
463+
# raise WSGITimeout("Timed out waiting for response from authorization server")
464464

465465
# Note: using https here because oauthlib is very picky that
466466
# OAuth 2.0 should only occur over https.
467-
authorization_response = wsgi_app.last_request_uri.replace("http", "https")
467+
try:
468+
authorization_response = wsgi_app.last_request_uri.replace(
469+
"http", "https"
470+
)
471+
except AttributeError as e:
472+
raise WSGITimeout(
473+
"Timed out waiting for response from authorization server"
474+
) from e
475+
468476
self.fetch_token(
469477
authorization_response=authorization_response, audience=token_audience
470478
)
@@ -517,5 +525,5 @@ def __call__(self, environ, start_response):
517525
return [self._success_message.encode("utf-8")]
518526

519527

520-
class WSGITimeout(Exception):
528+
class WSGITimeout(AttributeError):
521529
"""Raised when the WSGI server times out waiting for a response."""

tests/unit/test_flow.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,20 @@ def test_run_local_server_logs_and_prints_url(
496496
urllib.parse.quote(instance.redirect_uri, safe="")
497497
in print_mock.call_args[0][0]
498498
)
499+
500+
@mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True)
501+
@mock.patch("wsgiref.simple_server.make_server", autospec=True)
502+
def test_run_local_server_timeout(
503+
self, make_server_mock, webbrowser_mock, instance, mock_fetch_token
504+
):
505+
mock_server = mock.Mock()
506+
make_server_mock.return_value = mock_server
507+
508+
# handle_request does nothing (simulating timeout), so last_request_uri remains None
509+
mock_server.handle_request.return_value = None
510+
511+
with pytest.raises(flow.WSGITimeout):
512+
instance.run_local_server(timeout_seconds=1)
513+
514+
webbrowser_mock.get.assert_called_with(None)
515+
webbrowser_mock.get.return_value.open.assert_called_once()

0 commit comments

Comments
 (0)