1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import datetime
16- import functools
17-
18- import freezegun
1915import mock
20- import pytest
2116import requests
2217import requests .adapters
2318from six .moves import http_client
2722from tests .transport import compliance
2823
2924
30- @pytest .fixture
31- def frozen_time ():
32- with freezegun .freeze_time ("1970-01-01 00:00:00" , tick = False ) as frozen :
33- yield frozen
34-
35-
3625class TestRequestResponse (compliance .RequestResponseTests ):
3726 def make_request (self ):
3827 return google .auth .transport .requests .Request ()
@@ -45,41 +34,6 @@ def test_timeout(self):
4534 assert http .request .call_args [1 ]["timeout" ] == 5
4635
4736
48- class TestTimeoutGuard (object ):
49- def make_guard (self , * args , ** kwargs ):
50- return google .auth .transport .requests .TimeoutGuard (* args , ** kwargs )
51-
52- def test_tracks_elapsed_time (self , frozen_time ):
53- with self .make_guard (timeout = 10 ) as guard :
54- frozen_time .tick (delta = 3.8 )
55- assert guard .remaining_timeout == 6.2
56-
57- def test_noop_if_no_timeout (self , frozen_time ):
58- with self .make_guard (timeout = None ) as guard :
59- frozen_time .tick (delta = datetime .timedelta (days = 3650 ))
60- # NOTE: no timeout error raised, despite years have passed
61- assert guard .remaining_timeout is None
62-
63- def test_error_on_timeout (self , frozen_time ):
64- with pytest .raises (requests .exceptions .Timeout ):
65- with self .make_guard (timeout = 10 ) as guard :
66- frozen_time .tick (delta = 10.001 )
67- assert guard .remaining_timeout == pytest .approx (- 0.001 )
68-
69- def test_custom_timeout_error_type (self , frozen_time ):
70- class FooError (Exception ):
71- pass
72-
73- with pytest .raises (FooError ):
74- with self .make_guard (timeout = 1 , timeout_error_type = FooError ):
75- frozen_time .tick (2 )
76-
77- def test_lets_errors_bubble_up (self , frozen_time ):
78- with pytest .raises (IndexError ):
79- with self .make_guard (timeout = 1 ):
80- [1 , 2 , 3 ][3 ]
81-
82-
8337class CredentialsStub (google .auth .credentials .Credentials ):
8438 def __init__ (self , token = "token" ):
8539 super (CredentialsStub , self ).__init__ ()
@@ -95,18 +49,6 @@ def refresh(self, request):
9549 self .token += "1"
9650
9751
98- class TimeTickCredentialsStub (CredentialsStub ):
99- """Credentials that spend some (mocked) time when refreshing a token."""
100-
101- def __init__ (self , time_tick , token = "token" ):
102- self ._time_tick = time_tick
103- super (TimeTickCredentialsStub , self ).__init__ (token = token )
104-
105- def refresh (self , request ):
106- self ._time_tick ()
107- super (TimeTickCredentialsStub , self ).refresh (requests )
108-
109-
11052class AdapterStub (requests .adapters .BaseAdapter ):
11153 def __init__ (self , responses , headers = None ):
11254 super (AdapterStub , self ).__init__ ()
@@ -127,18 +69,6 @@ def close(self): # pragma: NO COVER
12769 return
12870
12971
130- class TimeTickAdapterStub (AdapterStub ):
131- """Adapter that spends some (mocked) time when making a request."""
132-
133- def __init__ (self , time_tick , responses , headers = None ):
134- self ._time_tick = time_tick
135- super (TimeTickAdapterStub , self ).__init__ (responses , headers = headers )
136-
137- def send (self , request , ** kwargs ):
138- self ._time_tick ()
139- return super (TimeTickAdapterStub , self ).send (request , ** kwargs )
140-
141-
14272def make_response (status = http_client .OK , data = None ):
14373 response = requests .Response ()
14474 response .status_code = status
@@ -191,9 +121,7 @@ def test_request_refresh(self):
191121 [make_response (status = http_client .UNAUTHORIZED ), final_response ]
192122 )
193123
194- authed_session = google .auth .transport .requests .AuthorizedSession (
195- credentials , refresh_timeout = 60
196- )
124+ authed_session = google .auth .transport .requests .AuthorizedSession (credentials )
197125 authed_session .mount (self .TEST_URL , adapter )
198126
199127 result = authed_session .request ("GET" , self .TEST_URL )
@@ -208,44 +136,3 @@ def test_request_refresh(self):
208136
209137 assert adapter .requests [1 ].url == self .TEST_URL
210138 assert adapter .requests [1 ].headers ["authorization" ] == "token1"
211-
212- def test_request_timout (self , frozen_time ):
213- tick_one_second = functools .partial (frozen_time .tick , delta = 1.0 )
214-
215- credentials = mock .Mock (
216- wraps = TimeTickCredentialsStub (time_tick = tick_one_second )
217- )
218- adapter = TimeTickAdapterStub (
219- time_tick = tick_one_second ,
220- responses = [
221- make_response (status = http_client .UNAUTHORIZED ),
222- make_response (status = http_client .OK ),
223- ],
224- )
225-
226- authed_session = google .auth .transport .requests .AuthorizedSession (credentials )
227- authed_session .mount (self .TEST_URL , adapter )
228-
229- # Because at least two requests have to be made, and each takes one
230- # second, the total timeout specified will be exceeded.
231- with pytest .raises (requests .exceptions .Timeout ):
232- authed_session .request ("GET" , self .TEST_URL , timeout = 1.9 )
233-
234- def test_request_timeout_w_refresh_timeout (self , frozen_time ):
235- credentials = mock .Mock (wraps = CredentialsStub ())
236- adapter = TimeTickAdapterStub (
237- time_tick = functools .partial (frozen_time .tick , delta = 1.0 ), # one second
238- responses = [
239- make_response (status = http_client .UNAUTHORIZED ),
240- make_response (status = http_client .OK ),
241- ],
242- )
243-
244- authed_session = google .auth .transport .requests .AuthorizedSession (
245- credentials , refresh_timeout = 0.9
246- )
247- authed_session .mount (self .TEST_URL , adapter )
248-
249- # The timeout is long, but the short refresh timeout will prevail.
250- with pytest .raises (requests .exceptions .Timeout ):
251- authed_session .request ("GET" , self .TEST_URL , timeout = 60 )
0 commit comments