| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0994627 commit adc3ee6
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -410,8 +410,9 @@ def run_local_server( | |||
| 410 | 410 | in the user's browser. | |
| 411 | 411 | redirect_uri_trailing_slash (bool): whether or not to add trailing | |
| 412 | 412 | slash when constructing the redirect_uri. Default value is True. | |
| 413 | - timeout_seconds (int): It will raise an error after the timeout timing | ||
| 414 | - if there are no credentials response. The value is in seconds. | ||
| 413 | + timeout_seconds (int): It will raise a WSGITimeoutError exception after the | ||
| 414 | + timeout timing if there are no credentials response. The value is in | ||
| 415 | + seconds. | ||
| 415 | 416 | When set to None there is no timeout. | |
| 416 | 417 | Default value is None. | |
| 417 | 418 | token_audience (str): Passed along with the request for an access | |
@@ -425,6 +426,10 @@ def run_local_server( | |||
| 425 | 426 | Returns: | |
| 426 | 427 | google.oauth2.credentials.Credentials: The OAuth 2.0 credentials | |
| 427 | 428 | for the user. | |
| 429 | + | ||
| 430 | + Raises: | ||
| 431 | + WSGITimeoutError: If there is a timeout when waiting for the response from the | ||
| 432 | + authorization server. | ||
| 428 | 433 | """ | |
| 429 | 434 | wsgi_app = _RedirectWSGIApp(success_message) | |
| 430 | 435 | # Fail fast if the address is occupied | |
@@ -455,7 +460,15 @@ def run_local_server( | |||
| 455 | 460 | ||
| 456 | 461 | # Note: using https here because oauthlib is very picky that | |
| 457 | 462 | # OAuth 2.0 should only occur over https. | |
| 458 | - authorization_response = wsgi_app.last_request_uri.replace("http", "https") | ||
| 463 | + try: | ||
| 464 | + authorization_response = wsgi_app.last_request_uri.replace( | ||
| 465 | + "http", "https" | ||
| 466 | + ) | ||
| 467 | + except AttributeError as e: | ||
| 468 | + raise WSGITimeoutError( | ||
| 469 | + "Timed out waiting for response from authorization server" | ||
| 470 | + ) from e | ||
| 471 | + | ||
| 459 | 472 | self.fetch_token( | |
| 460 | 473 | authorization_response=authorization_response, audience=token_audience | |
| 461 | 474 | ) | |
@@ -506,3 +519,7 @@ def __call__(self, environ, start_response): | |||
| 506 | 519 | start_response("200 OK", [("Content-type", "text/plain; charset=utf-8")]) | |
| 507 | 520 | self.last_request_uri = wsgiref.util.request_uri(environ) | |
| 508 | 521 | return [self._success_message.encode("utf-8")] | |
| 522 | + | ||
| 523 | + | ||
| 524 | + class WSGITimeoutError(AttributeError): | ||
| 525 | + """Raised when the WSGI server times out waiting for a response.""" | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -497,3 +497,20 @@ def test_run_local_server_logs_and_prints_url( | |||
| 497 | 497 | urllib.parse.quote(instance.redirect_uri, safe="") | |
| 498 | 498 | in print_mock.call_args[0][0] | |
| 499 | 499 | ) | |
| 500 | + | ||
| 501 | + @mock.patch("google_auth_oauthlib.flow.webbrowser", autospec=True) | ||
| 502 | + @mock.patch("wsgiref.simple_server.make_server", autospec=True) | ||
| 503 | + def test_run_local_server_timeout( | ||
| 504 | + self, make_server_mock, webbrowser_mock, instance, mock_fetch_token | ||
| 505 | + ): | ||
| 506 | + mock_server = mock.Mock() | ||
| 507 | + make_server_mock.return_value = mock_server | ||
| 508 | + | ||
| 509 | + # handle_request does nothing (simulating timeout), so last_request_uri remains None | ||
| 510 | + mock_server.handle_request.return_value = None | ||
| 511 | + | ||
| 512 | + with pytest.raises(flow.WSGITimeoutError): | ||
| 513 | + instance.run_local_server(timeout_seconds=1) | ||
| 514 | + | ||
| 515 | + webbrowser_mock.get.assert_called_with(None) | ||
| 516 | + webbrowser_mock.get.return_value.open.assert_called_once() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments