15
15
except ImportError :
16
16
from mock import patch
17
17
18
+ Client .RETRY_DELAY = 10
19
+
18
20
class TinifyClientRequestWhenValid (TestHelper ):
19
21
def setUp (self ):
20
22
super (type (self ), self ).setUp ()
@@ -79,7 +81,7 @@ def test_should_issue_request_with_proxy_authorization(self):
79
81
80
82
self .assertEqual (self .request .headers ['proxy-authorization' ], 'Basic dXNlcjpwYXNz' )
81
83
82
- class TinifyClientRequestWithTimeout (TestHelper ):
84
+ class TinifyClientRequestWithTimeoutRepeatedly (TestHelper ):
83
85
@patch ('requests.sessions.Session.request' , RaiseException (requests .exceptions .Timeout ))
84
86
def test_should_raise_connection_error (self ):
85
87
with self .assertRaises (ConnectionError ) as context :
@@ -92,7 +94,15 @@ def test_should_raise_connection_error_with_cause(self):
92
94
Client ('key' ).request ('GET' , '/' )
93
95
self .assertIsInstance (context .exception .__cause__ , requests .exceptions .Timeout )
94
96
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 ):
96
106
@patch ('requests.sessions.Session.request' , RaiseException (requests .exceptions .ConnectionError ('connection error' )))
97
107
def test_should_raise_connection_error (self ):
98
108
with self .assertRaises (ConnectionError ) as context :
@@ -105,14 +115,30 @@ def test_should_raise_connection_error_with_cause(self):
105
115
Client ('key' ).request ('GET' , '/' )
106
116
self .assertIsInstance (context .exception .__cause__ , requests .exceptions .ConnectionError )
107
117
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 ):
109
127
@patch ('requests.sessions.Session.request' , RaiseException (RuntimeError ('some error' )))
110
128
def test_should_raise_connection_error (self ):
111
129
with self .assertRaises (ConnectionError ) as context :
112
130
Client ('key' ).request ('GET' , '/' )
113
131
self .assertEqual ('Error while connecting: some error' , str (context .exception ))
114
132
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 ):
116
142
def test_should_raise_server_error (self ):
117
143
httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 584 ,
118
144
body = '{"error":"InternalServerError","message":"Oops!"}' )
@@ -121,7 +147,18 @@ def test_should_raise_server_error(self):
121
147
Client ('key' ).request ('GET' , '/' )
122
148
self .assertEqual ('Oops! (HTTP 584/InternalServerError)' , str (context .exception ))
123
149
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 ):
125
162
def test_should_raise_server_error (self ):
126
163
httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 543 ,
127
164
body = '<!-- this is not json -->' )
@@ -132,6 +169,17 @@ def test_should_raise_server_error(self):
132
169
msg = r'Error while parsing response: .* \(HTTP 543/ParseError\)'
133
170
self .assertRegexpMatches (str (context .exception ), msg )
134
171
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
+
135
183
class TinifyClientRequestWithClientError (TestHelper ):
136
184
def test_should_raise_client_error (self ):
137
185
httpretty .register_uri (httpretty .GET , 'https://api.tinify.com/' , status = 492 ,
0 commit comments