File tree 3 files changed +34
-6
lines changed
system_tests/system_tests_sync
3 files changed +34
-6
lines changed Original file line number Diff line number Diff line change @@ -340,17 +340,19 @@ def __init__(
340
340
self ._default_host = default_host
341
341
342
342
if auth_request is None :
343
- auth_request_session = requests .Session ()
343
+ self . _auth_request_session = requests .Session ()
344
344
345
345
# Using an adapter to make HTTP requests robust to network errors.
346
346
# This adapter retrys HTTP requests when network errors occur
347
347
# and the requests seems safely retryable.
348
348
retry_adapter = requests .adapters .HTTPAdapter (max_retries = 3 )
349
- auth_request_session .mount ("https://" , retry_adapter )
349
+ self . _auth_request_session .mount ("https://" , retry_adapter )
350
350
351
351
# Do not pass `self` as the session here, as it can lead to
352
352
# infinite recursion.
353
- auth_request = Request (auth_request_session )
353
+ auth_request = Request (self ._auth_request_session )
354
+ else :
355
+ self ._auth_request_session = None
354
356
355
357
# Request instance used by internal methods (for example,
356
358
# credentials.refresh).
@@ -533,3 +535,8 @@ def request(
533
535
def is_mtls (self ):
534
536
"""Indicates if the created SSL channel is mutual TLS."""
535
537
return self ._is_mtls
538
+
539
+ def close (self ):
540
+ if self ._auth_request_session is not None :
541
+ self ._auth_request_session .close ()
542
+ super (AuthorizedSession , self ).close ()
Original file line number Diff line number Diff line change @@ -32,8 +32,10 @@ def test_authorized_session_with_service_account_and_self_signed_jwt():
32
32
33
33
# List Pub/Sub Topics through the REST API
34
34
# https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list
35
- response = session .get ("https://pubsub.googleapis.com/v1/projects/{}/topics" .format (project_id ))
36
- response .raise_for_status ()
35
+ url = "https://pubsub.googleapis.com/v1/projects/{}/topics" .format (project_id )
36
+ with session :
37
+ response = session .get (url )
38
+ response .raise_for_status ()
37
39
38
40
# Check that self-signed JWT was created and is being used
39
41
assert credentials ._jwt_credentials is not None
Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ def test_constructor_with_auth_request(self):
213
213
mock .sentinel .credentials , auth_request = auth_request
214
214
)
215
215
216
- assert authed_session ._auth_request == auth_request
216
+ assert authed_session ._auth_request is auth_request
217
217
218
218
def test_request_default_timeout (self ):
219
219
credentials = mock .Mock (wraps = CredentialsStub ())
@@ -504,3 +504,22 @@ def test_configure_mtls_channel_without_client_cert_env(
504
504
auth_session .configure_mtls_channel (mock_callback )
505
505
assert not auth_session .is_mtls
506
506
mock_callback .assert_not_called ()
507
+
508
+ def test_close_wo_passed_in_auth_request (self ):
509
+ authed_session = google .auth .transport .requests .AuthorizedSession (
510
+ mock .sentinel .credentials
511
+ )
512
+ authed_session ._auth_request_session = mock .Mock (spec = ["close" ])
513
+
514
+ authed_session .close ()
515
+
516
+ authed_session ._auth_request_session .close .assert_called_once_with ()
517
+
518
+ def test_close_w_passed_in_auth_request (self ):
519
+ http = mock .create_autospec (requests .Session )
520
+ auth_request = google .auth .transport .requests .Request (http )
521
+ authed_session = google .auth .transport .requests .AuthorizedSession (
522
+ mock .sentinel .credentials , auth_request = auth_request
523
+ )
524
+
525
+ authed_session .close () # no raise
You can’t perform that action at this time.
0 commit comments