1515except ImportError :
1616 from mock import patch
1717
18+ Client .RETRY_DELAY = 10
19+
1820class TinifyClientRequestWhenValid (TestHelper ):
1921 def setUp (self ):
2022 super (type (self ), self ).setUp ()
@@ -79,7 +81,7 @@ def test_should_issue_request_with_proxy_authorization(self):
7981
8082 self .assertEqual (self .request .headers ['proxy-authorization' ], 'Basic dXNlcjpwYXNz' )
8183
82- class TinifyClientRequestWithTimeout (TestHelper ):
84+ class TinifyClientRequestWithTimeoutRepeatedly (TestHelper ):
8385 @patch ('requests.sessions.Session.request' , RaiseException (requests .exceptions .Timeout ))
8486 def test_should_raise_connection_error (self ):
8587 with self .assertRaises (ConnectionError ) as context :
@@ -92,7 +94,15 @@ def test_should_raise_connection_error_with_cause(self):
9294 Client ('key' ).request ('GET' , '/' )
9395 self .assertIsInstance (context .exception .__cause__ , requests .exceptions .Timeout )
9496
95- class TinifyClientRequestWithConnectionError (TestHelper ):
97+ class TinifyClientRequestWithTimeoutOnce (TestHelper ):
98+ @patch ('requests.sessions.Session.request' )
99+ def test_should_issue_request (self , mock ):
100+ mock .side_effect = RaiseException (requests .exceptions .Timeout , num = 1 )
101+ mock .return_value = requests .Response ()
102+ mock .return_value .status_code = 201
103+ self .assertIsInstance (Client ('key' ).request ('GET' , '/' , {}), requests .Response )
104+
105+ class TinifyClientRequestWithConnectionErrorRepeatedly (TestHelper ):
96106 @patch ('requests.sessions.Session.request' , RaiseException (requests .exceptions .ConnectionError ('connection error' )))
97107 def test_should_raise_connection_error (self ):
98108 with self .assertRaises (ConnectionError ) as context :
@@ -105,14 +115,30 @@ def test_should_raise_connection_error_with_cause(self):
105115 Client ('key' ).request ('GET' , '/' )
106116 self .assertIsInstance (context .exception .__cause__ , requests .exceptions .ConnectionError )
107117
108- class TinifyClientRequestWithSomeError (TestHelper ):
118+ class TinifyClientRequestWithConnectionErrorOnce (TestHelper ):
119+ @patch ('requests.sessions.Session.request' )
120+ def test_should_issue_request (self , mock ):
121+ mock .side_effect = RaiseException (requests .exceptions .ConnectionError , num = 1 )
122+ mock .return_value = requests .Response ()
123+ mock .return_value .status_code = 201
124+ self .assertIsInstance (Client ('key' ).request ('GET' , '/' , {}), requests .Response )
125+
126+ class TinifyClientRequestWithSomeErrorRepeatedly (TestHelper ):
109127 @patch ('requests.sessions.Session.request' , RaiseException (RuntimeError ('some error' )))
110128 def test_should_raise_connection_error (self ):
111129 with self .assertRaises (ConnectionError ) as context :
112130 Client ('key' ).request ('GET' , '/' )
113131 self .assertEqual ('Error while connecting: some error' , str (context .exception ))
114132
115- class TinifyClientRequestWithServerError (TestHelper ):
133+ class TinifyClientRequestWithSomeErrorOnce (TestHelper ):
134+ @patch ('requests.sessions.Session.request' )
135+ def test_should_issue_request (self , mock ):
136+ mock .side_effect = RaiseException (RuntimeError ('some error' ), num = 1 )
137+ mock .return_value = requests .Response ()
138+ mock .return_value .status_code = 201
139+ self .assertIsInstance (Client ('key' ).request ('GET' , '/' , {}), requests .Response )
140+
141+ class TinifyClientRequestWithServerErrorRepeatedly (TestHelper ):
116142 def test_should_raise_server_error (self ):
117143 httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 584 ,
118144 body = '{"error":"InternalServerError","message":"Oops!"}' )
@@ -121,7 +147,18 @@ def test_should_raise_server_error(self):
121147 Client ('key' ).request ('GET' , '/' )
122148 self .assertEqual ('Oops! (HTTP 584/InternalServerError)' , str (context .exception ))
123149
124- class TinifyClientRequestWithBadServerResponse (TestHelper ):
150+ class TinifyClientRequestWithServerErrorOnce (TestHelper ):
151+ def test_should_issue_request (self ):
152+ httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' ,
153+ responses = [
154+ httpretty .Response (body = '{"error":"InternalServerError","message":"Oops!"}' , status = 584 ),
155+ httpretty .Response (body = 'all good' , status = 201 ),
156+ ])
157+
158+ response = Client ('key' ).request ('GET' , '/' )
159+ self .assertEqual ('201' , str (response .status_code ))
160+
161+ class TinifyClientRequestWithBadServerResponseRepeatedly (TestHelper ):
125162 def test_should_raise_server_error (self ):
126163 httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 543 ,
127164 body = '<!-- this is not json -->' )
@@ -132,6 +169,17 @@ def test_should_raise_server_error(self):
132169 msg = r'Error while parsing response: .* \(HTTP 543/ParseError\)'
133170 self .assertRegexpMatches (str (context .exception ), msg )
134171
172+ class TinifyClientRequestWithBadServerResponseOnce (TestHelper ):
173+ def test_should_issue_request (self ):
174+ httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' ,
175+ responses = [
176+ httpretty .Response (body = '<!-- this is not json -->' , status = 543 ),
177+ httpretty .Response (body = 'all good' , status = 201 ),
178+ ])
179+
180+ response = Client ('key' ).request ('GET' , '/' )
181+ self .assertEqual ('201' , str (response .status_code ))
182+
135183class TinifyClientRequestWithClientError (TestHelper ):
136184 def test_should_raise_client_error (self ):
137185 httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 492 ,
0 commit comments